#Overide upload filename sanitization options

27 messages · Page 1 of 1 (latest)

vale beacon
#

I need to override the default filename sanitization options (https://github.com/payloadcms/payload/blob/main/packages/payload/src/uploads/getSafeFilename.ts#L10) to preserve asset URL structure for a big migration I'm doing...tomorrow. I didn't realize til just now that the filename value passed to generateFileURL is already pre-sanitized.

I'm guessing the fastest way to get this to work the way I want would be to fork the repo and make the modifications myself...any tips? 😅

GitHub

The best way to build a modern backend + admin UI. No black magic, all TypeScript, and fully open-source, Payload is both an app framework and a headless CMS. - payloadcms/payload

fiery pebbleBOT
vale beacon
#

the existing assets use wordpress conventions /yyyy/mm/dd/<filename> and get sanitized to yyyymmdd<filename>. since it's a predictable pattern I could probably recreate the original filename by parsing the sanitized one within generateFileURL 💭

#

hmm nope that breaks, the upload still uses the sanitized filename

median basin
#

Try a beforeRead hook

vale beacon
#

ohh hmm

median basin
vale beacon
#

i think i would need a beforeCreate hook

#

or beforeChange i mean

median basin
#

No so on your before read you take a URL and change it to point to your live asset URL

#

Like a proxy mapping

#

Your before read will trigger as long as the API call is going to that collection’s endpoint. You would take the name of the file you wish to serve, mutate the name to match the Payload version, then use payload.find() to return the correct one

vale beacon
#

right, the issue is that i have payload access control disabled for uploads. so there are existing links out there in the world that point to these assets that would break, unless I set up some kind of proxy on the asset CDN level

#

which sounds buggy anyway, i really just need to find a way to preserve the slashes in my filenames

median basin
#

A before read! You can modify it to include the slashes

vale beacon
#

the before read won't fire tho. if my app is at payload.foo.com and the asset URL is assets.foo.com. the request is not going through payload

median basin
#

But your request would still hit Payload?

vale beacon
#

nope

median basin
#

I’m confused. What are you using Payload for?

vale beacon
#

haha. it's the CMS. I am using cloud storage and opting out of payload access control for uploads, so the upload URLs that payload returns point directly to the externally hosted asset (https://assets.foo.com/my/upload.png instead of /media/my/upload.png).

median basin
#

Oh I see

#

It could be multer that is doing it. Or bodyparser

vale beacon
#

looks like it's sanitize-filename, I linked to the invocation above in my OP ^^

#

thanks for your suggestions 🙂

median basin
#

Instead of forking. You could patch the dependency

vale beacon