Browse Source

Revert "WIP"

This reverts commit 2e217a19095372e50bf3ec7ccb6eb491d092d399.
Otani Haruhiko 6 years ago
parent
commit
77b1fceaaf

+ 1 - 1
src/server/service/file-uploader/aws.js

@@ -6,7 +6,7 @@ const aws = require('aws-sdk');
 module.exports = function(crowi) {
   const Uploader = require('./uploader');
   const { configManager } = crowi;
-  const lib = new Uploader(crowi);
+  const lib = new Uploader(configManager);
 
   function getAwsConfig() {
     return {

+ 17 - 2
src/server/service/file-uploader/gcs.js

@@ -9,7 +9,7 @@ let _instance;
 module.exports = function(crowi) {
   const Uploader = require('./uploader');
   const { configManager } = crowi;
-  const lib = new Uploader(crowi);
+  const lib = new Uploader(configManager);
 
   function getGcsBucket() {
     return configManager.getConfig('crowi', 'gcs:bucket');
@@ -101,8 +101,23 @@ module.exports = function(crowi) {
    */
   lib.checkLimit = async(uploadFileSize) => {
     const maxFileSize = crowi.configManager.getConfig('crowi', 'app:maxFileSize');
+    if (uploadFileSize > maxFileSize) {
+      return { isUploadable: false, errorMessage: 'File size exceeds the size limit per file' };
+    }
+    const Attachment = crowi.model('Attachment');
+    // Get attachment total file size
+    const res = await Attachment.aggregate().group({
+      _id: null,
+      total: { $sum: '$fileSize' },
+    });
+    const usingFilesSize = res[0].total;
+
     const gcsTotalLimit = crowi.configManager.getConfig('crowi', 'app:fileUploadTotalLimit');
-    return lib.doCheckLimit(uploadFileSize, maxFileSize, gcsTotalLimit);
+    if (usingFilesSize + uploadFileSize > gcsTotalLimit) {
+      return { isUploadable: false, errorMessage: 'GCS for uploading files reaches limit' };
+    }
+
+    return { isUploadable: true };
   };
 
   return lib;

+ 1 - 1
src/server/service/file-uploader/gridfs.js

@@ -4,7 +4,7 @@ const util = require('util');
 
 module.exports = function(crowi) {
   const Uploader = require('./uploader');
-  const lib = new Uploader(crowi);
+  const lib = new Uploader(crowi.configManager);
   const COLLECTION_NAME = 'attachmentFiles';
   const CHUNK_COLLECTION_NAME = `${COLLECTION_NAME}.chunks`;
 

+ 1 - 1
src/server/service/file-uploader/local.js

@@ -7,7 +7,7 @@ const streamToPromise = require('stream-to-promise');
 
 module.exports = function(crowi) {
   const Uploader = require('./uploader');
-  const lib = new Uploader(crowi);
+  const lib = new Uploader(crowi.configManager);
   const basePath = path.posix.join(crowi.publicDir, 'uploads');
 
   function getFilePathOnStorage(attachment) {

+ 1 - 1
src/server/service/file-uploader/none.js

@@ -3,7 +3,7 @@
 module.exports = function(crowi) {
   const debug = require('debug')('growi:service:fileUploaderNone');
   const Uploader = require('./uploader');
-  const lib = new Uploader(crowi);
+  const lib = new Uploader(crowi.configManager);
 
   lib.getIsUploadable = function() {
     return false;

+ 2 - 24
src/server/service/file-uploader/uploader.js

@@ -3,9 +3,8 @@
 
 class Uploader {
 
-  constructor(crowi) {
-    this.crowi = crowi;
-    this.configManager = crowi.configManager;
+  constructor(configManager) {
+    this.configManager = configManager;
   }
 
   getIsUploadable() {
@@ -20,27 +19,6 @@ class Uploader {
     return !!this.configManager.getConfig('crowi', 'app:fileUpload');
   }
 
-  async doCheckLimit(uploadFileSize, maxFileSize, totalLimit) {
-    if (uploadFileSize > maxFileSize) {
-      return { isUploadable: false, errorMessage: 'File size exceeds the size limit per file' };
-    }
-    const Attachment = this.crowi.model('Attachment');
-    // Get attachment total file size
-    const res = await Attachment.aggregate().group({
-      _id: null,
-      total: { $sum: '$fileSize' },
-    });
-    // Return res is [] if not using
-    const usingFilesSize = res.length === 0 ? 0 : res[0].total;
-
-    if (usingFilesSize + uploadFileSize > totalLimit) {
-      return { isUploadable: false, errorMessage: 'Uploading files reaches limit' };
-    }
-
-    return { isUploadable: true };
-
-  }
-
 }
 
 module.exports = Uploader;