|
|
@@ -1,47 +1,53 @@
|
|
|
const logger = require('@alias/logger')('growi:service:fileUploaderGridfs');
|
|
|
const mongoose = require('mongoose');
|
|
|
+const { createModel } = require('mongoose-gridfs');
|
|
|
const util = require('util');
|
|
|
+const Uploader = require('./uploader');
|
|
|
|
|
|
-module.exports = function(crowi) {
|
|
|
- const Uploader = require('./uploader');
|
|
|
- const lib = new Uploader(crowi);
|
|
|
- const COLLECTION_NAME = 'attachmentFiles';
|
|
|
- // const CHUNK_COLLECTION_NAME = `${COLLECTION_NAME}.chunks`;
|
|
|
-
|
|
|
- // instantiate mongoose-gridfs
|
|
|
- const { createModel } = require('mongoose-gridfs');
|
|
|
- const AttachmentFile = createModel({
|
|
|
- modelName: COLLECTION_NAME,
|
|
|
- bucketName: COLLECTION_NAME,
|
|
|
- connection: mongoose.connection,
|
|
|
- });
|
|
|
- // get Collection instance of chunk
|
|
|
- // const chunkCollection = mongoose.connection.collection(CHUNK_COLLECTION_NAME);
|
|
|
-
|
|
|
- // create promisified method
|
|
|
- AttachmentFile.promisifiedWrite = util.promisify(AttachmentFile.write).bind(AttachmentFile);
|
|
|
- AttachmentFile.promisifiedUnlink = util.promisify(AttachmentFile.unlink).bind(AttachmentFile);
|
|
|
-
|
|
|
- lib.isValidUploadSettings = function() {
|
|
|
+const COLLECTION_NAME = 'attachmentFiles';
|
|
|
+
|
|
|
+class Gridfs extends Uploader {
|
|
|
+
|
|
|
+ constructor(crowi) {
|
|
|
+ super(crowi);
|
|
|
+
|
|
|
+ this.initialize();
|
|
|
+ }
|
|
|
+
|
|
|
+ initialize() {
|
|
|
+ this.AttachmentFile = createModel({
|
|
|
+ modelName: COLLECTION_NAME,
|
|
|
+ bucketName: COLLECTION_NAME,
|
|
|
+ connection: mongoose.connection,
|
|
|
+ });
|
|
|
+ // get Collection instance of chunk
|
|
|
+ // const chunkCollection = mongoose.connection.collection(CHUNK_COLLECTION_NAME);
|
|
|
+
|
|
|
+ // create promisified method
|
|
|
+ this.AttachmentFile.promisifiedWrite = util.promisify(this.AttachmentFile.write).bind(this.AttachmentFile);
|
|
|
+ this.AttachmentFile.promisifiedUnlink = util.promisify(this.AttachmentFile.unlink).bind(this.AttachmentFile);
|
|
|
+ }
|
|
|
+
|
|
|
+ getisValidUploadSettings() {
|
|
|
return true;
|
|
|
- };
|
|
|
+ }
|
|
|
|
|
|
- lib.deleteFile = async function(attachment) {
|
|
|
+ async deleteFile(attachment) {
|
|
|
let filenameValue = attachment.fileName;
|
|
|
|
|
|
if (attachment.filePath != null) { // backward compatibility for v3.3.x or below
|
|
|
filenameValue = attachment.filePath;
|
|
|
}
|
|
|
|
|
|
- const attachmentFile = await AttachmentFile.findOne({ filename: filenameValue });
|
|
|
+ const attachmentFile = await this.AttachmentFile.findOne({ filename: filenameValue });
|
|
|
|
|
|
if (attachmentFile == null) {
|
|
|
logger.warn(`Any AttachmentFile that relate to the Attachment (${attachment._id.toString()}) does not exist in GridFS`);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- return AttachmentFile.promisifiedUnlink({ _id: attachmentFile._id });
|
|
|
- };
|
|
|
+ return this.AttachmentFile.promisifiedUnlink({ _id: attachmentFile._id });
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* get size of data uploaded files using (Promise wrapper)
|
|
|
@@ -68,26 +74,27 @@ module.exports = function(crowi) {
|
|
|
* - per-file size limit (specified by MAX_FILE_SIZE)
|
|
|
* - mongodb(gridfs) size limit (specified by MONGO_GRIDFS_TOTAL_LIMIT)
|
|
|
*/
|
|
|
- lib.checkLimit = async(uploadFileSize) => {
|
|
|
- const maxFileSize = crowi.configManager.getConfig('crowi', 'app:maxFileSize');
|
|
|
+ async checkLimit(uploadFileSize) {
|
|
|
+ const maxFileSize = this.crowi.configManager.getConfig('crowi', 'app:maxFileSize');
|
|
|
|
|
|
// 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);
|
|
|
- };
|
|
|
+ const gridfsTotalLimit = this.crowi.configManager.getConfig('crowi', 'gridfs:totalLimit')
|
|
|
+ || this.crowi.configManager.getConfig('crowi', 'app:fileUploadTotalLimit');
|
|
|
+ return this.doCheckLimit(uploadFileSize, maxFileSize, gridfsTotalLimit);
|
|
|
+ }
|
|
|
|
|
|
- lib.uploadFile = async function(fileStream, attachment) {
|
|
|
+ async uploadFile(fileStream, attachment) {
|
|
|
logger.debug(`File uploading: fileName=${attachment.fileName}`);
|
|
|
|
|
|
- return AttachmentFile.promisifiedWrite(
|
|
|
+ return this.AttachmentFile.promisifiedWrite(
|
|
|
{
|
|
|
filename: attachment.fileName,
|
|
|
contentType: attachment.fileFormat,
|
|
|
},
|
|
|
fileStream,
|
|
|
);
|
|
|
- };
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
* Find data substance
|
|
|
@@ -95,22 +102,23 @@ module.exports = function(crowi) {
|
|
|
* @param {Attachment} attachment
|
|
|
* @return {stream.Readable} readable stream
|
|
|
*/
|
|
|
- lib.findDeliveryFile = async function(attachment) {
|
|
|
+ async findDeliveryFile(attachment) {
|
|
|
let filenameValue = attachment.fileName;
|
|
|
|
|
|
if (attachment.filePath != null) { // backward compatibility for v3.3.x or below
|
|
|
filenameValue = attachment.filePath;
|
|
|
}
|
|
|
|
|
|
- const attachmentFile = await AttachmentFile.findOne({ filename: filenameValue });
|
|
|
+ const attachmentFile = await this.AttachmentFile.findOne({ filename: filenameValue });
|
|
|
|
|
|
if (attachmentFile == null) {
|
|
|
throw new Error(`Any AttachmentFile that relate to the Attachment (${attachment._id.toString()}) does not exist in GridFS`);
|
|
|
}
|
|
|
|
|
|
// return stream.Readable
|
|
|
- return AttachmentFile.read({ _id: attachmentFile._id });
|
|
|
- };
|
|
|
+ return this.AttachmentFile.read({ _id: attachmentFile._id });
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
|
|
|
- return lib;
|
|
|
-};
|
|
|
+module.exports = Gridfs;
|