uploadController.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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).json({ success: false, description: 'not-authorized'})
  23. let album = req.body.album
  24. if(album !== undefined)
  25. if(req.headers.adminauth !== config.adminToken)
  26. return res.status(401).json({ success: false, description: '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. let basedomain = req.get('host')
  49. for(let domain of config.domains)
  50. if(domain.host === req.get('host'))
  51. if(domain.hasOwnProperty('resolve'))
  52. basedomain = domain.resolve
  53. res.json({
  54. success: true,
  55. files: files.map(file => {
  56. return {
  57. name: file.name,
  58. size: file.size,
  59. url: 'http://' + basedomain + '/' + file.name
  60. }
  61. })
  62. })
  63. })
  64. })
  65. }
  66. uploadsController.list = function(req, res){
  67. if(req.headers.auth !== config.adminToken)
  68. return res.status(401).json({ success: false, description: 'not-authorized'})
  69. db.table('files')
  70. .where(function(){
  71. if(req.params.id === undefined)
  72. this.where('id', '<>', '')
  73. else
  74. this.where('albumid', req.params.id)
  75. })
  76. .then((files) => {
  77. db.table('albums').then((albums) => {
  78. let basedomain = req.get('host')
  79. for(let domain of config.domains)
  80. if(domain.host === req.get('host'))
  81. if(domain.hasOwnProperty('resolve'))
  82. basedomain = domain.resolve
  83. for(let file of files){
  84. file.file = 'http://' + basedomain + '/' + file.name
  85. //file.file = config.basedomain + config.uploads.prefix + file.name
  86. file.date = new Date(file.timestamp * 1000)
  87. 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()
  88. file.album = ''
  89. if(file.albumid !== undefined)
  90. for(let album of albums)
  91. if(file.albumid === album.id)
  92. file.album = album.name
  93. }
  94. return res.json({
  95. success: true,
  96. files
  97. })
  98. })
  99. })
  100. }
  101. module.exports = uploadsController