Regular code improvements.
This commit is contained in:
Bobby Wibowo 2018-04-21 04:39:06 +07:00
parent a18d3a2e21
commit 674d20c62c
No known key found for this signature in database
GPG Key ID: 51C3A1E1E22D26CF
4 changed files with 44 additions and 43 deletions

View File

@ -242,7 +242,7 @@ albumsController.addFiles = async (req, res, next) => {
if (!user) { return }
const ids = req.body.ids
if (ids === undefined || !ids.length) {
if (!ids || !ids.length) {
return res.json({ success: false, description: 'No files specified.' })
}
@ -300,6 +300,7 @@ albumsController.addFiles = async (req, res, next) => {
.where('id', albumid)
.update('editedAt', Math.floor(Date.now() / 1000))
}))
return res.json({
success: true,
failedids

View File

@ -302,7 +302,7 @@ uploadsController.actuallyFinishChunks = async (req, res, user, albumid) => {
}
}
uploadsController.appendToStream = async (destFileStream, uuidDr, chunkNames) => {
uploadsController.appendToStream = (destFileStream, uuidDr, chunkNames) => {
return new Promise((resolve, reject) => {
const append = i => {
if (i < chunkNames.length) {
@ -325,7 +325,7 @@ uploadsController.appendToStream = async (destFileStream, uuidDr, chunkNames) =>
})
}
uploadsController.writeFilesToDb = async (req, res, user, infoMap) => {
uploadsController.writeFilesToDb = (req, res, user, infoMap) => {
return new Promise((resolve, reject) => {
let iteration = 0
const files = []

View File

@ -92,7 +92,7 @@ utilsController.generateThumbs = (file, basedomain) => {
})
}
utilsController.deleteFile = async file => {
utilsController.deleteFile = file => {
const ext = path.extname(file).toLowerCase()
return new Promise((resolve, reject) => {
fs.stat(path.join(__dirname, '..', config.uploads.folder, file), (error, stats) => {
@ -105,9 +105,7 @@ utilsController.deleteFile = async file => {
file = file.substr(0, file.lastIndexOf('.')) + '.png'
fs.stat(path.join(__dirname, '..', config.uploads.folder, 'thumbs/', file), (error, stats) => {
if (error) {
if (error.code !== 'ENOENT') {
console.log(error)
}
if (error.code !== 'ENOENT') { console.log(error) }
return resolve()
}
fs.unlink(path.join(__dirname, '..', config.uploads.folder, 'thumbs/', file), error => {
@ -132,42 +130,46 @@ utilsController.bulkDeleteFilesByIds = async (ids, user) => {
})
const failedids = ids.filter(id => !files.find(file => file.id === id))
const albumids = []
// First, we delete all the physical files
// Delete all files
await Promise.all(files.map(file => {
return utilsController.deleteFile(file.name).catch(error => {
// ENOENT is missing file, for whatever reason, then just delete from db anyways
if (error.code !== 'ENOENT') {
console.log(error)
failedids.push(file.id)
}
return new Promise(async resolve => {
const deleteFile = await utilsController.deleteFile(file.name)
.then(() => true)
.catch(error => {
if (error.code === 'ENOENT') { return true }
console.log(error)
failedids.push(file.id)
})
if (!deleteFile) { return resolve() }
await db.table('files')
.where('id', file.id)
.del()
.then(() => {
if (file.albumid && !albumids.includes(file.albumid)) {
albumids.push(file.albumid)
}
})
.catch(error => {
console.error(error)
failedids.push(file.id)
})
return resolve()
})
}))
// Second, we filter out failed IDs
const albumids = []
const updateDbIds = files.filter(file => !failedids.includes(file.id))
await Promise.all(updateDbIds.map(file => {
return db.table('files')
.where('id', file.id)
.del()
.then(() => {
if (file.albumid && !albumids.includes(file.albumid)) {
albumids.push(file.albumid)
}
})
.catch(error => {
console.error(error)
failedids.push(file.id)
})
}))
// Third, we update albums, if necessary
await Promise.all(albumids.map(albumid => {
return db.table('albums')
.where('id', albumid)
.update('editedAt', Math.floor(Date.now() / 1000))
}))
// Update albums if necessary
if (albumids.length) {
await Promise.all(albumids.map(albumid => {
return db.table('albums')
.where('id', albumid)
.update('editedAt', Math.floor(Date.now() / 1000))
}))
}
return failedids
}

View File

@ -56,12 +56,10 @@ safe.use('/api', api)
for (const page of config.pages) {
if (fs.existsSync(`./pages/custom/${page}.html`)) {
safe.get(`/${page}`, (req, res, next) => res.sendFile(`${page}.html`, { root: './pages/custom/' }))
} else if (page === 'home') {
safe.get('/', (req, res, next) => res.render('home'))
} else {
if (page === 'home') {
safe.get('/', (req, res, next) => res.render('home'))
} else {
safe.get(`/${page}`, (req, res, next) => res.render(page))
}
safe.get(`/${page}`, (req, res, next) => res.render(page))
}
}