finkinfridom / payload-cloudinary-plugin

Upload media to Cloudinary service
MIT License
54 stars 3 forks source link

Directory with collection name being created on upload #132

Closed GhostOrder28 closed 8 months ago

GhostOrder28 commented 8 months ago

My image collection have set disableLocalStorage to true, so images are not being stored locally but payload will still create an empty directory with the name of the collection at root, is it possible to disable this behavior too?

finkinfridom commented 8 months ago

hey @GhostOrder28 , the only reference where the plugin creates a directory is here https://github.com/finkinfridom/payload-cloudinary-plugin/blob/f5e8c4c0c5c96bcae6c55c3415f3110d15d2d810/src/services/cloudinaryService.ts#L51 and it's a mandatory workflow as the cloudinary package needs a file to exist before uploading. What is the name of the created directory you see? Maybe the issue should be opened to payload official repository?

GhostOrder28 commented 8 months ago

Hey @finkinfridom thank you for the response, I've just tested the vanilla upload in a fresh project and there is no directory being created when I set disableLocalStorage to true, whereas if I install and configure the plugin then a directory (with the collection name) is always created.

So I think this is related to the plugin... which is rare because in the source code I can see you are removing the temporary directory after the upload is done.

if (disableLocalStorage) {
  await fs.promises.rm(tmpFileName);
}
finkinfridom commented 8 months ago

Hey @GhostOrder28 this morning I updated the plugin to resolve issue #130 (that was coming from a change in payload repository). Now the tmp folder is always created during upload (and always removed too). could you please update your repo and check again?

GhostOrder28 commented 8 months ago

hey I just updated the package (from 0.1.1 to 0.1.2) but the issue still persists, I also tried removing and installing it again

finkinfridom commented 8 months ago

Could you please send me an example of the directory that is created? Because the plugin doesn't use the collection's name but a simple __tmp_media__ name.

GhostOrder28 commented 8 months ago

I just created a repo, but basically the created directory name is the same as the collection slug:

https://github.com/GhostOrder28/payload-cloudinary-upload-test

finkinfridom commented 8 months ago

hey @GhostOrder28, I just tried to reproduce the issue you mentioned. The official documentation for the upload functionality (https://payloadcms.com/docs/upload/overview#collection-upload-options) asks for 2 mandatory properties:

const { staticDir = "__tmp_media__", staticURL = "/media" } =
      collectionConfig?.upload || {};

is the collection's slug.

The plugin, when disableLocalStorage is true, automatically removes the just uploaded file but doesn't remove the folder it creates because, given it's a collection-based config, it could lead to unwanted files removal (e.g. multiple people uploading files for the same collections).

I'd consider providing the above properties for your configuration (especially the staticDir one to be sure the folder created is exactly where you want).

GhostOrder28 commented 8 months ago

Hey @finkinfridom, unfortunatelly providing staticDir didn't solve the issue, this is my collection upload config:

upload: {
  disableLocalStorage: true,
  staticDir: '../uploads',
}

The uploads directory is created but is not being removed, only the uploaded file is.

Providing staticUrl didn't help, it seems that value conflicts with the public id which is what the plugin expect at some point, if I set it to staticURL: '../uploads', then I get this error when trying to upload an image:

APIError: Cloudinary: {"message":"public_id (../uploads/tvup0hup3k1nmnsgqvyh) is invalid","name":"Error","http_code":400}
finkinfridom commented 8 months ago

@GhostOrder28 as per my previous comment, removing the staticDir folder won't be an option as it may lead to inconsistent behaviors when 2+ people work on the same collections (as it may lead to unwanted deletion caused by race conditions). I see 2 options:

  1. Have temp folders created at each upload but won't solve your requests as it will create several folders
  2. temporarily drop all the uploaded images in the root of your project but, again, it doesn't seem a good approach as your root folder may become messy when several uploads happens.

In any case, what's the point of having an empty folder in your delivered app?