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

adjust findDeliveryFile of gridfs

yusueketk 7 лет назад
Родитель
Сommit
66db92e2fb
2 измененных файлов с 42 добавлено и 51 удалено
  1. 6 4
      src/server/routes/attachment.js
  2. 36 47
      src/server/service/file-uploader/gridfs.js

+ 6 - 4
src/server/routes/attachment.js

@@ -40,6 +40,10 @@ module.exports = function(crowi, app) {
             if (fileName.match(/^\/uploads/)) {
               return res.download(path.join(crowi.publicDir, fileName), data.originalName);
             }
+            // gridfs
+            else if (fileName.match(/^.*getMongo.*/)) {
+              return res.download(path.join(crowi.publicDir, fileName), data.originalName);
+            }
             // aws
             else {
               const options = {
@@ -64,16 +68,14 @@ module.exports = function(crowi, app) {
    * @apiName getAttachments
    * @apiGroup Attachment
    *
-   * @apiParam {String} attachment_path
+   * @apiParam {String} filePath
    */
   api.getMongoFile = function(req, res) {
     const filePath = req.query.filePath;
-    AttachmentFile.find({filename: filePath}, function(err, file) {
+    AttachmentFile.find({filename: filePath}, async function(err, file) {
       if (err) {
         throw new Error(err);
       }
-    })
-    .then(async function(file) {
       const id = file[0].id;
       const contentType = file[0].contentType;
       const readStream = new Promise((resolve, reject) => {

+ 36 - 47
src/server/service/file-uploader/gridfs.js

@@ -38,61 +38,50 @@ module.exports = function(crowi) {
         if (error) {
           throw new Error('Failed to upload ' + createdFile + 'to gridFS', error);
         }
-        return createdFile._id;
       });
   };
 
   lib.findDeliveryFile = function(fileId, filePath) {
-  //   const cacheFile = lib.createCacheFileName(fileId);
-
-  //   debug('find delivery file', cacheFile);
-  //   if (!lib.shouldUpdateCacheFile(cacheFile)) {
-  //     return cacheFile;
-  //   }
-
-  //   const loader = require('https');
-
-  //   const fileStream = fs.createWriteStream(cacheFile);
-  //   const fileUrl = lib.generateUrl(filePath);
-  //   debug('Load attachement file into local cache file', fileUrl, cacheFile);
-  //   loader.get(fileUrl, function(response) {
-  //     response.pipe(fileStream, {
-  //       end: false
-  //     });
-  //     response.on('end', () => {
-  //       fileStream.end();
-  //       return cacheFile;
-  //     });
-  //   });
-  // };
+    const cacheFile = lib.createCacheFileName(fileId);
 
-  // // private
-  // lib.createCacheFileName = function(fileId) {
-  //   return path.join(crowi.cacheDir, `attachment-${fileId}`);
-  // };
+    debug('find delivery file', cacheFile);
+    if (!lib.shouldUpdateCacheFile(cacheFile)) {
+      return cacheFile;
+    }
 
-  // // private
-  // lib.shouldUpdateCacheFile = function(filePath) {
-  //   try {
-  //     const stats = fs.statSync(filePath);
+    const fileStream = fs.createWriteStream(cacheFile);
+    const fileUrl = lib.generateUrl(filePath);
+    debug('Load attachement file into local cache file', fileUrl, cacheFile);
+    return cacheFile;
+  };
 
-  //     if (!stats.isFile()) {
-  //       debug('Cache file not found or the file is not a regular fil.');
-  //       return true;
-  //     }
+  // private
+  lib.createCacheFileName = function(fileId) {
+    return path.join(crowi.cacheDir, `attachment-${fileId}`);
+  };
 
-  //     if (stats.size <= 0) {
-  //       debug('Cache file found but the size is 0');
-  //       return true;
-  //     }
-  //   }
-  //   catch (e) {
-  //     // no such file or directory
-  //     debug('Stats error', e);
-  //     return true;
-  //   }
-
-  //   return false;
+  // private
+  lib.shouldUpdateCacheFile = function(filePath) {
+    try {
+      const stats = fs.statSync(filePath);
+
+      if (!stats.isFile()) {
+        debug('Cache file not found or the file is not a regular fil.');
+        return true;
+      }
+
+      if (stats.size <= 0) {
+        debug('Cache file found but the size is 0');
+        return true;
+      }
+    }
+    catch (e) {
+      // no such file or directory
+      debug('Stats error', e);
+      return true;
+    }
+
+    return false;
   };