|
|
@ -3,8 +3,6 @@ const request = require('superagent'); |
|
|
|
const parse = require('async-busboy'); |
|
|
|
const path = require('path') |
|
|
|
const fs = require('fs'); |
|
|
|
const OSS = require('ali-oss'); |
|
|
|
const uuid = require('uuid'); |
|
|
|
|
|
|
|
const UploadPath = { |
|
|
|
project: ['.txt', '.dwg', '.doc', '.docx', '.xls', '.xlsx', ".csv", '.pdf', '.pptx', '.png', '.jpg', '.svg', '.rar', '.zip', '.jpeg', '.mp4'], |
|
|
@ -162,78 +160,9 @@ module.exports = { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
const uploadAliOSS = async (ctx,) => { |
|
|
|
// 这个是上传到阿里
|
|
|
|
try { |
|
|
|
const { aliOss } = opts |
|
|
|
const { p = 'default' } = ctx.params; |
|
|
|
const { files } = await parse(ctx.req); |
|
|
|
const file = files[0]; |
|
|
|
const filename = file.filename || path.basename(file); |
|
|
|
|
|
|
|
const client = new OSS({ |
|
|
|
// yourRegion填写Bucket所在地域.以华东1(杭州)为例,Region填写为oss-cn-hangzhou.
|
|
|
|
region: aliOss.region, |
|
|
|
// 阿里云账号AccessKey拥有所有API的访问权限,风险很高.强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户.
|
|
|
|
accessKeyId: aliOss.accessKey, |
|
|
|
accessKeySecret: aliOss.secretKey, |
|
|
|
// 填写Bucket名称,例如examplebucket.
|
|
|
|
bucket: aliOss.bucket, |
|
|
|
}); |
|
|
|
|
|
|
|
let uploadPath = path.posix.join(p, uuid.v4(), filename); |
|
|
|
let result = await client.putStream( |
|
|
|
uploadPath, |
|
|
|
file, |
|
|
|
// { contentLength: size }
|
|
|
|
); |
|
|
|
|
|
|
|
ctx.status = 200; |
|
|
|
ctx.body = { |
|
|
|
key: result.name, |
|
|
|
uploaded: result.name, |
|
|
|
url: result.url, |
|
|
|
}; |
|
|
|
} catch (error) { |
|
|
|
ctx.status = 400; |
|
|
|
ctx.fs.logger.error(error); |
|
|
|
ctx.body = { err: 'upload error.' }; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
const downloadFromAli = async (ctx) => { |
|
|
|
try { |
|
|
|
const { aliOss } = opts |
|
|
|
const { path, filename } = ctx.query |
|
|
|
const client = new OSS({ |
|
|
|
// yourRegion填写Bucket所在地域.以华东1(杭州)为例,Region填写为oss-cn-hangzhou.
|
|
|
|
region: aliOss.region, |
|
|
|
// 阿里云账号AccessKey拥有所有API的访问权限,风险很高.强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户.
|
|
|
|
accessKeyId: aliOss.accessKey, |
|
|
|
accessKeySecret: aliOss.secretKey, |
|
|
|
// 填写Bucket名称,例如examplebucket.
|
|
|
|
bucket: aliOss.bucket, |
|
|
|
}); |
|
|
|
|
|
|
|
const filename_ = filename || path.split('/').pop() |
|
|
|
|
|
|
|
const result = await client.get(path); |
|
|
|
ctx.status = 200; |
|
|
|
ctx.set('Content-Type', 'application/x-xls'); |
|
|
|
ctx.set('Content-disposition', 'attachment; filename=' + filename_); |
|
|
|
ctx.body = result.content; |
|
|
|
} catch (error) { |
|
|
|
ctx.status = 400; |
|
|
|
ctx.fs.logger.error(error); |
|
|
|
ctx.body = { err: 'download error.' }; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
router.use(download_); |
|
|
|
router.post('/_upload/new', upload); |
|
|
|
router.delete('/_upload/cleanup', remove); |
|
|
|
router.post('/_upload/attachments/ali/:p', uploadAliOSS); |
|
|
|
router.get('/_download/attachments/ali', downloadFromAli); |
|
|
|
router.post('/_upload/attachments/:p', upload_); |
|
|
|
} |
|
|
|
}; |
|
|
|