mirror of
https://github.com/hexastack/hexabot
synced 2025-02-23 04:48:51 +00:00
fix: handle multer upload in middleware (long pooling only)
This commit is contained in:
parent
1288eb87cf
commit
e8f031da02
@ -8,7 +8,7 @@
|
|||||||
|
|
||||||
import { BadRequestException, Injectable } from '@nestjs/common';
|
import { BadRequestException, Injectable } from '@nestjs/common';
|
||||||
import { EventEmitter2, OnEvent } from '@nestjs/event-emitter';
|
import { EventEmitter2, OnEvent } from '@nestjs/event-emitter';
|
||||||
import { Request, Response } from 'express';
|
import { NextFunction, Request, Response } from 'express';
|
||||||
import multer, { diskStorage, memoryStorage } from 'multer';
|
import multer, { diskStorage, memoryStorage } from 'multer';
|
||||||
import { Socket } from 'socket.io';
|
import { Socket } from 'socket.io';
|
||||||
import { v4 as uuidv4 } from 'uuid';
|
import { v4 as uuidv4 } from 'uuid';
|
||||||
@ -616,49 +616,19 @@ export default abstract class BaseWebChannelHandler<
|
|||||||
*/
|
*/
|
||||||
async handleWebUpload(
|
async handleWebUpload(
|
||||||
req: Request,
|
req: Request,
|
||||||
res: Response,
|
_res: Response,
|
||||||
): Promise<Attachment | null | undefined> {
|
): Promise<Attachment | null | undefined> {
|
||||||
try {
|
try {
|
||||||
const upload = multer({
|
|
||||||
limits: {
|
|
||||||
fileSize: config.parameters.maxUploadSize,
|
|
||||||
},
|
|
||||||
storage: (() => {
|
|
||||||
if (config.parameters.storageMode === 'memory') {
|
|
||||||
return memoryStorage();
|
|
||||||
} else {
|
|
||||||
return diskStorage({});
|
|
||||||
}
|
|
||||||
})(),
|
|
||||||
}).single('file'); // 'file' is the field name in the form
|
|
||||||
|
|
||||||
const multerUpload = new Promise<Express.Multer.File | null>(
|
|
||||||
(resolve, reject) => {
|
|
||||||
upload(req as Request, res as Response, async (err?: any) => {
|
|
||||||
if (err) {
|
|
||||||
this.logger.error('Unable to store uploaded file', err);
|
|
||||||
reject(new Error('Unable to upload file!'));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (req.file) {
|
|
||||||
resolve(req.file);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
const file = await multerUpload;
|
|
||||||
|
|
||||||
// Check if any file is provided
|
// Check if any file is provided
|
||||||
if (!file) {
|
if (!req.file) {
|
||||||
this.logger.debug('No files provided');
|
this.logger.debug('No files provided');
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return await this.attachmentService.store(file, {
|
return await this.attachmentService.store(req.file, {
|
||||||
name: file.originalname,
|
name: req.file.originalname,
|
||||||
size: file.size,
|
size: req.file.size,
|
||||||
type: file.mimetype,
|
type: req.file.mimetype,
|
||||||
resourceRef: AttachmentResourceRef.MessageAttachment,
|
resourceRef: AttachmentResourceRef.MessageAttachment,
|
||||||
access: AttachmentAccess.Private,
|
access: AttachmentAccess.Private,
|
||||||
createdByRef: AttachmentCreatedByRef.Subscriber,
|
createdByRef: AttachmentCreatedByRef.Subscriber,
|
||||||
@ -1330,4 +1300,35 @@ export default abstract class BaseWebChannelHandler<
|
|||||||
return !!message;
|
return !!message;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Web channel middleware to handle multipart uploads (Long Pooling only)
|
||||||
|
* @param req Express Request
|
||||||
|
* @param res Express Response
|
||||||
|
* @param next Callback function
|
||||||
|
*/
|
||||||
|
async middleware(req: Request, res: Response, next: NextFunction) {
|
||||||
|
if (
|
||||||
|
!this.isSocketRequest(req) &&
|
||||||
|
req.headers['content-type']?.includes('multipart/form-data')
|
||||||
|
) {
|
||||||
|
const upload = multer({
|
||||||
|
limits: {
|
||||||
|
fileSize: config.parameters.maxUploadSize,
|
||||||
|
},
|
||||||
|
storage: (() => {
|
||||||
|
if (config.parameters.storageMode === 'memory') {
|
||||||
|
return memoryStorage();
|
||||||
|
} else {
|
||||||
|
return diskStorage({});
|
||||||
|
}
|
||||||
|
})(),
|
||||||
|
}).single('file'); // 'file' is the field name in the form
|
||||||
|
|
||||||
|
upload(req, res, next);
|
||||||
|
} else {
|
||||||
|
// Do nothing
|
||||||
|
next();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user