upload.js 2.77 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
const express = require('express')
const router = express.Router()
const _ = require('lodash')
const multer = require('multer')
const path = require('path')
const sanitize = require('sanitize-filename')

/* global WIKI */

/**
 * Upload files
 */
13 14 15 16 17 18 19 20 21
router.post('/u', (req, res, next) => {
  multer({
    dest: path.resolve(WIKI.ROOTPATH, WIKI.config.dataPath, 'uploads'),
    limits: {
      fileSize: WIKI.config.uploads.maxFileSize,
      files: WIKI.config.uploads.maxFiles
    }
  }).array('mediaUpload')(req, res, next)
}, async (req, res, next) => {
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
  if (!_.some(req.user.permissions, pm => _.includes(['write:assets', 'manage:system'], pm))) {
    return res.status(403).json({
      succeeded: false,
      message: 'You are not authorized to upload files.'
    })
  } else if (req.files.length < 1) {
    return res.status(400).json({
      succeeded: false,
      message: 'Missing upload payload.'
    })
  } else if (req.files.length > 1) {
    return res.status(400).json({
      succeeded: false,
      message: 'You cannot upload multiple files within the same request.'
    })
  }
  const fileMeta = _.get(req, 'files[0]', false)
  if (!fileMeta) {
    return res.status(500).json({
      succeeded: false,
      message: 'Missing upload file metadata.'
    })
  }

Nick's avatar
Nick committed
46 47
  // Get folder Id
  let folderId = null
48 49 50
  try {
    const folderRaw = _.get(req, 'body.mediaUpload', false)
    if (folderRaw) {
Nick's avatar
Nick committed
51 52 53 54 55 56
      folderId = _.get(JSON.parse(folderRaw), 'folderId', null)
      if (folderId === 0) {
        folderId = null
      }
    } else {
      throw new Error('Missing File Metadata')
57 58 59 60 61 62 63 64
    }
  } catch (err) {
    return res.status(400).json({
      succeeded: false,
      message: 'Missing upload folder metadata.'
    })
  }

Nick's avatar
Nick committed
65 66 67 68 69 70 71 72 73 74 75 76 77
  // Build folder hierarchy
  let hierarchy = []
  if (folderId) {
    try {
      hierarchy = await WIKI.models.assetFolders.getHierarchy(folderId)
    } catch (err) {
      return res.status(400).json({
        succeeded: false,
        message: 'Failed to fetch folder hierarchy.'
      })
    }
  }

78
  // Sanitize filename
79
  fileMeta.originalname = sanitize(fileMeta.originalname.toLowerCase().replace(/[\s,;#]+/g, '_'))
80

Nick's avatar
Nick committed
81
  // Check if user can upload at path
Nick's avatar
Nick committed
82
  const assetPath = (folderId) ? hierarchy.map(h => h.slug).join('/') + `/${fileMeta.originalname}` : fileMeta.originalname
Nick's avatar
Nick committed
83
  if (!WIKI.auth.checkAccess(req.user, ['write:assets'], { path: assetPath })) {
84 85 86 87 88 89
    return res.status(403).json({
      succeeded: false,
      message: 'You are not authorized to upload files to this folder.'
    })
  }

Nick's avatar
Nick committed
90
  // Process upload file
91 92
  await WIKI.models.assets.upload({
    ...fileMeta,
93
    mode: 'upload',
Nick's avatar
Nick committed
94
    folderId: folderId,
Nick's avatar
Nick committed
95
    assetPath,
96
    user: req.user
97 98 99 100 101 102 103 104 105 106 107
  })
  res.send('ok')
})

router.get('/u', async (req, res, next) => {
  res.json({
    ok: true
  })
})

module.exports = router