From 8427d238077c694516e529393507de932a659509 Mon Sep 17 00:00:00 2001 From: Kel Date: Sun, 1 Oct 2017 23:33:32 -0600 Subject: [PATCH] Delete thumbnails when file deletes Tested working with a couple file types. Fixed some coding errors from earlier revision. --- controllers/uploadController.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/controllers/uploadController.js b/controllers/uploadController.js index 3e4fdfe..850ab17 100644 --- a/controllers/uploadController.js +++ b/controllers/uploadController.js @@ -232,17 +232,26 @@ uploadsController.delete = function(req, res) { } uploadsController.deleteFile = function(file) { - + const ext = path.extname(file).toLowerCase() return new Promise(function(resolve, reject) { fs.stat(path.join(__dirname, '..', config.uploads.folder, file), function(err, stats) { if (err) { return reject(err) } fs.unlink(path.join(__dirname, '..', config.uploads.folder, file), function(err) { if (err) { return reject(err) } - return resolve() + if(!utils.imageExtensions.includes(ext) && !utils.videoExtensions.includes(ext)) { + return resolve() + } + file = file.substr(0, file.lastIndexOf(".")) + ".png" + fs.stat(path.join(__dirname, '..', config.uploads.folder, "thumbs/", file), function(err, stats) { + if (err) { return reject(err) } + fs.unlink(path.join(__dirname, '..', config.uploads.folder, "thumbs/", file), function(err) { + if (err) { return reject(err) } + return resolve() + }) + }) }) }) }) - } uploadsController.list = function(req, res) {