Upload Collection URL Field

default discord avatar
paritypete
4 months ago
6

Hello,



I'm just using a simple Upload field in a Media collection. I've uploaded a video and payload now returns a "url" property for this video. Is there a way to rename or map the "url" property to "src"?



Motivation: I have existing components that are looking for a "src" property... is there an easy way to expose the "url" value as a "src" property?



{ "id": "645a933e4345b7c3d6b302ca", "alt": "Alt Text", "filename": "home-masthead-video.mp4", "mimeType": "video/mp4", "filesize": 20000000, "createdAt": "2023-05-09T18:38:54.528Z", "updatedAt": "2023-05-09T18:38:54.528Z", "url": "https://s3.amazonaws.com/path-to-video.mp4" }

Thanks!

  • default discord avatar
    notchr
    4 months ago

    @paritypete URL is the common property name for uploads



    Wouldn't it be easier to map it to src?



    Or create a duplicate field that copies the value to a prop named src

  • default discord avatar
    paritypete
    4 months ago

    @notchr Yes I would like to map the current "url" property to a "src" property. How would I do that? I'm not sure how to modify the data that gets exposed by the upload field.



    My current "videoUrl" field looks like:


    { name: 'videoUrl', label: 'Video URL', type: 'upload', relationTo: 'media', },

    And the Media collection is:


    export const Media: CollectionConfig = { slug: 'media', upload: { disableLocalStorage: true, staticDir: path.resolve(__dirname, '../../media'), adminThumbnail: ({ doc }) =>

    ${process.env.NEXT_PUBLIC_S3_ENDPOINT}/${process.env.NEXT_PUBLIC_S3_BUCKET}/${doc.filename}

    , }, access: { read: () => true, }, fields: [ { name: 'alt', type: 'text', required: true, }, ] }


    If it's not easy to do in payload then yes I can simply fix it in my react components... it's just a matter of making this change in potentially hundreds of places... if it's easy to change in payload then I'd rather change it in payload. Thanks!



    For anyone who might find this question, I found I was able to add a

    src

    property using an

    afterRead

    hook:



    hooks: {
        afterRead: [
            async ({ doc }) => {
                return {
                    ...doc,
                    src: doc.url,
                };
            },
        ],
    },
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.