yusueketk 7 سال پیش
والد
کامیت
de3f4126fb
3فایلهای تغییر یافته به همراه53 افزوده شده و 50 حذف شده
  1. 9 8
      src/server/routes/attachment.js
  2. 1 1
      src/server/routes/index.js
  3. 43 41
      src/server/service/file-uploader/gridfs.js

+ 9 - 8
src/server/routes/attachment.js

@@ -40,10 +40,10 @@ module.exports = function(crowi, app) {
             if (fileName.match(/^\/uploads/)) {
             if (fileName.match(/^\/uploads/)) {
               return res.download(path.join(crowi.publicDir, fileName), data.originalName);
               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);
-            }
+            // // gridfs
+            // else if (fileName.match(/^.*getMongo.*/)) {
+            //   return res.download(path.join(crowi.publicDir, fileName), data.originalName);
+            // }
             // aws
             // aws
             else {
             else {
               const options = {
               const options = {
@@ -64,14 +64,15 @@ module.exports = function(crowi, app) {
   };
   };
 
 
   /**
   /**
-   * @api {get} /attachments.get get attachments from MongoDB
-   * @apiName getAttachments
+   * @api {get} /attachments.get get attachments from mongoDB
+   * @apiName get
    * @apiGroup Attachment
    * @apiGroup Attachment
    *
    *
    * @apiParam {String} filePath
    * @apiParam {String} filePath
    */
    */
-  api.getMongoFile = function(req, res) {
-    const filePath = req.query.filePath;
+  api.get = function(req, res) {
+    const filePath = req.params.filePath;
+
     AttachmentFile.find({filename: filePath}, async function(err, file) {
     AttachmentFile.find({filename: filePath}, async function(err, file) {
       if (err) {
       if (err) {
         throw new Error(err);
         throw new Error(err);

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

@@ -176,6 +176,7 @@ module.exports = function(crowi, app) {
   app.get( '/:id([0-9a-z]{24})'       , loginRequired(crowi, app, false) , page.api.redirector);
   app.get( '/:id([0-9a-z]{24})'       , loginRequired(crowi, app, false) , page.api.redirector);
   app.get( '/_r/:id([0-9a-z]{24})'    , loginRequired(crowi, app, false) , page.api.redirector); // alias
   app.get( '/_r/:id([0-9a-z]{24})'    , loginRequired(crowi, app, false) , page.api.redirector); // alias
   app.get( '/download/:id([0-9a-z]{24})' , loginRequired(crowi, app, false) , attachment.api.download);
   app.get( '/download/:id([0-9a-z]{24})' , loginRequired(crowi, app, false) , attachment.api.download);
+  app.get( '/files/:id([0-9a-z]{24})'  , loginRequired(crowi, app, false), attachment.api.get);
 
 
   app.get( '/_search'                 , loginRequired(crowi, app, false) , search.searchPage);
   app.get( '/_search'                 , loginRequired(crowi, app, false) , search.searchPage);
   app.get( '/_api/search'             , accessTokenParser , loginRequired(crowi, app, false) , search.api.search);
   app.get( '/_api/search'             , accessTokenParser , loginRequired(crowi, app, false) , search.api.search);
@@ -208,7 +209,6 @@ module.exports = function(crowi, app) {
   app.post('/_api/bookmarks.remove'   , accessTokenParser , loginRequired(crowi, app) , csrf, bookmark.api.remove);
   app.post('/_api/bookmarks.remove'   , accessTokenParser , loginRequired(crowi, app) , csrf, bookmark.api.remove);
   app.post('/_api/likes.add'          , accessTokenParser , loginRequired(crowi, app) , csrf, page.api.like);
   app.post('/_api/likes.add'          , accessTokenParser , loginRequired(crowi, app) , csrf, page.api.like);
   app.post('/_api/likes.remove'       , accessTokenParser , loginRequired(crowi, app) , csrf, page.api.unlike);
   app.post('/_api/likes.remove'       , accessTokenParser , loginRequired(crowi, app) , csrf, page.api.unlike);
-  app.get( '/_api/attachments.getMongo'  , accessTokenParser , loginRequired(crowi, app, false) , attachment.api.getMongoFile);
   app.get( '/_api/attachments.list'   , accessTokenParser , loginRequired(crowi, app, false) , attachment.api.list);
   app.get( '/_api/attachments.list'   , accessTokenParser , loginRequired(crowi, app, false) , attachment.api.list);
   app.post('/_api/attachments.add'    , uploads.single('file'), accessTokenParser, loginRequired(crowi, app) ,csrf, attachment.api.add);
   app.post('/_api/attachments.add'    , uploads.single('file'), accessTokenParser, loginRequired(crowi, app) ,csrf, attachment.api.add);
   app.post('/_api/attachments.remove' , accessTokenParser , loginRequired(crowi, app) , csrf, attachment.api.remove);
   app.post('/_api/attachments.remove' , accessTokenParser , loginRequired(crowi, app) , csrf, attachment.api.remove);

+ 43 - 41
src/server/service/file-uploader/gridfs.js

@@ -3,7 +3,8 @@
 module.exports = function(crowi) {
 module.exports = function(crowi) {
   'use strict';
   'use strict';
 
 
-  var debug = require('debug')('growi:service:fileUploaderLocal')
+  var debug = require('debug')('growi:service:fileUploadergridfs')
+  var logger = require('@alias/logger')('growi:routes:attachment')
   var mongoose = require('mongoose');
   var mongoose = require('mongoose');
   var path = require('path');
   var path = require('path');
   var fs = require('fs');
   var fs = require('fs');
@@ -42,55 +43,56 @@ module.exports = function(crowi) {
   };
   };
 
 
   lib.findDeliveryFile = function(fileId, filePath) {
   lib.findDeliveryFile = function(fileId, filePath) {
-    const cacheFile = lib.createCacheFileName(fileId);
+  //   const cacheFile = lib.createCacheFileName(fileId);
 
 
-    debug('find delivery file', cacheFile);
-    if (!lib.shouldUpdateCacheFile(cacheFile)) {
-      return cacheFile;
-    }
+  //   debug('find delivery file', cacheFile);
+  //   if (!lib.shouldUpdateCacheFile(cacheFile)) {
+  //     return cacheFile;
+  //   }
 
 
-    const fileStream = fs.createWriteStream(cacheFile);
-    const fileUrl = lib.generateUrl(filePath);
-    debug('Load attachement file into local cache file', fileUrl, cacheFile);
-    return cacheFile;
-  };
+  //   const fileStream = fs.createWriteStream(cacheFile);
+  //   const fileUrl = lib.generateUrl(filePath);
+  //   debug('Load attachement file into local cache file', fileUrl, cacheFile);
+  //   return cacheFile;
+  // };
 
 
-  // private
-  lib.createCacheFileName = function(fileId) {
-    return path.join(crowi.cacheDir, `attachment-${fileId}`);
-  };
+  // // private
+  // lib.createCacheFileName = function(fileId) {
+  //   return path.join(crowi.cacheDir, `attachment-${fileId}`);
+  // };
 
 
-  // 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;
+  // // 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;
   };
   };
 
 
 
 
   lib.generateUrl = function(filePath) {
   lib.generateUrl = function(filePath) {
-    var config = crowi.getConfig();
-    var baseUrl = (config.crowi['app:siteUrl:fixed'] || '');
+    // var config = crowi.getConfig();
+    // var baseUrl = (config.crowi['app:siteUrl:fixed'] || '');
 
 
-    const url = `${baseUrl}/_api/attachments.getMongo?filePath=${filePath}`;
-    return url;
+    // const url = `${baseUrl}/_api/attachments.getMongo?filePath=${filePath}`;
+    // return url;
+    return `/files/${filePath}`;
   };
   };
 
 
   return lib;
   return lib;