oc-resizer-plugin/Plugin.php

110 lines
3.4 KiB
PHP
Raw Normal View History

2019-09-17 23:14:29 +02:00
<?php namespace Aweos\Resizer;
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\FileObserver;
use Aweos\Resizer\Classes\ImageResizer;
use Aweos\Resizer\Classes\ResizeJob;
2019-09-17 23:14:29 +02:00
use Aweos\Resizer\Console\ResizeMake;
use Aweos\Resizer\Console\ResizePurge;
2021-09-09 19:18:41 +02:00
use Aweos\Resizer\Lib\MediaPath;
2021-09-06 02:31:27 +02:00
use Event;
use Queue;
use System\Classes\MediaLibrary;
use System\Classes\PluginBase;
use System\Models\File;
2019-09-17 23:14:29 +02:00
/**
* resizer Plugin Information File
*/
class Plugin extends PluginBase
{
/**
* Returns information about this plugin.
*
* @return array
*/
public function pluginDetails()
{
return [
'name' => '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);
}
/**
* Boot method, called right before the request route.
*
* @return array
*/
public function boot()
{
2021-09-06 02:31:27 +02:00
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);
});
Event::listen('media.file.upload', function($widget, $filePath, $uploadedFile) {
2021-09-09 19:18:41 +02:00
if ((new MediaPath($filePath))->shouldProcess()) {
2021-09-06 02:31:27 +02:00
Queue::push(ResizeJob::class, [$filePath]);
}
});
Event::listen('media.file.delete', function($widget, $filePath) {
2021-09-09 19:18:41 +02:00
app(FileObserver::class)->delete(new MediaPath($filePath));
app(CacheManager::class)->delete(new MediaPath($filePath));
2021-09-06 02:31:27 +02:00
});
Event::listen('media.file.move', function($widget, $old, $new) {
2021-09-09 19:18:41 +02:00
app(FileObserver::class)->rename(new MediaPath($old), new MediaPath($new));
app(CacheManager::class)->delete(new MediaPath($old));
2021-09-06 02:31:27 +02:00
});
Event::listen('media.file.rename', function($widget, $old, $new) {
2021-09-09 19:18:41 +02:00
app(FileObserver::class)->rename(new MediaPath($old), new MediaPath($new));
app(CacheManager::class)->delete(new MediaPath($old));
2021-09-06 02:31:27 +02:00
});
2019-09-17 23:14:29 +02:00
}
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' => [
2021-09-09 19:18:41 +02:00
'resize' => fn ($media, $size = 'original', $sizes = null) => app(CacheManager::class)->get(
new MediaPath($media),
$size,
$sizes,
)
2019-09-17 23:14:29 +02:00
]
];
}
}