From dcb72734fe00fc9b7e060d66a4f247b7c9a56382 Mon Sep 17 00:00:00 2001 From: Bobby Wibowo Date: Sun, 18 Mar 2018 19:21:04 +0700 Subject: Patch to allow "retries" when generating random name --- controllers/uploadController.js | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) (limited to 'controllers') diff --git a/controllers/uploadController.js b/controllers/uploadController.js index 4e4b2e0..ec9e066 100644 --- a/controllers/uploadController.js +++ b/controllers/uploadController.js @@ -9,12 +9,26 @@ const utils = require('./utilsController.js'); const uploadsController = {}; +// Let's default it to only 1 try +const maxTries = config.uploads.maxTries || 1; +const uploadDir = path.join(__dirname, '..', config.uploads.folder); + const storage = multer.diskStorage({ destination: function(req, file, cb) { - cb(null, path.join(__dirname, '..', config.uploads.folder)); + cb(null, uploadDir); }, filename: function(req, file, cb) { - cb(null, 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)); + console.log(`A file named "${name}" already exists (${i + 1}/${maxTries}).`); + } catch (err) { + // Note: fs.accessSync() will throw an Error if a file with the same name does not exist + return cb(null, name); + } + } + return cb('Could not allocate a unique file name. Try again?'); } }); -- cgit v1.2.3 From 5be27c129d66ac9f872f11031e9887502559ed4e Mon Sep 17 00:00:00 2001 From: Bobby Wibowo Date: Sun, 18 Mar 2018 23:32:59 +0700 Subject: Uses async --- controllers/uploadController.js | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) (limited to 'controllers') diff --git a/controllers/uploadController.js b/controllers/uploadController.js index ec9e066..51c0cf4 100644 --- a/controllers/uploadController.js +++ b/controllers/uploadController.js @@ -17,18 +17,17 @@ const storage = multer.diskStorage({ destination: function(req, file, cb) { cb(null, uploadDir); }, - filename: function(req, file, cb) { - for (let i = 0; i < maxTries; i++) { + filename: function(req, file, cb) { + const access = i => { const name = randomstring.generate(config.uploads.fileLength) + path.extname(file.originalname); - try { - fs.accessSync(path.join(uploadDir, name)); - console.log(`A file named "${name}" already exists (${i + 1}/${maxTries}).`); - } catch (err) { - // Note: fs.accessSync() will throw an Error if a file with the same name does not exist - return cb(null, name); - } - } - return cb('Could not allocate a unique file name. Try again?'); + fs.access(path.join(uploadDir, name), err => { + if (err) return cb(null, name); + console.log(`A file named "${name}" already exists (${++i}/${maxTries}).`); + if (i < maxTries) return access(i); + return cb('Could not allocate a unique file name. Try again?'); + }); + }; + access(0); } }); -- cgit v1.2.3