Skip to content

Instantly share code, notes, and snippets.

@neverything
Created October 18, 2024 08:17
Show Gist options
  • Save neverything/b7892a65077d6f340eeb1d4f5d6665fd to your computer and use it in GitHub Desktop.
Save neverything/b7892a65077d6f340eeb1d4f5d6665fd to your computer and use it in GitHub Desktop.
Set Image and Video dimension for the Spatie Laravel Media Library as custom properties: https://silvanhagen.com/writing/image-and-video-dimensions-laravel-media-library
<?php
namespace App\Jobs;
use App\Actions\Media\UpdateMediaInfoAction;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\Attributes\DeleteWhenMissingModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
#[DeleteWhenMissingModels]
class UpdateMediaInfoJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected Media $media;
public function __construct(Media $media)
{
$this->media = $media;
}
public function handle(UpdateMediaInfoAction $action): void
{
// Check again if the media has already been processed
if ($this->media->hasCustomProperty('dimensions')) {
Log::info("Media {$this->media->id} already processed. Skipping.");
return;
}
$result = $action->execute($this->media);
Log::info("Media info update job completed for id: {$this->media->id}", $result);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment