mirror of
https://github.com/hexastack/hexabot
synced 2025-02-23 04:48:51 +00:00
Merge branch 'main' into 700-refactor-manage-persistent-menu-dialogs-add-edit-delete
This commit is contained in:
commit
5c7e180105
@ -8,7 +8,8 @@
|
|||||||
|
|
||||||
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 bodyParser from 'body-parser';
|
||||||
|
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';
|
||||||
@ -63,6 +64,20 @@ import { WEB_CHANNEL_NAME, WEB_CHANNEL_NAMESPACE } from './settings';
|
|||||||
import { Web } from './types';
|
import { Web } from './types';
|
||||||
import WebEventWrapper from './wrapper';
|
import WebEventWrapper from './wrapper';
|
||||||
|
|
||||||
|
// Handle multipart uploads (Long Pooling only)
|
||||||
|
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
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export default abstract class BaseWebChannelHandler<
|
export default abstract class BaseWebChannelHandler<
|
||||||
N extends ChannelName,
|
N extends ChannelName,
|
||||||
@ -616,49 +631,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 +1315,36 @@ export default abstract class BaseWebChannelHandler<
|
|||||||
return !!message;
|
return !!message;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Web channel middleware
|
||||||
|
*
|
||||||
|
* @param req Express Request
|
||||||
|
* @param res Express Response
|
||||||
|
* @param next Callback function
|
||||||
|
*/
|
||||||
|
async middleware(req: Request, res: Response, next: NextFunction) {
|
||||||
|
if (!this.isSocketRequest(req)) {
|
||||||
|
if (req.headers['content-type']?.includes('multipart/form-data')) {
|
||||||
|
// Handle multipart uploads (Long Pooling only)
|
||||||
|
return upload(req, res, next);
|
||||||
|
} else if (req.headers['content-type']?.includes('text/plain')) {
|
||||||
|
// Handle plain text payloads as JSON (retro-compability)
|
||||||
|
const textParser = bodyParser.text({ type: 'text/plain' });
|
||||||
|
|
||||||
|
return textParser(req, res, () => {
|
||||||
|
try {
|
||||||
|
req.body =
|
||||||
|
typeof req.body === 'string' ? JSON.parse(req.body) : req.body;
|
||||||
|
next();
|
||||||
|
} catch (err) {
|
||||||
|
next(err);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Do nothing
|
||||||
|
next();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user