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:
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!...
Jump to solution
14 Replies
awcodes
awcodes2y ago
Are you using the Translatable plugin too by any chance?
rich06
rich06OP2y ago
Nope
awcodes
awcodes2y ago
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.
rich06
rich06OP2y ago
Thanks for the info which doesn't sound too good... 😦 who maintains the plugin Spatie themselves or someone else?
awcodes
awcodes2y ago
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.
rich06
rich06OP2y ago
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",
awcodes
awcodes2y ago
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.
rich06
rich06OP2y ago
Filament is v3.0.34 - Livewire is v3.0.0
awcodes
awcodes2y ago
Try running and update without the -stable.
rich06
rich06OP2y ago
"filament/filament": "^3.0-stable", remove it from here as well?
awcodes
awcodes2y ago
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 possibility
Solution
rich06
rich062y ago
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!
awcodes
awcodes2y ago
Glad you figured it out.
rich06
rich06OP2y ago
Thanks for your help!

Did you find this page helpful?