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

Merge pull request #1254 from weseek/imprv/234-321-refactor

Imprv/234 321 refactor
Haruhiko Otani 6 лет назад
Родитель
Сommit
714fae63ba

+ 3 - 2
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(configManager);
+  const lib = new Uploader(crowi);
 
   function getAwsConfig() {
     return {
@@ -133,7 +133,8 @@ module.exports = function(crowi) {
    */
   lib.checkLimit = async(uploadFileSize) => {
     const maxFileSize = crowi.configManager.getConfig('crowi', 'app:maxFileSize');
-    return { isUploadable: uploadFileSize <= maxFileSize, errorMessage: 'File size exceeds the size limit per file' };
+    const totalLimit = crowi.configManager.getConfig('crowi', 'app:fileUploadTotalLimit');
+    return lib.doCheckLimit(uploadFileSize, maxFileSize, totalLimit);
   };
 
   return lib;

+ 2 - 17
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(configManager);
+  const lib = new Uploader(crowi);
 
   function getGcsBucket() {
     return configManager.getConfig('crowi', 'gcs:bucket');
@@ -101,23 +101,8 @@ 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');
-    if (usingFilesSize + uploadFileSize > gcsTotalLimit) {
-      return { isUploadable: false, errorMessage: 'GCS for uploading files reaches limit' };
-    }
-
-    return { isUploadable: true };
+    return lib.doCheckLimit(uploadFileSize, maxFileSize, gcsTotalLimit);
   };
 
   return lib;

+ 18 - 34
src/server/service/file-uploader/gridfs.js

@@ -4,9 +4,9 @@ const util = require('util');
 
 module.exports = function(crowi) {
   const Uploader = require('./uploader');
-  const lib = new Uploader(crowi.configManager);
+  const lib = new Uploader(crowi);
   const COLLECTION_NAME = 'attachmentFiles';
-  const CHUNK_COLLECTION_NAME = `${COLLECTION_NAME}.chunks`;
+  // const CHUNK_COLLECTION_NAME = `${COLLECTION_NAME}.chunks`;
 
   // instantiate mongoose-gridfs
   const { createModel } = require('mongoose-gridfs');
@@ -16,7 +16,7 @@ module.exports = function(crowi) {
     connection: mongoose.connection,
   });
   // get Collection instance of chunk
-  const chunkCollection = mongoose.connection.collection(CHUNK_COLLECTION_NAME);
+  // const chunkCollection = mongoose.connection.collection(CHUNK_COLLECTION_NAME);
 
   // create promisified method
   AttachmentFile.promisifiedWrite = util.promisify(AttachmentFile.write).bind(AttachmentFile);
@@ -46,20 +46,20 @@ module.exports = function(crowi) {
   /**
    * get size of data uploaded files using (Promise wrapper)
    */
-  const getCollectionSize = () => {
-    return new Promise((resolve, reject) => {
-      chunkCollection.stats((err, data) => {
-        if (err) {
-          // return 0 if not exist
-          if (err.errmsg.includes('not found')) {
-            return resolve(0);
-          }
-          return reject(err);
-        }
-        return resolve(data.size);
-      });
-    });
-  };
+  // const getCollectionSize = () => {
+  //   return new Promise((resolve, reject) => {
+  //     chunkCollection.stats((err, data) => {
+  //       if (err) {
+  //         // return 0 if not exist
+  //         if (err.errmsg.includes('not found')) {
+  //           return resolve(0);
+  //         }
+  //         return reject(err);
+  //       }
+  //       return resolve(data.size);
+  //     });
+  //   });
+  // };
 
   /**
    * check the file size limit
@@ -70,27 +70,11 @@ 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' };
-    }
-
-    let usingFilesSize;
-    try {
-      usingFilesSize = await getCollectionSize();
-    }
-    catch (err) {
-      logger.error(err);
-      return { isUploadable: false, errorMessage: err.errmsg };
-    }
 
     // Use app:fileUploadTotalLimit if gridfs:totalLimit is null (default for gridfs:totalLimitd is null)
     const gridfsTotalLimit = crowi.configManager.getConfig('crowi', 'gridfs:totalLimit')
       || crowi.configManager.getConfig('crowi', 'app:fileUploadTotalLimit');
-    if (usingFilesSize + uploadFileSize > gridfsTotalLimit) {
-      return { isUploadable: false, errorMessage: 'MongoDB for uploading files reaches limit' };
-    }
-
-    return { isUploadable: true };
+    return lib.doCheckLimit(uploadFileSize, maxFileSize, gridfsTotalLimit);
   };
 
   lib.uploadFile = async function(fileStream, attachment) {

+ 3 - 2
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.configManager);
+  const lib = new Uploader(crowi);
   const basePath = path.posix.join(crowi.publicDir, 'uploads');
 
   function getFilePathOnStorage(attachment) {
@@ -88,7 +88,8 @@ module.exports = function(crowi) {
    */
   lib.checkLimit = async(uploadFileSize) => {
     const maxFileSize = crowi.configManager.getConfig('crowi', 'app:maxFileSize');
-    return { isUploadable: uploadFileSize <= maxFileSize, errorMessage: 'File size exceeds the size limit per file' };
+    const totalLimit = crowi.configManager.getConfig('crowi', 'app:fileUploadTotalLimit');
+    return lib.doCheckLimit(uploadFileSize, maxFileSize, totalLimit);
   };
 
   return lib;

+ 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.configManager);
+  const lib = new Uploader(crowi);
 
   lib.getIsUploadable = function() {
     return false;

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

@@ -3,8 +3,9 @@
 
 class Uploader {
 
-  constructor(configManager) {
-    this.configManager = configManager;
+  constructor(crowi) {
+    this.crowi = crowi;
+    this.configManager = crowi.configManager;
   }
 
   getIsUploadable() {
@@ -19,6 +20,36 @@ class Uploader {
     return !!this.configManager.getConfig('crowi', 'app:fileUpload');
   }
 
+  /**
+   * Check files size limits for all uploaders
+   *
+   * @param {*} uploadFileSize
+   * @param {*} maxFileSize
+   * @param {*} totalLimit
+   * @returns
+   * @memberof Uploader
+   */
+  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;