Yuki Takei 7 лет назад
Родитель
Сommit
5caac4a45f

+ 4 - 19
src/server/models/attachment.js

@@ -70,7 +70,7 @@ module.exports = function(crowi) {
       Attachment.find({ page: pageId})
       .then((attachments) => {
         for (let attachment of attachments) {
-          Attachment.removeAttachment(attachment).then((res) => {
+          attachment.removeWithSubstance().then((res) => {
             // do nothing
           }).catch((err) => {
             debug('Attachment remove error', err);
@@ -85,24 +85,9 @@ module.exports = function(crowi) {
 
   };
 
-  attachmentSchema.statics.removeAttachment = function(attachment) {
-    const Attachment = this;
-    const filePath = attachment.filePath;
-
-    return new Promise((resolve, reject) => {
-      Attachment.remove({_id: attachment._id}, (err, data) => {
-        if (err) {
-          return reject(err);
-        }
-
-        fileUploader.deleteFile(attachment._id, filePath)
-        .then(data => {
-          resolve(data); // this may null
-        }).catch(err => {
-          reject(err);
-        });
-      });
-    });
+  attachmentSchema.methods.removeWithSubstance = async function() {
+    await fileUploader.deleteFile(this);
+    return await this.remove();
   };
 
   return mongoose.model('Attachment', attachmentSchema);

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

@@ -285,7 +285,7 @@ module.exports = function(crowi, app) {
     .then(function(data) {
       const attachment = data;
 
-      Attachment.removeAttachment(attachment)
+      attachment.removeWithSubstance()
       .then(data => {
         debug('removeAttachment', data);
         return res.json(ApiResponse.success({}));

+ 7 - 14
src/server/service/file-uploader/aws.js

@@ -49,7 +49,12 @@ module.exports = function(crowi) {
     return filePath;
   }
 
-  lib.deleteFile = function(fileId, filePath) {
+  lib.deleteFile = async function(attachment) {
+    const filePath = getFilePathOnStorage(attachment);
+    return lib.deleteFileByFilePath(filePath);
+  };
+
+  lib.deleteFileByFilePath = async function(filePath) {
     const s3 = S3Factory();
     const awsConfig = getAwsConfig();
 
@@ -58,19 +63,7 @@ module.exports = function(crowi) {
       Key: filePath,
     };
 
-    return new Promise((resolve, reject) => {
-      s3.deleteObject(params, (err, data) => {
-        if (err) {
-          logger.debug('Failed to delete object from s3', err);
-          return reject(err);
-        }
-
-        // asynclonousely delete cache
-        lib.clearCache(fileId);
-
-        resolve(data);
-      });
-    });
+    return s3.deleteObject(params).promise();
   };
 
   lib.uploadFile = function(fileStream, attachment) {

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

@@ -21,17 +21,14 @@ module.exports = function(crowi) {
   // create promisified method
   AttachmentFile.promisifiedWrite = util.promisify(AttachmentFile.write).bind(AttachmentFile);
 
-  // delete a file
-  lib.deleteFile = async function(fileId, filePath) {
-    logger.debug('File deletion: ' + fileId);
-    const file = await getFile(filePath);
-    const id = file.id;
-    AttachmentFile.unlinkById(id, function(error, unlinkedAttachment) {
+  lib.deleteFile = async function(attachment) {
+    const attachmentFile = await AttachmentFile.findOne({ filename: attachment.fileName });
+
+    AttachmentFile.unlinkById(attachmentFile._id, function(error, unlinkedFile) {
       if (error) {
         throw new Error(error);
       }
     });
-    // clearCache(fileId);
   };
 
   /**

+ 10 - 14
src/server/service/file-uploader/local.js

@@ -17,26 +17,22 @@ module.exports = function(crowi) {
       filePath = path.posix.join(basePath, attachment.filePath);
     }
     else {
-    const dirName = (attachment.page != null)
-      ? 'attachment'
-      : 'user';
+      const dirName = (attachment.page != null)
+        ? 'attachment'
+        : 'user';
       filePath = path.posix.join(basePath, dirName, attachment.fileName);
     }
 
     return filePath;
   }
 
-  lib.deleteFile = function(fileId, filePath) {
-    logger.debug('File deletion: ' + filePath);
-    return new Promise(function(resolve, reject) {
-      fs.unlink(path.posix.join(basePath, filePath), function(err) {
-        if (err) {
-          return reject(err);
-        }
-
-        resolve();
-      });
-    });
+  lib.deleteFile = async function(attachment) {
+    const filePath = getFilePathOnStorage(attachment);
+    return lib.deleteFileByFilePath(filePath);
+  };
+
+  lib.deleteFileByFilePath = async function(filePath) {
+    return fs.unlinkSync(filePath);
   };
 
   lib.uploadFile = async function(fileStream, attachment) {