spatie / laravel-medialibrary

Associate files with Eloquent models
https://spatie.be/docs/laravel-medialibrary
MIT License
5.78k stars 1.08k forks source link

ConversionHasBeenCompleted is not called on a queued conversion #3679

Closed richardhulbert closed 3 months ago

richardhulbert commented 4 months ago

Discussed in https://github.com/spatie/laravel-medialibrary/discussions/3678

Originally posted by **richardhulbert** July 24, 2024 Hi So I have a model called MediaStore and the following: ```php public static function MEDIA_TYPES(): array { return ['image', 'video', 'audio', 'pdf']; } public function registerMediaCollections(): void { foreach (self::MEDIA_TYPES() as $type) if ($type === 'image') { $this->addMediaCollection('image')->withResponsiveImages(); $this->addMediaConversion('thumb')->width(300)->crop('crop-center', 300, 200)->nonQueued(); } $this->addMediaCollection($type); ``` When I add media in the controller like this: ```php public function store(MediaStoreRequest $request) { $new = MediaStore::create([ 'name' => $request->name, 'description' => $request->description, 'type' => $request->type, 'user_id' => $request->user()->id ]); try { $new->addMediaFromRequest('file')->toMediaCollection(MediaStore::MEDIA_TYPES()[$request->type]);; } catch (\Exception $exception) { return response($exception->getMessage(), 502); } $media = $new->getMedia('*'); if (count($media) > 0) { $new->url = $media[0]->getUrl(); if ((int) $new->type === 0) { $new->thumb = $media[0]->getUrl('thumb'); $new->scrset = $media[0]->getSrcset(); } } return response($new->load('owner')); } ``` The thumb is generated straight away and the ResponsiveImages conversion is sent to the queue. I would expect both conversions to generate ConversionHasBeenCompleted events but this does NOT seems to be the case. my listener: ```php class MediaConversionCompleteListener { public function __construct() { } public function handle(ConversionHasBeenCompleted $event): void { $media = $event->media; $path = $media->getPath(); $conversion = $event->conversion; $conversionName = $conversion->getName(); Log::info("file {$path} has been converted using conversion:{$conversionName} for media {$media->id}"); } } ``` results in this in the log ```log [2024-07-24 14:52:43] local.INFO: file /Users/richard/PhpstormProjects/spa/storage/app/public/media/30/IMG_4840.HEIC has been saved for media 30 [2024-07-24 14:52:44] local.INFO: file /Users/richard/PhpstormProjects/spa/storage/app/public/media/30/IMG_4840.HEIC has been converted using conversion:thumb for media 30 ``` I want to broadcast to the MediaStore that a new piece of media is ready to be used. Ideas?
chrispage1 commented 3 months ago

So it looks from your logs like the event is being fired.

You could then called $event->media->model->doSomeAction() which would call the doSomeAction function against your MediaStore.

Alternatively, if you truly mean broadcasting as in websockets, then make MediaStore Notifiable and send a notification to it. Then anyone listening to that channel who can subscribe will be notified.