2018-01-23 20:06:30 +00:00
|
|
|
const config = require('../config.js')
|
|
|
|
const path = require('path')
|
|
|
|
const multer = require('multer')
|
|
|
|
const randomstring = require('randomstring')
|
|
|
|
const db = require('knex')(config.database)
|
|
|
|
const crypto = require('crypto')
|
|
|
|
const fs = require('fs')
|
2018-03-28 11:36:28 +00:00
|
|
|
const rimraf = require('rimraf')
|
2018-01-23 20:06:30 +00:00
|
|
|
const utils = require('./utilsController.js')
|
2017-01-13 07:34:21 +00:00
|
|
|
|
2018-01-23 20:06:30 +00:00
|
|
|
const uploadsController = {}
|
2017-01-13 07:34:21 +00:00
|
|
|
|
2018-03-28 11:36:28 +00:00
|
|
|
// Let's default it to only 1 try (for missing config key)
|
2018-03-18 13:13:08 +00:00
|
|
|
const maxTries = config.uploads.maxTries || 1
|
2018-03-13 14:51:39 +00:00
|
|
|
const uploadDir = path.join(__dirname, '..', config.uploads.folder)
|
2018-03-28 11:36:28 +00:00
|
|
|
const chunkedUploads = config.uploads.chunkedUploads && config.uploads.chunkedUploads.enabled
|
|
|
|
const chunksDir = path.join(uploadDir, 'chunks')
|
|
|
|
const maxSizeBytes = parseInt(config.uploads.maxSize) * 1000000
|
2018-03-13 14:51:39 +00:00
|
|
|
|
2017-01-13 07:34:21 +00:00
|
|
|
const storage = multer.diskStorage({
|
2018-01-23 20:06:30 +00:00
|
|
|
destination: function (req, file, cb) {
|
2018-03-28 11:36:28 +00:00
|
|
|
// If chunked uploads is disabled or the uploaded file is not a chunk
|
|
|
|
if (!chunkedUploads || (req.body.uuid === undefined && req.body.chunkindex === undefined)) {
|
|
|
|
return cb(null, uploadDir)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for the existence of UUID dir in chunks dir
|
|
|
|
const uuidDir = path.join(chunksDir, req.body.uuid)
|
2018-03-29 23:22:08 +00:00
|
|
|
fs.access(uuidDir, error => {
|
2018-03-28 11:36:28 +00:00
|
|
|
// If it exists, callback
|
2018-03-29 23:22:08 +00:00
|
|
|
if (!error) { return cb(null, uuidDir) }
|
2018-03-28 11:36:28 +00:00
|
|
|
// It it doesn't, then make it first
|
2018-03-29 23:22:08 +00:00
|
|
|
fs.mkdir(uuidDir, error => {
|
2018-03-28 11:36:28 +00:00
|
|
|
// If there was no error, callback
|
2018-03-29 23:22:08 +00:00
|
|
|
if (!error) { return cb(null, uuidDir) }
|
2018-03-28 11:36:28 +00:00
|
|
|
// Otherwise, log it
|
2018-03-29 23:22:08 +00:00
|
|
|
console.log(error)
|
2018-03-18 16:59:03 +00:00
|
|
|
// eslint-disable-next-line standard/no-callback-literal
|
2018-03-28 11:36:28 +00:00
|
|
|
return cb('Could not process the chunked upload. Try again?')
|
2018-03-18 16:59:03 +00:00
|
|
|
})
|
2018-03-28 11:36:28 +00:00
|
|
|
})
|
|
|
|
},
|
|
|
|
filename: function (req, file, cb) {
|
|
|
|
const extension = path.extname(file.originalname)
|
|
|
|
|
|
|
|
// If chunked uploads is disabled or the uploaded file is not a chunk
|
|
|
|
if (!chunkedUploads || (req.body.uuid === undefined && req.body.chunkindex === undefined)) {
|
|
|
|
const length = uploadsController.getFileNameLength(req)
|
|
|
|
return uploadsController.getUniqueRandomName(length, extension, cb)
|
2018-03-13 14:51:39 +00:00
|
|
|
}
|
2018-03-28 11:36:28 +00:00
|
|
|
|
2018-03-28 14:10:20 +00:00
|
|
|
// index.extension (e.i. 0.jpg, 1.jpg, ..., n.jpg - will prepend zeros depending on the amount of chunks)
|
|
|
|
const digits = req.body.totalchunkcount !== undefined ? String(req.body.totalchunkcount - 1).length : 1
|
|
|
|
const zeros = new Array(digits + 1).join('0')
|
|
|
|
const name = (zeros + req.body.chunkindex).slice(-digits)
|
|
|
|
return cb(null, name + extension)
|
2018-01-23 20:06:30 +00:00
|
|
|
}
|
|
|
|
})
|
2017-01-13 07:34:21 +00:00
|
|
|
|
|
|
|
const upload = multer({
|
2018-03-28 11:36:28 +00:00
|
|
|
storage,
|
|
|
|
limits: {
|
|
|
|
fileSize: config.uploads.maxSize
|
|
|
|
},
|
2018-01-23 20:06:30 +00:00
|
|
|
fileFilter: function (req, file, cb) {
|
2018-03-28 11:36:28 +00:00
|
|
|
// If there are no blocked extensions
|
|
|
|
if (config.blockedExtensions === undefined) {
|
|
|
|
return cb(null, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the extension is blocked
|
|
|
|
if (config.blockedExtensions.some(extension => {
|
|
|
|
return path.extname(file.originalname).toLowerCase() === extension.toLowerCase()
|
|
|
|
})) {
|
2018-03-24 13:52:47 +00:00
|
|
|
// eslint-disable-next-line standard/no-callback-literal
|
|
|
|
return cb('This file extension is not allowed.')
|
2018-01-23 20:06:30 +00:00
|
|
|
}
|
2018-03-28 11:36:28 +00:00
|
|
|
|
|
|
|
if (chunkedUploads) {
|
|
|
|
// Re-map Dropzone keys so people can manually use the API without prepending 'dz'
|
|
|
|
const keys = Object.keys(req.body)
|
|
|
|
if (keys.length) {
|
|
|
|
for (const key of keys) {
|
2018-03-28 17:40:50 +00:00
|
|
|
if (!/^dz/.test(key)) { continue }
|
2018-03-28 11:36:28 +00:00
|
|
|
req.body[key.replace(/^dz/, '')] = req.body[key]
|
|
|
|
delete req.body[key]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const totalFileSize = parseInt(req.body.totalfilesize)
|
|
|
|
if (!isNaN(totalFileSize) && totalFileSize > maxSizeBytes) {
|
|
|
|
// eslint-disable-next-line standard/no-callback-literal
|
|
|
|
return cb('Chunked upload error. Total file size is larger than maximum file size.')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the extension is not blocked
|
2018-01-23 20:06:30 +00:00
|
|
|
return cb(null, true)
|
|
|
|
}
|
|
|
|
}).array('files[]')
|
2017-01-13 07:34:21 +00:00
|
|
|
|
2018-03-28 11:36:28 +00:00
|
|
|
uploadsController.getFileNameLength = req => {
|
|
|
|
// If the user has a preferred file length, make sure it is within the allowed range
|
|
|
|
if (req.headers.filelength) {
|
|
|
|
return Math.min(Math.max(req.headers.filelength, config.uploads.fileLength.min), config.uploads.fileLength.max)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let's default it to 32 characters when config key is falsy
|
|
|
|
return config.uploads.fileLength.default || 32
|
|
|
|
}
|
|
|
|
|
|
|
|
uploadsController.getUniqueRandomName = (length, extension, cb) => {
|
|
|
|
const access = i => {
|
|
|
|
const name = randomstring.generate(length) + extension
|
2018-03-29 23:22:08 +00:00
|
|
|
fs.access(path.join(uploadDir, name), error => {
|
2018-03-28 11:36:28 +00:00
|
|
|
// If a file with the same name does not exist
|
2018-03-29 23:22:08 +00:00
|
|
|
if (error) { return cb(null, name) }
|
2018-03-28 11:36:28 +00:00
|
|
|
// If a file with the same name already exists, log to console
|
|
|
|
console.log(`A file named ${name} already exists (${++i}/${maxTries}).`)
|
|
|
|
// If it still haven't reached allowed maximum tries, then try again
|
2018-03-28 17:40:50 +00:00
|
|
|
if (i < maxTries) { return access(i) }
|
2018-03-28 11:36:28 +00:00
|
|
|
// eslint-disable-next-line standard/no-callback-literal
|
|
|
|
return cb('Could not allocate a unique random name. Try again?')
|
|
|
|
})
|
|
|
|
}
|
|
|
|
// Get us a unique random name
|
|
|
|
access(0)
|
|
|
|
}
|
|
|
|
|
2017-10-04 00:13:38 +00:00
|
|
|
uploadsController.upload = async (req, res, next) => {
|
2018-03-24 19:47:41 +00:00
|
|
|
let user
|
2018-01-23 20:06:30 +00:00
|
|
|
if (config.private === true) {
|
2018-03-24 19:47:41 +00:00
|
|
|
user = await utils.authorize(req, res)
|
2018-03-28 17:40:50 +00:00
|
|
|
if (!user) { return }
|
2018-03-24 19:47:41 +00:00
|
|
|
} else if (req.headers.token) {
|
|
|
|
user = await db.table('users').where('token', req.headers.token).first()
|
2018-01-23 20:06:30 +00:00
|
|
|
}
|
|
|
|
|
2018-03-14 06:57:09 +00:00
|
|
|
if (user && (user.enabled === false || user.enabled === 0)) {
|
|
|
|
return res.json({
|
|
|
|
success: false,
|
2018-03-24 13:52:47 +00:00
|
|
|
description: 'This account has been disabled.'
|
2018-03-14 06:57:09 +00:00
|
|
|
})
|
|
|
|
}
|
2018-03-28 11:36:28 +00:00
|
|
|
|
|
|
|
if (user && user.fileLength && !req.headers.filelength) {
|
|
|
|
req.headers.filelength = user.fileLength
|
2018-03-24 13:52:47 +00:00
|
|
|
}
|
2018-03-28 11:36:28 +00:00
|
|
|
|
2018-01-23 20:06:30 +00:00
|
|
|
const albumid = req.headers.albumid || req.params.albumid
|
|
|
|
|
|
|
|
if (albumid && user) {
|
|
|
|
const album = await db.table('albums').where({ id: albumid, userid: user.id }).first()
|
|
|
|
if (!album) {
|
|
|
|
return res.json({
|
|
|
|
success: false,
|
2018-03-24 13:52:47 +00:00
|
|
|
description: 'Album doesn\'t exist or it doesn\'t belong to the user.'
|
2018-01-23 20:06:30 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return uploadsController.actuallyUpload(req, res, user, albumid)
|
|
|
|
}
|
|
|
|
return uploadsController.actuallyUpload(req, res, user, albumid)
|
|
|
|
}
|
2017-01-19 06:34:48 +00:00
|
|
|
|
2018-03-28 11:36:28 +00:00
|
|
|
uploadsController.actuallyUpload = async (req, res, user, albumid) => {
|
2018-03-29 23:22:08 +00:00
|
|
|
const erred = error => {
|
|
|
|
console.log(error)
|
2018-03-28 11:36:28 +00:00
|
|
|
res.json({
|
|
|
|
success: false,
|
2018-03-29 23:22:08 +00:00
|
|
|
description: error.toString()
|
2018-03-28 11:36:28 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-03-29 23:22:08 +00:00
|
|
|
upload(req, res, async error => {
|
|
|
|
if (error) { return erred(error) }
|
2018-03-28 11:36:28 +00:00
|
|
|
|
2018-03-28 17:40:50 +00:00
|
|
|
if (req.files.length === 0) { return erred(new Error('No files.')) }
|
2018-03-28 11:36:28 +00:00
|
|
|
|
|
|
|
// If chunked uploads is enabeld and the uploaded file is a chunk, then just say that it was a success
|
2018-03-28 17:40:50 +00:00
|
|
|
if (chunkedUploads && req.body.uuid) { return res.json({ success: true }) }
|
2018-03-28 11:36:28 +00:00
|
|
|
|
|
|
|
const infoMap = req.files.map(file => {
|
|
|
|
return {
|
|
|
|
path: path.join(__dirname, '..', config.uploads.folder, file.filename),
|
|
|
|
data: file
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
const result = await uploadsController.writeFilesToDb(req, res, user, albumid, infoMap)
|
|
|
|
.catch(erred)
|
|
|
|
|
|
|
|
if (result) {
|
|
|
|
return uploadsController.processFilesForDisplay(req, res, result.files, result.existingFiles)
|
2018-01-23 20:06:30 +00:00
|
|
|
}
|
2018-03-28 11:36:28 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
uploadsController.finishChunks = async (req, res, next) => {
|
|
|
|
if (!config.uploads.chunkedUploads || !config.uploads.chunkedUploads.enabled) {
|
|
|
|
return res.json({
|
|
|
|
success: false,
|
|
|
|
description: 'Chunked uploads is disabled at the moment.'
|
|
|
|
})
|
|
|
|
}
|
2018-01-23 20:06:30 +00:00
|
|
|
|
2018-03-28 11:36:28 +00:00
|
|
|
let user
|
|
|
|
if (config.private === true) {
|
|
|
|
user = await utils.authorize(req, res)
|
2018-03-28 17:40:50 +00:00
|
|
|
if (!user) { return }
|
2018-03-28 11:36:28 +00:00
|
|
|
} else if (req.headers.token) {
|
|
|
|
user = await db.table('users').where('token', req.headers.token).first()
|
|
|
|
}
|
2018-01-23 20:06:30 +00:00
|
|
|
|
2018-03-28 11:36:28 +00:00
|
|
|
if (user && (user.enabled === false || user.enabled === 0)) {
|
|
|
|
return res.json({
|
|
|
|
success: false,
|
|
|
|
description: 'This account has been disabled.'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if (user && user.fileLength && !req.headers.filelength) {
|
|
|
|
req.headers.filelength = user.fileLength
|
|
|
|
}
|
|
|
|
|
|
|
|
const albumid = req.headers.albumid || req.params.albumid
|
|
|
|
|
|
|
|
if (albumid && user) {
|
|
|
|
const album = await db.table('albums').where({ id: albumid, userid: user.id }).first()
|
|
|
|
if (!album) {
|
|
|
|
return res.json({
|
|
|
|
success: false,
|
|
|
|
description: 'Album doesn\'t exist or it doesn\'t belong to the user.'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return uploadsController.actuallyFinishChunks(req, res, user, albumid)
|
|
|
|
}
|
|
|
|
return uploadsController.actuallyFinishChunks(req, res, user, albumid)
|
|
|
|
}
|
|
|
|
|
|
|
|
uploadsController.actuallyFinishChunks = async (req, res, user, albumid) => {
|
2018-03-29 23:22:08 +00:00
|
|
|
const erred = error => {
|
|
|
|
console.log(error)
|
2018-03-28 11:36:28 +00:00
|
|
|
res.json({
|
|
|
|
success: false,
|
2018-03-29 23:22:08 +00:00
|
|
|
description: error.toString()
|
2018-03-28 11:36:28 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const files = req.body.files
|
2018-03-28 17:40:50 +00:00
|
|
|
if (!files) { return erred(new Error('Missing files array.')) }
|
2018-03-28 11:36:28 +00:00
|
|
|
|
|
|
|
let iteration = 0
|
|
|
|
const infoMap = []
|
|
|
|
files.forEach(file => {
|
|
|
|
const { uuid, count } = file
|
2018-03-28 17:40:50 +00:00
|
|
|
if (!uuid || !count) { return erred(new Error('Missing UUID and/or chunks count.')) }
|
2018-03-28 11:36:28 +00:00
|
|
|
|
|
|
|
const chunksDirUuid = path.join(chunksDir, uuid)
|
|
|
|
|
2018-03-29 23:22:08 +00:00
|
|
|
fs.readdir(chunksDirUuid, async (error, chunks) => {
|
|
|
|
if (error) { return erred(error) }
|
2018-03-28 17:40:50 +00:00
|
|
|
if (count < chunks.length) { return erred(new Error('Chunks count mismatch.')) }
|
2018-03-28 11:36:28 +00:00
|
|
|
|
|
|
|
const extension = path.extname(chunks[0])
|
|
|
|
const length = uploadsController.getFileNameLength(req)
|
|
|
|
|
2018-03-29 23:22:08 +00:00
|
|
|
uploadsController.getUniqueRandomName(length, extension, async (error, name) => {
|
|
|
|
if (error) { return erred(error) }
|
2018-03-28 11:36:28 +00:00
|
|
|
|
|
|
|
const destination = path.join(uploadDir, name)
|
|
|
|
const destFileStream = fs.createWriteStream(destination, { flags: 'a' })
|
|
|
|
|
|
|
|
chunks.sort()
|
|
|
|
const appended = await uploadsController.appendToStream(destFileStream, chunksDirUuid, chunks)
|
|
|
|
.catch(erred)
|
|
|
|
|
2018-03-29 23:22:08 +00:00
|
|
|
rimraf(chunksDirUuid, error => {
|
|
|
|
if (error) {
|
|
|
|
console.log(error)
|
2018-03-28 11:36:28 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-03-28 17:40:50 +00:00
|
|
|
if (!appended) { return }
|
2018-03-28 11:36:28 +00:00
|
|
|
|
|
|
|
infoMap.push({
|
|
|
|
path: destination,
|
|
|
|
data: {
|
|
|
|
filename: name,
|
|
|
|
originalname: file.original || '',
|
|
|
|
mimetype: file.type || '',
|
|
|
|
size: file.size || 0
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
iteration++
|
|
|
|
if (iteration >= files.length) {
|
|
|
|
const result = await uploadsController.writeFilesToDb(req, res, user, albumid, infoMap)
|
|
|
|
.catch(erred)
|
|
|
|
|
|
|
|
if (result) {
|
|
|
|
return uploadsController.processFilesForDisplay(req, res, result.files, result.existingFiles)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
uploadsController.appendToStream = async (destFileStream, chunksDirUuid, chunks) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const append = i => {
|
|
|
|
if (i < chunks.length) {
|
|
|
|
fs.createReadStream(path.join(chunksDirUuid, chunks[i]))
|
|
|
|
.on('end', () => {
|
|
|
|
append(i + 1)
|
|
|
|
})
|
2018-03-29 23:22:08 +00:00
|
|
|
.on('error', error => {
|
|
|
|
console.log(error)
|
2018-03-28 11:36:28 +00:00
|
|
|
destFileStream.end()
|
2018-03-29 23:22:08 +00:00
|
|
|
return reject(error)
|
2018-03-28 11:36:28 +00:00
|
|
|
})
|
|
|
|
.pipe(destFileStream, { end: false })
|
|
|
|
} else {
|
|
|
|
destFileStream.end()
|
|
|
|
return resolve(true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
append(0)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
uploadsController.writeFilesToDb = async (req, res, user, albumid, infoMap) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let iteration = 0
|
2018-01-23 20:06:30 +00:00
|
|
|
const files = []
|
|
|
|
const existingFiles = []
|
|
|
|
|
2018-03-28 11:36:28 +00:00
|
|
|
infoMap.forEach(info => {
|
2018-01-23 20:06:30 +00:00
|
|
|
// Check if the file exists by checking hash and size
|
2018-03-28 11:36:28 +00:00
|
|
|
const hash = crypto.createHash('md5')
|
|
|
|
const stream = fs.createReadStream(info.path)
|
2018-01-23 20:06:30 +00:00
|
|
|
|
|
|
|
stream.on('data', data => {
|
|
|
|
hash.update(data, 'utf8')
|
|
|
|
})
|
|
|
|
|
|
|
|
stream.on('end', async () => {
|
|
|
|
const fileHash = hash.digest('hex')
|
|
|
|
const dbFile = await db.table('files')
|
|
|
|
.where(function () {
|
2018-03-28 17:40:50 +00:00
|
|
|
if (user === undefined) {
|
|
|
|
this.whereNull('userid')
|
|
|
|
} else {
|
|
|
|
this.where('userid', user.id)
|
|
|
|
}
|
2018-01-23 20:06:30 +00:00
|
|
|
})
|
|
|
|
.where({
|
|
|
|
hash: fileHash,
|
2018-03-28 11:36:28 +00:00
|
|
|
size: info.data.size
|
2018-01-23 20:06:30 +00:00
|
|
|
})
|
|
|
|
.first()
|
|
|
|
|
|
|
|
if (!dbFile) {
|
|
|
|
files.push({
|
2018-03-28 11:36:28 +00:00
|
|
|
name: info.data.filename,
|
|
|
|
original: info.data.originalname,
|
|
|
|
type: info.data.mimetype,
|
|
|
|
size: info.data.size,
|
2018-01-23 20:06:30 +00:00
|
|
|
hash: fileHash,
|
|
|
|
ip: req.ip,
|
2018-03-28 11:36:28 +00:00
|
|
|
albumid,
|
2018-03-24 19:47:41 +00:00
|
|
|
userid: user !== undefined ? user.id : null,
|
2018-01-23 20:06:30 +00:00
|
|
|
timestamp: Math.floor(Date.now() / 1000)
|
|
|
|
})
|
|
|
|
} else {
|
2018-03-30 02:39:53 +00:00
|
|
|
utils.deleteFile(info.data.filename).then(() => {}).catch(error => console.log(error))
|
2018-01-23 20:06:30 +00:00
|
|
|
existingFiles.push(dbFile)
|
|
|
|
}
|
|
|
|
|
|
|
|
iteration++
|
2018-03-28 11:36:28 +00:00
|
|
|
if (iteration >= infoMap.length) {
|
|
|
|
return resolve({ files, existingFiles })
|
|
|
|
}
|
2018-01-23 20:06:30 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2017-10-04 00:13:38 +00:00
|
|
|
|
|
|
|
uploadsController.processFilesForDisplay = async (req, res, files, existingFiles) => {
|
2018-01-23 20:06:30 +00:00
|
|
|
let basedomain = config.domain
|
|
|
|
if (files.length === 0) {
|
|
|
|
return res.json({
|
|
|
|
success: true,
|
|
|
|
files: existingFiles.map(file => {
|
|
|
|
return {
|
|
|
|
name: file.name,
|
|
|
|
size: file.size,
|
|
|
|
url: `${basedomain}/${file.name}`
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-03-28 11:36:28 +00:00
|
|
|
// Insert new files to DB
|
2018-01-23 20:06:30 +00:00
|
|
|
await db.table('files').insert(files)
|
2018-03-28 11:36:28 +00:00
|
|
|
|
|
|
|
// Push existing files to array for response
|
|
|
|
for (let efile of existingFiles) {
|
|
|
|
files.push(efile)
|
|
|
|
}
|
2018-01-23 20:06:30 +00:00
|
|
|
|
|
|
|
res.json({
|
|
|
|
success: true,
|
|
|
|
files: files.map(file => {
|
|
|
|
return {
|
|
|
|
name: file.name,
|
|
|
|
size: file.size,
|
|
|
|
url: `${basedomain}/${file.name}`
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
for (let file of files) {
|
|
|
|
let ext = path.extname(file.name).toLowerCase()
|
2018-03-24 13:52:47 +00:00
|
|
|
if ((config.uploads.generateThumbnails.image && utils.imageExtensions.includes(ext)) || (config.uploads.generateThumbnails.video && utils.videoExtensions.includes(ext))) {
|
2018-01-23 20:06:30 +00:00
|
|
|
file.thumb = `${basedomain}/thumbs/${file.name.slice(0, -ext.length)}.png`
|
|
|
|
utils.generateThumbs(file)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (file.albumid) {
|
2018-03-30 02:39:53 +00:00
|
|
|
db.table('albums')
|
|
|
|
.where('id', file.albumid)
|
|
|
|
.update('editedAt', file.timestamp)
|
|
|
|
.then(() => {})
|
2018-03-24 13:52:47 +00:00
|
|
|
.catch(error => { console.log(error); res.json({ success: false, description: 'Error updating album.' }) })
|
2018-01-23 20:06:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-03-17 00:53:29 +00:00
|
|
|
|
2017-10-04 00:13:38 +00:00
|
|
|
uploadsController.delete = async (req, res) => {
|
2018-01-23 20:06:30 +00:00
|
|
|
const user = await utils.authorize(req, res)
|
2018-03-28 17:40:50 +00:00
|
|
|
if (!user) { return }
|
2018-01-23 20:06:30 +00:00
|
|
|
const id = req.body.id
|
|
|
|
if (id === undefined || id === '') {
|
2018-03-24 13:52:47 +00:00
|
|
|
return res.json({ success: false, description: 'No file specified.' })
|
2018-01-23 20:06:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const file = await db.table('files')
|
|
|
|
.where('id', id)
|
|
|
|
.where(function () {
|
|
|
|
if (user.username !== 'root') {
|
|
|
|
this.where('userid', user.id)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.first()
|
|
|
|
|
|
|
|
try {
|
2018-03-30 02:39:53 +00:00
|
|
|
await utils.deleteFile(file.name).catch(error => {
|
2018-03-29 23:22:08 +00:00
|
|
|
// ENOENT is missing file, for whatever reason, then just delete from db anyways
|
|
|
|
if (error.code !== 'ENOENT') { throw error }
|
2018-03-13 14:51:39 +00:00
|
|
|
})
|
2018-03-30 02:39:53 +00:00
|
|
|
await db.table('files')
|
|
|
|
.where('id', id)
|
|
|
|
.del()
|
2018-01-23 20:06:30 +00:00
|
|
|
if (file.albumid) {
|
2018-03-30 02:39:53 +00:00
|
|
|
await db.table('albums')
|
|
|
|
.where('id', file.albumid)
|
|
|
|
.update('editedAt', Math.floor(Date.now() / 1000))
|
2018-01-23 20:06:30 +00:00
|
|
|
}
|
2018-03-29 23:22:08 +00:00
|
|
|
} catch (error) {
|
|
|
|
console.log(error)
|
2018-01-23 20:06:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return res.json({ success: true })
|
|
|
|
}
|
|
|
|
|
2018-03-29 23:22:08 +00:00
|
|
|
uploadsController.bulkDelete = async (req, res) => {
|
|
|
|
const user = await utils.authorize(req, res)
|
|
|
|
if (!user) { return }
|
|
|
|
const ids = req.body.ids
|
|
|
|
if (ids === undefined || !ids.length) {
|
|
|
|
return res.json({ success: false, description: 'No files specified.' })
|
|
|
|
}
|
|
|
|
|
2018-03-30 02:39:53 +00:00
|
|
|
const failedIds = await utils.bulkDeleteFilesByIds(ids, user)
|
|
|
|
if (failedIds.length < ids.length) {
|
|
|
|
return res.json({
|
|
|
|
success: true,
|
|
|
|
failedIds
|
2018-03-29 23:22:08 +00:00
|
|
|
})
|
2018-03-30 02:39:53 +00:00
|
|
|
}
|
2018-03-29 23:22:08 +00:00
|
|
|
|
|
|
|
return res.json({
|
2018-03-30 02:39:53 +00:00
|
|
|
success: false,
|
|
|
|
description: 'Could not delete any of the selected files.'
|
2018-01-23 20:06:30 +00:00
|
|
|
})
|
|
|
|
}
|
2017-10-04 00:13:38 +00:00
|
|
|
|
|
|
|
uploadsController.list = async (req, res) => {
|
2018-01-23 20:06:30 +00:00
|
|
|
const user = await utils.authorize(req, res)
|
2018-03-28 17:40:50 +00:00
|
|
|
if (!user) { return }
|
2018-01-23 20:06:30 +00:00
|
|
|
|
|
|
|
let offset = req.params.page
|
2018-03-28 17:40:50 +00:00
|
|
|
if (offset === undefined) { offset = 0 }
|
2018-01-23 20:06:30 +00:00
|
|
|
|
|
|
|
const files = await db.table('files')
|
|
|
|
.where(function () {
|
2018-03-28 17:40:50 +00:00
|
|
|
if (req.params.id === undefined) {
|
|
|
|
this.where('id', '<>', '')
|
|
|
|
} else {
|
|
|
|
this.where('albumid', req.params.id)
|
|
|
|
}
|
2018-01-23 20:06:30 +00:00
|
|
|
})
|
|
|
|
.where(function () {
|
2018-03-28 17:40:50 +00:00
|
|
|
if (user.username !== 'root') { this.where('userid', user.id) }
|
2018-01-23 20:06:30 +00:00
|
|
|
})
|
|
|
|
.orderBy('id', 'DESC')
|
|
|
|
.limit(25)
|
|
|
|
.offset(25 * offset)
|
2018-03-24 16:45:51 +00:00
|
|
|
.select('id', 'albumid', 'timestamp', 'name', 'userid', 'size')
|
2018-01-23 20:06:30 +00:00
|
|
|
|
|
|
|
const albums = await db.table('albums')
|
|
|
|
let basedomain = config.domain
|
|
|
|
let userids = []
|
|
|
|
|
|
|
|
for (let file of files) {
|
|
|
|
file.file = `${basedomain}/${file.name}`
|
|
|
|
file.date = new Date(file.timestamp * 1000)
|
|
|
|
file.date = utils.getPrettyDate(file.date)
|
2018-03-24 16:45:51 +00:00
|
|
|
file.size = utils.getPrettyBytes(parseInt(file.size))
|
2018-01-23 20:06:30 +00:00
|
|
|
|
|
|
|
file.album = ''
|
|
|
|
|
|
|
|
if (file.albumid !== undefined) {
|
|
|
|
for (let album of albums) {
|
|
|
|
if (file.albumid === album.id) {
|
|
|
|
file.album = album.name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only push usernames if we are root
|
|
|
|
if (user.username === 'root') {
|
|
|
|
if (file.userid !== undefined && file.userid !== null && file.userid !== '') {
|
|
|
|
userids.push(file.userid)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let ext = path.extname(file.name).toLowerCase()
|
2018-03-24 13:52:47 +00:00
|
|
|
if ((config.uploads.generateThumbnails.image && utils.imageExtensions.includes(ext)) || (config.uploads.generateThumbnails.video && utils.videoExtensions.includes(ext))) {
|
2018-01-23 20:06:30 +00:00
|
|
|
file.thumb = `${basedomain}/thumbs/${file.name.slice(0, -ext.length)}.png`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we are a normal user, send response
|
2018-03-28 17:40:50 +00:00
|
|
|
if (user.username !== 'root') { return res.json({ success: true, files }) }
|
2018-01-23 20:06:30 +00:00
|
|
|
|
|
|
|
// If we are root but there are no uploads attached to a user, send response
|
2018-03-28 17:40:50 +00:00
|
|
|
if (userids.length === 0) { return res.json({ success: true, files }) }
|
2018-01-23 20:06:30 +00:00
|
|
|
|
|
|
|
const users = await db.table('users').whereIn('id', userids)
|
|
|
|
for (let dbUser of users) {
|
|
|
|
for (let file of files) {
|
|
|
|
if (file.userid === dbUser.id) {
|
|
|
|
file.username = dbUser.username
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.json({ success: true, files })
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = uploadsController
|