zamis 5 years ago
parent
commit
68538f0f3b

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

@@ -6,7 +6,8 @@ module.exports = function(crowi) {
   const Uploader = require('./uploader');
   const lib = new Uploader(crowi);
   const COLLECTION_NAME = 'attachmentFiles';
-  // const CHUNK_COLLECTION_NAME = `${COLLECTION_NAME}.chunks`;
+  const CHUNK_COLLECTION_NAME = `${COLLECTION_NAME}.chunks`;
+  const FILES_COLLECTION_NAME = `${COLLECTION_NAME}.files`;
 
   // instantiate mongoose-gridfs
   const { createModel } = require('mongoose-gridfs');
@@ -16,7 +17,10 @@ module.exports = function(crowi) {
     connection: mongoose.connection,
   });
   // get Collection instance of chunk
-  // const chunkCollection = mongoose.connection.collection(CHUNK_COLLECTION_NAME);
+  const chunkCollection = mongoose.connection.collection(CHUNK_COLLECTION_NAME);
+  const filesCollection = mongoose.connection.collection(FILES_COLLECTION_NAME);
+  const unorderChunkCollection = chunkCollection.initializeUnorderedBulkOp();
+  const unorderFilesCollection = filesCollection.initializeUnorderedBulkOp();
 
   // create promisified method
   AttachmentFile.promisifiedWrite = util.promisify(AttachmentFile.write).bind(AttachmentFile);
@@ -28,19 +32,29 @@ module.exports = function(crowi) {
 
   lib.deleteFile = async function(attachment) {
     let filenameValue = attachment.fileName;
-
     if (attachment.filePath != null) { // backward compatibility for v3.3.x or below
       filenameValue = attachment.filePath;
     }
-
     const attachmentFile = await AttachmentFile.findOne({ filename: filenameValue });
 
+    const fileId = attachmentFile._id;
+    filenameValue = attachmentFile.fileName;
+    console.log(fileId);
+    console.log(filenameValue);
+
+    unorderChunkCollection.find({ files_id: fileId }).remove();
+    unorderFilesCollection.find({ fileName: filenameValue }).remove();
+
+
     if (attachmentFile == null) {
       logger.warn(`Any AttachmentFile that relate to the Attachment (${attachment._id.toString()}) does not exist in GridFS`);
       return;
     }
+    await unorderFilesCollection.execute();
+    await unorderChunkCollection.execute();
 
-    return AttachmentFile.promisifiedUnlink({ _id: attachmentFile._id });
+    // return AttachmentFile.promisifiedUnlink({ _id: attachmentFile._id });
+    return;
   };
 
   /**

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

@@ -30,8 +30,13 @@ module.exports = function(crowi) {
     return true;
   };
 
+  // lib.getFilePathOnStorage = function(attachment) {
+  //   return getFilePathOnStorage(attachment);
+  // };
+
   lib.deleteFile = async function(attachment) {
     const filePath = getFilePathOnStorage(attachment);
+
     return lib.deleteFileByFilePath(filePath);
   };