mirror of
https://github.com/VueFileManager/vuefilemanager.git
synced 2026-04-23 17:50:38 +00:00
make queue for thumbnails generation
This commit is contained in:
@@ -2,10 +2,22 @@
|
||||
namespace Domain\Files\Actions;
|
||||
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Spatie\QueueableAction\QueueableAction;
|
||||
use Intervention\Image\ImageManagerStatic as Image;
|
||||
use Domain\Files\Actions\CreateImageThumbnailAcionQueue;
|
||||
|
||||
|
||||
class CreateImageThumbnailAction
|
||||
{
|
||||
use QueueableAction;
|
||||
|
||||
public function __construct(
|
||||
CreateImageThumbnailAcionQueue $action,
|
||||
)
|
||||
{
|
||||
$this->action = $action;
|
||||
}
|
||||
|
||||
private array $availableFormats = [
|
||||
'image/gif',
|
||||
'image/jpeg',
|
||||
@@ -22,23 +34,50 @@ class CreateImageThumbnailAction
|
||||
$file,
|
||||
string $user_id
|
||||
): void {
|
||||
|
||||
// Create thumbnail from image
|
||||
if (in_array($file->getClientMimeType(), $this->availableFormats)) {
|
||||
|
||||
// Make copy of file for the thumbnail generation
|
||||
Storage::disk('local')->copy("files/$user_id/{$file_name}", "temp/$user_id/{$file_name}");
|
||||
|
||||
// Create intervention image
|
||||
$intervention = Image::make($file)->orientate();
|
||||
|
||||
// Generate avatar sizes
|
||||
collect(config('vuefilemanager.image_sizes'))
|
||||
collect(config('vuefilemanager.image_sizes.execute') )
|
||||
->each(function ($size) use ($intervention, $file_name, $user_id) {
|
||||
|
||||
// Create thumbnail only if image is larger than predefined image sizes
|
||||
if ($intervention->getWidth() > $size['size']) {
|
||||
// Generate thumbnail
|
||||
$intervention->resize($size['size'], null, fn ($constraint) => $constraint->aspectRatio())->stream();
|
||||
|
||||
// Store thumbnail to disk
|
||||
Storage::put("files/$user_id/{$size['name']}-{$file_name}", $intervention);
|
||||
// Create thumbnail instantly
|
||||
$this->action->execute($size, $file_name, $user_id, null);
|
||||
}
|
||||
});
|
||||
|
||||
$last = last(config('vuefilemanager.image_sizes.queue'));
|
||||
|
||||
collect(config('vuefilemanager.image_sizes.queue'))
|
||||
->each(function ($size) use ($intervention, $file_name, $user_id, $last) {
|
||||
|
||||
$is_last = $last['size'] === $size['size'] ? true : false;
|
||||
|
||||
// Create thumbnail only if image is larger than predefined image sizes
|
||||
if ($intervention->getWidth() > $size['size']) {
|
||||
|
||||
// Create thumbnail queue job
|
||||
$this->action->onQueue()->execute($size, $file_name, $user_id, $is_last);
|
||||
|
||||
} else {
|
||||
|
||||
// Delete file after generate a thumbnail
|
||||
Storage::disk('local')->delete("temp/$user_id/{$file_name}");
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user