uploadController.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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.params.albumid
  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. if(req.files.length === 0) return res.json({ success: false, description: 'no-files' })
  36. let files = []
  37. req.files.forEach(function(file) {
  38. files.push({
  39. name: file.filename,
  40. original: file.originalname,
  41. type: file.mimetype,
  42. size: file.size,
  43. ip: req.ip,
  44. albumid: album,
  45. timestamp: Math.floor(Date.now() / 1000)
  46. })
  47. })
  48. db.table('files').insert(files).then(() => {
  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. res.json({
  55. success: true,
  56. files: files.map(file => {
  57. return {
  58. name: file.name,
  59. size: file.size,
  60. url: 'http://' + basedomain + '/' + file.name
  61. }
  62. })
  63. })
  64. })
  65. })
  66. }
  67. uploadsController.list = function(req, res){
  68. if(req.headers.auth !== config.adminToken)
  69. return res.status(401).json({ success: false, description: 'not-authorized'})
  70. db.table('files')
  71. .where(function(){
  72. if(req.params.id === undefined)
  73. this.where('id', '<>', '')
  74. else
  75. this.where('albumid', req.params.id)
  76. })
  77. .sort('id', 'DESC')
  78. .then((files) => {
  79. db.table('albums').then((albums) => {
  80. let basedomain = req.get('host')
  81. for(let domain of config.domains)
  82. if(domain.host === req.get('host'))
  83. if(domain.hasOwnProperty('resolve'))
  84. basedomain = domain.resolve
  85. for(let file of files){
  86. file.file = 'http://' + basedomain + '/' + file.name
  87. //file.file = config.basedomain + config.uploads.prefix + file.name
  88. file.date = new Date(file.timestamp * 1000)
  89. 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()
  90. file.album = ''
  91. if(file.albumid !== undefined)
  92. for(let album of albums)
  93. if(file.albumid === album.id)
  94. file.album = album.name
  95. }
  96. return res.json({
  97. success: true,
  98. files
  99. })
  100. })
  101. })
  102. }
  103. module.exports = uploadsController