mirror of
https://github.com/BobbyWibowo/lolisafe.git
synced 2024-12-13 16:06:21 +00:00
Merge pull request #85 from BobbyWibowo/pr-retry-names
Patch to allow "retries" when generating random name
This commit is contained in:
commit
0157388217
@ -61,6 +61,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)
|
||||
|
@ -9,12 +9,25 @@ 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));
|
||||
const access = i => {
|
||||
const name = randomstring.generate(config.uploads.fileLength) + path.extname(file.originalname);
|
||||
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);
|
||||
}
|
||||
});
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user