Filenames and Spatie
I don't want to save original filenames, they cause problems, especially long ones causing errors like below. How can I just use dynamically generated ones and never store the original filename? . i know there is an option to preserver original filenames, but i don't want them to be saved at all because of issues above.
22 Replies
@krekas that’s just the generated name. A spatie media library record has file_name and name fields.
But all file upload methods work with for spatie field
But none of them solve the file_name issue, which is the original file name. They only change the generated file name. If a user uploads a 200 or 300 character file name and upload it, the db is going to throw an error because that string length limit is only 191 for the file name field. Yes, I could update the length of the db field, but that only gives more space and doesn’t solve the issue in general. I have zero use for the original file names.
Maybe silly solution (I'm beginner in Laravel and totally new to filament) but what about intercepting original file name in the
mutateFormDataBeforeCreate
method for the create/edit resource and replace it with the generated one or something else?@estebanohdez_14903 not available for the spatie field
validate uploaded file name for max length
Might try that to at least stop the upload, but thats more an improvement than a solution as that still stops a user from being able to upload the file they want to upload. Its just kind of dumb to store the original when it provides zero value. Seems to be unique to spatie.
so the more ive looked into it, i dont think there is way a round it and a limitation/requirement of spatie
why they have so long filenames should be question
google photo filenames can be pretty darn long
google photos has original filename
sorry, google images
so just make a live validation that filename is too long
i think i already covered that with my earlier statement
but how do you long image names from google images? when they show images from other pages
Just testing, with this code:
I was able to change the original file name by whatever I introduced in the 'name' input field. I think you can make the logic for your custom name in the closure inside
mediaName
Excited to give it a try, though I would be extremely surprised if that actually changed the file_name field value in the db versus just the name field in spaties media table
Thanks for the idea though and will let you know
@estebanohdez_14903 yep, unfortunately that just sets the 'name' value and not the 'filename'
not truly a unique name, but i think it will work fine with how we are storing things
Why not just use str random or uuid? Faker in the app feels so hacky. And isn't it installed as a dev?
Faker is needed for pest tests too
Pest is installed as dev
As is faker
So it won't work in production for you
You are right about pest. It’s part of the deployment github actions, but not actually use in the actual environment. Idk, to be honest why the faker is used, I didn’t write the faker part of the code we had there. Was leftover from an earlier iteration where the save process was done outside the fileupload field and from another dev. Probably should use something else.
Just use str uuid as I said