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

implement func to check the file size limit

utsushiiro 7 лет назад
Родитель
Сommit
bdf4db4586

+ 3 - 14
src/client/js/components/PageEditor.js

@@ -115,33 +115,22 @@ export default class PageEditor extends React.Component {
 
 
   /**
   /**
    * the upload event handler
    * the upload event handler
-   * @param {any} files
+   * @param {any} file
    */
    */
   async onUpload(file) {
   async onUpload(file) {
     try {
     try {
       let res = await this.props.crowi.apiGet('/attachments.limit', { _csrf: this.props.crowi.csrfToken, fileSize: file.size });
       let res = await this.props.crowi.apiGet('/attachments.limit', { _csrf: this.props.crowi.csrfToken, fileSize: file.size });
       if (!res.isUploadable) {
       if (!res.isUploadable) {
-        toastr.error(undefined, 'MongoDB for uploading files reaches limit', {
-          closeButton: true,
-          progressBar: true,
-          newestOnTop: false,
-          showDuration: '100',
-          hideDuration: '100',
-          timeOut: '5000',
-        });
-        throw new Error('MongoDB for uploading files reaches limit');
+        throw new Error(res.errorMessage);
       }
       }
-      const endpoint = '/attachments.add';
 
 
-      // create a FromData instance
       const formData = new FormData();
       const formData = new FormData();
       formData.append('_csrf', this.props.crowi.csrfToken);
       formData.append('_csrf', this.props.crowi.csrfToken);
       formData.append('file', file);
       formData.append('file', file);
       formData.append('path', this.props.pagePath);
       formData.append('path', this.props.pagePath);
       formData.append('page_id', this.state.pageId || 0);
       formData.append('page_id', this.state.pageId || 0);
 
 
-      // post
-      res = await this.props.crowi.apiPost(endpoint, formData);
+      res = await this.props.crowi.apiPost('/attachments.add', formData);
       const attachment = res.attachment;
       const attachment = res.attachment;
       const fileName = attachment.originalName;
       const fileName = attachment.originalName;
 
 

+ 5 - 6
src/server/routes/attachment.js

@@ -100,10 +100,10 @@ module.exports = function(crowi, app) {
   }
   }
 
 
   async function createAttachment(file, user, pageId = null) {
   async function createAttachment(file, user, pageId = null) {
-    // check capacity
-    const isUploadable = await fileUploader.checkCapacity(file.size);
-    if (!isUploadable) {
-      throw new Error('File storage reaches limit');
+    // check limit
+    const res = await fileUploader.checkLimit(file.size);
+    if (!res.isUploadable) {
+      throw new Error(res.errorMessage);
     }
     }
 
 
     const fileStream = fs.createReadStream(file.path, {
     const fileStream = fs.createReadStream(file.path, {
@@ -204,8 +204,7 @@ module.exports = function(crowi, app) {
    * @apiGroup Attachment
    * @apiGroup Attachment
    */
    */
   api.limit = async function(req, res) {
   api.limit = async function(req, res) {
-    const isUploadable = await fileUploader.checkCapacity(req.query.fileSize);
-    return res.json(ApiResponse.success({ isUploadable }));
+    return res.json(ApiResponse.success(await fileUploader.checkLimit(req.query.fileSize)));
   };
   };
 
 
   /**
   /**

+ 3 - 2
src/server/service/file-uploader/aws.js

@@ -111,8 +111,9 @@ module.exports = function(crowi) {
   /**
   /**
    * chech storage for fileUpload reaches MONGO_GRIDFS_TOTAL_LIMIT (for gridfs)
    * chech storage for fileUpload reaches MONGO_GRIDFS_TOTAL_LIMIT (for gridfs)
    */
    */
-  lib.checkCapacity = async(uploadFileSize) => {
-    return true;
+  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' };
   };
   };
 
 
   return lib;
   return lib;

+ 12 - 3
src/server/service/file-uploader/gridfs.js

@@ -52,14 +52,23 @@ module.exports = function(crowi) {
   /**
   /**
    * chech storage for fileUpload reaches MONGO_GRIDFS_TOTAL_LIMIT (for gridfs)
    * chech storage for fileUpload reaches MONGO_GRIDFS_TOTAL_LIMIT (for gridfs)
    */
    */
-  lib.checkCapacity = async(uploadFileSize) => {
+  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' };
+    }
+
     // skip checking if env var is undefined
     // skip checking if env var is undefined
     if (process.env.MONGO_GRIDFS_TOTAL_LIMIT == null) {
     if (process.env.MONGO_GRIDFS_TOTAL_LIMIT == null) {
-      return true;
+      return { isUploadable: true };
     }
     }
 
 
     const usingFilesSize = await getCollectionSize();
     const usingFilesSize = await getCollectionSize();
-    return (+process.env.MONGO_GRIDFS_TOTAL_LIMIT > usingFilesSize + +uploadFileSize);
+    if (+process.env.MONGO_GRIDFS_TOTAL_LIMIT > usingFilesSize + +uploadFileSize) {
+      return { isUploadable: false, errorMessage: 'MongoDB for uploading files reaches limit' };
+    }
+
+    return { isUploadable: true };
   };
   };
 
 
   lib.uploadFile = async function(fileStream, attachment) {
   lib.uploadFile = async function(fileStream, attachment) {

+ 3 - 2
src/server/service/file-uploader/local.js

@@ -70,8 +70,9 @@ module.exports = function(crowi) {
   /**
   /**
    * chech storage for fileUpload reaches MONGO_GRIDFS_TOTAL_LIMIT (for gridfs)
    * chech storage for fileUpload reaches MONGO_GRIDFS_TOTAL_LIMIT (for gridfs)
    */
    */
-  lib.checkCapacity = async(uploadFileSize) => {
-    return true;
+  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' };
   };
   };
 
 
   return lib;
   return lib;