oc-resizer-plugin/Plugin.php

149 lines
5.3 KiB
PHP

<?php
namespace Aweos\Resizer;
use Aweos\Resizer\Classes\CacheManager;
use Aweos\Resizer\Classes\ImageResizer;
use Aweos\Resizer\Console\ResizeCacheClear;
use Aweos\Resizer\Console\ResizeMake;
use Aweos\Resizer\Console\ResizePurge;
use Aweos\Resizer\Jobs\DeleteJob;
use Aweos\Resizer\Jobs\MoveJob;
use Aweos\Resizer\Jobs\ResizeJob;
use Aweos\Resizer\Lib\StorageMediaPath;
use Aweos\Resizer\Models\Setting;
use Event;
use MediaLibrary;
use Queue;
use System\Classes\PluginBase;
use System\Models\File;
use ValidationException;
/**
* resizer Plugin Information File.
*/
class Plugin extends PluginBase
{
/**
* Returns information about this plugin.
*
* @return array
*/
public function pluginDetails()
{
return [
'name' => 'aweos.resizer',
'description' => 'No description provided yet...',
'author' => 'aweos',
'icon' => 'icon-leaf',
];
}
/**
* 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);
$this->registerConsoleCommand('resizer.resize-cache-clear', ResizeCacheClear::class);
}
/**
* Boot method, called right before the request route.
*
* @return array
*/
public function boot()
{
app()->bind(ImageResizer::class, function () {
$disk = (new File())->getDisk();
$dir = (new File(['is_public' => true]))->getStorageDirectory() . 'c/';
$media = MediaLibrary::instance();
return new ImageResizer($disk, $dir, $media);
});
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))->sluggifyPath();
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) {
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'));
}
});
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()
{
return [
'resizer' => [
'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.*'],
],
];
}
public function registerMarkupTags()
{
return [
'filters' => [
'resize' => fn ($media, $size = 'original', $sizes = null, $options = []) => app(CacheManager::class)->get(
new StorageMediaPath($media),
$size,
$sizes,
$options,
),
],
];
}
}