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

Moved alternative response processing to file-uploader/*

Daiki51 5 лет назад
Родитель
Сommit
e03319aadc

+ 3 - 31
src/server/routes/attachment.js

@@ -4,7 +4,6 @@
 const logger = require('@alias/logger')('growi:routes:attachment');
 
 const fs = require('fs');
-const path = require('path');
 
 const ApiResponse = require('../util/apiResponse');
 
@@ -133,7 +132,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
@@ -194,8 +193,8 @@ module.exports = function(crowi, app) {
       return res.sendStatus(304);
     }
 
-    if (isEnableInternalRedirect()) {
-      return responseForInternalRedirect(res, attachment);
+    if (fileUploader.canRespond()) {
+      return fileUploader.respond(res, attachment);
     }
 
     let fileStream;
@@ -210,33 +209,6 @@ 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
    *

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

@@ -4,6 +4,7 @@ const fs = require('fs');
 const path = require('path');
 const mkdir = require('mkdirp');
 const streamToPromise = require('stream-to-promise');
+const urljoin = require('url-join');
 
 module.exports = function(crowi) {
   const Uploader = require('./uploader');
@@ -92,5 +93,29 @@ module.exports = function(crowi) {
     return lib.doCheckLimit(uploadFileSize, maxFileSize, totalLimit);
   };
 
+  /**
+   * Checks if Uploader can respond to the HTTP request.
+   */
+  lib.canRespond = () => {
+    // Check whether to use internal redirect of nginx or Apache.
+    return process.env.FILE_UPLOAD == "local" && lib.configManager.getConfig('crowi', 'app:useInternalRedirect');
+  };
+
+  /**
+   * Respond to the HTTP request.
+   * @param {Response} res 
+   * @param {Response} attachment 
+   */
+  lib.respond = (res, attachment) => {
+    // Responce using internal redirect of nginx or Apache.
+    const dirName = (attachment.page != null) ? 'attachment' : 'user';
+    const internalPathRoot = lib.configManager.getConfig('crowi', 'app:internalRedirectPath');
+    const internalPath = urljoin(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();
+  };
+
   return lib;
 };

+ 15 - 0
src/server/service/file-uploader/uploader.js

@@ -54,6 +54,21 @@ class Uploader {
 
   }
 
+  /**
+   * Checks if Uploader can respond to the HTTP request.
+   */
+  canRespond() {
+    return false;
+  }
+
+  /**
+   * Respond to the HTTP request.
+   * @param {Response} res
+   * @param {Response} attachment
+   */
+  respond(res, attachment) {
+    throw new Error('Implement this');
+  }
 }
 
 module.exports = Uploader;