mirror of
https://github.com/BobbyWibowo/lolisafe.git
synced 2025-01-31 07:11:33 +00:00
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.
This commit is contained in:
parent
076be2cfec
commit
070f4bdafd
@ -67,6 +67,13 @@ module.exports = {
|
|||||||
// The length of the random generated name for the uploaded files
|
// The length of the random generated name for the uploaded files
|
||||||
fileLength: 32,
|
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
|
NOTE: Thumbnails are only for the admin panel and they require you
|
||||||
to install a separate binary called graphicsmagick (http://www.graphicsmagick.org)
|
to install a separate binary called graphicsmagick (http://www.graphicsmagick.org)
|
||||||
|
@ -9,7 +9,7 @@ const utils = require('./utilsController.js')
|
|||||||
|
|
||||||
const uploadsController = {}
|
const uploadsController = {}
|
||||||
|
|
||||||
const maxStrikes = 3
|
const maxTries = config.uploads.maxTries || 1
|
||||||
const uploadDir = path.join(__dirname, '..', config.uploads.folder)
|
const uploadDir = path.join(__dirname, '..', config.uploads.folder)
|
||||||
|
|
||||||
const storage = multer.diskStorage({
|
const storage = multer.diskStorage({
|
||||||
@ -17,25 +17,18 @@ const storage = multer.diskStorage({
|
|||||||
cb(null, uploadDir)
|
cb(null, uploadDir)
|
||||||
},
|
},
|
||||||
filename: function (req, file, cb) {
|
filename: function (req, file, cb) {
|
||||||
let name
|
for (let i = 0; i < maxTries; i++) {
|
||||||
let strike = 0
|
const name = randomstring.generate(config.uploads.fileLength) + path.extname(file.originalname)
|
||||||
|
|
||||||
do {
|
|
||||||
name = randomstring.generate(config.uploads.fileLength) + path.extname(file.originalname)
|
|
||||||
try {
|
try {
|
||||||
fs.accessSync(path.join(uploadDir, name))
|
fs.accessSync(path.join(uploadDir, name))
|
||||||
strike++
|
console.log(`A file named "${name}" already exists (${i + 1}/${maxTries}).`)
|
||||||
console.log(`"${name}" already exists in upload dir (${strike}x)\u2000`)
|
|
||||||
} catch (err) {
|
} 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) {
|
fileFilter: function (req, file, cb) {
|
||||||
if (config.blockedExtensions !== undefined) {
|
if (config.blockedExtensions !== undefined) {
|
||||||
if (config.blockedExtensions.some(extension => path.extname(file.originalname).toLowerCase() === extension)) {
|
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)
|
return cb(null, true)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user