mirror of
https://github.com/BobbyWibowo/lolisafe.git
synced 2025-01-19 01:31:34 +00:00
Uses async
This commit is contained in:
parent
dcb72734fe
commit
5be27c129d
@ -17,18 +17,17 @@ const storage = multer.diskStorage({
|
|||||||
destination: function(req, file, cb) {
|
destination: function(req, file, cb) {
|
||||||
cb(null, uploadDir);
|
cb(null, uploadDir);
|
||||||
},
|
},
|
||||||
filename: function(req, file, cb) {
|
filename: function(req, file, cb) {
|
||||||
for (let i = 0; i < maxTries; i++) {
|
const access = i => {
|
||||||
const name = randomstring.generate(config.uploads.fileLength) + path.extname(file.originalname);
|
const name = randomstring.generate(config.uploads.fileLength) + path.extname(file.originalname);
|
||||||
try {
|
fs.access(path.join(uploadDir, name), err => {
|
||||||
fs.accessSync(path.join(uploadDir, name));
|
if (err) return cb(null, name);
|
||||||
console.log(`A file named "${name}" already exists (${i + 1}/${maxTries}).`);
|
console.log(`A file named "${name}" already exists (${++i}/${maxTries}).`);
|
||||||
} catch (err) {
|
if (i < maxTries) return access(i);
|
||||||
// Note: fs.accessSync() will throw an Error if a file with the same name does not exist
|
return cb('Could not allocate a unique file name. Try again?');
|
||||||
return cb(null, name);
|
});
|
||||||
}
|
};
|
||||||
}
|
access(0);
|
||||||
return cb('Could not allocate a unique file name. Try again?');
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user