Browse Source

// const parse = require('async-busboy');

simplify_dependencies
巴林闲侠 2 years ago
parent
commit
1a88eab824
  1. 88
      console/routes/attachment/index.js

88
console/routes/attachment/index.js

@ -1,6 +1,6 @@
'use strict';
const request = require('superagent');
const parse = require('async-busboy');
// const parse = require('async-busboy');
const path = require('path')
const fs = require('fs');
@ -76,33 +76,33 @@ module.exports = {
let upload = async function (ctx, next) {
try {
const { files } = await parse(ctx.req);
const file = files[0];
const extname = path.extname(file.filename).toLowerCase();
const fileType = ctx.query.type || "image";
const fileFolder = ctx.query.fileFolder || 'common';
if (ext[fileType].indexOf(extname) < 0) {
ctx.status = 400;
ctx.body = JSON.stringify({ name: 'UploadFailed', message: '文件格式无效' });
return;
}
const date = new Date().toLocaleDateString();
const time = new Date().getTime();
let fileName = time + '_' + file.filename;
let saveFile = path.join(__dirname, '../../', `/client/assets/files/${fileFolder}`, fileName);
const pathUrl = `./client/assets/files/${fileFolder}`;
// const { files } = await parse(ctx.req);
// const file = files[0];
// const extname = path.extname(file.filename).toLowerCase();
// const fileType = ctx.query.type || "image";
// const fileFolder = ctx.query.fileFolder || 'common';
// if (ext[fileType].indexOf(extname) < 0) {
// ctx.status = 400;
// ctx.body = JSON.stringify({ name: 'UploadFailed', message: '文件格式无效' });
// return;
// }
// const date = new Date().toLocaleDateString();
// const time = new Date().getTime();
// let fileName = time + '_' + file.filename;
// let saveFile = path.join(__dirname, '../../', `/client/assets/files/${fileFolder}`, fileName);
// const pathUrl = `./client/assets/files/${fileFolder}`;
const res1 = fs.existsSync(`./client/assets/files/${fileFolder}`);
!res1 && fs.mkdirSync(`./client/assets/files/${fileFolder}`);
const res = fs.existsSync(pathUrl);
!res && fs.mkdirSync(pathUrl);
let stream = fs.createWriteStream(saveFile);
fs.createReadStream(file.path).pipe(stream);
stream.on('error', function (err) {
app.fs.logger.log('error', '[Upload Heatmap]', err);
});
ctx.status = 200;
ctx.body = { filename: path.join(`/assets/files/${fileFolder}`, fileName), name: 'UploadSuccess', message: '上传成功' };
// const res1 = fs.existsSync(`./client/assets/files/${fileFolder}`);
// !res1 && fs.mkdirSync(`./client/assets/files/${fileFolder}`);
// const res = fs.existsSync(pathUrl);
// !res && fs.mkdirSync(pathUrl);
// let stream = fs.createWriteStream(saveFile);
// fs.createReadStream(file.path).pipe(stream);
// stream.on('error', function (err) {
// app.fs.logger.log('error', '[Upload Heatmap]', err);
// });
// ctx.status = 200;
// ctx.body = { filename: path.join(`/assets/files/${fileFolder}`, fileName), name: 'UploadSuccess', message: '上传成功' };
} catch (err) {
ctx.status = 500;
ctx.fs.logger.error(err);
@ -136,23 +136,23 @@ module.exports = {
let upload_ = async function (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 });
fkey = fileInfo.key;
ctx.body = { uploaded: fkey };
}
// 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 });
// fkey = fileInfo.key;
// ctx.body = { uploaded: fkey };
// }
} catch (err) {
ctx.status = 500;
ctx.fs.logger.error(err);

Loading…
Cancel
Save