Authored by zhangxiaoru

整理

'use strict';
const request = require('request-promise');
const fs = require('fs');
const _ = require('lodash');
const utils = require('../models/utils');
const uploadImg = (req, res, next) => {
let files = req.files && req.files.file || [];
let errTip = null;
if (!_.isArray(files)) {
files = [files];
}
req.body.files = [];
files.forEach(file => {
let types = file.type.split('/');
if (!types || types[0] !== 'image') {
errTip = '上传文件格式不正确!';
}
if (file.size > 10 * 1024 * 1024) {
errTip = '上传文件尺寸太大!';
}
req.body.files.push(fs.createReadStream(file.path));
req.body.files.push(file.name);
});
if (errTip) {
return res.status(403).send({
message: errTip
});
}
request({
method: 'post',
url: 'http://upload.static.yohobuy.com',
formData: {
fileData: req.body.files,
project: req.body.bucket
},
json: true
}).then(function(result) {
if (result && result.code === 200) {
result.data = result.data || {};
result.data.images = _.map(_.get(result, 'data.imagesList'), (it) => {
return utils.getUploadImgAbsoluteUrl(it, req.body.bucket);
});
}
res.json(result);
}).catch(next);
};
module.exports = {
uploadImg
};
... ... @@ -7,20 +7,16 @@
const router = require('express').Router(); // eslint-disable-line
const cRoot = './controllers';
var multipart = require('connect-multiparty');
var multipartMiddleware = multipart();
// var multipart = require('connect-multiparty');
// var multipartMiddleware = multipart();
const uploadCtrl = require(`${cRoot}/upload`);
// const uploadCtrl = require(`${cRoot}/upload`);
const shopCtrl = require(`${cRoot}/shop`);
router.get('/', shopCtrl.index);
router.get('/apply', shopCtrl.apply);
router.get('/getTwoCategory', shopCtrl.getTwoCategoryAction);
router.post('/applysave', shopCtrl.applysave);
router.post('/upload', multipartMiddleware, uploadCtrl.uploadImg);
// router.post('/upload', multipartMiddleware, uploadCtrl.uploadImg);
module.exports = router;
... ...