From d2253a341723e241565b3926badf54a6cece4294 Mon Sep 17 00:00:00 2001 From: philipp lang Date: Tue, 22 Mar 2022 22:54:57 +0100 Subject: [PATCH] Fixed: Sluggify created folder --- Plugin.php | 28 ++++++++++++++++++++-- lib/MediaPath.php | 20 +++++++++++----- tests/SlugTest.php | 59 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 99 insertions(+), 8 deletions(-) create mode 100644 tests/SlugTest.php diff --git a/Plugin.php b/Plugin.php index 3d0c340..a4b3a7c 100644 --- a/Plugin.php +++ b/Plugin.php @@ -16,6 +16,7 @@ use MediaLibrary; use Queue; use System\Classes\PluginBase; use System\Models\File; +use ValidationException; /** * resizer Plugin Information File. @@ -65,8 +66,32 @@ class Plugin extends PluginBase app()->bind('resize', fn () => app(CacheManager::class)); + Event::listen('media.folder.create', function ($widget, string $folder) { + $folder = '/'.trim($folder, '/'); + $pathinfo = pathinfo($folder); + + if ('/' === $pathinfo['dirname']) { + $sluggedFolder = '/'.StorageMediaPath::sluggifyString(trim($folder, '/')); + if ($sluggedFolder !== $folder) { + if (MediaLibrary::instance()->folderExists($sluggedFolder)) { + MediaLibrary::instance()->deleteFolder($folder); + throw new ValidationException(['error' => 'Ordner existiert bereits.']); + } + MediaLibrary::instance()->moveFolder($folder, $sluggedFolder); + } + } else { + $sluggedFolder = $pathinfo['dirname'].'/'.StorageMediaPath::sluggifyString($pathinfo['filename']); + if ($sluggedFolder !== $folder) { + if (MediaLibrary::instance()->folderExists($sluggedFolder)) { + MediaLibrary::instance()->deleteFolder($folder); + throw new ValidationException(['error' => 'Ordner existiert bereits.']); + } + MediaLibrary::instance()->moveFolder($folder, $sluggedFolder); + } + } + }); Event::listen('media.file.upload', function ($widget, $filePath, $uploadedFile) { - $sluggedPath = (new StorageMediaPath($filePath))->sluggify(); + $sluggedPath = (new StorageMediaPath($filePath))->sluggifyPath(); if ($filePath !== $sluggedPath) { MediaLibrary::instance()->moveFile($filePath, $sluggedPath); } @@ -77,7 +102,6 @@ class Plugin extends PluginBase Event::listen('media.file.delete', function ($widget, $filePath) { Queue::push(DeleteJob::class, [$filePath], Setting::get('queue')); }); - Event::listen('media.file.move', function ($widget, $old, $new) { if ((new StorageMediaPath($new))->shouldProcess() || (new StorageMediaPath($old))->shouldProcess()) { Queue::push(MoveJob::class, [$old, $new.'/'.pathinfo($old, PATHINFO_FILENAME)], Setting::get('queue')); diff --git a/lib/MediaPath.php b/lib/MediaPath.php index 8e30af8..95425a2 100644 --- a/lib/MediaPath.php +++ b/lib/MediaPath.php @@ -106,21 +106,29 @@ abstract class MediaPath return $return; } + public function sluggifyPath(): string + { + $fileinfo = pathinfo($this->path); + $filename = $fileinfo['dirname'].'/'; + + return $filename + .static::sluggifyString($fileinfo['filename']) + .'.' + .$fileinfo['extension']; + } + protected function storagePath(): string { return "media/{$this->normal()}"; } - public function sluggify(): string + public static function sluggifyString(string $input): string { - $fileinfo = pathinfo($this->path); - $filename = $fileinfo['dirname'].'/'; - - return $filename.str_slug(strtr($fileinfo['filename'], [ + return str_slug(strtr($input, [ 'ö' => 'oe', 'ä' => 'ae', 'ü' => 'ue', 'ß' => 'ss', - ])).'.'.$fileinfo['extension']; + ])); } } diff --git a/tests/SlugTest.php b/tests/SlugTest.php new file mode 100644 index 0000000..6f56082 --- /dev/null +++ b/tests/SlugTest.php @@ -0,0 +1,59 @@ +media = MediaLibrary::instance(); + } + + public function testDontRenameFolderIfSluggingIsNotNeeded(): void + { + $file = UploadedFile::fake()->image('test.jpg', 500, 600); + $this->media->makeFolder('testbb'); + + Event::fire('media.folder.create', [null, '//testbb', null]); + + Storage::disk('local')->assertExists('media/testbb'); + } + + public function testRenameFolder(): void + { + $file = UploadedFile::fake()->image('test.jpg', 500, 600); + $this->media->makeFolder('test bb'); + + Event::fire('media.folder.create', [null, '//test bb', null]); + + Storage::disk('local')->assertExists('media/test-bb'); + Storage::disk('local')->assertMissing('media/test bb'); + } + + public function testRenameSubfolder(): void + { + $file = UploadedFile::fake()->image('test.jpg', 500, 600); + $this->media->makeFolder('test/aa bb'); + + Event::fire('media.folder.create', [null, '/test/aa bb', null]); + + Storage::disk('local')->assertExists('media/test/aa-bb'); + } + + public function testRenameSubfolderWithDoubleSlash(): void + { + $file = UploadedFile::fake()->image('test.jpg', 500, 600); + $this->media->makeFolder('test/aa bb'); + + Event::fire('media.folder.create', [null, '//test/aa bb', null]); + + Storage::disk('local')->assertExists('media/test/aa-bb'); + } +}