|
@@ -8,7 +8,6 @@
|
|
|
* @version
|
|
|
*/
|
|
|
const auditConst = require('../const/audit').stage;
|
|
|
-const sendToWormhole = require('stream-wormhole');
|
|
|
const path = require('path');
|
|
|
const moment = require('moment');
|
|
|
const fs = require('fs');
|
|
@@ -198,37 +197,33 @@ module.exports = app => {
|
|
|
}
|
|
|
|
|
|
async uploadFile(ctx) {
|
|
|
- let stream;
|
|
|
try {
|
|
|
- const parts = ctx.multipart({ autoFields: true });
|
|
|
- let index = 0;
|
|
|
+ const bonus = await ctx.service.stageBonus.getStageDataById(ctx.request.body.bonus_id);
|
|
|
+ if (!bonus || bonus.sid !== ctx.stage.id) throw '该奖罚金,当前不允许上传附件';
|
|
|
const create_time = Date.parse(new Date()) / 1000;
|
|
|
- let bonus;
|
|
|
- while ((stream = await parts()) !== undefined) {
|
|
|
- if (!stream.filename) {
|
|
|
- throw '未发现上传文件!';
|
|
|
- }
|
|
|
- if (!bonus) bonus = await ctx.service.stageBonus.getStageDataById(parts.field.bonus_id);
|
|
|
- if (!bonus || bonus.sid !== ctx.stage.id) throw '该奖罚金,当前不允许上传附件';
|
|
|
|
|
|
- const fileInfo = path.parse(stream.filename);
|
|
|
- const dirName = 'app/public/upload/extra/' + moment().format('YYYYMMDD');
|
|
|
- const fileName = create_time + '_' + index + fileInfo.ext;
|
|
|
+ let index = 0;
|
|
|
+ for (const file of ctx.request.files) {
|
|
|
+ try {
|
|
|
+ const fileInfo = path.parse(file.filename);
|
|
|
+ const dirName = 'app/public/upload/extra/' + moment().format('YYYYMMDD');
|
|
|
+ const fileName = create_time + '_' + index + fileInfo.ext;
|
|
|
|
|
|
- // 保存文件
|
|
|
- await ctx.helper.saveStreamFile(stream, path.join(this.app.baseDir, dirName, fileName));
|
|
|
- await sendToWormhole(stream);
|
|
|
+ await this.ctx.helper.recursiveMkdirSync(path.join(this.app.baseDir, dirName));
|
|
|
+ await fs.copyFileSync(file.filepath, path.join(this.app.baseDir, dirName, fileName));
|
|
|
|
|
|
- // 插入到stage_pay对应的附件列表中
|
|
|
- bonus.proof_file.push({
|
|
|
- filename: fileInfo.name,
|
|
|
- fileext: fileInfo.ext,
|
|
|
- filesize: Array.isArray(parts.field.size) ? parts.field.size[index] : parts.field.size,
|
|
|
- filepath: path.join(dirName, fileName),
|
|
|
- uid: ctx.session.sessionUser.accountId,
|
|
|
- in_time: moment(create_time * 1000).format('YYYY-MM-DD'),
|
|
|
- });
|
|
|
- ++index;
|
|
|
+ bonus.proof_file.push({
|
|
|
+ filename: fileInfo.name,
|
|
|
+ fileext: fileInfo.ext,
|
|
|
+ filesize: Array.isArray(ctx.request.body.size) ? ctx.request.body.size[index] : ctx.request.body.size,
|
|
|
+ filepath: path.join(dirName, fileName),
|
|
|
+ uid: ctx.session.sessionUser.accountId,
|
|
|
+ in_time: moment(create_time * 1000).format('YYYY-MM-DD'),
|
|
|
+ });
|
|
|
+ ++index;
|
|
|
+ } finally {
|
|
|
+ await fs.unlinkSync(file.filepath);
|
|
|
+ }
|
|
|
}
|
|
|
const result = await ctx.service.stageBonus.updateDatas({
|
|
|
update: [
|
|
@@ -242,10 +237,6 @@ module.exports = app => {
|
|
|
ctx.body = {err: 0, msg: '', data: bonus.proof_file};
|
|
|
} catch (error) {
|
|
|
ctx.helper.log(error);
|
|
|
- // 失败需要消耗掉stream 以防卡死
|
|
|
- if (stream) {
|
|
|
- await sendToWormhole(stream);
|
|
|
- }
|
|
|
ctx.body = this.ajaxErrorBody(error, '上传附件失败,请重试');
|
|
|
}
|
|
|
}
|