file-controller.js 3.36 KB
/**
 * 文件相关controller
 * @author: feng.chen<feng.chen@yoho.cn>
 * @date: 2017/04/13
 */
'use strict';

const Context = require('../framework/context');
const FileService = require('../service/file-service');
const XlsxService = require('../service/xlsx-service');
const Api = require('../common/api');
const apiDomain = require('../common/api-domain');
const fs = require('fs');
const _ = require('lodash');

class FileController extends Context {
    constructor() {
        super();
        this.api = this.instance(Api);
        this.fileService = this.instance(FileService);
        this.xlsxService = this.instance(XlsxService);
    }
    uploadImage(req, res, next) {
        let files = req.files && req.files.file || [];
        let errTip = null;

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

        req.body.files = [];
        _.each(files, 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
            });
        }


        if (req.body.bucket === 'goodsimg') {
            this.api.upload('http://upload.static.yohobuy.com', {
                fileData: req.body.files,
                project: req.body.bucket
            }).then(result => {
                if (result.code === 200 && _.get(result, 'data.imagesList.length', 0)) {
                    result.data.imagesList = _.map(result.data.imagesList, imgUrl => {
                        return this.fileService.getAbsoluteUrl(imgUrl, req.body.bucket);
                    });
                }
                res.json(result);
            }).catch(next);
        } else {
            this.api.upload(apiDomain.platform.uploads, {
                files: req.body.files,
                bucket: req.body.bucket
            }).then(result => {
                let newResult = {
                    code: result.code,
                    data: {},
                    message: result.message
                };

                if (result.code === 200) {
                    newResult.data.imagesList = [result.data[0]];
                }
                res.json(newResult);
            }).catch(next);
        }
    }
    uploadXlsx(req, res, next) {
        let file = req.files && req.files.file;

        return this.xlsxService.handle(file.path).then((result) => {
            if (result.code === 500) {
                return res.json(result);
            }

            req.body = result.body;
            req.url = result.path;

            next();
        }).catch(next);
    }

    importSeller(req, res, next) {
        let file = req.files && req.files.file;

        this.api.upload(apiDomain.platform.importSeller, {
            brandId: req.body.brandId,
            shopsId: req.cookies._sign,
            userId: req.session.LOGIN_UID,
            file: fs.createReadStream(file.path),
            type: 'sellerProductImport'
        }).then((result) => {
            res.json(result);
        }).catch(next);
    }
}

module.exports = FileController;