uploadController.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. //const crypto = require('crypto')
  7. const fs = require('fs')
  8. let uploadsController = {}
  9. const storage = multer.diskStorage({
  10. destination: function (req, file, cb) {
  11. cb(null, './' + config.uploads.folder + '/')
  12. },
  13. filename: function (req, file, cb) {
  14. cb(null, randomstring.generate(config.uploads.fileLength) + path.extname(file.originalname))
  15. }
  16. })
  17. const upload = multer({
  18. storage: storage,
  19. limits: { fileSize: config.uploads.maxSize }
  20. }).array('files[]')
  21. uploadsController.upload = function(req, res, next){
  22. if(config.private === true)
  23. if(req.headers.auth !== config.clientToken)
  24. return res.status(401).json({ success: false, description: 'not-authorized'})
  25. let album = req.params.albumid
  26. if(album !== undefined)
  27. if(req.headers.adminauth !== config.adminToken)
  28. return res.status(401).json({ success: false, description: 'not-authorized'})
  29. upload(req, res, function (err) {
  30. if (err) {
  31. console.error(err)
  32. return res.json({
  33. success: false,
  34. description: err
  35. })
  36. }
  37. if(req.files.length === 0) return res.json({ success: false, description: 'no-files' })
  38. let files = []
  39. //let existingFiles = []
  40. req.files.forEach(function(file) {
  41. /*
  42. // Check if the file exists by checking hash and size
  43. let hash = crypto.createHash('md5')
  44. let stream = fs.createReadStream('./' + config.uploads.folder + '/' + file.filename)
  45. stream.on('data', function (data) {
  46. hash.update(data, 'utf8')
  47. })
  48. stream.on('end', function () {
  49. let fileHash = hash.digest('hex') // 34f7a3113803f8ed3b8fd7ce5656ebec
  50. })*/
  51. files.push({
  52. name: file.filename,
  53. original: file.originalname,
  54. type: file.mimetype,
  55. size: file.size,
  56. ip: req.ip,
  57. albumid: album,
  58. timestamp: Math.floor(Date.now() / 1000)
  59. })
  60. })
  61. db.table('files').insert(files).then(() => {
  62. let basedomain = req.get('host')
  63. for(let domain of config.domains)
  64. if(domain.host === req.get('host'))
  65. if(domain.hasOwnProperty('resolve'))
  66. basedomain = domain.resolve
  67. res.json({
  68. success: true,
  69. files: files.map(file => {
  70. return {
  71. name: file.name,
  72. size: file.size,
  73. url: 'http://' + basedomain + '/' + file.name
  74. }
  75. })
  76. })
  77. }).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
  78. })
  79. }
  80. uploadsController.delete = function(req, res){
  81. if(req.headers.auth !== config.adminToken)
  82. return res.status(401).json({ success: false, description: 'not-authorized'})
  83. let id = req.body.id
  84. if(id === undefined || id === '')
  85. return res.json({ success: false, description: 'No file specified' })
  86. db.table('files').where('id', id).then((file) => {
  87. fs.stat('./' + config.uploads.folder + '/' + file[0].name, function (err, stats) {
  88. if (err) { return res.json({ success: false, description: err.toString() }) }
  89. fs.unlink('./' + config.uploads.folder + '/' + file[0].name, function(err){
  90. if (err) { return res.json({ success: false, description: err.toString() }) }
  91. db.table('files').where('id', id).del().then(() =>{
  92. return res.json({ success: true })
  93. }).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
  94. })
  95. })
  96. }).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
  97. }
  98. uploadsController.list = function(req, res){
  99. if(req.headers.auth !== config.adminToken)
  100. return res.status(401).json({ success: false, description: 'not-authorized'})
  101. db.table('files')
  102. .where(function(){
  103. if(req.params.id === undefined)
  104. this.where('id', '<>', '')
  105. else
  106. this.where('albumid', req.params.id)
  107. })
  108. .orderBy('id', 'DESC')
  109. .then((files) => {
  110. db.table('albums').then((albums) => {
  111. let basedomain = req.get('host')
  112. for(let domain of config.domains)
  113. if(domain.host === req.get('host'))
  114. if(domain.hasOwnProperty('resolve'))
  115. basedomain = domain.resolve
  116. for(let file of files){
  117. file.file = 'http://' + basedomain + '/' + file.name
  118. file.date = new Date(file.timestamp * 1000)
  119. 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()
  120. file.album = ''
  121. if(file.albumid !== undefined)
  122. for(let album of albums)
  123. if(file.albumid === album.id)
  124. file.album = album.name
  125. }
  126. return res.json({
  127. success: true,
  128. files
  129. })
  130. })
  131. }).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
  132. }
  133. module.exports = uploadsController