-
|
I want to send the uploaded files to an ftp server (using basic-ftp) and receive them from there also. The process has been already mentioned in the doc here: https://payloadcms.com/docs/production/deployment#file-storage But I am a bit confused about how I can use hooks there? import { CollectionConfig } from 'payload/types';
const Media: CollectionConfig = {
slug: 'media',
access: {
read: () => true,
},
hooks: {
beforeChange: [
(operation) => {
if (operation.req.headers.hook === 'beforeChange') {
operation.data.description += '-beforeChangeSuffix';
// Todo: send to ftp
}
return operation.data;
},
],
},
upload: {
staticURL: '/media',
staticDir: 'media',
imageSizes: [
{
name: 'thumbnail',
width: 400,
height: 300,
crop: 'centre',
},
{
name: 'card',
width: 768,
height: 1024,
crop: 'centre',
},
],
adminThumbnail: 'thumbnail'
},
fields: [],
}; |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
Hey @aljubaer — You're almost there! You have a bug in your hook, though. Your So basically, right now, code within your Pro tip: You might want to do different actions based on if your {
beforeChange: [
({
data, // incoming data to update or create with
req, // full express request
operation, // name of the operation ie. 'create', 'update'
originalDoc, // original document
}) => {
console.log(operation) // will be either 'update' or 'create'
return data;
}
]
}For example, a Take a look at the Hooks documentation for more! https://payloadcms.com/docs/hooks/collections#beforechange Does this answer your question? |
Beta Was this translation helpful? Give feedback.
Hey @aljubaer —
You're almost there! You have a bug in your hook, though.
Your
ifstatement that is checking if theoperation.req.headers.hookis equal tobeforeChangeis incorrect and unnecessary. Because you are adding a function to thebeforeChangehooks array itself, you know that the code will only be running in abeforeChangecontext.So basically, right now, code within your
ifwill never fire. Just remove it!Pro tip:
You might want to do different actions based on if your
beforeChangehook is either running in response to acreateor anupdateoperation. You can gain access to which operation is active by destructuringoperationout of the hook arguments, like so: