Yuki Takei 5 лет назад
Родитель
Сommit
400ca33540
2 измененных файлов с 7 добавлено и 9 удалено
  1. 2 8
      src/server/models/attachment.js
  2. 5 1
      src/server/routes/attachment.js

+ 2 - 8
src/server/models/attachment.js

@@ -42,8 +42,7 @@ module.exports = function(crowi) {
   attachmentSchema.set('toJSON', { virtuals: true });
 
 
-  attachmentSchema.statics.create = async function(pageId, user, fileStream, originalName, fileFormat, fileSize) {
-    const fileUploader = require('../service/file-uploader')(crowi);
+  attachmentSchema.statics.createWithoutSave = function(pageId, user, fileStream, originalName, fileFormat, fileSize) {
     const Attachment = this;
 
     const extname = path.extname(originalName);
@@ -52,7 +51,7 @@ module.exports = function(crowi) {
       fileName = `${fileName}${extname}`;
     }
 
-    let attachment = new Attachment();
+    const attachment = new Attachment();
     attachment.page = pageId;
     attachment.creator = user._id;
     attachment.originalName = originalName;
@@ -61,11 +60,6 @@ module.exports = function(crowi) {
     attachment.fileSize = fileSize;
     attachment.createdAt = Date.now();
 
-    // upload file
-    await fileUploader.uploadFile(fileStream, attachment);
-    // save attachment
-    attachment = await attachment.save();
-
     return attachment;
   };
 

+ 5 - 1
src/server/routes/attachment.js

@@ -235,6 +235,8 @@ module.exports = function(crowi, app) {
   }
 
   async function createAttachment(file, user, pageId = null) {
+    const fileUploader = require('../service/file-uploader')(crowi);
+
     // check limit
     const res = await fileUploader.checkLimit(file.size);
     if (!res.isUploadable) {
@@ -248,7 +250,9 @@ module.exports = function(crowi, app) {
     // create an Attachment document and upload file
     let attachment;
     try {
-      attachment = await Attachment.create(pageId, user, fileStream, file.originalname, file.mimetype, file.size);
+      attachment = Attachment.createWithoutSave(pageId, user, fileStream, file.originalname, file.mimetype, file.size);
+      await fileUploader.uploadFile(fileStream, attachment);
+      await attachment.save();
     }
     catch (err) {
       // delete temporary file