|
@@ -1,3 +1,4 @@
|
|
|
|
|
+import type { ReadStream } from 'fs';
|
|
|
import { Readable } from 'stream';
|
|
import { Readable } from 'stream';
|
|
|
import util from 'util';
|
|
import util from 'util';
|
|
|
|
|
|
|
@@ -16,6 +17,17 @@ import { ContentHeaders } from './utils';
|
|
|
const logger = loggerFactory('growi:service:fileUploaderGridfs');
|
|
const logger = loggerFactory('growi:service:fileUploaderGridfs');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+const COLLECTION_NAME = 'attachmentFiles';
|
|
|
|
|
+const CHUNK_COLLECTION_NAME = `${COLLECTION_NAME}.chunks`;
|
|
|
|
|
+
|
|
|
|
|
+// instantiate mongoose-gridfs
|
|
|
|
|
+const AttachmentFile = createModel({
|
|
|
|
|
+ modelName: COLLECTION_NAME,
|
|
|
|
|
+ bucketName: COLLECTION_NAME,
|
|
|
|
|
+ connection: mongoose.connection,
|
|
|
|
|
+});
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
// TODO: rewrite this module to be a type-safe implementation
|
|
// TODO: rewrite this module to be a type-safe implementation
|
|
|
class GridfsFileUploader extends AbstractFileUploader {
|
|
class GridfsFileUploader extends AbstractFileUploader {
|
|
|
|
|
|
|
@@ -47,6 +59,24 @@ class GridfsFileUploader extends AbstractFileUploader {
|
|
|
throw new Error('Method not implemented.');
|
|
throw new Error('Method not implemented.');
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * @inheritdoc
|
|
|
|
|
+ */
|
|
|
|
|
+ override async uploadAttachment(readStream: ReadStream, attachment: IAttachmentDocument): Promise<void> {
|
|
|
|
|
+ logger.debug(`File uploading: fileName=${attachment.fileName}`);
|
|
|
|
|
+
|
|
|
|
|
+ const contentHeaders = new ContentHeaders(attachment);
|
|
|
|
|
+
|
|
|
|
|
+ return AttachmentFile.promisifiedWrite(
|
|
|
|
|
+ {
|
|
|
|
|
+ // put type and the file name for reference information when uploading
|
|
|
|
|
+ filename: attachment.fileName,
|
|
|
|
|
+ contentType: contentHeaders.contentType?.value.toString(),
|
|
|
|
|
+ },
|
|
|
|
|
+ readStream,
|
|
|
|
|
+ );
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* @inheritdoc
|
|
* @inheritdoc
|
|
|
*/
|
|
*/
|
|
@@ -73,15 +103,6 @@ class GridfsFileUploader extends AbstractFileUploader {
|
|
|
|
|
|
|
|
module.exports = function(crowi) {
|
|
module.exports = function(crowi) {
|
|
|
const lib = new GridfsFileUploader(crowi);
|
|
const lib = new GridfsFileUploader(crowi);
|
|
|
- const COLLECTION_NAME = 'attachmentFiles';
|
|
|
|
|
- const CHUNK_COLLECTION_NAME = `${COLLECTION_NAME}.chunks`;
|
|
|
|
|
-
|
|
|
|
|
- // instantiate mongoose-gridfs
|
|
|
|
|
- const AttachmentFile = createModel({
|
|
|
|
|
- modelName: COLLECTION_NAME,
|
|
|
|
|
- bucketName: COLLECTION_NAME,
|
|
|
|
|
- connection: mongoose.connection,
|
|
|
|
|
- });
|
|
|
|
|
|
|
|
|
|
// get Collection instance of chunk
|
|
// get Collection instance of chunk
|
|
|
const chunkCollection = mongoose.connection.collection(CHUNK_COLLECTION_NAME);
|
|
const chunkCollection = mongoose.connection.collection(CHUNK_COLLECTION_NAME);
|
|
@@ -150,21 +171,6 @@ module.exports = function(crowi) {
|
|
|
return lib.doCheckLimit(uploadFileSize, maxFileSize, totalLimit);
|
|
return lib.doCheckLimit(uploadFileSize, maxFileSize, totalLimit);
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
- (lib as any).uploadAttachment = async function(fileStream, attachment) {
|
|
|
|
|
- logger.debug(`File uploading: fileName=${attachment.fileName}`);
|
|
|
|
|
-
|
|
|
|
|
- const contentHeaders = new ContentHeaders(attachment);
|
|
|
|
|
-
|
|
|
|
|
- return AttachmentFile.promisifiedWrite(
|
|
|
|
|
- {
|
|
|
|
|
- // put type and the file name for reference information when uploading
|
|
|
|
|
- filename: attachment.fileName,
|
|
|
|
|
- contentType: contentHeaders.contentType?.value.toString(),
|
|
|
|
|
- },
|
|
|
|
|
- fileStream,
|
|
|
|
|
- );
|
|
|
|
|
- };
|
|
|
|
|
-
|
|
|
|
|
lib.saveFile = async function({ filePath, contentType, data }) {
|
|
lib.saveFile = async function({ filePath, contentType, data }) {
|
|
|
const readable = new Readable();
|
|
const readable = new Readable();
|
|
|
readable.push(data);
|
|
readable.push(data);
|