Fixed: Slug a filename when uploading
This commit is contained in:
parent
fe794ab094
commit
6c9d9b908b
57
Plugin.php
57
Plugin.php
|
@ -3,14 +3,12 @@
|
|||
namespace Aweos\Resizer;
|
||||
|
||||
use Aweos\Resizer\Classes\CacheManager;
|
||||
use Aweos\Resizer\Classes\FileObserver;
|
||||
use Aweos\Resizer\Classes\ImageResizer;
|
||||
use Aweos\Resizer\Jobs\ResizeJob;
|
||||
use Aweos\Resizer\Console\ResizeMake;
|
||||
use Aweos\Resizer\Console\ResizePurge;
|
||||
use Aweos\Resizer\Jobs\DeleteJob;
|
||||
use Aweos\Resizer\Jobs\MoveJob;
|
||||
use Aweos\Resizer\Lib\MediaPath;
|
||||
use Aweos\Resizer\Jobs\ResizeJob;
|
||||
use Aweos\Resizer\Lib\StorageMediaPath;
|
||||
use Aweos\Resizer\Models\Setting;
|
||||
use Event;
|
||||
|
@ -20,7 +18,7 @@ use System\Classes\PluginBase;
|
|||
use System\Models\File;
|
||||
|
||||
/**
|
||||
* resizer Plugin Information File
|
||||
* resizer Plugin Information File.
|
||||
*/
|
||||
class Plugin extends PluginBase
|
||||
{
|
||||
|
@ -32,10 +30,10 @@ class Plugin extends PluginBase
|
|||
public function pluginDetails()
|
||||
{
|
||||
return [
|
||||
'name' => 'aweos.resizer',
|
||||
'name' => 'aweos.resizer',
|
||||
'description' => 'No description provided yet...',
|
||||
'author' => 'aweos',
|
||||
'icon' => 'icon-leaf'
|
||||
'author' => 'aweos',
|
||||
'icon' => 'icon-leaf',
|
||||
];
|
||||
}
|
||||
|
||||
|
@ -57,7 +55,7 @@ class Plugin extends PluginBase
|
|||
*/
|
||||
public function boot()
|
||||
{
|
||||
app()->bind(ImageResizer::class, function() {
|
||||
app()->bind(ImageResizer::class, function () {
|
||||
$disk = (new File())->getDisk();
|
||||
$dir = (new File(['is_public' => true]))->getStorageDirectory().'c/';
|
||||
$media = MediaLibrary::instance();
|
||||
|
@ -67,43 +65,49 @@ class Plugin extends PluginBase
|
|||
|
||||
app()->bind('resize', fn () => app(CacheManager::class));
|
||||
|
||||
Event::listen('media.file.upload', function($widget, $filePath, $uploadedFile) {
|
||||
if ((new StorageMediaPath($filePath))->shouldProcess()) {
|
||||
Queue::push(ResizeJob::class, [$filePath], Setting::get('queue'));
|
||||
Event::listen('media.file.upload', function ($widget, $filePath, $uploadedFile) {
|
||||
$sluggedPath = (new StorageMediaPath($filePath))->sluggify();
|
||||
if ($filePath !== $sluggedPath) {
|
||||
MediaLibrary::instance()->moveFile($filePath, $sluggedPath);
|
||||
}
|
||||
if ((new StorageMediaPath($sluggedPath))->shouldProcess()) {
|
||||
Queue::push(ResizeJob::class, [$sluggedPath], Setting::get('queue'));
|
||||
}
|
||||
});
|
||||
Event::listen('media.file.delete', function($widget, $filePath) {
|
||||
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) {
|
||||
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'));
|
||||
}
|
||||
});
|
||||
Event::listen('media.file.rename', function($widget, $old, $new) {
|
||||
Event::listen('media.file.rename', function ($widget, $old, $new) {
|
||||
if ((new StorageMediaPath($new))->shouldProcess() || (new StorageMediaPath($old))->shouldProcess()) {
|
||||
Queue::push(MoveJob::class, [$old, $new], Setting::get('queue'));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public function registerSettings() {
|
||||
public function registerSettings()
|
||||
{
|
||||
return [
|
||||
'resizer' => [
|
||||
'label' => 'Resizer Settings',
|
||||
'label' => 'Resizer Settings',
|
||||
'description' => 'Change how images are resized and compressed',
|
||||
'category' => 'Base',
|
||||
'icon' => 'icon-cog',
|
||||
'class' => '\Aweos\Resizer\Models\Setting',
|
||||
'order' => 500,
|
||||
'keywords' => 'setting',
|
||||
'permissions' => ['aweos.resizer.*']
|
||||
]
|
||||
'category' => 'Base',
|
||||
'icon' => 'icon-cog',
|
||||
'class' => '\Aweos\Resizer\Models\Setting',
|
||||
'order' => 500,
|
||||
'keywords' => 'setting',
|
||||
'permissions' => ['aweos.resizer.*'],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public function registerMarkupTags() {
|
||||
public function registerMarkupTags()
|
||||
{
|
||||
return [
|
||||
'filters' => [
|
||||
'resize' => fn ($media, $size = 'original', $sizes = null, $options = []) => app(CacheManager::class)->get(
|
||||
|
@ -111,9 +115,8 @@ class Plugin extends PluginBase
|
|||
$size,
|
||||
$sizes,
|
||||
$options,
|
||||
)
|
||||
]
|
||||
),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -7,14 +7,15 @@ use Aweos\Resizer\Compressors\Factory as CompressorFactory;
|
|||
use Aweos\Resizer\Models\Setting;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use MediaLibrary;
|
||||
|
||||
abstract class MediaPath
|
||||
{
|
||||
|
||||
protected string $path;
|
||||
|
||||
abstract public function get(): string;
|
||||
|
||||
abstract public function root(): string;
|
||||
|
||||
abstract public function publicUrl(): string;
|
||||
|
||||
public function __construct(string $path)
|
||||
|
@ -68,9 +69,9 @@ abstract class MediaPath
|
|||
|
||||
public function shouldProcess(): bool
|
||||
{
|
||||
return collect(Setting::get('folders'))->pluck('folder')->first(
|
||||
return null !== collect(Setting::get('folders'))->pluck('folder')->first(
|
||||
fn ($folder) => starts_with('/'.$this->normal(), $folder.'/')
|
||||
) !== null;
|
||||
);
|
||||
}
|
||||
|
||||
public function versions(): Collection
|
||||
|
@ -110,4 +111,16 @@ abstract class MediaPath
|
|||
return "media/{$this->normal()}";
|
||||
}
|
||||
|
||||
public function sluggify(): string
|
||||
{
|
||||
$fileinfo = pathinfo($this->path);
|
||||
$filename = $fileinfo['dirname'].'/';
|
||||
|
||||
return $filename.str_slug(strtr($fileinfo['filename'], [
|
||||
'ö' => 'oe',
|
||||
'ä' => 'ae',
|
||||
'ü' => 'ue',
|
||||
'ß' => 'ss',
|
||||
])).'.'.$fileinfo['extension'];
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,7 +4,6 @@ namespace Aweos\Resizer\Tests;
|
|||
|
||||
use Aweos\Resizer\Exceptions\ResizerException;
|
||||
use Aweos\Resizer\Models\Setting;
|
||||
use Aweos\Resizer\Tests\TestCase;
|
||||
use Event;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
@ -13,7 +12,6 @@ use Storage;
|
|||
|
||||
class ResizerTest extends TestCase
|
||||
{
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
@ -90,6 +88,21 @@ class ResizerTest extends TestCase
|
|||
$this->assertHasFile('pages/test-500x600.jpg');
|
||||
}
|
||||
|
||||
public function testSluggifyFilename(): void
|
||||
{
|
||||
Setting::set('folders', [['folder' => '/pages']]);
|
||||
Setting::set('sizes', []);
|
||||
Setting::set('breakpoints', []);
|
||||
|
||||
$file = UploadedFile::fake()->image('test.jpg', 500, 600);
|
||||
$this->media->put('/pages/test ö space.jpg', $file->get());
|
||||
Event::fire('media.file.upload', [null, 'pages/test ö space.jpg', null]);
|
||||
Storage::assertExists('media/pages/test-oe-space.jpg');
|
||||
|
||||
$this->assertFileCount(1, 'pages');
|
||||
$this->assertHasFile('pages/test-oe-space-500x600.jpg');
|
||||
}
|
||||
|
||||
public function testCopyTwoDirectoriesDeepButNotAnotherDirectory(): void
|
||||
{
|
||||
Setting::set('folders', [['folder' => '/pages']]);
|
||||
|
@ -256,5 +269,4 @@ class ResizerTest extends TestCase
|
|||
$this->assertHasFile('pages/test-1275x1650.pdf.jpg');
|
||||
$this->assertFileCount(4, 'pages');
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue