upload.js 1.05 KB
'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 || [];

    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
        },
        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
};