Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deleteHook doesnt delete custom sizes files #21

Open
maxbaile opened this issue Oct 7, 2021 · 1 comment
Open

deleteHook doesnt delete custom sizes files #21

maxbaile opened this issue Oct 7, 2021 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@maxbaile
Copy link

maxbaile commented Oct 7, 2021

Hi!

I have a problem with deleting images.

When I add a file, your plugin does add all custom sizes with a .map of all sizes.

I can find all the files on S3 with all the different sizes.

On the other hand, when I delete an image from payload, it does delete the original file, but does not delete all custom sized files.

When I take a closer look at the plugin, the 'deleteHook ()' hook doesn't map the different file sizes but only the original file.

Have you noticed this problem? Is this normal or is there a way to map the sizes object like on the 'uploadHook ()' hook.

Here is the difference between the two hooks, we better understand their behavior:

uploadHook

const uploadHook = (adapter: AdapterInterface) => {
  const beforeChange: CollectionBeforeChangeHook = async (args) => {
    const { req, data } = args
    if (req?.files?.file) {
      let uploadedFile: UploadedFile
      if (Array.isArray(req.files.file)) {
        uploadedFile = req.files.file[0]
      } else {
        uploadedFile = req.files.file
      }

      const resizedBuffers = req.payloadUploadSizes
        ? Object.keys(req.payloadUploadSizes)
          .map(uploadSize => {
            const name = data?.sizes?.[uploadSize]?.filename
            const mimetype = uploadedFile?.mimetype
            const buffer = req?.payloadUploadSizes?.[uploadSize]

            if (name && mimetype && buffer) {
              return adapter.upload({
                name,
                data: buffer,
                mimetype: mimetype
              })
            }
          })
          .filter(buffer => typeof buffer !== 'undefined')
        : []

      await Promise.all([adapter.upload(uploadedFile), ...resizedBuffers])
    }
  }

  return beforeChange
}

export default uploadHook

deleteHook

export const deleteHook = (adapter: AdapterInterface) => {
  const afterDelete: CollectionAfterDeleteHook = async (args) => {
    const { doc } = args
    await adapter.delete(doc.filename)
  }

  return afterDelete
}

Thank's in advance for your answer!

And sorry for my bad english, i'm a french developer and i don't speak English very well....

@richardvanbergen
Copy link
Owner

Hi there. Sorry it's taken so long to get back to you on this.

Yes it seems as though you're correct I do need to loop over the sizes. Good catch!

I'll address this later this week.

@richardvanbergen richardvanbergen self-assigned this Dec 12, 2021
@richardvanbergen richardvanbergen added the bug Something isn't working label Dec 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants