Organize media files by subfolders

default discord avatar
mihanc
2 months ago
2

Hi there! Does anyone know a workaround on how to organize media files by subfolders?


I want to create an admin panel for a blog where authors will work on writing articles. Each article may have at least 5-10 pictures. If all of them are uploaded to a media folder without sorting by subfolders, I'll get a mess...


Thanks in advance!

  • default discord avatar
    thgh
    2 months ago

    Here is a way to prefix the filename (not sure if folders are supported)


    // Field
    {
      name: 'original',
      label: 'Original filename',
      type: 'text',
      admin: { hidden: true },
    },
    // Hook
    beforeOperation: [
      async ({ operation, args }) => {
        if (operation === 'create') {
          // Save original filename
          const original = args.req.files.file.name || 'example.jpg'
          args.data.original = original
    
          // Add filename prefix
          args.req.files.file.name = new Date().toJSON().slice(0, 10) + '-' + original
        }
      },
    ],
  • discord user avatar
    jesschow
    Payload Team
    last month

    Hey @mihanc - you could also organize your media by adding a tag field or similar, then you can filter by this tag in your collection view.

Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

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