SpatieMediaLibrary Image upload weirdness
I am building a user maintenance form which includes the ability to upload 3 profile images. The image uploads are handled using the SpatieMediaLibrary form control.
The control seems okay when new images are added in that the media table is populated and the files are saved to disk fine. However if (for example) there are 3 images already uploaded and 1 image is deleted from the collection then when the form is saved all the images get deleted from storage but the 2 media database table entries remain which point to non-existent files. So Is there something I've missed here?
Solution:Jump to solution
Thanks - have done that and it made no difference.
However I drilled down into the plugin code and found the reason. When an image is deleted from a collection the
Spatie\MediaLibrary\MediaCollections\Filesystem
class method removeAllFiles()
gets called. As you would expect this call will remove all files in the directory where the uploaded file is stored. In this case the media path generator had placed all the profile images for a user in the same directory so all files got zapped when only a single delete was needed. So this was an implementation bug not an issue with Fil or SML plugin.
Sorry for the noise!...14 Replies
Are you using the Translatable plugin too by any chance?
Nope
Hmm. Not sure at the moment. There is an open bug around this with translatable. There are also some bugs in livewire regarding entangled state at the moment too. Maybe it’s tied to that somehow.
Thanks for the info which doesn't sound too good... 😦 who maintains the plugin Spatie themselves or someone else?
Filament has a plugin for it. I assume that’s what you are using, but if the issue is in Livewire then we can’t do anything about it until livewire fixes it’s issues.
I'm not seeing any console errors and I see the data reaching the LW component ( I tested separately in a custom page) it just when the data is saved to the model things seem to go wrong ...
And yes I'm using the plug-in
"filament/spatie-laravel-media-library-plugin": "^3.0-stable",
Can you run a ‘php artisan about’ and let me know what versions of filament and livewire are showing?
You can also remove the -stable at this point.
Filament is v3.0.34 - Livewire is v3.0.0
Try running and update without the -stable.
"filament/filament": "^3.0-stable"
, remove it from here as well?LW should be on 3.0.1. There was some important bug fixes with that patch
Leave the ^3.0 but drop the -stable
Then run
composer update
Not saying this will fix the issue but will help to eliminate the possibilitySolution
Thanks - have done that and it made no difference.
However I drilled down into the plugin code and found the reason. When an image is deleted from a collection the
Spatie\MediaLibrary\MediaCollections\Filesystem
class method removeAllFiles()
gets called. As you would expect this call will remove all files in the directory where the uploaded file is stored. In this case the media path generator had placed all the profile images for a user in the same directory so all files got zapped when only a single delete was needed. So this was an implementation bug not an issue with Fil or SML plugin.
Sorry for the noise!Glad you figured it out.
Thanks for your help!