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.

Upload Collection URL Field

default discord avatar
paritypete2 years ago
2

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
    notchr2 years ago
    @1018604694987346012

    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
    paritypete2 years ago
    @1049775120559898725

    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,
                };
            },
        ],
    },
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.