refactor: uploadController pass errors

This commit is contained in:
Bobby Wibowo 2022-07-10 14:20:49 +07:00
parent 0f47ed76b0
commit b3a304729f
No known key found for this signature in database
GPG Key ID: 51C3A1E1E22D26CF

View File

@ -8,7 +8,6 @@ const searchQuery = require('search-query-parser')
const paths = require('./pathsController') const paths = require('./pathsController')
const perms = require('./permissionController') const perms = require('./permissionController')
const utils = require('./utilsController') const utils = require('./utilsController')
const apiErrorsHandler = require('./handlers/apiErrorsHandler.js')
const ClientError = require('./utils/ClientError') const ClientError = require('./utils/ClientError')
const multerStorage = require('./utils/multerStorage') const multerStorage = require('./utils/multerStorage')
const ServerError = require('./utils/ServerError') const ServerError = require('./utils/ServerError')
@ -275,8 +274,8 @@ self.parseStripTags = stripTags => {
/** Uploads */ /** Uploads */
self.upload = async (req, res, next) => { self.upload = (req, res, next) => {
try { Promise.resolve().then(async () => {
let user let user
if (config.private === true) { if (config.private === true) {
user = await utils.authorize(req) user = await utils.authorize(req)
@ -297,9 +296,7 @@ self.upload = async (req, res, next) => {
const func = req.body.urls ? self.actuallyUploadUrls : self.actuallyUploadFiles const func = req.body.urls ? self.actuallyUploadUrls : self.actuallyUploadFiles
await func(req, res, user, albumid, age) await func(req, res, user, albumid, age)
} catch (error) { }).catch(next)
return apiErrorsHandler(error, req, res, next)
}
} }
self.actuallyUploadFiles = async (req, res, user, albumid, age) => { self.actuallyUploadFiles = async (req, res, user, albumid, age) => {
@ -540,8 +537,8 @@ self.actuallyUploadUrls = async (req, res, user, albumid, age) => {
/** Chunk uploads */ /** Chunk uploads */
self.finishChunks = async (req, res, next) => { self.finishChunks = (req, res, next) => {
try { Promise.resolve().then(async () => {
if (!chunkedUploads) { if (!chunkedUploads) {
throw new ClientError('Chunked upload is disabled.', { statusCode: 403 }) throw new ClientError('Chunked upload is disabled.', { statusCode: 403 })
} }
@ -555,9 +552,7 @@ self.finishChunks = async (req, res, next) => {
} }
await self.actuallyFinishChunks(req, res, user) await self.actuallyFinishChunks(req, res, user)
} catch (error) { }).catch(next)
return apiErrorsHandler(error, req, res, next)
}
} }
self.actuallyFinishChunks = async (req, res, user) => { self.actuallyFinishChunks = async (req, res, user) => {
@ -984,8 +979,8 @@ self.delete = async (req, res, next) => {
return self.bulkDelete(req, res, next) return self.bulkDelete(req, res, next)
} }
self.bulkDelete = async (req, res, next) => { self.bulkDelete = (req, res, next) => {
try { Promise.resolve().then(async () => {
const user = await utils.authorize(req) const user = await utils.authorize(req)
const field = req.body.field || 'id' const field = req.body.field || 'id'
@ -997,15 +992,13 @@ self.bulkDelete = async (req, res, next) => {
const failed = await utils.bulkDeleteFromDb(field, values, user) const failed = await utils.bulkDeleteFromDb(field, values, user)
await res.json({ success: true, failed }) await res.json({ success: true, failed })
} catch (error) { }).catch(next)
return apiErrorsHandler(error, req, res, next)
}
} }
/** List uploads */ /** List uploads */
self.list = async (req, res, next) => { self.list = (req, res, next) => {
try { Promise.resolve().then(async () => {
const user = await utils.authorize(req) const user = await utils.authorize(req)
const all = req.headers.all === '1' const all = req.headers.all === '1'
@ -1582,15 +1575,13 @@ self.list = async (req, res, next) => {
} }
await res.json({ success: true, files, count, users, albums, basedomain }) await res.json({ success: true, files, count, users, albums, basedomain })
} catch (error) { }).catch(next)
return apiErrorsHandler(error, req, res, next)
}
} }
/** Get file info */ /** Get file info */
self.get = async (req, res, next) => { self.get = (req, res, next) => {
try { Promise.resolve().then(async () => {
const user = await utils.authorize(req) const user = await utils.authorize(req)
const ismoderator = perms.is(user, 'moderator') const ismoderator = perms.is(user, 'moderator')
@ -1613,9 +1604,7 @@ self.get = async (req, res, next) => {
} }
await res.json({ success: true, file }) await res.json({ success: true, file })
} catch (error) { }).catch(next)
return apiErrorsHandler(error, req, res, next)
}
} }
module.exports = self module.exports = self