You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
2.8 KiB

  1. const express = require('express')
  2. const router = express.Router()
  3. const _ = require('lodash')
  4. const multer = require('multer')
  5. const path = require('path')
  6. const sanitize = require('sanitize-filename')
  7. /* global WIKI */
  8. /**
  9. * Upload files
  10. */
  11. router.post('/u', (req, res, next) => {
  12. multer({
  13. dest: path.resolve(WIKI.ROOTPATH, WIKI.config.dataPath, 'uploads'),
  14. limits: {
  15. fileSize: WIKI.config.uploads.maxFileSize,
  16. files: WIKI.config.uploads.maxFiles
  17. }
  18. }).array('mediaUpload')(req, res, next)
  19. }, async (req, res, next) => {
  20. if (!_.some(req.user.permissions, pm => _.includes(['write:assets', 'manage:system'], pm))) {
  21. return res.status(403).json({
  22. succeeded: false,
  23. message: 'You are not authorized to upload files.'
  24. })
  25. } else if (req.files.length < 1) {
  26. return res.status(400).json({
  27. succeeded: false,
  28. message: 'Missing upload payload.'
  29. })
  30. } else if (req.files.length > 1) {
  31. return res.status(400).json({
  32. succeeded: false,
  33. message: 'You cannot upload multiple files within the same request.'
  34. })
  35. }
  36. const fileMeta = _.get(req, 'files[0]', false)
  37. if (!fileMeta) {
  38. return res.status(500).json({
  39. succeeded: false,
  40. message: 'Missing upload file metadata.'
  41. })
  42. }
  43. // Get folder Id
  44. let folderId = null
  45. try {
  46. const folderRaw = _.get(req, 'body.mediaUpload', false)
  47. if (folderRaw) {
  48. folderId = _.get(JSON.parse(folderRaw), 'folderId', null)
  49. if (folderId === 0) {
  50. folderId = null
  51. }
  52. } else {
  53. throw new Error('Missing File Metadata')
  54. }
  55. } catch (err) {
  56. return res.status(400).json({
  57. succeeded: false,
  58. message: 'Missing upload folder metadata.'
  59. })
  60. }
  61. // Build folder hierarchy
  62. let hierarchy = []
  63. if (folderId) {
  64. try {
  65. hierarchy = await WIKI.models.assetFolders.getHierarchy(folderId)
  66. } catch (err) {
  67. return res.status(400).json({
  68. succeeded: false,
  69. message: 'Failed to fetch folder hierarchy.'
  70. })
  71. }
  72. }
  73. // Sanitize filename
  74. fileMeta.originalname = sanitize(fileMeta.originalname.toLowerCase().replace(/[\s,;#]+/g, '_'))
  75. // Check if user can upload at path
  76. const assetPath = (folderId) ? hierarchy.map(h => h.slug).join('/') + `/${fileMeta.originalname}` : fileMeta.originalname
  77. if (!WIKI.auth.checkAccess(req.user, ['write:assets'], { path: assetPath })) {
  78. return res.status(403).json({
  79. succeeded: false,
  80. message: 'You are not authorized to upload files to this folder.'
  81. })
  82. }
  83. // Process upload file
  84. await WIKI.models.assets.upload({
  85. ...fileMeta,
  86. mode: 'upload',
  87. folderId: folderId,
  88. assetPath,
  89. user: req.user
  90. })
  91. res.send('ok')
  92. })
  93. router.get('/u', async (req, res, next) => {
  94. res.json({
  95. ok: true
  96. })
  97. })
  98. module.exports = router