Browse Source

工程变更,附件上传代码调整

MaiXinRong 4 years ago
parent
commit
077585c0cb
1 changed files with 34 additions and 53 deletions
  1. 34 53
      app/controller/change_controller.js

+ 34 - 53
app/controller/change_controller.js

@@ -9,7 +9,6 @@
  */
 
 const moment = require('moment');
-const sendToWormhole = require('stream-wormhole');
 const fs = require('fs');
 const path = require('path');
 const audit = require('../const/audit');
@@ -989,66 +988,48 @@ module.exports = app => {
          * @return {void}
          */
         async uploadFile(ctx) {
-            const responseData = {
-                err: 0,
-                msg: '',
-                data: [],
-            };
-            let stream;
+            const responseData = { err: 0, msg: '', data: [], };
             try {
-                const parts = ctx.multipart({ autoFields: true });
-                const files = [];
-                let index = 0;
                 const change = await ctx.service.change.getDataByCondition({ cid: ctx.params.cid });
                 const extra_upload = change.status === audit.flow.status.checked;
-                while ((stream = await parts()) !== undefined) {
-                    // 判断用户是否选择上传文件
-                    if (!stream.filename) {
-                        throw '请选择上传的文件!';
-                    }
-                    // const create_time = Date.parse(new Date()) / 1000;
-                    // const fileInfo = path.parse(stream.filename);
-                    // const dirName = 'app/public/upload/changes/' + moment().format('YYYYMMDD');
-                    // const fileName = 'changes' + create_time + '_' + index + fileInfo.ext;
-                    // // 判断文件夹是否存在,不存在则直接创建文件夹
-                    // if (!fs.existsSync(path.join(this.app.baseDir, dirName))) {
-                    //     await fs.mkdirSync(path.join(this.app.baseDir, dirName));
-                    // }
-                    // // 保存文件
-                    // await ctx.helper.saveStreamFile(stream, path.join(this.app.baseDir, dirName, fileName));
-                    const fileInfo = path.parse(stream.filename);
-                    const create_time = Date.parse(new Date()) / 1000;
-                    const filepath = `app/public/upload/change/fujian_${create_time + index.toString() + fileInfo.ext}`;
-                    await ctx.helper.saveStreamFile(stream, path.resolve(this.app.baseDir, filepath));
-                    await sendToWormhole(stream);
-                    // 保存数据到att表
-                    const fileData = {
-                        in_time: create_time,
-                        filename: fileInfo.name,
-                        fileext: fileInfo.ext,
-                        filesize: Array.isArray(parts.field.size) ? parts.field.size[index] : parts.field.size,
-                        filepath,
-                        extra_upload,
-                    };
-                    const result = await ctx.service.changeAtt.save(parts.field, fileData, ctx.session.sessionUser.accountId);
-                    if (!result) {
-                        throw '导入数据库保存失败';
+                const files = [];
+
+                let index = 0;
+                for (const file of ctx.request.files) {
+                    if (!file.filename) throw '请选择上传的文件!';
+                    try {
+                        const fileInfo = path.parse(file.filename);
+                        const create_time = Date.parse(new Date()) / 1000;
+                        const filepath = `app/public/upload/change/fujian_${create_time + index.toString() + fileInfo.ext}`;
+
+                        await this.ctx.helper.recursiveMkdirSync(path.dirname(path.resolve(this.app.baseDir, filepath)));
+                        await fs.copyFileSync(file.filepath, path.resolve(this.app.baseDir, filepath));
+
+                        const fileData = {
+                            in_time: create_time,
+                            filename: fileInfo.name,
+                            fileext: fileInfo.ext,
+                            filesize: Array.isArray(ctx.request.body.size) ? ctx.request.body.size[index] : ctx.request.body.size,
+                            uid: ctx.session.sessionUser.accountId,
+                            filepath,
+                            extra_upload
+                        };
+                        const result = await ctx.service.changeAtt.save(ctx.request.body, fileData, ctx.session.sessionUser.accountId);
+                        if (!result) {
+                            throw '导入数据库保存失败';
+                        }
+                        fileData.uid = ctx.session.sessionUser.accountId;
+                        fileData.id = result.insertId;
+                        delete fileData.filepath;
+                        files.push(fileData);
+                        ++index;
+                    } finally {
+                        await fs.unlinkSync(file.filepath);
                     }
-                    // fileData.in_time = moment(create_time * 1000).format('YYYY-MM-DD');
-                    // fileData.filesize = await ctx.helper.bytesToSize(fileData.filesize);
-                    fileData.uid = ctx.session.sessionUser.accountId;
-                    fileData.id = result.insertId;
-                    delete fileData.filepath;
-                    files.push(fileData);
-                    ++index;
                 }
                 responseData.data = files;
             } catch (err) {
                 this.log(err);
-                // 失败需要消耗掉stream 以防卡死
-                if (stream) {
-                    await sendToWormhole(stream);
-                }
                 this.setMessage(err.toString(), this.messageType.ERROR);
             }
             ctx.body = responseData;