Hi, I have an upload collection with GCS adapter configured called
product-images. In another collection,
products, i have an upload field called
frontImage.
Is there any way of programatically overwriting the filename when it's uploaded, based on other fields in the current
productsentry? Let's say I'm configuring a new iPhone 12 Red, when I upload the frontImage, I'd like it to be uploaded in my bucket with the name
product-images/{brand}_{model}_{color}.jpg, indifferent of the name of the file that was uploaded in the UI.
So by default, I'm not sure if this can be done, but I think with a custom hook this can be done.
I imagine the flow would be..
- Upload a file on a collection
- The afterChange hook fires, giving
you access to the change and the rest of the fields in the document.
- You confirm the change was to the field in question
- You get the location of the uploaded file.
- You rename the file and save the collectionyea that's pretty much what i ended up doing:
- add image in the frontImage field
- it uploads it to a standard folder with whatever name the user had it with
- afterChange hook on the frontImage field fires
- check if the prefix is the standard one > if it is, i move the picture in the appropriate folder with the new composed name, and update the prefix and filename in payload as well
at first glance it works perfectly, but i'll come back here if i observe any problems, maybe it'll help someone else in the future
thanks
@1049775120559898725🙏
Star
Discord
online
Get dedicated engineering support directly from the Payload team.