You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

129 lines
4.9 KiB

/**
* Created by PengLing on 2018/10/23.
*/
'use strict';
const request = require('superagent');
const parse = require('async-busboy');
const path = require('path');
const UploadPath = {
project: [".txt", ".dwg", ".doc", ".docx", ".xls", ".xlsx", ".pdf", ".png", ".jpg"],
report: [".doc", ".docx", ".xls", ".xlsx", ".pdf"],
data: [".txt", ".xls", ".xlsx"],
image: [".png", ".jpg", ".svg"],
three: [".js"],
video: [".mp4"]
};
module.exports = {
entry: function (app, router, opts) {
let download_ = async function (ctx, next) {
const { fetchUrl } = opts.qiniu;
if (ctx.path && ctx.path.includes(fetchUrl)) {
try {
const { filename } = ctx.request.query;
const fkey = decodeURI(ctx.path.slice(fetchUrl.length + 1)).replace(/\.json$/, '.js');
if (ctx.path) {
const extNames = ctx.path.split('.');
app.fs.logger.log('info', 'extNames', extNames);
if (extNames.length > 0) {
let fileType = extNames[extNames.length - 1].toLowerCase();
if (fileType === 'pdf') {
ctx.type = 'application/pdf';
app.fs.logger.log('info', 'application/pdf', fileType);
}
}
}
const publicDownloadUrl = await app.fs.attachment.download(fkey);
ctx.status = 200;
if (filename) ctx.attachment(filename);
ctx.body = request.get(publicDownloadUrl);
} catch (err) {
ctx.fs.logger.error(err);
ctx.status = 404;
ctx.body = { error: 'file not found.' }
}
} else {
await next();
}
}
async function upload(ctx, next) {
let fkey = null;
try {
const { p } = ctx.params;
const { files } = await parse(ctx.req);
const file = files[0];
const extname = path.extname(file.filename).toLowerCase();
// if (!UploadPath[p]) {
// ctx.status = 400;
// ctx.body = JSON.stringify({ error: '附件存放的文件夹名称无效' });
// return;
// } else if (UploadPath[p].indexOf(extname) < 0) {
// ctx.status = 400;
// ctx.body = JSON.stringify({ error: '文件格式无效' });
// return;
// } else {
// const fileInfo = await ctx.app.fs.attachment.upload(file, { uploadPath: p });
const fileInfo = await ctx.app.fs.attachment.upload(file);
fkey = fileInfo.key;
ctx.body = { uploaded: fkey };
// }
} catch (err) {
ctx.status = 500;
ctx.fs.logger.error(err);
ctx.body = { err: 'upload error.' };
}
}
async function download(ctx, next) {
try {
const { fetchUrl } = opts.qiniu;
const { src, filename } = ctx.query;
const fkey = src.replace(/\.json$/, '.js');
const publicDownloadUrl = await ctx.app.fs.attachment.download(fkey);
if (filename) { // download
ctx.attachment(filename);
} else { // display
if (/\.(png)|(jpg)|(jpeg)|(gif)$/g.test(src.toLowerCase())) ctx.type = 'image/png';
};
ctx.status = 200;
ctx.body = request.get(publicDownloadUrl);
} catch (err) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${err}`);
ctx.status = 400;
ctx.body = { name: 'FindError', message: '附件下载失败' }
}
}
let remove = async function (ctx, next) {
try {
const fkeys = ctx.request.body;
for (let index = 0; index < fkeys.length; index++) {
await app.fs.attachment.remove(fkeys[index]);
}
ctx.status = 200;
ctx.body = fkeys;
} catch (err) {
ctx.status = 500;
ctx.fs.logger.error(err);
ctx.body = { err: 'upload cleanup error.' };
}
}
const getResourceRoot = async function (ctx) {
const { domain } = opts.qiniu;
ctx.status = 200;
ctx.body = { root: domain };
}
router.use(download_);
router.post('/_upload/attachments/:p', upload);
router.get('/_upload/attachments', download);
router.post('/_upload/remove/attachments', remove);
router.get('/_resource/root', getResourceRoot);
}
}