From 8330fa86bcfccdcb9fb0f44759c27f1d90744351 Mon Sep 17 00:00:00 2001 From: Milos Stojanovic Date: Mon, 23 Mar 2020 13:37:54 +0100 Subject: [PATCH] New config param: overwrite_on_upload --- CHANGELOG.md | 2 ++ backend/Controllers/FileController.php | 4 ++-- backend/Controllers/UploadController.php | 4 +++- backend/Services/Storage/Filesystem.php | 8 +++++-- configuration_sample.php | 1 + tests/backend/Unit/FilesystemTest.php | 29 +++++++++++++++++++++++- tests/backend/configuration.php | 1 + 7 files changed, 43 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e08ef15..beae506 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Upcoming... +* New config param: overwrite files on upload + ## 7.3.3 - 2020-03-08 * Download filename bugfix diff --git a/backend/Controllers/FileController.php b/backend/Controllers/FileController.php index d48f380..323ef4f 100644 --- a/backend/Controllers/FileController.php +++ b/backend/Controllers/FileController.php @@ -182,8 +182,8 @@ class FileController fwrite($stream, $content); rewind($stream); - $res = $this->storage->deleteFile($path.$this->separator.$name); - $res = $this->storage->store($path, $name, $stream); + $this->storage->deleteFile($path.$this->separator.$name); + $this->storage->store($path, $name, $stream); if (is_resource($stream)) { fclose($stream); diff --git a/backend/Controllers/UploadController.php b/backend/Controllers/UploadController.php index c65bc5e..147cf0f 100644 --- a/backend/Controllers/UploadController.php +++ b/backend/Controllers/UploadController.php @@ -65,6 +65,8 @@ class UploadController $file = $request->files->get('file'); + $overwrite_on_upload = (bool) $this->config->get('overwrite_on_upload', false); + if (! $file || ! $file->isValid() || $file->getSize() > $this->config->get('frontend_config.upload_max_size')) { return $response->json('Bad file', 422); } @@ -103,7 +105,7 @@ class UploadController } $final = $this->tmpfs->readStream($file_name); - $res = $this->storage->store($destination, $final['filename'], $final['stream']); + $res = $this->storage->store($destination, $final['filename'], $final['stream'], $overwrite_on_upload); // cleanup $this->tmpfs->remove($file_name); diff --git a/backend/Services/Storage/Filesystem.php b/backend/Services/Storage/Filesystem.php index a748206..5b15350 100644 --- a/backend/Services/Storage/Filesystem.php +++ b/backend/Services/Storage/Filesystem.php @@ -161,12 +161,16 @@ class Filesystem implements Service return $this->storage->rename($from, $to); } - public function store(string $path, string $name, $resource): bool + public function store(string $path, string $name, $resource, bool $overwrite = false): bool { $destination = $this->joinPaths($this->applyPathPrefix($path), $name); while ($this->storage->has($destination)) { - $destination = $this->upcountName($destination); + if ($overwrite) { + $this->deleteFile($destination); + } else { + $destination = $this->upcountName($destination); + } } return $this->storage->putStream($destination, $resource); diff --git a/configuration_sample.php b/configuration_sample.php index 334e680..7ccb9ae 100644 --- a/configuration_sample.php +++ b/configuration_sample.php @@ -3,6 +3,7 @@ return [ 'public_path' => APP_PUBLIC_PATH, 'public_dir' => APP_PUBLIC_DIR, + 'overwrite_on_upload' => false, 'frontend_config' => [ 'app_name' => 'FileGator', diff --git a/tests/backend/Unit/FilesystemTest.php b/tests/backend/Unit/FilesystemTest.php index 5eccb5b..6df7553 100644 --- a/tests/backend/Unit/FilesystemTest.php +++ b/tests/backend/Unit/FilesystemTest.php @@ -10,10 +10,10 @@ namespace Tests\Unit; +use Exception; use Filegator\Services\Storage\Filesystem; use League\Flysystem\Adapter\Local; use Tests\TestCase; -use Exception; /** * @internal @@ -265,6 +265,33 @@ class FilesystemTest extends TestCase $this->assertEquals('croissant', stream_get_contents($ret['stream'])); } + public function testStoringFileWithTheSameNameOverwritesOriginalFile() + { + // create dummy file + $string = 'lorem ipsum'; + $resource = fopen('data://text/plain;base64,'.base64_encode($string), 'r'); + + // and store it + $this->storage->store('/', 'singletone.txt', $resource); + fclose($resource); + + // first file contains lorem ipsum + $ret = $this->storage->readStream('singletone.txt'); + $this->assertEquals('lorem ipsum', stream_get_contents($ret['stream'])); + + // create another dummy file + $string = 'croissant'; + $resource = fopen('data://text/plain;base64,'.base64_encode($string), 'r'); + + // and store it with the same name + $this->storage->store('/', 'singletone.txt', $resource, true); + fclose($resource); + + // first file is overwritten + $ret = $this->storage->readStream('singletone.txt'); + $this->assertEquals('croissant', stream_get_contents($ret['stream'])); + } + public function testCreatingFileWithTheSameNameUpcountsFilenameRecursively() { $this->storage->createFile('/', 'test.txt'); diff --git a/tests/backend/configuration.php b/tests/backend/configuration.php index 6758a1a..0285db1 100644 --- a/tests/backend/configuration.php +++ b/tests/backend/configuration.php @@ -3,6 +3,7 @@ return [ 'public_path' => '', 'public_dir' => __DIR__.'/../../dist', + 'overwrite_on_upload' => false, 'frontend_config' => [ 'app_name' => 'FileGator',