const {static_prefix} = require('../config/sys')
const Router = require('koa-router');
// const qiniu = require('qiniu')
const path = require('path');
const router = new Router();
const permission = require('../intercept/permission');

const fileLocal = path.join(__dirname, '../static/images');
const fileRemote = static_prefix + '/images/';
const uploadFile = require('../util/upload')(fileLocal)

// const accessKey = 'dlPPwgZky_F-iP8CbSbJpiAtAcqw3BYwb9rdHMrS'
// const secretKey = 'YEtkLKDsImXB-8m1CT1zV_YwCwwGvrUvo2ktj9KZ'
// const scope = 'cgaii'
// const expires = 2 * 60 * 60 * 1000
// const mac = new qiniu.auth.digest.Mac(accessKey, secretKey)
// const putPolicy = new qiniu.rs.PutPolicy({ scope, expires});
// let token

// (function updateToken () {
//   token = putPolicy.uploadToken(mac);
//   console.log(token)
//   setTimeout(updateToken, expires - 60 * 1000)
// })()


/**
 * 文件上传
 */
router.post('/upload', permission, async ctx => {
  let {file} = await uploadFile(ctx);
  ctx.body = {
    msg: '文件上传成功',
    content: fileRemote + file
  }
});

router.post('/uploads', permission, async ctx => {
  let {file} = await uploadFile(ctx);
  ctx.body = {
    errno: 0,
    data: [ fileRemote + file ]
  }
});

// router.get('/qiniuToken', permission, async ctx => {
//   ctx.body = {
//     msg: '获取token成功',
//     content: token
//   }
// })

module.exports = exports = { path: '/system', router };