Keystone: fileAdapter.delete bug

Created on 24 Apr 2020  路  2Comments  路  Source: keystonejs/keystone

Bug report

Describe the bug

when setting up hooks in my list to delete a file from Cloudinary after the record has been deleted like this...

  keystone.createList('post', {
    fields: {
      text: { type: Text, required: true },
      media: {
        type: File,
        adapter: fileAdapter,
        hooks: {
          beforeChange: async ({ existingItem }) => {
            if (existingItem && existingItem.media) {
              await fileAdapter.delete(existingItem.media);
            }
          },
        },
      },
      poster: { type: Relationship, ref: 'socialProfile', many: false }
    },
    hooks: {
      afterDelete: async ({ existingItem }) => {
        if (existingItem.media) {
          await fileAdapter.delete(existingItem.media);
        }
      },
    },
  });

I get an error from GraphQl stating that an api_key is required.

image

bug good first issue verified

Most helpful comment

It appears that the API credentials passed to CloudinaryAdapter are not passed to the delete function, as they are in the upload function. The options are just defaulted to an empty object:

https://github.com/keystonejs/keystone/blob/070519dbec289b759759343d084bc5d2de9d4b37/packages/file-adapters/lib/cloudinary.js#L53-L67

A simple workaround would be to additionally pass the API creds directly to the delete function, like so:

await fileAdapter.delete(existingItem.image, {
      cloud_name: CLOUD_NAME,
      api_key: API_KEY,
      api_secret: API_SECRET,
})

All 2 comments

It appears that the API credentials passed to CloudinaryAdapter are not passed to the delete function, as they are in the upload function. The options are just defaulted to an empty object:

https://github.com/keystonejs/keystone/blob/070519dbec289b759759343d084bc5d2de9d4b37/packages/file-adapters/lib/cloudinary.js#L53-L67

A simple workaround would be to additionally pass the API creds directly to the delete function, like so:

await fileAdapter.delete(existingItem.image, {
      cloud_name: CLOUD_NAME,
      api_key: API_KEY,
      api_secret: API_SECRET,
})

Hmmm, we should be able to pass these through to the delete function when the adapter is initialised. This looks like a reasonably small issue for someone familiar with the cloudinary adapter. PRs welcome, otherwise it will go on my backlog :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

JedWatson picture JedWatson  路  17Comments

ra-external picture ra-external  路  10Comments

wesbos picture wesbos  路  16Comments

amorote picture amorote  路  21Comments

thekevinbrown picture thekevinbrown  路  31Comments