fix: use Multer File type

This commit is contained in:
Mohamed Marrouchi 2024-12-29 10:27:20 +01:00
parent c5520c96b3
commit 79dcaadce8
3 changed files with 17 additions and 20 deletions

View File

@ -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. * Uploads files to the server. If a storage plugin is configured it uploads files accordingly.
* Otherwise, uploads files to the local directory. * 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. * @returns A promise that resolves to an array of uploaded attachments.
*/ */
async store( async store(
file: Buffer | Readable | string, file: Buffer | Readable | Express.Multer.File,
metadata: AttachmentMetadataDto, metadata: AttachmentMetadataDto,
): Promise<Attachment> { ): Promise<Attachment> {
if (this.getStoragePlugin()) { if (this.getStoragePlugin()) {
@ -210,12 +210,7 @@ export class AttachmentService extends BaseService<Attachment> {
throw new Error('Invalid file path'); throw new Error('Invalid file path');
} }
if (typeof file === 'string') { if (Buffer.isBuffer(file)) {
// 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)) {
await fsPromises.writeFile(filePath, file); await fsPromises.writeFile(filePath, file);
} else if (file instanceof Readable) { } else if (file instanceof Readable) {
await new Promise((resolve, reject) => { await new Promise((resolve, reject) => {
@ -225,7 +220,14 @@ export class AttachmentService extends BaseService<Attachment> {
writeStream.on('error', reject); writeStream.on('error', reject);
}); });
} else { } 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, ''); const location = filePath.replace(dirPath, '');

View File

@ -673,16 +673,11 @@ export default abstract class BaseWebChannelHandler<
try { try {
const file = req.file; const file = req.file;
const attachment = await this.attachmentService.store( const attachment = await this.attachmentService.store(file, {
config.parameters.storageMode === 'memory' name: file.originalname,
? file.buffer size: file.size,
: file.path, type: file.mimetype,
{ });
name: file.originalname,
size: file.size,
type: file.mimetype,
},
);
next(null, { next(null, {
type: Attachment.getTypeByMime(attachment.type), type: Attachment.getTypeByMime(attachment.type),
url: Attachment.getAttachmentUrl(attachment.id, attachment.name), url: Attachment.getAttachmentUrl(attachment.id, attachment.name),

View File

@ -41,7 +41,7 @@ export abstract class BaseStoragePlugin extends BasePlugin {
readAsBuffer?(attachment: Attachment): Promise<Buffer>; readAsBuffer?(attachment: Attachment): Promise<Buffer>;
store?( store?(
file: Buffer | Readable | string, file: Buffer | Readable | Express.Multer.File,
metadata: AttachmentMetadataDto, metadata: AttachmentMetadataDto,
): Promise<Attachment>; ): Promise<Attachment>;
} }