filesafe/controllers/uploadController.js

300 lines
9.2 KiB
JavaScript
Raw Normal View History

2017-01-13 07:34:21 +00:00
const config = require('../config.js')
2017-03-18 04:36:50 +00:00
const path = require('path')
2017-03-17 04:14:24 +00:00
const multer = require('multer')
2017-01-13 07:34:21 +00:00
const randomstring = require('randomstring')
const db = require('knex')(config.database)
const crypto = require('crypto')
const fs = require('fs')
2017-03-17 04:19:33 +00:00
const utils = require('./utilsController.js')
2017-01-13 07:34:21 +00:00
let uploadsController = {}
const storage = multer.diskStorage({
2017-03-17 04:14:24 +00:00
destination: function(req, file, cb) {
cb(null, path.join(__dirname, '..', config.uploads.folder))
2017-01-13 07:34:21 +00:00
},
2017-03-17 04:14:24 +00:00
filename: function(req, file, cb) {
2017-01-14 08:50:18 +00:00
cb(null, randomstring.generate(config.uploads.fileLength) + path.extname(file.originalname))
2017-01-13 07:34:21 +00:00
}
})
const upload = multer({
storage: storage,
limits: { fileSize: config.uploads.maxSize },
fileFilter: function(req, file, cb) {
if (config.blockedExtensions.some((extension) => { return path.extname(file.originalname) === extension; })) {
return cb('This file extension is not allowed');
}
return cb(null, true);
}
}).array('files[]')
2017-01-13 07:34:21 +00:00
2017-03-17 04:14:24 +00:00
uploadsController.upload = function(req, res, next) {
2017-01-13 07:34:21 +00:00
2017-01-30 01:51:54 +00:00
// Get the token
let token = req.headers.token
// If we're running in private and there's no token, error
2017-03-17 04:14:24 +00:00
if (config.private === true)
if (token === undefined) return res.status(401).json({ success: false, description: 'No token provided' })
2017-01-30 01:51:54 +00:00
2017-01-30 08:10:39 +00:00
// If there is no token then just leave it blank so the query fails
2017-03-17 04:14:24 +00:00
if (token === undefined) token = ''
2017-01-30 01:51:54 +00:00
db.table('users').where('token', token).then((user) => {
let userid
2017-03-17 04:14:24 +00:00
if (user.length > 0) userid = user[0].id
2017-01-30 01:51:54 +00:00
// Check if user is trying to upload to an album
2017-03-17 04:14:24 +00:00
let album
if (userid !== undefined) {
2017-01-30 01:51:54 +00:00
album = req.headers.albumid
2017-03-17 04:14:24 +00:00
if (album === undefined)
2017-01-30 01:51:54 +00:00
album = req.params.albumid
2017-01-13 07:34:21 +00:00
}
2017-01-30 01:51:54 +00:00
upload(req, res, function (err) {
if (err) {
console.error(err)
return res.json({
success: false,
description: err
})
}
2017-01-19 06:04:22 +00:00
2017-03-17 04:14:24 +00:00
if (req.files.length === 0) return res.json({ success: false, description: 'no-files' })
2017-01-19 06:34:48 +00:00
2017-01-30 01:51:54 +00:00
let files = []
let existingFiles = []
let iteration = 1
2017-01-19 06:34:48 +00:00
2017-01-30 01:51:54 +00:00
req.files.forEach(function(file) {
2017-01-19 06:34:48 +00:00
2017-01-30 01:51:54 +00:00
// Check if the file exists by checking hash and size
let hash = crypto.createHash('md5')
2017-03-17 04:14:24 +00:00
let stream = fs.createReadStream(path.join(__dirname, '..', config.uploads.folder, file.filename))
2017-01-19 06:34:48 +00:00
2017-01-30 01:51:54 +00:00
stream.on('data', function (data) {
hash.update(data, 'utf8')
})
2017-01-19 06:34:48 +00:00
2017-01-30 01:51:54 +00:00
stream.on('end', function () {
2017-03-17 04:14:24 +00:00
let fileHash = hash.digest('hex')
2017-01-30 01:51:54 +00:00
db.table('files')
2017-03-17 04:14:24 +00:00
.where(function() {
if (userid === undefined)
this.whereNull('userid')
else
this.where('userid', userid)
})
.where({
2017-01-30 01:51:54 +00:00
hash: fileHash,
size: file.size
}).then((dbfile) => {
2017-03-17 04:14:24 +00:00
if (dbfile.length !== 0) {
2017-01-30 01:51:54 +00:00
uploadsController.deleteFile(file.filename).then(() => {}).catch((e) => console.error(e))
existingFiles.push(dbfile[0])
2017-03-17 04:14:24 +00:00
} else {
2017-01-30 01:51:54 +00:00
files.push({
name: file.filename,
original: file.originalname,
type: file.mimetype,
size: file.size,
hash: fileHash,
ip: req.ip,
albumid: album,
userid: userid,
timestamp: Math.floor(Date.now() / 1000)
})
}
2017-03-17 04:14:24 +00:00
if (iteration === req.files.length)
2017-01-30 01:51:54 +00:00
return uploadsController.processFilesForDisplay(req, res, files, existingFiles)
iteration++
2017-03-17 04:14:24 +00:00
}).catch(function(error) { console.log(error); res.json({ success: false, description: 'error' }) })
2017-01-30 01:51:54 +00:00
})
})
})
2017-03-17 04:14:24 +00:00
}).catch(function(error) { console.log(error); res.json({ success: false, description: 'error' }) })
}
2017-03-17 04:14:24 +00:00
uploadsController.processFilesForDisplay = function(req, res, files, existingFiles) {
let basedomain = req.get('host')
2017-03-17 04:14:24 +00:00
for (let domain of config.domains)
if (domain.host === req.get('host'))
if (domain.hasOwnProperty('resolve'))
basedomain = domain.resolve
2017-01-21 20:24:20 +00:00
2017-03-17 04:14:24 +00:00
if (files.length === 0) {
return res.json({
success: true,
files: existingFiles.map(file => {
return {
name: file.name,
size: file.size,
url: basedomain + '/' + file.name
}
2017-01-13 07:34:21 +00:00
})
})
}
db.table('files').insert(files).then(() => {
2017-01-13 07:34:21 +00:00
2017-03-17 04:14:24 +00:00
for (let efile of existingFiles) files.push(efile)
res.json({
success: true,
files: files.map(file => {
return {
name: file.name,
size: file.size,
url: basedomain + '/' + file.name
}
})
})
2017-03-17 04:14:24 +00:00
for (let file of files) {
2017-03-18 04:36:50 +00:00
let ext = path.extname(file.name).toLowerCase()
if (utils.extensions.includes(ext)) {
file.thumb = basedomain + '/thumbs/' + file.name.slice(0, -ext.length) + '.png'
utils.generateThumbs(file)
}
}
2017-03-17 04:14:24 +00:00
}).catch(function(error) { console.log(error); res.json({ success: false, description: 'error' }) })
2017-01-13 07:34:21 +00:00
}
2017-03-17 04:14:24 +00:00
uploadsController.delete = function(req, res) {
2017-01-30 01:51:54 +00:00
let token = req.headers.token
2017-03-17 04:14:24 +00:00
if (token === undefined) return res.status(401).json({ success: false, description: 'No token provided' })
let id = req.body.id
2017-03-17 04:14:24 +00:00
if (id === undefined || id === '')
return res.json({ success: false, description: 'No file specified' })
2017-01-30 01:51:54 +00:00
db.table('users').where('token', token).then((user) => {
2017-03-17 04:14:24 +00:00
if (user.length === 0) return res.status(401).json({ success: false, description: 'Invalid token' })
2017-01-30 01:51:54 +00:00
db.table('files')
.where('id', id)
2017-03-17 04:14:24 +00:00
.where(function() {
if (user[0].username !== 'root')
2017-01-30 07:42:15 +00:00
this.where('userid', user[0].id)
})
2017-01-30 01:51:54 +00:00
.then((file) => {
uploadsController.deleteFile(file[0].name).then(() => {
2017-03-17 04:14:24 +00:00
db.table('files').where('id', id).del().then(() => {
2017-01-30 01:51:54 +00:00
return res.json({ success: true })
2017-03-17 04:14:24 +00:00
}).catch(function(error) { console.log(error); res.json({ success: false, description: 'error' }) })
2017-01-30 01:51:54 +00:00
}).catch((e) => {
console.log(e.toString())
2017-03-17 04:14:24 +00:00
db.table('files').where('id', id).del().then(() => {
2017-01-30 01:51:54 +00:00
return res.json({ success: true })
2017-03-17 04:14:24 +00:00
}).catch(function(error) { console.log(error); res.json({ success: false, description: 'error' }) })
2017-01-30 01:51:54 +00:00
})
2017-03-17 04:14:24 +00:00
}).catch(function(error) { console.log(error); res.json({ success: false, description: 'error' }) })
}).catch(function(error) { console.log(error); res.json({ success: false, description: 'error' }) })
}
2017-03-17 04:14:24 +00:00
uploadsController.deleteFile = function(file) {
2017-03-17 04:14:24 +00:00
return new Promise(function(resolve, reject) {
fs.stat(path.join(__dirname, '..', config.uploads.folder, file), function(err, stats) {
if (err) { return reject(err) }
2017-03-17 04:14:24 +00:00
fs.unlink(path.join(__dirname, '..', config.uploads.folder, file), function(err) {
if (err) { return reject(err) }
return resolve()
})
})
})
}
2017-03-17 04:14:24 +00:00
uploadsController.list = function(req, res) {
2017-01-30 01:51:54 +00:00
let token = req.headers.token
2017-03-17 04:14:24 +00:00
if (token === undefined) return res.status(401).json({ success: false, description: 'No token provided' })
2017-01-30 01:51:54 +00:00
db.table('users').where('token', token).then((user) => {
2017-03-17 04:14:24 +00:00
if (user.length === 0) return res.status(401).json({ success: false, description: 'Invalid token'})
2017-01-21 08:17:29 +00:00
2017-01-30 01:51:54 +00:00
let offset = req.params.page
2017-03-17 04:14:24 +00:00
if (offset === undefined) offset = 0
2017-01-22 21:01:39 +00:00
2017-01-30 01:51:54 +00:00
db.table('files')
2017-03-17 04:14:24 +00:00
.where(function() {
if (req.params.id === undefined)
2017-01-30 01:51:54 +00:00
this.where('id', '<>', '')
else
this.where('albumid', req.params.id)
})
2017-03-17 04:14:24 +00:00
.where(function() {
if (user[0].username !== 'root')
2017-01-30 07:42:15 +00:00
this.where('userid', user[0].id)
2017-01-30 01:51:54 +00:00
})
.orderBy('id', 'DESC')
.limit(25)
.offset(25 * offset)
.select('id', 'albumid', 'timestamp', 'name', 'userid')
2017-01-30 01:51:54 +00:00
.then((files) => {
db.table('albums').then((albums) => {
let basedomain = req.get('host')
2017-03-17 04:14:24 +00:00
for (let domain of config.domains)
if (domain.host === req.get('host'))
if (domain.hasOwnProperty('resolve'))
2017-01-30 01:51:54 +00:00
basedomain = domain.resolve
let userids = []
2017-03-17 04:14:24 +00:00
for (let file of files) {
2017-01-30 01:51:54 +00:00
file.file = basedomain + '/' + file.name
file.date = new Date(file.timestamp * 1000)
2017-03-17 04:14:24 +00:00
file.date = utils.getPrettyDate(file.date) // file.date.getFullYear() + '-' + (file.date.getMonth() + 1) + '-' + file.date.getDate() + ' ' + (file.date.getHours() < 10 ? '0' : '') + file.date.getHours() + ':' + (file.date.getMinutes() < 10 ? '0' : '') + file.date.getMinutes() + ':' + (file.date.getSeconds() < 10 ? '0' : '') + file.date.getSeconds()
2017-01-30 01:51:54 +00:00
file.album = ''
2017-03-17 04:14:24 +00:00
if (file.albumid !== undefined)
for (let album of albums)
if (file.albumid === album.id)
2017-01-30 01:51:54 +00:00
file.album = album.name
// Only push usernames if we are root
2017-03-17 04:14:24 +00:00
if (user[0].username === 'root')
if (file.userid !== undefined && file.userid !== null && file.userid !== '')
userids.push(file.userid)
2017-03-18 04:36:50 +00:00
let ext = path.extname(file.name).toLowerCase()
if (utils.extensions.includes(ext)) {
file.thumb = basedomain + '/thumbs/' + file.name.slice(0, -ext.length) + '.png'
utils.generateThumbs(file)
}
2017-01-22 21:01:39 +00:00
}
// If we are a normal user, send response
2017-03-17 04:14:24 +00:00
if (user[0].username !== 'root') return res.json({ success: true, files })
// If we are root but there are no uploads attached to a user, send response
2017-03-17 04:14:24 +00:00
if (userids.length === 0) return res.json({ success: true, files })
db.table('users').whereIn('id', userids).then((users) => {
2017-03-17 04:14:24 +00:00
for (let user of users)
for (let file of files)
if (file.userid === user.id)
file.username = user.username
return res.json({ success: true, files })
2017-03-17 04:14:24 +00:00
}).catch(function(error) { console.log(error); res.json({ success: false, description: 'error' }) })
}).catch(function(error) { console.log(error); res.json({ success: false, description: 'error' }) })
}).catch(function(error) { console.log(error); res.json({ success: false, description: 'error' }) })
})
}
2017-02-07 00:15:39 +00:00
module.exports = uploadsController