Created
October 18, 2024 08:17
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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