Fixed: Sluggify created folder
continuous-integration/drone/push Build encountered an error Details

This commit is contained in:
philipp lang 2022-03-22 22:54:57 +01:00
parent 6c9d9b908b
commit d2253a3417
3 changed files with 99 additions and 8 deletions

View File

@ -16,6 +16,7 @@ use MediaLibrary;
use Queue; use Queue;
use System\Classes\PluginBase; use System\Classes\PluginBase;
use System\Models\File; use System\Models\File;
use ValidationException;
/** /**
* resizer Plugin Information File. * resizer Plugin Information File.
@ -65,8 +66,32 @@ class Plugin extends PluginBase
app()->bind('resize', fn () => app(CacheManager::class)); 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) { Event::listen('media.file.upload', function ($widget, $filePath, $uploadedFile) {
$sluggedPath = (new StorageMediaPath($filePath))->sluggify(); $sluggedPath = (new StorageMediaPath($filePath))->sluggifyPath();
if ($filePath !== $sluggedPath) { if ($filePath !== $sluggedPath) {
MediaLibrary::instance()->moveFile($filePath, $sluggedPath); MediaLibrary::instance()->moveFile($filePath, $sluggedPath);
} }
@ -77,7 +102,6 @@ class Plugin extends PluginBase
Event::listen('media.file.delete', function ($widget, $filePath) { Event::listen('media.file.delete', function ($widget, $filePath) {
Queue::push(DeleteJob::class, [$filePath], Setting::get('queue')); Queue::push(DeleteJob::class, [$filePath], Setting::get('queue'));
}); });
Event::listen('media.file.move', function ($widget, $old, $new) { Event::listen('media.file.move', function ($widget, $old, $new) {
if ((new StorageMediaPath($new))->shouldProcess() || (new StorageMediaPath($old))->shouldProcess()) { if ((new StorageMediaPath($new))->shouldProcess() || (new StorageMediaPath($old))->shouldProcess()) {
Queue::push(MoveJob::class, [$old, $new.'/'.pathinfo($old, PATHINFO_FILENAME)], Setting::get('queue')); Queue::push(MoveJob::class, [$old, $new.'/'.pathinfo($old, PATHINFO_FILENAME)], Setting::get('queue'));

View File

@ -106,21 +106,29 @@ abstract class MediaPath
return $return; 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 protected function storagePath(): string
{ {
return "media/{$this->normal()}"; return "media/{$this->normal()}";
} }
public function sluggify(): string public static function sluggifyString(string $input): string
{ {
$fileinfo = pathinfo($this->path); return str_slug(strtr($input, [
$filename = $fileinfo['dirname'].'/';
return $filename.str_slug(strtr($fileinfo['filename'], [
'ö' => 'oe', 'ö' => 'oe',
'ä' => 'ae', 'ä' => 'ae',
'ü' => 'ue', 'ü' => 'ue',
'ß' => 'ss', 'ß' => 'ss',
])).'.'.$fileinfo['extension']; ]));
} }
} }

59
tests/SlugTest.php Normal file
View File

@ -0,0 +1,59 @@
<?php
namespace Aweos\Resizer\Tests;
use Event;
use Illuminate\Http\UploadedFile;
use MediaLibrary;
use Storage;
class SlugTest extends TestCase
{
public function setUp(): void
{
parent::setUp();
Storage::fake('local');
$this->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');
}
}