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

Supports nginx internal redirect and Apache SendFile.

Daiki51 5 лет назад
Родитель
Сommit
1df6b6d2a4
2 измененных файлов с 45 добавлено и 1 удалено
  1. 33 1
      src/server/routes/attachment.js
  2. 12 0
      src/server/service/config-loader.js

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

@@ -4,6 +4,7 @@
 const logger = require('@alias/logger')('growi:routes:attachment');
 
 const fs = require('fs');
+const path = require('path');
 
 const ApiResponse = require('../util/apiResponse');
 
@@ -132,7 +133,7 @@ module.exports = function(crowi, app) {
   const User = crowi.model('User');
   const Page = crowi.model('Page');
   const fileUploader = require('../service/file-uploader')(crowi, app);
-
+  const configManager = crowi.configManager;
 
   /**
    * Check the user is accessible to the related page
@@ -193,6 +194,10 @@ module.exports = function(crowi, app) {
       return res.sendStatus(304);
     }
 
+    if (isEnableInternalRedirect()) {
+      return responseForInternalRedirect(res, attachment);
+    }
+
     let fileStream;
     try {
       fileStream = await fileUploader.findDeliveryFile(attachment);
@@ -205,6 +210,33 @@ module.exports = function(crowi, app) {
     return fileStream.pipe(res);
   }
 
+  /**
+   * check whether to use internal redirect of nginx or Apache.
+   */
+  function isEnableInternalRedirect() {
+    return process.env.FILE_UPLOAD == "local" && configManager.getConfig('crowi', 'app:useInternalRedirect');
+  }
+
+  /**
+   * responce using internal redirect of nginx or Apache.
+   *
+   * @param {Response} res
+   * @param {Attachment} attachment
+   */
+  function responseForInternalRedirect(res, attachment) {
+    const config = crowi.getConfig();
+    const dirName = (attachment.page != null) ? 'attachment' : 'user';
+    let internalPathRoot = configManager.getConfig('crowi', 'app:internalRedirectPath');
+    if (internalPathRoot.slice(-1) != "/") {
+      internalPathRoot += "/";
+    }
+    const internalPath = `${internalPathRoot}uploads/${dirName}/${attachment.fileName}`;
+    const storagePath = path.posix.join(crowi.publicDir, 'uploads', dirName, attachment.fileName);
+    res.set('X-Accel-Redirect', internalPath);
+    res.set('X-Sendfile', storagePath);
+    return res.end();
+  }
+
   /**
    * set http response header
    *

+ 12 - 0
src/server/service/config-loader.js

@@ -137,6 +137,18 @@ const ENV_VAR_NAME_TO_CONFIG_INFO = {
     type:    TYPES.BOOLEAN,
     default: false,
   },
+  USE_INTERNAL_REDIRECT: {
+    ns:      'crowi',
+    key:     'app:useInternalRedirect',
+    type:    TYPES.BOOLEAN,
+    default: false,
+  },
+  INTERNAL_REDIRECT_PATH: {
+    ns:      'crowi',
+    key:     'app:internalRedirectPath',
+    type:    TYPES.STRING,
+    default: "/growi-internal/",
+  },
   ELASTICSEARCH_URI: {
     ns:      'crowi',
     key:     'app:elasticsearchUri',