mirror of
https://github.com/hexastack/hexabot
synced 2025-06-04 03:26:22 +00:00
fix: use Multer File type
This commit is contained in:
parent
c5520c96b3
commit
79dcaadce8
@ -191,11 +191,11 @@ export class AttachmentService extends BaseService<Attachment> {
|
||||
* Uploads files to the server. If a storage plugin is configured it uploads files accordingly.
|
||||
* Otherwise, uploads files to the local directory.
|
||||
*
|
||||
* @param file - The file path or the Buffer / Readable
|
||||
* @param file - The file
|
||||
* @returns A promise that resolves to an array of uploaded attachments.
|
||||
*/
|
||||
async store(
|
||||
file: Buffer | Readable | string,
|
||||
file: Buffer | Readable | Express.Multer.File,
|
||||
metadata: AttachmentMetadataDto,
|
||||
): Promise<Attachment> {
|
||||
if (this.getStoragePlugin()) {
|
||||
@ -210,12 +210,7 @@ export class AttachmentService extends BaseService<Attachment> {
|
||||
throw new Error('Invalid file path');
|
||||
}
|
||||
|
||||
if (typeof file === 'string') {
|
||||
// For example, if the file is an instance of `Express.Multer.File` (diskStorage case)
|
||||
const srcFilePath = path.resolve(file);
|
||||
await fsPromises.copyFile(srcFilePath, filePath);
|
||||
await fsPromises.unlink(srcFilePath);
|
||||
} else if (Buffer.isBuffer(file)) {
|
||||
if (Buffer.isBuffer(file)) {
|
||||
await fsPromises.writeFile(filePath, file);
|
||||
} else if (file instanceof Readable) {
|
||||
await new Promise((resolve, reject) => {
|
||||
@ -225,7 +220,14 @@ export class AttachmentService extends BaseService<Attachment> {
|
||||
writeStream.on('error', reject);
|
||||
});
|
||||
} else {
|
||||
throw new TypeError('Unrecognized file object');
|
||||
if (file.path) {
|
||||
// For example, if the file is an instance of `Express.Multer.File` (diskStorage case)
|
||||
const srcFilePath = path.resolve(file.path);
|
||||
await fsPromises.copyFile(srcFilePath, filePath);
|
||||
await fsPromises.unlink(srcFilePath);
|
||||
} else {
|
||||
await fsPromises.writeFile(filePath, file.buffer);
|
||||
}
|
||||
}
|
||||
|
||||
const location = filePath.replace(dirPath, '');
|
||||
|
@ -673,16 +673,11 @@ export default abstract class BaseWebChannelHandler<
|
||||
|
||||
try {
|
||||
const file = req.file;
|
||||
const attachment = await this.attachmentService.store(
|
||||
config.parameters.storageMode === 'memory'
|
||||
? file.buffer
|
||||
: file.path,
|
||||
{
|
||||
name: file.originalname,
|
||||
size: file.size,
|
||||
type: file.mimetype,
|
||||
},
|
||||
);
|
||||
const attachment = await this.attachmentService.store(file, {
|
||||
name: file.originalname,
|
||||
size: file.size,
|
||||
type: file.mimetype,
|
||||
});
|
||||
next(null, {
|
||||
type: Attachment.getTypeByMime(attachment.type),
|
||||
url: Attachment.getAttachmentUrl(attachment.id, attachment.name),
|
||||
|
@ -41,7 +41,7 @@ export abstract class BaseStoragePlugin extends BasePlugin {
|
||||
readAsBuffer?(attachment: Attachment): Promise<Buffer>;
|
||||
|
||||
store?(
|
||||
file: Buffer | Readable | string,
|
||||
file: Buffer | Readable | Express.Multer.File,
|
||||
metadata: AttachmentMetadataDto,
|
||||
): Promise<Attachment>;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user