Upload - Use ID instead of filename?

default discord avatar
alphamoonbase.berlinlast year
3

When accessing a file in directus, it does happen via the ID of the file object.


When Uploading a file and accessing it in PayLoad, it does so via the clear filename.


i.E. i Upload a file with the name

demo.pdf

. I can now access the file via

uploads/demo.pdf

instead, it would be nice if instead it could be just a simple UUID or at least the ID of the object.


I saw that there is a hashing-plugin, which still uses the clear name though.



So, main question: Is there already a way of handling file-access this way or do I need to write a plugin for that?

  • discord user avatar
    denolfe
    last year

    You could rename the file to a uuid in a

    beforeOperation

    collection hook. There are some examples here (from one of the links by the bot):

    https://payloadcms.com/community-help/github/rename-file-on-upload-not-overwriting-other-uploads
  • default discord avatar
    alphamoonbase.berlinlast year

    Yeah, thats what I though of too. The issue then of course is that the filename gets lost.


    In Directus files store they actualy filename and then the "OnDisk" filename, which basicly is the ID of the object in the database and the extension of the original file.


    You can then either use /assets/{file_id} to get the raw data (and meta-data in the header derived from the DB-entry, i.E. the original filename)


    or items/files/{file_id} to get the data stored in the DB.



    With Payload I basicly have to access the data of the item first to get the download-path because the downloadpath is returned via API and not easily understandable.



    I suppose, it could be somewhat the other way around. Using a preRead hook and checking if the ID exists and returning the download-link. Hmmm.

  • discord user avatar
    denolfe
    last year

    Yeah, you could definitely get creative here. You could write the incoming filename to a separate field in the hook, so it could still be stored.

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.