From 070f4bdafd16d49e9fd145bba88a035afb973763 Mon Sep 17 00:00:00 2001 From: Bobby Wibowo Date: Sun, 18 Mar 2018 20:13:08 +0700 Subject: [PATCH] Updates * Updated file name checker to use for-loop instead of do-while-loop. * Replaced all instances of eslint-disable-line with eslint-disable-next-line. --- config.sample.js | 7 +++++++ controllers/uploadController.js | 26 ++++++++++---------------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/config.sample.js b/config.sample.js index 05ece13..912265a 100644 --- a/config.sample.js +++ b/config.sample.js @@ -67,6 +67,13 @@ module.exports = { // The length of the random generated name for the uploaded files fileLength: 32, + /* + This option will limit how many times it will try to generate random names + for uploaded files. If this value is higher than 1, it will help in cases + where files with the same name already exists (higher chance with shorter file name length). + */ + maxTries: 1, + /* NOTE: Thumbnails are only for the admin panel and they require you to install a separate binary called graphicsmagick (http://www.graphicsmagick.org) diff --git a/controllers/uploadController.js b/controllers/uploadController.js index 59f999b..42aa5f3 100644 --- a/controllers/uploadController.js +++ b/controllers/uploadController.js @@ -9,7 +9,7 @@ const utils = require('./utilsController.js') const uploadsController = {} -const maxStrikes = 3 +const maxTries = config.uploads.maxTries || 1 const uploadDir = path.join(__dirname, '..', config.uploads.folder) const storage = multer.diskStorage({ @@ -17,25 +17,18 @@ const storage = multer.diskStorage({ cb(null, uploadDir) }, filename: function (req, file, cb) { - let name - let strike = 0 - - do { - name = randomstring.generate(config.uploads.fileLength) + path.extname(file.originalname) + for (let i = 0; i < maxTries; i++) { + const name = randomstring.generate(config.uploads.fileLength) + path.extname(file.originalname) try { fs.accessSync(path.join(uploadDir, name)) - strike++ - console.log(`"${name}" already exists in upload dir (${strike}x)\u2000`) + console.log(`A file named "${name}" already exists (${i + 1}/${maxTries}).`) } catch (err) { - strike = 0 + // Note: fs.accessSync() will throw an Error if a file with the same name does not exist + return cb(null, name) } - } while (strike > 0 && strike < maxStrikes) - - if (strike >= maxStrikes) { - cb('Could not allocate a file name. Try again?') // eslint-disable-line standard/no-callback-literal - } else { - cb(null, name) } + // eslint-disable-next-line standard/no-callback-literal + return cb('Could not allocate a unique file name. Try again?') } }) @@ -45,7 +38,8 @@ const upload = multer({ fileFilter: function (req, file, cb) { if (config.blockedExtensions !== undefined) { if (config.blockedExtensions.some(extension => path.extname(file.originalname).toLowerCase() === extension)) { - return cb('This file extension is not allowed') // eslint-disable-line standard/no-callback-literal + // eslint-disable-next-line standard/no-callback-literal + return cb('This file extension is not allowed') } return cb(null, true) }