From 557bed941917fd8d35ca2c3e478dbf3c071dbd19 Mon Sep 17 00:00:00 2001 From: Simon vom Eyser Date: Wed, 14 Oct 2020 19:03:47 +0200 Subject: [PATCH 1/3] Add additional naming strategy to replace files on upload --- .../Services/ReplaceFileNamingStrategy.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/Http/Services/ReplaceFileNamingStrategy.php diff --git a/src/Http/Services/ReplaceFileNamingStrategy.php b/src/Http/Services/ReplaceFileNamingStrategy.php new file mode 100644 index 0000000..ec0ba98 --- /dev/null +++ b/src/Http/Services/ReplaceFileNamingStrategy.php @@ -0,0 +1,19 @@ +getClientOriginalName(); + + if ($this->storage->exists($currentFolder . '/' . $filename)) { + $this->storage->delete($currentFolder . '/' . $filename); + } + + return $filename; + } +} From 317540d1d1941a29f3cb1b9347746881f270bc7a Mon Sep 17 00:00:00 2001 From: Simon vom Eyser Date: Wed, 14 Oct 2020 20:08:07 +0200 Subject: [PATCH 2/3] Add documentation for new naming strategy --- docs/2.1/customization.md | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/docs/2.1/customization.md b/docs/2.1/customization.md index a5d29e0..ad585b4 100644 --- a/docs/2.1/customization.md +++ b/docs/2.1/customization.md @@ -4,13 +4,22 @@ You can resolve the upload file name creating your own class and setting in `config/filemanager.php` -These class should extends `Infinety\Filemanager\Http\Services\AbstractNamingStrategy`. +These classes should extend `Infinety\Filemanager\Http\Services\AbstractNamingStrategy`. +Per default, a random text of 7 characters will be added, if the file already exists. -Default class get the filename of uploaded file and check if exists. If exists add a random text of 7 characters. +You can also use the "Replace Strategy" to replace a file when it has the same name -You can create your custom class to customize the uploaded name. +```php + // config/filemanager.php + + 'naming' => \Infinety\Filemanager\Http\Services\ReplaceFileNamingStrategy::class, +``` + + +You can easily create your own custom class to customize the uploaded name. +This is the implementation of the Default Class: ```php namespace Infinety\Filemanager\Http\Services; From 175a39d8c7092ec3412b4221b3f760d963d58b92 Mon Sep 17 00:00:00 2001 From: Simon vom Eyser Date: Wed, 14 Oct 2020 20:18:29 +0200 Subject: [PATCH 3/3] Fix styleci issue --- src/Http/Services/ReplaceFileNamingStrategy.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Http/Services/ReplaceFileNamingStrategy.php b/src/Http/Services/ReplaceFileNamingStrategy.php index ec0ba98..a37fa6b 100644 --- a/src/Http/Services/ReplaceFileNamingStrategy.php +++ b/src/Http/Services/ReplaceFileNamingStrategy.php @@ -10,8 +10,8 @@ public function name(string $currentFolder, UploadedFile $file): string { $filename = $file->getClientOriginalName(); - if ($this->storage->exists($currentFolder . '/' . $filename)) { - $this->storage->delete($currentFolder . '/' . $filename); + if ($this->storage->exists($currentFolder.'/'.$filename)) { + $this->storage->delete($currentFolder.'/'.$filename); } return $filename;