Просмотр исходного кода

Refactor createAttachment method to use disposeTmpFileCallback for handling temporary file cleanup

Shun Miyazawa 10 месяцев назад
Родитель
Сommit
ec8e7d92f1
2 измененных файлов с 5 добавлено и 5 удалено
  1. 1 1
      apps/app/src/server/routes/apiv3/attachment.js
  2. 4 4
      apps/app/src/server/service/attachment.js

+ 1 - 1
apps/app/src/server/routes/apiv3/attachment.js

@@ -360,7 +360,7 @@ module.exports = (crowi) => {
           return res.apiv3Err(`Forbidden to access to the page '${page.id}'`);
           return res.apiv3Err(`Forbidden to access to the page '${page.id}'`);
         }
         }
 
 
-        const attachment = await attachmentService.createAttachment(file, req.user, pageId, AttachmentType.WIKI_PAGE, autoReap(req, res, () => {}));
+        const attachment = await attachmentService.createAttachment(file, req.user, pageId, AttachmentType.WIKI_PAGE, () => autoReap(req, res, () => {}));
 
 
         const result = {
         const result = {
           page: serializePageSecurely(page),
           page: serializePageSecurely(page),

+ 4 - 4
apps/app/src/server/service/attachment.js

@@ -28,7 +28,7 @@ class AttachmentService {
     this.crowi = crowi;
     this.crowi = crowi;
   }
   }
 
 
-  async createAttachment(file, user, pageId = null, attachmentType, onAttached) {
+  async createAttachment(file, user, pageId = null, attachmentType, disposeTmpFileCallback) {
     const { fileUploadService } = this.crowi;
     const { fileUploadService } = this.crowi;
 
 
     // check limit
     // check limit
@@ -58,12 +58,12 @@ class AttachmentService {
 
 
       // Do not await, run in background
       // Do not await, run in background
       Promise.all(attachedHandlerPromises).then(() => {
       Promise.all(attachedHandlerPromises).then(() => {
-        onAttached?.(file);
+        disposeTmpFileCallback?.(file);
       });
       });
     }
     }
     catch (err) {
     catch (err) {
-      // delete temporary file
-      fs.unlink(file.path, (err) => { if (err) { logger.error('Error while deleting tmp file.') } });
+      logger.error('Error while creating attachment', err);
+      disposeTmpFileCallback?.(file);
       throw err;
       throw err;
     }
     }