qiniu.js 969 Bytes
/**
 * 七牛上传
 * @author: chenfeng <feng.chen@yoho.cn>
 * @date: 29/06/2017
 */
const qiniu = require('qiniu');
const qiniuConfig = global.yoho.config.qiniu;
const qiniuYohoCdnConfig = global.yoho.config.qiniuYohoCdn;
const request = require('request');
const { config } = global.yoho;

function getToken(req, res) {
  let conf = qiniuConfig;
  let type = req.query.type || '';

  if (type.toLowerCase() === 'yohocdn') {
    conf = qiniuYohoCdnConfig;
  }

  let mac = new qiniu.auth.digest.Mac(conf.ACCESS_KEY, conf.SECRET_KEY);
  let options = {
    scope: conf.BUCKET_NAME
  };
  let putPolicy = new qiniu.rs.PutPolicy(options);
  let uploadToken = putPolicy.uploadToken(mac);

  res.json({
    code: 200,
    uptoken: uploadToken
  });
}

function upload(req, res, next) {
  let localReq = request(config.domains.api);

  req.pipe(localReq).pipe(res);

  localReq.on('error', function(err) {
    next(err);
  });
}

module.exports = {
  getToken,
  upload
};