2024-06-01 03:10:49 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Importers\Image\Jobs;
|
|
|
|
|
|
|
|
|
|
use App\Models\Image;
|
|
|
|
|
use Illuminate\Bus\Batchable;
|
|
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
|
use Intervention\Image\Laravel\Facades\Image as InterventionImage;
|
|
|
|
|
use \Throwable;
|
|
|
|
|
|
|
|
|
|
class ImportImage implements ShouldQueue
|
|
|
|
|
{
|
|
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, Batchable;
|
|
|
|
|
|
|
|
|
|
public function __construct(public string $tmpFile, public Image $image)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function handle(): void
|
|
|
|
|
{
|
2024-06-12 19:51:41 +02:00
|
|
|
if (method_exists($this, 'batch') && $this->batch()?->cancelled()) {
|
2024-06-01 03:10:49 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$encoded = InterventionImage::read($this->tmpFile)->toAvif(config('gallery.image.quality', 80));
|
|
|
|
|
Storage::disk('images')->put("{$this->image->album->id}/original/{$this->image->id}.avif", $encoded);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function failed(?Throwable $exception): void
|
|
|
|
|
{
|
|
|
|
|
Log::error($exception, ['image' => $this->image, 'tmpFile' => $this->tmpFile]);
|
|
|
|
|
$this->image->delete();
|
|
|
|
|
}
|
|
|
|
|
}
|