2021-11-01 12:52:45 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Aweos\Resizer;
|
2019-09-17 23:14:29 +02:00
|
|
|
|
2021-09-06 14:37:35 +02:00
|
|
|
use Aweos\Resizer\Classes\CacheManager;
|
2021-09-06 02:31:27 +02:00
|
|
|
use Aweos\Resizer\Classes\ImageResizer;
|
2024-09-28 01:31:05 +02:00
|
|
|
use Aweos\Resizer\Console\ResizeCacheClear;
|
2019-09-17 23:14:29 +02:00
|
|
|
use Aweos\Resizer\Console\ResizeMake;
|
|
|
|
use Aweos\Resizer\Console\ResizePurge;
|
2022-02-16 02:49:19 +01:00
|
|
|
use Aweos\Resizer\Jobs\DeleteJob;
|
|
|
|
use Aweos\Resizer\Jobs\MoveJob;
|
2022-03-22 22:14:39 +01:00
|
|
|
use Aweos\Resizer\Jobs\ResizeJob;
|
2022-02-16 02:49:19 +01:00
|
|
|
use Aweos\Resizer\Lib\StorageMediaPath;
|
2021-11-01 12:52:45 +01:00
|
|
|
use Aweos\Resizer\Models\Setting;
|
2021-09-06 02:31:27 +02:00
|
|
|
use Event;
|
2021-12-17 01:29:15 +01:00
|
|
|
use MediaLibrary;
|
2021-09-06 02:31:27 +02:00
|
|
|
use Queue;
|
|
|
|
use System\Classes\PluginBase;
|
|
|
|
use System\Models\File;
|
2022-03-22 22:54:57 +01:00
|
|
|
use ValidationException;
|
2019-09-17 23:14:29 +02:00
|
|
|
|
|
|
|
/**
|
2022-03-22 22:14:39 +01:00
|
|
|
* resizer Plugin Information File.
|
2019-09-17 23:14:29 +02:00
|
|
|
*/
|
|
|
|
class Plugin extends PluginBase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Returns information about this plugin.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function pluginDetails()
|
|
|
|
{
|
|
|
|
return [
|
2022-03-22 22:14:39 +01:00
|
|
|
'name' => 'aweos.resizer',
|
2019-09-17 23:14:29 +02:00
|
|
|
'description' => 'No description provided yet...',
|
2022-03-22 22:14:39 +01:00
|
|
|
'author' => 'aweos',
|
|
|
|
'icon' => 'icon-leaf',
|
2019-09-17 23:14:29 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register method, called when the plugin is first registered.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function register()
|
|
|
|
{
|
|
|
|
$this->registerConsoleCommand('resizer.resizemake', ResizeMake::class);
|
|
|
|
$this->registerConsoleCommand('resizer.resizepurge', ResizePurge::class);
|
2024-09-28 01:31:05 +02:00
|
|
|
$this->registerConsoleCommand('resizer.resize-cache-clear', ResizeCacheClear::class);
|
2019-09-17 23:14:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Boot method, called right before the request route.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function boot()
|
|
|
|
{
|
2022-03-22 22:14:39 +01:00
|
|
|
app()->bind(ImageResizer::class, function () {
|
2021-09-06 02:31:27 +02:00
|
|
|
$disk = (new File())->getDisk();
|
2024-09-28 01:31:05 +02:00
|
|
|
$dir = (new File(['is_public' => true]))->getStorageDirectory() . 'c/';
|
2021-09-06 02:31:27 +02:00
|
|
|
$media = MediaLibrary::instance();
|
|
|
|
|
|
|
|
return new ImageResizer($disk, $dir, $media);
|
|
|
|
});
|
|
|
|
|
2021-09-19 23:08:11 +02:00
|
|
|
app()->bind('resize', fn () => app(CacheManager::class));
|
|
|
|
|
2022-03-22 22:54:57 +01:00
|
|
|
Event::listen('media.folder.create', function ($widget, string $folder) {
|
2024-09-28 01:31:05 +02:00
|
|
|
$folder = '/' . trim($folder, '/');
|
2022-03-22 22:54:57 +01:00
|
|
|
$pathinfo = pathinfo($folder);
|
|
|
|
|
|
|
|
if ('/' === $pathinfo['dirname']) {
|
2024-09-28 01:31:05 +02:00
|
|
|
$sluggedFolder = '/' . StorageMediaPath::sluggifyString(trim($folder, '/'));
|
2022-03-22 22:54:57 +01:00
|
|
|
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 {
|
2024-09-28 01:31:05 +02:00
|
|
|
$sluggedFolder = $pathinfo['dirname'] . '/' . StorageMediaPath::sluggifyString($pathinfo['filename']);
|
2022-03-22 22:54:57 +01:00
|
|
|
if ($sluggedFolder !== $folder) {
|
|
|
|
if (MediaLibrary::instance()->folderExists($sluggedFolder)) {
|
|
|
|
MediaLibrary::instance()->deleteFolder($folder);
|
|
|
|
throw new ValidationException(['error' => 'Ordner existiert bereits.']);
|
|
|
|
}
|
|
|
|
MediaLibrary::instance()->moveFolder($folder, $sluggedFolder);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2022-03-22 22:14:39 +01:00
|
|
|
Event::listen('media.file.upload', function ($widget, $filePath, $uploadedFile) {
|
2022-03-22 22:54:57 +01:00
|
|
|
$sluggedPath = (new StorageMediaPath($filePath))->sluggifyPath();
|
2022-03-22 22:14:39 +01:00
|
|
|
if ($filePath !== $sluggedPath) {
|
|
|
|
MediaLibrary::instance()->moveFile($filePath, $sluggedPath);
|
|
|
|
}
|
|
|
|
if ((new StorageMediaPath($sluggedPath))->shouldProcess()) {
|
|
|
|
Queue::push(ResizeJob::class, [$sluggedPath], Setting::get('queue'));
|
2021-09-06 02:31:27 +02:00
|
|
|
}
|
|
|
|
});
|
2022-03-22 22:14:39 +01:00
|
|
|
Event::listen('media.file.delete', function ($widget, $filePath) {
|
2022-02-16 02:49:19 +01:00
|
|
|
Queue::push(DeleteJob::class, [$filePath], Setting::get('queue'));
|
2021-09-06 02:31:27 +02:00
|
|
|
});
|
2022-03-22 22:14:39 +01:00
|
|
|
Event::listen('media.file.move', function ($widget, $old, $new) {
|
2022-02-16 02:49:19 +01:00
|
|
|
if ((new StorageMediaPath($new))->shouldProcess() || (new StorageMediaPath($old))->shouldProcess()) {
|
2024-09-28 01:31:05 +02:00
|
|
|
Queue::push(MoveJob::class, [$old, $new . '/' . pathinfo($old, PATHINFO_FILENAME)], Setting::get('queue'));
|
2022-02-16 02:49:19 +01:00
|
|
|
}
|
2021-09-06 02:31:27 +02:00
|
|
|
});
|
2022-03-22 22:14:39 +01:00
|
|
|
Event::listen('media.file.rename', function ($widget, $old, $new) {
|
2022-02-16 03:03:55 +01:00
|
|
|
if ((new StorageMediaPath($new))->shouldProcess() || (new StorageMediaPath($old))->shouldProcess()) {
|
2022-02-16 02:49:19 +01:00
|
|
|
Queue::push(MoveJob::class, [$old, $new], Setting::get('queue'));
|
|
|
|
}
|
2021-09-06 02:31:27 +02:00
|
|
|
});
|
2019-09-17 23:14:29 +02:00
|
|
|
}
|
|
|
|
|
2022-03-22 22:14:39 +01:00
|
|
|
public function registerSettings()
|
|
|
|
{
|
2019-09-17 23:14:29 +02:00
|
|
|
return [
|
|
|
|
'resizer' => [
|
2022-03-22 22:14:39 +01:00
|
|
|
'label' => 'Resizer Settings',
|
2019-09-17 23:14:29 +02:00
|
|
|
'description' => 'Change how images are resized and compressed',
|
2022-03-22 22:14:39 +01:00
|
|
|
'category' => 'Base',
|
|
|
|
'icon' => 'icon-cog',
|
|
|
|
'class' => '\Aweos\Resizer\Models\Setting',
|
|
|
|
'order' => 500,
|
|
|
|
'keywords' => 'setting',
|
|
|
|
'permissions' => ['aweos.resizer.*'],
|
|
|
|
],
|
2019-09-17 23:14:29 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2022-03-22 22:14:39 +01:00
|
|
|
public function registerMarkupTags()
|
|
|
|
{
|
2019-09-17 23:14:29 +02:00
|
|
|
return [
|
|
|
|
'filters' => [
|
2021-09-14 02:13:29 +02:00
|
|
|
'resize' => fn ($media, $size = 'original', $sizes = null, $options = []) => app(CacheManager::class)->get(
|
2022-02-16 02:49:19 +01:00
|
|
|
new StorageMediaPath($media),
|
2021-09-09 19:18:41 +02:00
|
|
|
$size,
|
|
|
|
$sizes,
|
2021-09-14 02:13:29 +02:00
|
|
|
$options,
|
2022-03-22 22:14:39 +01:00
|
|
|
),
|
|
|
|
],
|
2019-09-17 23:14:29 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|