Ctrl K
ComunidadGuías…

app/Jobs/GenerateInventoryArticleImageThumbnail.php

Rama main · réplica de lectura del repositorio original.

Archivo original ↗Preguntar a KiBot
<?php

namespace App\Jobs;

use Artwork\Modules\Inventory\Models\InventoryArticleImage;
use Artwork\Modules\Inventory\Services\InventoryArticleImageService;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Support\Facades\Storage;

class GenerateInventoryArticleImageThumbnail implements ShouldQueue
{
    use Queueable;

    public bool $deleteWhenMissingModels = true;

    public function __construct(public InventoryArticleImage $articleImage)
    {
    }

    public function handle(InventoryArticleImageService $imageService): void
    {
        if ($this->articleImage->image && $imageService->isHeic($this->articleImage->image)) {
            $converted = $imageService->convertHeicToJpeg($this->articleImage->image);

            if ($converted !== null) {
                $oldPath = $this->articleImage->image;
                $this->articleImage->update(['image' => $converted, 'thumbnail' => null]);

                $stillReferenced = InventoryArticleImage::withTrashed()
                    ->where('image', $oldPath)
                    ->where('id', '!=', $this->articleImage->id)
                    ->exists();

                if (!$stillReferenced) {
                    Storage::disk('public')->delete($oldPath);
                }
            }
        }

        if ($this->articleImage->thumbnail !== null) {
            return;
        }

        $thumbnail = $imageService->generateThumbnail($this->articleImage->image);

        if ($thumbnail !== null) {
            $this->articleImage->update(['thumbnail' => $thumbnail]);
        }
    }
}