uploadController.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. const path = require('path')
  2. const config = require('../config.js')
  3. const multer = require('multer')
  4. const randomstring = require('randomstring')
  5. const db = require('knex')(config.database)
  6. let uploadsController = {}
  7. const storage = multer.diskStorage({
  8. destination: function (req, file, cb) {
  9. cb(null, './' + config.uploads.folder + '/')
  10. },
  11. filename: function (req, file, cb) {
  12. cb(null, randomstring.generate(config.uploads.fileLength) + path.extname(file.originalname))
  13. }
  14. })
  15. const upload = multer({
  16. storage: storage,
  17. limits: { fileSize: config.uploads.maxSize }
  18. }).array('files[]')
  19. uploadsController.upload = function(req, res, next){
  20. if(config.private === true)
  21. if(req.headers.auth !== config.clientToken)
  22. return res.status(401).send('not-authorized')
  23. let album = req.headers.album
  24. if(album !== undefined)
  25. if(req.headers.adminauth !== config.adminToken)
  26. return res.status(401).send('not-authorized')
  27. upload(req, res, function (err) {
  28. if (err) {
  29. console.error(err)
  30. return res.json({
  31. success: false,
  32. description: err
  33. })
  34. }
  35. let files = []
  36. req.files.forEach(function(file) {
  37. files.push({
  38. name: file.filename,
  39. original: file.originalname,
  40. type: file.mimetype,
  41. size: file.size,
  42. ip: req.ip,
  43. albumid: album,
  44. timestamp: Math.floor(Date.now() / 1000)
  45. })
  46. })
  47. db.table('files').insert(files).then(() => {
  48. console.log('basedomain: ' + req.get('host'))
  49. let basedomain = req.get('host')
  50. for(let domain of config.domains)
  51. if(domain.host === req.get('host'))
  52. if(domain.hasOwnProperty('resolve'))
  53. basedomain = domain.resolve
  54. console.log('basedomain: ' + basedomain)
  55. res.json({
  56. success: true,
  57. files: files.map(file => {
  58. return {
  59. name: file.name,
  60. size: file.size,
  61. url: 'http://' + basedomain + '/' + file.name
  62. }
  63. })
  64. })
  65. })
  66. })
  67. }
  68. uploadsController.list = function(req, res){
  69. if(req.headers.auth !== config.adminToken)
  70. return res.status(401).send('not-authorized')
  71. db.table('files')
  72. .where(function(){
  73. if(req.headers.albumid === undefined)
  74. this.where('id', '<>', '')
  75. else
  76. this.where('albumid', req.headers.albumid)
  77. })
  78. .then((files) => {
  79. db.table('albums').then((albums) => {
  80. for(let file of files){
  81. file.file = config.basedomain + config.uploads.prefix + file.name
  82. file.date = new Date(file.timestamp * 1000)
  83. 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()
  84. file.album = ''
  85. if(file.albumid !== undefined)
  86. for(let album of albums)
  87. if(file.albumid === album.id)
  88. file.album = album.name
  89. }
  90. return res.json(files)
  91. })
  92. })
  93. }
  94. module.exports = uploadsController