medusajs / medusa

The world's most flexible commerce platform.
https://medusajs.com
MIT License
25.58k stars 2.55k forks source link

[medusa-file-local] Cannot delete files #5608

Open Scyks opened 11 months ago

Scyks commented 11 months ago

Bug report

Describe the bug

Seems like deletion of files using the medusa-file-local plugin doesn't work. Currently extending my backend using files to upload. Ofc course they need to be deleted as well. The call using https://docs.medusajs.com/api/admin#uploads_deleteuploads goes through and reports the file as deleted.

Never the less, the file still exists.

Quickly checked the plugin and found out that the api accepts file_key and passes this key to the fileService. Sadly the fileService uses fileKey to delete the file.

System information

Medusa version (including plugins):

"@medusajs/admin": "7.1.5",
"@medusajs/cache-inmemory": "1.8.8",
"@medusajs/cache-redis": "1.8.8",
"@medusajs/event-bus-local": "1.9.6",
"@medusajs/event-bus-redis": "1.8.9",
"@medusajs/file-local": "1.0.2",
"@medusajs/medusa": "1.17.2",

Node.js version: 18.12.1 Database: postgres Operating system: MacOs Browser (if relevant): Chrome

Steps to reproduce the behavior

  1. Go to the admin ui and upload a picture for a product
  2. save the uploads
  3. verify the file has been uploaded
  4. delete the file
  5. check that the file still exists after deletion

Expected behavior

File should be deleted

Possible Fix

async delete(file): Promise<void> {
    const filePath = `${this.uploadDir_}/${file.file_key}`
    if (fs.existsSync(filePath)) {
      fs.unlinkSync(filePath)
    }
olivermrbl commented 11 months ago

PRs for minor bugs/improvements are always welcomed :)

However, please keep in mind the local file plugin is not recommended for production. It is meant for development only. The files are stored locally in a folder in your project, so you can delete the content of this folder if you want them gone.

Will label this as wontfix, as we won't be solving this ourselves.

viveksingh801 commented 10 months ago

That's fine - but it makes sense to have the file removed, when the product which had those images are removed via admin. Deleting a product shall get the files cleanup from the local too.

For now, it's okay - but It would be helpful, and I will try to put a fix for the same here.