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?
SQLSTATE[22001]: String data, right truncated: 1406 Data too long for column 'file_name' at row 1
SQLSTATE[22001]: String data, right truncated: 1406 Data too long for column 'file_name' at row 1
. 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
Mark Chaney
Mark ChaneyOP16mo ago
@krekas that’s just the generated name. A spatie media library record has file_name and name fields.
krekas
krekas16mo ago
But all file upload methods work with for spatie field
Mark Chaney
Mark ChaneyOP16mo ago
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.
Esteban Ojeda
Esteban Ojeda16mo ago
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?
Mark Chaney
Mark ChaneyOP16mo ago
@estebanohdez_14903 not available for the spatie field
krekas
krekas16mo ago
validate uploaded file name for max length
Mark Chaney
Mark ChaneyOP16mo ago
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
krekas
krekas16mo ago
why they have so long filenames should be question
Mark Chaney
Mark ChaneyOP16mo ago
google photo filenames can be pretty darn long
krekas
krekas16mo ago
google photos has original filename
Mark Chaney
Mark ChaneyOP16mo ago
sorry, google images
krekas
krekas16mo ago
so just make a live validation that filename is too long
Mark Chaney
Mark ChaneyOP16mo ago
i think i already covered that with my earlier statement
krekas
krekas16mo ago
but how do you long image names from google images? when they show images from other pages
Esteban Ojeda
Esteban Ojeda16mo ago
Just testing, with this code:
Forms\Components\SpatieMediaLibraryFileUpload::make('file')
->mediaName(fn (Get $get) => $get('name'))
Forms\Components\SpatieMediaLibraryFileUpload::make('file')
->mediaName(fn (Get $get) => $get('name'))
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
Mark Chaney
Mark ChaneyOP16mo ago
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'
->mediaName(function (TemporaryUploadedFile $file) {
$name = fake()->numerify('logo-########');
return $name . '.' . $file->guessExtension();
})
->getUploadedFileNameForStorageUsing(function (TemporaryUploadedFile $file) {
$name = fake()->numerify('logo-########');
return $name . '.' . $file->guessExtension();
})
->mediaName(function (TemporaryUploadedFile $file) {
$name = fake()->numerify('logo-########');
return $name . '.' . $file->guessExtension();
})
->getUploadedFileNameForStorageUsing(function (TemporaryUploadedFile $file) {
$name = fake()->numerify('logo-########');
return $name . '.' . $file->guessExtension();
})
not truly a unique name, but i think it will work fine with how we are storing things
krekas
krekas16mo ago
Why not just use str random or uuid? Faker in the app feels so hacky. And isn't it installed as a dev?
Mark Chaney
Mark ChaneyOP16mo ago
Faker is needed for pest tests too
krekas
krekas16mo ago
Pest is installed as dev As is faker So it won't work in production for you
Mark Chaney
Mark ChaneyOP16mo ago
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.
krekas
krekas16mo ago
Just use str uuid as I said
Want results from more Discord servers?
Add your server