Przeglądaj źródła

change how to get using storage for uploaded files with GridFS

yusueketk 7 lat temu
rodzic
commit
afa6b5db20

+ 1 - 1
config/env.dev.js

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

+ 1 - 1
src/client/js/components/PageEditor.js

@@ -118,7 +118,7 @@ export default class PageEditor extends React.Component {
   async onUpload(file) {
   async onUpload(file) {
     const res  = await this.props.crowi.apiGet('/attachments.limit', {_csrf: this.props.crowi.csrfToken});
     const res  = await this.props.crowi.apiGet('/attachments.limit', {_csrf: this.props.crowi.csrfToken});
     if (file.size > res.usableCapacity) {
     if (file.size > res.usableCapacity) {
-      throw new Error('mongoDB for file uploading reaches the limit');
+      throw new Error('mongoDB for file uploading reaches limit');
     }
     }
     else {
     else {
       const endpoint = '/attachments.add';
       const endpoint = '/attachments.add';

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

@@ -139,16 +139,17 @@ module.exports = function(crowi, app) {
     debug('id and path are: ', id, path);
     debug('id and path are: ', id, path);
 
 
     var tmpFile = req.file || null;
     var tmpFile = req.file || null;
-    const usingFilesSize = await fileUploader.getCollectionSize();
-    const usableCapacity = +process.env.GRIDFS_LIMIT - usingFilesSize;
-    if (tmpFile.size > usableCapacity) {
-      throw new Error('mongoDB for file uploading reaches limit');
+    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 file uploading reaches limit'));
+      }
     }
     }
     debug('Uploaded tmpFile: ', tmpFile);
     debug('Uploaded tmpFile: ', tmpFile);
     if (!tmpFile) {
     if (!tmpFile) {
       return res.json(ApiResponse.error('File error.'));
       return res.json(ApiResponse.error('File error.'));
     }
     }
-
     new Promise(function(resolve, reject) {
     new Promise(function(resolve, reject) {
       if (id == 0) {
       if (id == 0) {
         if (path === null) {
         if (path === null) {

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

@@ -18,6 +18,7 @@ module.exports = function(crowi) {
 
 
   // obtain a model
   // obtain a model
   const AttachmentFile = gridfs.model;
   const AttachmentFile = gridfs.model;
+  const Chunks = mongoose.model('Chunks', gridfs.schema, 'attachmentFiles.chunks');
 
 
   // delete a file
   // delete a file
   lib.deleteFile = async function(fileId, filePath) {
   lib.deleteFile = async function(fileId, filePath) {
@@ -49,15 +50,8 @@ module.exports = function(crowi) {
    */
    */
   lib.getCollectionSize = () => {
   lib.getCollectionSize = () => {
     return new Promise((resolve, reject) => {
     return new Promise((resolve, reject) => {
-      AttachmentFile.find((e, files) => {
-        let data = 0;
-        files.forEach((file) => {
-          data += file.length;
-        });
-        if (e) {
-          reject(e);
-        }
-        resolve(data);
+      Chunks.collection.stats((e, data) => {
+        resolve(data.size);
       });
       });
     });
     });
   };
   };