Fixed: Slug a filename when uploading
This commit is contained in:
parent
fe794ab094
commit
6c9d9b908b
41
Plugin.php
41
Plugin.php
|
@ -3,14 +3,12 @@
|
||||||
namespace Aweos\Resizer;
|
namespace Aweos\Resizer;
|
||||||
|
|
||||||
use Aweos\Resizer\Classes\CacheManager;
|
use Aweos\Resizer\Classes\CacheManager;
|
||||||
use Aweos\Resizer\Classes\FileObserver;
|
|
||||||
use Aweos\Resizer\Classes\ImageResizer;
|
use Aweos\Resizer\Classes\ImageResizer;
|
||||||
use Aweos\Resizer\Jobs\ResizeJob;
|
|
||||||
use Aweos\Resizer\Console\ResizeMake;
|
use Aweos\Resizer\Console\ResizeMake;
|
||||||
use Aweos\Resizer\Console\ResizePurge;
|
use Aweos\Resizer\Console\ResizePurge;
|
||||||
use Aweos\Resizer\Jobs\DeleteJob;
|
use Aweos\Resizer\Jobs\DeleteJob;
|
||||||
use Aweos\Resizer\Jobs\MoveJob;
|
use Aweos\Resizer\Jobs\MoveJob;
|
||||||
use Aweos\Resizer\Lib\MediaPath;
|
use Aweos\Resizer\Jobs\ResizeJob;
|
||||||
use Aweos\Resizer\Lib\StorageMediaPath;
|
use Aweos\Resizer\Lib\StorageMediaPath;
|
||||||
use Aweos\Resizer\Models\Setting;
|
use Aweos\Resizer\Models\Setting;
|
||||||
use Event;
|
use Event;
|
||||||
|
@ -20,7 +18,7 @@ use System\Classes\PluginBase;
|
||||||
use System\Models\File;
|
use System\Models\File;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* resizer Plugin Information File
|
* resizer Plugin Information File.
|
||||||
*/
|
*/
|
||||||
class Plugin extends PluginBase
|
class Plugin extends PluginBase
|
||||||
{
|
{
|
||||||
|
@ -35,7 +33,7 @@ class Plugin extends PluginBase
|
||||||
'name' => 'aweos.resizer',
|
'name' => 'aweos.resizer',
|
||||||
'description' => 'No description provided yet...',
|
'description' => 'No description provided yet...',
|
||||||
'author' => 'aweos',
|
'author' => 'aweos',
|
||||||
'icon' => 'icon-leaf'
|
'icon' => 'icon-leaf',
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -57,7 +55,7 @@ class Plugin extends PluginBase
|
||||||
*/
|
*/
|
||||||
public function boot()
|
public function boot()
|
||||||
{
|
{
|
||||||
app()->bind(ImageResizer::class, function() {
|
app()->bind(ImageResizer::class, function () {
|
||||||
$disk = (new File())->getDisk();
|
$disk = (new File())->getDisk();
|
||||||
$dir = (new File(['is_public' => true]))->getStorageDirectory().'c/';
|
$dir = (new File(['is_public' => true]))->getStorageDirectory().'c/';
|
||||||
$media = MediaLibrary::instance();
|
$media = MediaLibrary::instance();
|
||||||
|
@ -67,28 +65,33 @@ class Plugin extends PluginBase
|
||||||
|
|
||||||
app()->bind('resize', fn () => app(CacheManager::class));
|
app()->bind('resize', fn () => app(CacheManager::class));
|
||||||
|
|
||||||
Event::listen('media.file.upload', function($widget, $filePath, $uploadedFile) {
|
Event::listen('media.file.upload', function ($widget, $filePath, $uploadedFile) {
|
||||||
if ((new StorageMediaPath($filePath))->shouldProcess()) {
|
$sluggedPath = (new StorageMediaPath($filePath))->sluggify();
|
||||||
Queue::push(ResizeJob::class, [$filePath], Setting::get('queue'));
|
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'));
|
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'));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
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()) {
|
if ((new StorageMediaPath($new))->shouldProcess() || (new StorageMediaPath($old))->shouldProcess()) {
|
||||||
Queue::push(MoveJob::class, [$old, $new], Setting::get('queue'));
|
Queue::push(MoveJob::class, [$old, $new], Setting::get('queue'));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public function registerSettings() {
|
public function registerSettings()
|
||||||
|
{
|
||||||
return [
|
return [
|
||||||
'resizer' => [
|
'resizer' => [
|
||||||
'label' => 'Resizer Settings',
|
'label' => 'Resizer Settings',
|
||||||
|
@ -98,12 +101,13 @@ class Plugin extends PluginBase
|
||||||
'class' => '\Aweos\Resizer\Models\Setting',
|
'class' => '\Aweos\Resizer\Models\Setting',
|
||||||
'order' => 500,
|
'order' => 500,
|
||||||
'keywords' => 'setting',
|
'keywords' => 'setting',
|
||||||
'permissions' => ['aweos.resizer.*']
|
'permissions' => ['aweos.resizer.*'],
|
||||||
]
|
],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function registerMarkupTags() {
|
public function registerMarkupTags()
|
||||||
|
{
|
||||||
return [
|
return [
|
||||||
'filters' => [
|
'filters' => [
|
||||||
'resize' => fn ($media, $size = 'original', $sizes = null, $options = []) => app(CacheManager::class)->get(
|
'resize' => fn ($media, $size = 'original', $sizes = null, $options = []) => app(CacheManager::class)->get(
|
||||||
|
@ -111,9 +115,8 @@ class Plugin extends PluginBase
|
||||||
$size,
|
$size,
|
||||||
$sizes,
|
$sizes,
|
||||||
$options,
|
$options,
|
||||||
)
|
),
|
||||||
]
|
],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,14 +7,15 @@ use Aweos\Resizer\Compressors\Factory as CompressorFactory;
|
||||||
use Aweos\Resizer\Models\Setting;
|
use Aweos\Resizer\Models\Setting;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
use Illuminate\Support\Facades\Storage;
|
use Illuminate\Support\Facades\Storage;
|
||||||
use MediaLibrary;
|
|
||||||
|
|
||||||
abstract class MediaPath
|
abstract class MediaPath
|
||||||
{
|
{
|
||||||
|
|
||||||
protected string $path;
|
protected string $path;
|
||||||
|
|
||||||
abstract public function get(): string;
|
abstract public function get(): string;
|
||||||
|
|
||||||
abstract public function root(): string;
|
abstract public function root(): string;
|
||||||
|
|
||||||
abstract public function publicUrl(): string;
|
abstract public function publicUrl(): string;
|
||||||
|
|
||||||
public function __construct(string $path)
|
public function __construct(string $path)
|
||||||
|
@ -68,9 +69,9 @@ abstract class MediaPath
|
||||||
|
|
||||||
public function shouldProcess(): bool
|
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.'/')
|
fn ($folder) => starts_with('/'.$this->normal(), $folder.'/')
|
||||||
) !== null;
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function versions(): Collection
|
public function versions(): Collection
|
||||||
|
@ -110,4 +111,16 @@ abstract class MediaPath
|
||||||
return "media/{$this->normal()}";
|
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\Exceptions\ResizerException;
|
||||||
use Aweos\Resizer\Models\Setting;
|
use Aweos\Resizer\Models\Setting;
|
||||||
use Aweos\Resizer\Tests\TestCase;
|
|
||||||
use Event;
|
use Event;
|
||||||
use Illuminate\Http\UploadedFile;
|
use Illuminate\Http\UploadedFile;
|
||||||
use Illuminate\Support\Facades\Cache;
|
use Illuminate\Support\Facades\Cache;
|
||||||
|
@ -13,7 +12,6 @@ use Storage;
|
||||||
|
|
||||||
class ResizerTest extends TestCase
|
class ResizerTest extends TestCase
|
||||||
{
|
{
|
||||||
|
|
||||||
public function setUp(): void
|
public function setUp(): void
|
||||||
{
|
{
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
|
@ -90,6 +88,21 @@ class ResizerTest extends TestCase
|
||||||
$this->assertHasFile('pages/test-500x600.jpg');
|
$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
|
public function testCopyTwoDirectoriesDeepButNotAnotherDirectory(): void
|
||||||
{
|
{
|
||||||
Setting::set('folders', [['folder' => '/pages']]);
|
Setting::set('folders', [['folder' => '/pages']]);
|
||||||
|
@ -256,5 +269,4 @@ class ResizerTest extends TestCase
|
||||||
$this->assertHasFile('pages/test-1275x1650.pdf.jpg');
|
$this->assertHasFile('pages/test-1275x1650.pdf.jpg');
|
||||||
$this->assertFileCount(4, 'pages');
|
$this->assertFileCount(4, 'pages');
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue