2017-01-13 07:34:21 +00:00
|
|
|
const path = require('path')
|
|
|
|
const config = require('../config.js')
|
|
|
|
const multer = require('multer')
|
|
|
|
const randomstring = require('randomstring')
|
|
|
|
const db = require('knex')(config.database)
|
|
|
|
|
|
|
|
let uploadsController = {}
|
|
|
|
|
|
|
|
const storage = multer.diskStorage({
|
|
|
|
destination: function (req, file, cb) {
|
|
|
|
cb(null, './' + config.uploads.folder + '/')
|
|
|
|
},
|
|
|
|
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,
|
2017-01-18 23:38:13 +00:00
|
|
|
limits: { fileSize: config.uploads.maxSize }
|
2017-01-16 08:22:19 +00:00
|
|
|
}).array('files[]')
|
2017-01-13 07:34:21 +00:00
|
|
|
|
|
|
|
uploadsController.upload = function(req, res, next){
|
|
|
|
|
2017-01-17 03:37:54 +00:00
|
|
|
if(config.private === true)
|
2017-01-16 07:45:29 +00:00
|
|
|
if(req.headers.auth !== config.clientToken)
|
2017-01-14 21:13:58 +00:00
|
|
|
return res.status(401).send('not-authorized')
|
2017-01-13 07:34:21 +00:00
|
|
|
|
2017-01-17 22:05:00 +00:00
|
|
|
let album = req.headers.album
|
2017-01-13 07:34:21 +00:00
|
|
|
|
2017-01-18 06:00:36 +00:00
|
|
|
if(album !== undefined)
|
2017-01-18 06:40:16 +00:00
|
|
|
if(req.headers.adminauth !== config.adminToken)
|
2017-01-18 06:00:36 +00:00
|
|
|
return res.status(401).send('not-authorized')
|
|
|
|
|
2017-01-13 07:34:21 +00:00
|
|
|
upload(req, res, function (err) {
|
|
|
|
if (err) {
|
|
|
|
console.error(err)
|
2017-01-16 07:21:46 +00:00
|
|
|
return res.json({
|
|
|
|
success: false,
|
|
|
|
description: err
|
|
|
|
})
|
2017-01-13 07:34:21 +00:00
|
|
|
}
|
|
|
|
|
2017-01-16 08:22:19 +00:00
|
|
|
let files = []
|
|
|
|
req.files.forEach(function(file) {
|
|
|
|
files.push({
|
|
|
|
name: file.filename,
|
|
|
|
original: file.originalname,
|
|
|
|
type: file.mimetype,
|
|
|
|
size: file.size,
|
|
|
|
ip: req.ip,
|
2017-01-17 22:05:00 +00:00
|
|
|
albumid: album,
|
2017-01-18 05:40:14 +00:00
|
|
|
timestamp: Math.floor(Date.now() / 1000)
|
2017-01-16 08:22:19 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
db.table('files').insert(files).then(() => {
|
|
|
|
|
|
|
|
res.json({
|
2017-01-16 07:21:46 +00:00
|
|
|
success: true,
|
2017-01-16 08:22:19 +00:00
|
|
|
files: files.map(file => {
|
|
|
|
return {
|
|
|
|
name: file.name,
|
|
|
|
size: file.size,
|
|
|
|
url: config.basedomain + file.name
|
2017-01-16 07:21:46 +00:00
|
|
|
}
|
2017-01-16 08:22:19 +00:00
|
|
|
})
|
2017-01-13 07:34:21 +00:00
|
|
|
})
|
2017-01-16 08:22:19 +00:00
|
|
|
|
2017-01-13 07:34:21 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-01-16 07:21:46 +00:00
|
|
|
uploadsController.list = function(req, res){
|
|
|
|
|
2017-01-17 03:37:54 +00:00
|
|
|
if(req.headers.auth !== config.adminToken)
|
|
|
|
return res.status(401).send('not-authorized')
|
2017-01-16 07:21:46 +00:00
|
|
|
|
2017-01-18 07:05:56 +00:00
|
|
|
db.table('files')
|
|
|
|
.where(function(){
|
|
|
|
if(req.headers.albumid === undefined)
|
|
|
|
this.where('id', '<>', '')
|
|
|
|
else
|
|
|
|
this.where('albumid', req.headers.albumid)
|
|
|
|
})
|
|
|
|
.then((files) => {
|
2017-01-18 06:29:46 +00:00
|
|
|
db.table('albums').then((albums) => {
|
2017-01-16 07:21:46 +00:00
|
|
|
|
2017-01-18 06:29:46 +00:00
|
|
|
for(let file of files){
|
|
|
|
file.file = config.basedomain + config.uploads.prefix + file.name
|
|
|
|
file.date = new Date(file.timestamp * 1000)
|
|
|
|
file.date = file.date.getFullYear() + '-' + file.date.getMonth() + '-' + 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()
|
|
|
|
|
|
|
|
file.album = ''
|
|
|
|
|
|
|
|
if(file.albumid !== undefined)
|
|
|
|
for(let album of albums)
|
|
|
|
if(file.albumid === album.id)
|
|
|
|
file.album = album.name
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.json(files)
|
|
|
|
})
|
2017-01-16 07:21:46 +00:00
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-01-13 07:34:21 +00:00
|
|
|
module.exports = uploadsController
|