Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Uploaded filenames based on other fields

default discord avatar
renequez2 years ago
2

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

products

entry? 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.

  • default discord avatar
    notchr2 years ago
    @331906168375083008

    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 collection
  • default discord avatar
    renequez2 years ago

    yea 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 on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.