From d8391389ab626b1358e4e3547d16a45910caa93a Mon Sep 17 00:00:00 2001 From: Paul Date: Mon, 11 Nov 2024 18:59:27 -0600 Subject: [PATCH] feat(docs): add example for customising the filename of an upload via hooks (#9124) --- docs/upload/overview.mdx | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/docs/upload/overview.mdx b/docs/upload/overview.mdx index 026203889ae..4ffc8dee37c 100644 --- a/docs/upload/overview.mdx +++ b/docs/upload/overview.mdx @@ -140,6 +140,23 @@ export default buildConfig({ }) ``` +### Custom filename via hooks + +You can customize the filename before it's uploaded to the server by using a `beforeOperation` hook. + +```ts +beforeOperation: [ + ({ req, operation }) => { + if ((operation === 'create' || operation === 'update') && req.file) { + req.file.name = 'test.jpg' + } + }, +], +``` + +The `req.file` object will have additional information about the file, such as mimeType and extension, and you also have full access to the file data itself. +The filename from here will also be threaded to image sizes if they're enabled. + ## Image Sizes If you specify an array of `imageSizes` to your `upload` config, Payload will automatically crop and resize your uploads to fit each of the sizes specified by your config.