|
|
@@ -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,11 +70,27 @@ 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');
|
|
|
- return lib.doCheckLimit(uploadFileSize, maxFileSize, gridfsTotalLimit);
|
|
|
+ if (usingFilesSize + uploadFileSize > gridfsTotalLimit) {
|
|
|
+ return { isUploadable: false, errorMessage: 'MongoDB for uploading files reaches limit' };
|
|
|
+ }
|
|
|
+
|
|
|
+ return { isUploadable: true };
|
|
|
};
|
|
|
|
|
|
lib.uploadFile = async function(fileStream, attachment) {
|