docs: uploadController.js

This commit is contained in:
Bobby 2022-04-16 20:44:11 +07:00
parent fff5ebbdd4
commit 4b9740cf5c
No known key found for this signature in database
GPG Key ID: 941839794CBF5A09

View File

@ -20,6 +20,8 @@ const self = {
onHold: new Set() onHold: new Set()
} }
/** Preferences */
const fileIdentifierLengthFallback = 32 const fileIdentifierLengthFallback = 32
const fileIdentifierLengthChangeable = !config.uploads.fileIdentifierLength.force && const fileIdentifierLengthChangeable = !config.uploads.fileIdentifierLength.force &&
typeof config.uploads.fileIdentifierLength.min === 'number' && typeof config.uploads.fileIdentifierLength.min === 'number' &&
@ -48,6 +50,8 @@ const urlExtensionsFilter = Array.isArray(config.uploads.urlExtensionsFilter) &&
const temporaryUploads = Array.isArray(config.uploads.temporaryUploadAges) && const temporaryUploads = Array.isArray(config.uploads.temporaryUploadAges) &&
config.uploads.temporaryUploadAges.length config.uploads.temporaryUploadAges.length
/** Chunks helper class & function **/
class ChunksData { class ChunksData {
constructor (uuid, root) { constructor (uuid, root) {
this.uuid = uuid this.uuid = uuid
@ -96,6 +100,8 @@ const initChunks = async uuid => {
return chunksData[uuid] return chunksData[uuid]
} }
/** Multer */
const executeMulter = multer({ const executeMulter = multer({
// Guide: https://github.com/expressjs/multer#limits // Guide: https://github.com/expressjs/multer#limits
limits: { limits: {
@ -164,6 +170,8 @@ const executeMulter = multer({
}) })
}).array('files[]') }).array('files[]')
/** Helper functions */
self.isExtensionFiltered = extname => { self.isExtensionFiltered = extname => {
// If empty extension needs to be filtered // If empty extension needs to be filtered
if (!extname && config.filterNoExtension) return true if (!extname && config.filterNoExtension) return true
@ -257,6 +265,8 @@ self.parseStripTags = stripTags => {
return Boolean(parseInt(stripTags)) return Boolean(parseInt(stripTags))
} }
/** Uploads */
self.upload = async (req, res, next) => { self.upload = async (req, res, next) => {
try { try {
let user let user
@ -357,6 +367,8 @@ self.actuallyUploadFiles = async (req, res, user, albumid, age) => {
return self.sendUploadResponse(req, res, user, result) return self.sendUploadResponse(req, res, user, result)
} }
/** URL uploads */
self.actuallyUploadUrls = async (req, res, user, albumid, age) => { self.actuallyUploadUrls = async (req, res, user, albumid, age) => {
if (!config.uploads.urlMaxSize) { if (!config.uploads.urlMaxSize) {
throw new ClientError('Upload by URLs is disabled at the moment.', { statusCode: 403 }) throw new ClientError('Upload by URLs is disabled at the moment.', { statusCode: 403 })
@ -477,6 +489,8 @@ self.actuallyUploadUrls = async (req, res, user, albumid, age) => {
} }
} }
/** Chunk uploads */
self.finishChunks = async (req, res, next) => { self.finishChunks = async (req, res, next) => {
try { try {
if (!chunkedUploads) { if (!chunkedUploads) {
@ -620,6 +634,8 @@ self.cleanUpChunks = async (uuid, onTimeout) => {
delete chunksData[uuid] delete chunksData[uuid]
} }
/** Virus scanning (ClamAV) */
self.assertPassthroughScans = async (req, user, infoMap) => { self.assertPassthroughScans = async (req, user, infoMap) => {
const foundThreats = [] const foundThreats = []
const unableToScan = [] const unableToScan = []
@ -710,6 +726,8 @@ self.scanFiles = async (req, user, infoMap) => {
return result return result
} }
/** Strip tags (EXIF, etc.) */
self.stripTags = async (req, infoMap) => { self.stripTags = async (req, infoMap) => {
if (!self.parseStripTags(req.headers.striptags)) return if (!self.parseStripTags(req.headers.striptags)) return
@ -729,6 +747,8 @@ self.stripTags = async (req, infoMap) => {
} }
} }
/** Database functions */
self.storeFilesToDb = async (req, res, user, infoMap) => { self.storeFilesToDb = async (req, res, user, infoMap) => {
const files = [] const files = []
const exists = [] const exists = []
@ -839,6 +859,8 @@ self.storeFilesToDb = async (req, res, user, infoMap) => {
return [...files, ...exists] return [...files, ...exists]
} }
/** Final response */
self.sendUploadResponse = async (req, res, user, result) => { self.sendUploadResponse = async (req, res, user, result) => {
// Send response // Send response
return res.json({ return res.json({
@ -874,6 +896,8 @@ self.sendUploadResponse = async (req, res, user, result) => {
}) })
} }
/** Delete uploads */
self.delete = async (req, res, next) => { self.delete = async (req, res, next) => {
// Map /api/delete requests to /api/bulkdelete // Map /api/delete requests to /api/bulkdelete
let body let body
@ -915,6 +939,8 @@ self.bulkDelete = async (req, res, next) => {
} }
} }
/** List uploads */
self.list = async (req, res, next) => { self.list = async (req, res, next) => {
try { try {
const user = await utils.authorize(req) const user = await utils.authorize(req)