mizozobu 6 лет назад
Родитель
Сommit
55ef72d3d1

+ 1 - 26
src/server/models/config.js

@@ -302,20 +302,6 @@ module.exports = function(crowi) {
   //     });
   // };
 
-  configSchema.statics.isUploadable = function(config) {
-    const method = process.env.FILE_UPLOAD || 'aws';
-
-    if (method === 'aws' && (
-      !config.crowi['aws:accessKeyId']
-        || !config.crowi['aws:secretAccessKey']
-        || !config.crowi['aws:region']
-        || !config.crowi['aws:bucket'])) {
-      return false;
-    }
-
-    return method !== 'none';
-  };
-
   configSchema.statics.isGuestAllowedToRead = function(config) {
     // return true if puclic wiki mode
     if (crowi.aclService.getIsPublicWikiOnly()) {
@@ -419,17 +405,6 @@ module.exports = function(crowi) {
     }
   };
 
-  configSchema.statics.fileUploadEnabled = function(config) {
-    const Config = this;
-
-    if (!Config.isUploadable(config)) {
-      return false;
-    }
-
-    // convert to boolean
-    return !!config.crowi['app:fileUpload'];
-  };
-
   configSchema.statics.hasSlackConfig = function(config) {
     return Config.hasSlackToken(config) || Config.hasSlackIwhUrl(config);
   };
@@ -469,7 +444,7 @@ module.exports = function(crowi) {
       },
       upload: {
         image: crowi.fileUploadService.getIsUploadable(),
-        file: crowi.configManager.getConfig('crowi', 'app:fileUpload'),
+        file: crowi.fileUploadService.getFileUploadEnabled(),
       },
       behaviorType: crowi.configManager.getConfig('crowi', 'customize:behavior'),
       layoutType: crowi.configManager.getConfig('crowi', 'customize:layout'),

+ 4 - 6
src/server/service/file-uploader/aws.js

@@ -19,12 +19,10 @@ module.exports = function(crowi) {
     };
   }
 
-  function S3Factory() {
+  function S3Factory(isUploadable) {
     const awsConfig = getAwsConfig();
-    const Config = crowi.model('Config');
-    const config = crowi.getConfig();
 
-    if (!Config.isUploadable(config)) {
+    if (!isUploadable) {
       throw new Error('AWS is not configured.');
     }
 
@@ -56,7 +54,7 @@ module.exports = function(crowi) {
   };
 
   lib.deleteFileByFilePath = async function(filePath) {
-    const s3 = S3Factory();
+    const s3 = S3Factory(this.getIsUploadable());
     const awsConfig = getAwsConfig();
 
     const params = {
@@ -70,7 +68,7 @@ module.exports = function(crowi) {
   lib.uploadFile = function(fileStream, attachment) {
     logger.debug(`File uploading: fileName=${attachment.fileName}`);
 
-    const s3 = S3Factory();
+    const s3 = S3Factory(this.getIsUploadable());
     const awsConfig = getAwsConfig();
 
     const filePath = getFilePathOnStorage(attachment);

+ 1 - 0
src/server/service/file-uploader/index.js

@@ -22,6 +22,7 @@ class FileUploaderFactory {
 }
 
 const factory = new FileUploaderFactory();
+
 module.exports = (crowi) => {
   return factory.getUploader(crowi);
 };

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

@@ -21,6 +21,14 @@ class Uploader {
     return method !== 'none';
   }
 
+  getFileUploadEnabled() {
+    if (!this.getIsUploadable()) {
+      return false;
+    }
+
+    return !!this.configManager.getConfig('crowi', 'app:fileUpload');
+  }
+
 }
 
 module.exports = Uploader;

+ 2 - 5
src/server/util/swigFunctions.js

@@ -9,6 +9,7 @@ module.exports = function(crowi, app, req, locals) {
     cdnResourcesService,
     passportService,
     appService,
+    fileUploadService,
   } = crowi;
   debug('initializing swigFunctions');
 
@@ -63,6 +64,7 @@ module.exports = function(crowi, app, req, locals) {
    * pass service class to swig
    */
   locals.appService = appService;
+  locals.fileUploadService = fileUploadService;
 
   locals.noCdn = function() {
     return !!process.env.NO_CDN;
@@ -245,11 +247,6 @@ module.exports = function(crowi, app, req, locals) {
     return configManager.getConfig('crowi', 'customize:isEnabledTimeline');
   };
 
-  locals.isUploadable = function() {
-    const config = crowi.getConfig();
-    return Config.isUploadable(config);
-  };
-
   locals.parentPath = function(path) {
     if (path === '/') {
       return path;

+ 1 - 1
src/server/views/admin/app.html

@@ -95,7 +95,7 @@
                      name="settingForm[app:fileUpload]"
                      value="1"
                      {% if settingForm['app:fileUpload'] %}checked{% endif %}
-                     {% if not isUploadable() %}disabled="disabled"{% endif %}>
+                     {% if not fileUploadService.getIsUploadable() %}disabled="disabled"{% endif %}>
               <label for="cbFileUpload">
                 {{ t("app_setting.enable_files_except_image") }}
               </label>

+ 1 - 1
src/server/views/me/index.html

@@ -172,7 +172,7 @@
             {{ t('Upload new image') }}
           </label>
           <div class="col-sm-8">
-            {% if isUploadable() %}
+            {% if fileUploadService.getIsUploadable() %}
             <form action="/_api/attachments.uploadProfileImage" id="pictureUploadForm" method="post" class="form-horizontal" role="form">
               <input type="hidden" name="_csrf" value="{{ csrf() }}">
               <input type="file" name="profileImage" accept="image/*">