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)
|
2017-01-21 06:49:04 +00:00
|
|
|
const crypto = require('crypto')
|
2017-01-20 06:28:26 +00:00
|
|
|
const fs = require('fs')
|
2017-01-13 07:34:21 +00:00
|
|
|
|
|
|
|
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-19 05:37:35 +00:00
|
|
|
return res.status(401).json({ success: false, description: 'not-authorized'})
|
2017-01-13 07:34:21 +00:00
|
|
|
|
2017-01-19 06:04:22 +00:00
|
|
|
let album = req.params.albumid
|
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-19 05:37:35 +00:00
|
|
|
return res.status(401).json({ success: false, description: 'not-authorized'})
|
2017-01-18 06:00:36 +00:00
|
|
|
|
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-19 06:04:22 +00:00
|
|
|
if(req.files.length === 0) return res.json({ success: false, description: 'no-files' })
|
|
|
|
|
2017-01-16 08:22:19 +00:00
|
|
|
let files = []
|
2017-01-21 06:49:04 +00:00
|
|
|
let existingFiles = []
|
|
|
|
let iteration = 1
|
2017-01-19 06:34:48 +00:00
|
|
|
|
2017-01-16 08:22:19 +00:00
|
|
|
req.files.forEach(function(file) {
|
2017-01-19 06:34:48 +00:00
|
|
|
|
|
|
|
// Check if the file exists by checking hash and size
|
|
|
|
let hash = crypto.createHash('md5')
|
|
|
|
let stream = fs.createReadStream('./' + config.uploads.folder + '/' + file.filename)
|
|
|
|
|
|
|
|
stream.on('data', function (data) {
|
|
|
|
hash.update(data, 'utf8')
|
|
|
|
})
|
|
|
|
|
|
|
|
stream.on('end', function () {
|
|
|
|
let fileHash = hash.digest('hex') // 34f7a3113803f8ed3b8fd7ce5656ebec
|
|
|
|
|
2017-01-21 06:49:04 +00:00
|
|
|
db.table('files').where({
|
|
|
|
hash: fileHash,
|
|
|
|
size: file.size
|
|
|
|
}).then((dbfile) => {
|
|
|
|
|
|
|
|
if(dbfile.length !== 0){
|
|
|
|
uploadsController.deleteFile(file.filename).then(() => {}).catch((e) => console.error(e))
|
|
|
|
existingFiles.push(dbfile[0])
|
|
|
|
}else{
|
|
|
|
files.push({
|
|
|
|
name: file.filename,
|
|
|
|
original: file.originalname,
|
|
|
|
type: file.mimetype,
|
|
|
|
size: file.size,
|
|
|
|
hash: fileHash,
|
|
|
|
ip: req.ip,
|
|
|
|
albumid: album,
|
|
|
|
timestamp: Math.floor(Date.now() / 1000)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if(iteration === req.files.length)
|
|
|
|
return something(req, res, files, existingFiles)
|
|
|
|
iteration++
|
|
|
|
})
|
2017-01-19 06:34:48 +00:00
|
|
|
|
2017-01-16 08:22:19 +00:00
|
|
|
})
|
2017-01-21 06:49:04 +00:00
|
|
|
|
2017-01-16 08:22:19 +00:00
|
|
|
})
|
|
|
|
|
2017-01-21 06:49:04 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function something(req, res, files, existingFiles){
|
|
|
|
|
|
|
|
let basedomain = req.get('host')
|
|
|
|
for(let domain of config.domains)
|
|
|
|
if(domain.host === req.get('host'))
|
|
|
|
if(domain.hasOwnProperty('resolve'))
|
|
|
|
basedomain = domain.resolve
|
|
|
|
|
|
|
|
if(files.length === 0){
|
|
|
|
return res.json({
|
|
|
|
success: true,
|
|
|
|
files: existingFiles.map(file => {
|
|
|
|
return {
|
|
|
|
name: file.name,
|
|
|
|
size: file.size,
|
|
|
|
url: 'http://' + basedomain + '/' + file.name
|
|
|
|
}
|
2017-01-13 07:34:21 +00:00
|
|
|
})
|
2017-01-21 06:49:04 +00:00
|
|
|
})
|
|
|
|
}
|
2017-01-16 08:22:19 +00:00
|
|
|
|
2017-01-21 06:49:04 +00:00
|
|
|
db.table('files').insert(files).then(() => {
|
2017-01-13 07:34:21 +00:00
|
|
|
|
2017-01-21 06:49:04 +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: 'http://' + basedomain + '/' + file.name
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
}).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
|
2017-01-13 07:34:21 +00:00
|
|
|
}
|
|
|
|
|
2017-01-20 06:28:26 +00:00
|
|
|
uploadsController.delete = function(req, res){
|
|
|
|
|
|
|
|
if(req.headers.auth !== config.adminToken)
|
|
|
|
return res.status(401).json({ success: false, description: 'not-authorized'})
|
|
|
|
|
|
|
|
let id = req.body.id
|
|
|
|
if(id === undefined || id === '')
|
|
|
|
return res.json({ success: false, description: 'No file specified' })
|
|
|
|
|
|
|
|
db.table('files').where('id', id).then((file) => {
|
|
|
|
|
|
|
|
fs.stat('./' + config.uploads.folder + '/' + file[0].name, function (err, stats) {
|
|
|
|
|
|
|
|
if (err) { return res.json({ success: false, description: err.toString() }) }
|
|
|
|
|
|
|
|
fs.unlink('./' + config.uploads.folder + '/' + file[0].name, function(err){
|
|
|
|
if (err) { return res.json({ success: false, description: err.toString() }) }
|
|
|
|
|
|
|
|
db.table('files').where('id', id).del().then(() =>{
|
|
|
|
return res.json({ success: true })
|
|
|
|
}).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-01-21 06:49:04 +00:00
|
|
|
uploadsController.deleteFile = function(file){
|
|
|
|
|
|
|
|
return new Promise(function(resolve, reject){
|
|
|
|
fs.stat('./' + config.uploads.folder + '/' + file, function (err, stats) {
|
|
|
|
if (err) { return reject(err) }
|
|
|
|
fs.unlink('./' + config.uploads.folder + '/' + file, function(err){
|
|
|
|
if (err) { return reject(err) }
|
|
|
|
return resolve()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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)
|
2017-01-19 05:37:35 +00:00
|
|
|
return res.status(401).json({ success: false, description: 'not-authorized'})
|
2017-01-16 07:21:46 +00:00
|
|
|
|
2017-01-18 07:05:56 +00:00
|
|
|
db.table('files')
|
|
|
|
.where(function(){
|
2017-01-19 05:37:35 +00:00
|
|
|
if(req.params.id === undefined)
|
2017-01-18 07:05:56 +00:00
|
|
|
this.where('id', '<>', '')
|
|
|
|
else
|
2017-01-19 05:37:35 +00:00
|
|
|
this.where('albumid', req.params.id)
|
2017-01-18 07:05:56 +00:00
|
|
|
})
|
2017-01-19 06:34:48 +00:00
|
|
|
.orderBy('id', 'DESC')
|
2017-01-18 07:05:56 +00:00
|
|
|
.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-19 03:31:01 +00:00
|
|
|
let basedomain = req.get('host')
|
|
|
|
for(let domain of config.domains)
|
|
|
|
if(domain.host === req.get('host'))
|
|
|
|
if(domain.hasOwnProperty('resolve'))
|
|
|
|
basedomain = domain.resolve
|
|
|
|
|
2017-01-18 06:29:46 +00:00
|
|
|
for(let file of files){
|
2017-01-19 03:31:01 +00:00
|
|
|
file.file = 'http://' + basedomain + '/' + file.name
|
2017-01-18 06:29:46 +00:00
|
|
|
file.date = new Date(file.timestamp * 1000)
|
2017-01-19 03:31:01 +00:00
|
|
|
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-18 06:29:46 +00:00
|
|
|
|
|
|
|
file.album = ''
|
|
|
|
|
|
|
|
if(file.albumid !== undefined)
|
|
|
|
for(let album of albums)
|
|
|
|
if(file.albumid === album.id)
|
|
|
|
file.album = album.name
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-01-19 05:37:35 +00:00
|
|
|
return res.json({
|
|
|
|
success: true,
|
|
|
|
files
|
|
|
|
})
|
2017-01-18 06:29:46 +00:00
|
|
|
})
|
2017-01-16 07:21:46 +00:00
|
|
|
|
2017-01-20 06:28:26 +00:00
|
|
|
}).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
|
2017-01-16 07:21:46 +00:00
|
|
|
}
|
|
|
|
|
2017-01-13 07:34:21 +00:00
|
|
|
module.exports = uploadsController
|