Blame view

apps/api/controllers/upload.js 859 Bytes
陈轩 authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
/**
 * 上传接口
 * @author: kai.bi<kai.bi@yoho.cn>
 * @date: 2016/07/22
 */

'use strict';

const request = require('request-promise');
const fs = require('fs');
const _ = require('lodash');

const uploadImg = (req, res, next) => {
    let files = req.files && req.files.filename || [];

    if (!_.isArray(files)) {
        files = [files];
    }

    req.body.files = [];
    files.forEach(file => {
        req.body.files.push(fs.createReadStream(file.path));
        req.body.files.push(file.name);
    });

    request({
        method: 'post',
        url: 'http://upload.static.yohobuy.com',
        formData: {
            fileData: req.body.files,
            project: req.body.bucket || 'goodsimg'
        },
        json: true
    }).then(function(result) {
        res.json(result);
    }).catch(next);
};

module.exports = {
    uploadImg
};