yusueketk 7 anni fa
parent
commit
424007cdd1

+ 1 - 1
config/env.dev.js

@@ -1,7 +1,7 @@
 module.exports = {
   NODE_ENV: 'development',
   FILE_UPLOAD: 'mongodb',
-  GRIDFS_LIMIT: Infinity,
+  MONGODB_GRIDFS_LIMIT: Infinity,
   // MATHJAX: 1,
   ELASTICSEARCH_URI: 'http://localhost:9200/growi',
   HACKMD_URI: 'http://localhost:3010',

+ 36 - 39
src/client/js/components/PageEditor.js

@@ -9,7 +9,6 @@ import { EditorOptions, PreviewOptions } from './PageEditor/OptionsSelector';
 import Editor from './PageEditor/Editor';
 import Preview from './PageEditor/Preview';
 import scrollSyncHelper from './PageEditor/ScrollSyncHelper';
-import { promise } from 'when';
 import * as toastr from 'toastr';
 
 
@@ -118,8 +117,8 @@ export default class PageEditor extends React.Component {
    * @param {any} files
    */
   async onUpload(file) {
-    const res  = await this.props.crowi.apiGet('/attachments.limit', {_csrf: this.props.crowi.csrfToken});
-    if (res.usableCapacity && file.size > res.usableCapacity) {
+    const res  = await this.props.crowi.apiGet('/attachments.limit', {_csrf: this.props.crowi.csrfToken, fileSize: file.size});
+    if (!res.isUploadable) {
       toastr.error(undefined, 'MongoDB for uploading files reaches limit', {
         closeButton: true,
         progressBar: true,
@@ -130,42 +129,40 @@ export default class PageEditor extends React.Component {
       });
       throw new Error('MongoDB for uploading files reaches limit');
     }
-    else {
-      const endpoint = '/attachments.add';
-
-      // create a FromData instance
-      const formData = new FormData();
-      formData.append('_csrf', this.props.crowi.csrfToken);
-      formData.append('file', file);
-      formData.append('path', this.props.pagePath);
-      formData.append('page_id', this.state.pageId || 0);
-
-      // post
-      this.props.crowi.apiPost(endpoint, formData)
-        .then((res) => {
-          const url = res.url;
-          const attachment = res.attachment;
-          const fileName = attachment.originalName;
-
-          let insertText = `[${fileName}](${url})`;
-          // when image
-          if (attachment.fileFormat.startsWith('image/')) {
-            // modify to "![fileName](url)" syntax
-            insertText = '!' + insertText;
-          }
-          this.refs.editor.insertText(insertText);
-
-          // when if created newly
-          if (res.pageCreated) {
-            // do nothing
-          }
-        })
-        .catch(this.apiErrorHandler)
-        // finally
-        .then(() => {
-          this.refs.editor.terminateUploadingState();
-        });
-    }
+    const endpoint = '/attachments.add';
+
+    // create a FromData instance
+    const formData = new FormData();
+    formData.append('_csrf', this.props.crowi.csrfToken);
+    formData.append('file', file);
+    formData.append('path', this.props.pagePath);
+    formData.append('page_id', this.state.pageId || 0);
+
+    // post
+    this.props.crowi.apiPost(endpoint, formData)
+      .then((res) => {
+        const url = res.url;
+        const attachment = res.attachment;
+        const fileName = attachment.originalName;
+
+        let insertText = `[${fileName}](${url})`;
+        // when image
+        if (attachment.fileFormat.startsWith('image/')) {
+          // modify to "![fileName](url)" syntax
+          insertText = '!' + insertText;
+        }
+        this.refs.editor.insertText(insertText);
+
+        // when if created newly
+        if (res.pageCreated) {
+          // do nothing
+        }
+      })
+      .catch(this.apiErrorHandler)
+      // finally
+      .then(() => {
+        this.refs.editor.terminateUploadingState();
+      });
   }
 
   /**

+ 1 - 1
src/server/models/attachment.js

@@ -35,7 +35,7 @@ module.exports = function(crowi) {
     //   2. ensure backward compatibility of data
 
     // return `/files/${this._id}`;
-     return fileUploader.generateUrl(this.filePath);
+    return fileUploader.generateUrl(this.filePath);
   });
 
   attachmentSchema.statics.findById = function(id) {

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

@@ -112,14 +112,8 @@ module.exports = function(crowi, app) {
    * @apiGroup Attachment
    */
   api.limit = async function(req, res) {
-    if (process.env.FILE_UPLOAD !== 'mongodb') {
-      return res.json(ApiResponse.success({usableCapacity: null}));
-    }
-    else {
-      const usingFilesSize = await fileUploader.getCollectionSize();
-      const usableCapacity = +process.env.GRIDFS_LIMIT - usingFilesSize;
-      return res.json(ApiResponse.success({usableCapacity: usableCapacity}));
-    }
+    const isUploadable = await fileUploader.checkCapacity(req.query.fileSize);
+    return res.json(ApiResponse.success({isUploadable: isUploadable}));
   };
 
   /**
@@ -139,12 +133,9 @@ module.exports = function(crowi, app) {
     debug('id and path are: ', id, path);
 
     var tmpFile = req.file || null;
-    if (process.env.FILE_UPLOAD == 'mongodb') {
-      const usingFilesSize = await fileUploader.getCollectionSize();
-      const usableCapacity = +process.env.GRIDFS_LIMIT - usingFilesSize;
-      if (tmpFile.size > usableCapacity) {
-        return res.json(ApiResponse.error('MongoDB for uploading files reaches limit'));
-      }
+    const isUploadable = await fileUploader.checkCapacity(tmpFile.size);
+    if (!isUploadable) {
+      return res.json(ApiResponse.error('MongoDB for uploading files reaches limit'));
     }
     debug('Uploaded tmpFile: ', tmpFile);
     if (!tmpFile) {

+ 7 - 0
src/server/service/file-uploader/aws.js

@@ -161,6 +161,13 @@ module.exports = function(crowi) {
     return false;
   };
 
+  /**
+   * chech storage for fileUpload reaches MONGODB_GRIDFS_LIMIT (for gridfs)
+   */
+  lib.checkCapacity = async(uploadFileSize) => {
+    return true;
+  };
+
   return lib;
 };
 

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

@@ -48,7 +48,7 @@ module.exports = function(crowi) {
   /**
    * get size of data uploaded files using (Promise wrapper)
    */
-  lib.getCollectionSize = () => {
+  const getCollectionSize = () => {
     return new Promise((resolve, reject) => {
       Chunks.collection.stats((err, data) => {
         if (err) {
@@ -59,6 +59,17 @@ module.exports = function(crowi) {
     });
   };
 
+  /**
+   * chech storage for fileUpload reaches MONGODB_GRIDFS_LIMIT (for gridfs)
+   */
+  lib.checkCapacity = async(uploadFileSize) => {
+    const usingFilesSize = await getCollectionSize();
+    if (+process.env.MONGODB_GRIDFS_LIMIT > usingFilesSize + +uploadFileSize) {
+      return true;
+    }
+    return false;
+  };
+
   lib.uploadFile = async function(filePath, contentType, fileStream, options) {
     debug('File uploading: ' + filePath);
     await writeFile(filePath, contentType, fileStream);

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

@@ -55,6 +55,13 @@ module.exports = function(crowi) {
     return Promise.resolve(lib.generateUrl(filePath));
   };
 
+  /**
+   * chech storage for fileUpload reaches MONGODB_GRIDFS_LIMIT (for gridfs)
+   */
+  lib.checkCapacity = async(uploadFileSize) => {
+    return true;
+  };
+
   return lib;
 };