| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- <?php
-
- namespace App\Jobs;
-
- use App\Models\StoreImage;
- use App\StorePlan\BannerPhoto;
- use Illuminate\Contracts\Queue\ShouldQueue;
- use Illuminate\Foundation\Queue\Queueable;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\Storage;
- use Throwable;
-
- /**
- * Runs after a banner photo is uploaded: enlarges small photos, shrinks huge ones, and saves WebP
- * (see BannerPhoto). The design points to the photo by its id, so it picks up the new file by itself.
- * If anything goes wrong, the photo as uploaded stays in use.
- */
- class PrepareBannerPhoto implements ShouldQueue
- {
- use Queueable;
-
- public int $tries = 2;
-
- public int $timeout = 120;
-
- /** The merchant may delete the photo before this runs. Then there is nothing to do. */
- public bool $deleteWhenMissingModels = true;
-
- public function __construct(public StoreImage $image) {}
-
- public function handle(BannerPhoto $banner): void
- {
- $original = $this->image->path;
-
- if (str_ends_with($original, '.webp') && str_starts_with(basename($original), 'banner-')) {
- return; // Already done.
- }
-
- try {
- $prepared = $banner->prepare($original);
- } catch (Throwable $error) {
- Log::warning('Banner photo could not be prepared; the uploaded photo stays in use.', [
- 'store_image_id' => $this->image->id,
- 'error' => $error->getMessage(),
- ]);
-
- return;
- }
-
- $this->image->update(['path' => $prepared]);
- Storage::disk('public')->delete($original);
- }
- }
|