* Patched delete function to continue deleting the file from the database if the physical file is missing from the expected path.

* Patched delete function to not print any error message if the file does not have any thumbnail.

* Patched uploader to check the existence of file with the same name, then try to generate a new random name if true, up to 3 times. If it still can not generate a unique random name after 3 times, it will throw an error saying that it can not allocate a name to the client.
This will be useful when shortening file name in the config file.
This commit is contained in:
Bobby Wibowo 2018-03-13 21:51:39 +07:00
parent c1db799aee
commit cb0295e732
No known key found for this signature in database
GPG Key ID: 51C3A1E1E22D26CF

View File

@ -9,12 +9,33 @@ const utils = require('./utilsController.js')
const uploadsController = {} const uploadsController = {}
const maxStrikes = 3
const uploadDir = path.join(__dirname, '..', config.uploads.folder)
const storage = multer.diskStorage({ const storage = multer.diskStorage({
destination: function (req, file, cb) { destination: function (req, file, cb) {
cb(null, path.join(__dirname, '..', config.uploads.folder)) cb(null, uploadDir)
}, },
filename: function (req, file, cb) { filename: function (req, file, cb) {
cb(null, randomstring.generate(config.uploads.fileLength) + path.extname(file.originalname)) let name
let strike = 0
do {
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`)
} catch (err) {
strike = 0
}
} 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)
}
} }
}) })
@ -175,7 +196,10 @@ uploadsController.delete = async (req, res) => {
.first() .first()
try { try {
await uploadsController.deleteFile(file.name) await uploadsController.deleteFile(file.name).catch(err => {
// ENOENT is missing file, for whatever reason, then just delete from db
if (err.code !== 'ENOENT') throw err
})
await db.table('files').where('id', id).del() await db.table('files').where('id', id).del()
if (file.albumid) { if (file.albumid) {
await db.table('albums').where('id', file.albumid).update('editedAt', Math.floor(Date.now() / 1000)) await db.table('albums').where('id', file.albumid).update('editedAt', Math.floor(Date.now() / 1000))
@ -200,7 +224,9 @@ uploadsController.deleteFile = function (file) {
file = file.substr(0, file.lastIndexOf('.')) + '.png' file = file.substr(0, file.lastIndexOf('.')) + '.png'
fs.stat(path.join(__dirname, '..', config.uploads.folder, 'thumbs/', file), (err, stats) => { fs.stat(path.join(__dirname, '..', config.uploads.folder, 'thumbs/', file), (err, stats) => {
if (err) { if (err) {
console.log(err) if (err.code !== 'ENOENT') {
console.log(err)
}
return resolve() return resolve()
} }
fs.unlink(path.join(__dirname, '..', config.uploads.folder, 'thumbs/', file), err => { fs.unlink(path.join(__dirname, '..', config.uploads.folder, 'thumbs/', file), err => {