2018-03-18 01:14:50 +00:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const Router = require('express').Router
|
|
|
|
const formidable = require('formidable')
|
|
|
|
|
|
|
|
const config = require('../../config')
|
|
|
|
const logger = require('../../logger')
|
|
|
|
const response = require('../../response')
|
|
|
|
|
|
|
|
const imageRouter = module.exports = Router()
|
|
|
|
|
|
|
|
// upload image
|
|
|
|
imageRouter.post('/uploadimage', function (req, res) {
|
|
|
|
var form = new formidable.IncomingForm()
|
|
|
|
|
|
|
|
form.keepExtensions = true
|
|
|
|
|
2018-03-07 14:17:35 +00:00
|
|
|
if (config.imageUploadType === 'filesystem') {
|
2018-06-23 19:47:22 +00:00
|
|
|
form.uploadDir = config.uploadsPath
|
2018-03-18 01:14:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
form.parse(req, function (err, fields, files) {
|
|
|
|
if (err || !files.image || !files.image.path) {
|
2019-06-08 18:51:24 +00:00
|
|
|
logger.error(`formidable error: ${err}`)
|
2018-03-18 01:14:50 +00:00
|
|
|
response.errorForbidden(res)
|
|
|
|
} else {
|
2019-06-08 18:51:24 +00:00
|
|
|
logger.debug(`SERVER received uploadimage: ${JSON.stringify(files.image)}`)
|
2018-03-18 01:14:50 +00:00
|
|
|
|
2018-03-07 14:17:35 +00:00
|
|
|
const uploadProvider = require('./' + config.imageUploadType)
|
2019-06-08 18:51:24 +00:00
|
|
|
logger.debug(`imageRouter: Uploading ${files.image.path} using ${config.imageUploadType}`)
|
2018-03-18 01:14:50 +00:00
|
|
|
uploadProvider.uploadImage(files.image.path, function (err, url) {
|
|
|
|
if (err !== null) {
|
|
|
|
logger.error(err)
|
|
|
|
return res.status(500).end('upload image error')
|
|
|
|
}
|
2019-06-08 18:51:24 +00:00
|
|
|
logger.debug(`SERVER sending ${url} to client`)
|
2018-03-18 01:14:50 +00:00
|
|
|
res.send({
|
|
|
|
link: url
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|