Przeglądaj źródła

ensure not to throw errnr even when the attachment file does not exist

Yuki Takei 6 lat temu
rodzic
commit
1646cec4e9

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

@@ -332,6 +332,7 @@ module.exports = function(crowi, app) {
       await Attachment.removeWithSubstanceById(id);
     }
     catch (err) {
+      logger.error(err);
       return res.status(500).json(ApiResponse.error('Error while deleting file'));
     }
 

+ 2 - 0
src/server/service/file-uploader/aws.js

@@ -63,6 +63,8 @@ module.exports = function(crowi) {
       Key: filePath,
     };
 
+    // TODO: ensure not to throw error even when the file does not exist
+
     return s3.deleteObject(params).promise();
   };
 

+ 7 - 5
src/server/service/file-uploader/gridfs.js

@@ -20,6 +20,7 @@ module.exports = function(crowi) {
 
   // create promisified method
   AttachmentFile.promisifiedWrite = util.promisify(AttachmentFile.write).bind(AttachmentFile);
+  AttachmentFile.promisifiedUnlink = util.promisify(AttachmentFile.unlink).bind(AttachmentFile);
 
   lib.deleteFile = async function(attachment) {
     let filenameValue = attachment.fileName;
@@ -30,11 +31,12 @@ module.exports = function(crowi) {
 
     const attachmentFile = await AttachmentFile.findOne({ filename: filenameValue });
 
-    AttachmentFile.unlink({ _id: attachmentFile._id }, (error, unlinkedFile) => {
-      if (error) {
-        throw new Error(error);
-      }
-    });
+    if (attachmentFile == null) {
+      logger.warn(`Any AttachmentFile that relate to the Attachment (${attachment._id.toString()}) does not exist in GridFS`);
+      return;
+    }
+
+    return AttachmentFile.promisifiedUnlink({ _id: attachmentFile._id });
   };
 
   /**

+ 8 - 0
src/server/service/file-uploader/local.js

@@ -31,6 +31,14 @@ module.exports = function(crowi) {
   };
 
   lib.deleteFileByFilePath = async function(filePath) {
+    // check file exists
+    try {
+      fs.statSync(filePath);
+    }
+    catch (err) {
+      logger.warn(`Any AttachmentFile which path is '${filePath}' does not exist in local fs`);
+    }
+
     return fs.unlinkSync(filePath);
   };