#Dynamically populate upload field using beforeValidate hook for generating PDF thumbnails

3 messages · Page 1 of 1 (latest)

sweet frigate
#

Hi,
I am trying to create a thumbnail after uploading a PDF file.

So I have two fields.

  `  {
      name: "file",
      type: "upload",
      relationTo: "media",
      label: "File (PDF only)",
      required: true,
      filterOptions: {
        mimeType: { contains: "pdf" },
      },
      admin: {
        description:
          "Filename should have a readable and understandable name such as BPRO-Speisenverteilung.pdf – Avoid abreviations and filenames that users don’t comprehend.",
      },
    },
    {
      name: "thumbnailImage",
      type: "upload",
      relationTo: "media",
      label: "Thumbnail image",
      required: true,
      admin: {
        description: "Recommended size: 144x204px",
      },
    },`

I would like to use a library such as https://www.npmjs.com/package/pdf-thumbnails-generator to generate the thumbnail.

My idea is to leverage the beforeValidate hook to call this library with the PDF uploaded in the file field. Once I have the base64 image, I can use the localApi to upload the thumbnail to my media collection https://payloadcms.com/docs/upload/overview#uploading-files / https://stackoverflow.com/a/68671469/6318229

However, I don't know what to return in the hook so the thumbnail image field gets automatically populated based on the result of the localAPI create method.

If anyone has a better approach to do, I would love to hear your ideas.

Payload CMS

Payload is a headless CMS and application framework built with TypeScript, Node.js, React and MongoDB

peak tendonBOT
sweet frigate
#

Dynamically populate upload field using beforeValidate hook for generating PDF thumbnails