Ver código fonte

材料调差,附件改版

MaiXinRong 4 anos atrás
pai
commit
f31c0c4d72
1 arquivos alterados com 19 adições e 18 exclusões
  1. 19 18
      app/controller/material_controller.js

+ 19 - 18
app/controller/material_controller.js

@@ -868,33 +868,35 @@ module.exports = app => {
          * @param {*} ctx 上下文
          */
         async upload(ctx) {
-            let stream;
             try {
                 await this._checkMaterialFileCanModify(ctx);
-                const parts = this.ctx.multipart({
-                    autoFields: true,
-                });
+
                 const files = [];
-                const create_time = Date.parse(new Date()) / 1000;
                 let idx = 0;
+                const create_time = Date.parse(new Date()) / 1000;
                 const extra_upload = ctx.material.status === auditConst.status.checked;
-                while ((stream = await parts()) !== undefined) {
-                    if (!stream.filename) {
-                        // 如果没有传入直接返回
-                        return;
+                for (const file of ctx.request.files) {
+                    // 如果没有传入直接返回
+                    if (!file.filename) return;
+                    try {
+                        const fileInfo = path.parse(file.filename);
+                        const filepath = `public/upload/${this.ctx.tender.id.toString()}/tc/fujian_${create_time + idx.toString() + fileInfo.ext}`;
+                        await this.ctx.helper.recursiveMkdirSync(path.dirname(path.join(this.app.baseDir, 'app', filepath)));
+                        await fs.copyFileSync(file.filepath, path.resolve(this.app.baseDir, 'app', filepath));
+                        files.push({
+                            filepath, name: file.filename, ext: fileInfo.ext
+                        });
+                    } finally {
+                        await fs.unlinkSync(file.filepath);
                     }
-                    const fileInfo = path.parse(stream.filename);
-                    // const filepath = path.join('public/upload', this.ctx.tender.id.toString(), 'tc', 'fujian_' + create_time + fileInfo.ext);
-                    const filepath = `public/upload/${this.ctx.tender.id.toString()}/tc/fujian_${create_time + idx.toString() + fileInfo.ext}`;
-                    await ctx.helper.saveStreamFile(stream, path.resolve(this.app.baseDir, 'app', filepath));
-                    files.push({ filepath, name: stream.filename, ext: fileInfo.ext });
                     ++idx;
                 }
+
                 const upload_time = this.ctx.helper.dateTran(new Date());
                 const payload = files.map(file => {
                     let idx;
-                    if (Array.isArray(parts.field.name)) {
-                        idx = parts.field.name.findIndex(name => name === file.name);
+                    if (Array.isArray(ctx.request.body.name)) {
+                        idx = ctx.request.body.name.findIndex(name => name === file.name);
                     } else {
                         idx = 'isString';
                     }
@@ -905,7 +907,7 @@ module.exports = app => {
                         s_order: ctx.params.order,
                         upload_time,
                         filepath: file.filepath,
-                        file_size: ctx.helper.bytesToSize(idx === 'isString' ? parts.field.size : parts.field.size[idx]),
+                        file_size: ctx.helper.bytesToSize(idx === 'isString' ? ctx.request.body.size : ctx.request.body.size[idx]),
                         file_name: file.name,
                         fileext: file.ext,
                         extra_upload,
@@ -918,7 +920,6 @@ module.exports = app => {
                 const data = await ctx.service.materialFile.getAllMaterialFiles(ctx.tender.id);
                 ctx.body = { err: 0, msg: '', data };
             } catch (err) {
-                stream && (await sendToWormhole(stream));
                 this.log(err);
                 ctx.body = { err: 1, msg: err.toString(), data: null };
             }