From aa96dba18d894e6d5b8574dcea18516caf31ebaf Mon Sep 17 00:00:00 2001 From: yassinedorbozgithub Date: Mon, 9 Dec 2024 17:16:27 +0100 Subject: [PATCH 1/2] refactor: attachment service uploadFiles method --- .../attachment/services/attachment.service.ts | 42 +++++++++---------- 1 file changed, 19 insertions(+), 23 deletions(-) diff --git a/api/src/attachment/services/attachment.service.ts b/api/src/attachment/services/attachment.service.ts index 3f6077ce2..7d34a288b 100644 --- a/api/src/attachment/services/attachment.service.ts +++ b/api/src/attachment/services/attachment.service.ts @@ -148,34 +148,30 @@ export class AttachmentService extends BaseService { * @returns A promise that resolves to an array of uploaded attachments. */ async uploadFiles(files: { file: Express.Multer.File[] }) { + const uploadedFiles: Attachment[] = []; + if (this.getStoragePlugin()) { - const dtos = await Promise.all( - files.file.map((file) => { - return this.getStoragePlugin().upload(file); - }), - ); - const uploadedFiles = await Promise.all( - dtos.map((dto) => { - return this.create(dto); - }), - ); - return uploadedFiles; + for (const file of files?.file) { + const dto = await this.getStoragePlugin().upload(file); + const uploadedFile = await this.create(dto); + uploadedFiles.push(uploadedFile); + } } else { if (Array.isArray(files?.file)) { - const uploadedFiles = await Promise.all( - files?.file?.map(async ({ size, filename, mimetype }) => { - return await this.create({ - size, - type: mimetype, - name: filename, - channel: {}, - location: `/${filename}`, - }); - }), - ); - return uploadedFiles; + for (const { size, mimetype, filename } of files?.file) { + const uploadedFile = await this.repository.create({ + size, + type: mimetype, + name: filename, + channel: {}, + location: `/${filename}`, + }); + uploadedFiles.push(uploadedFile); + } } } + + return uploadedFiles; } /** From ecd6c43c82703791c4c57fb2893ac984db506ba2 Mon Sep 17 00:00:00 2001 From: yassinedorbozgithub Date: Mon, 9 Dec 2024 17:57:26 +0100 Subject: [PATCH 2/2] fix: uploadFiles logic --- api/src/attachment/services/attachment.service.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/src/attachment/services/attachment.service.ts b/api/src/attachment/services/attachment.service.ts index 7d34a288b..6a50e6f3a 100644 --- a/api/src/attachment/services/attachment.service.ts +++ b/api/src/attachment/services/attachment.service.ts @@ -159,7 +159,7 @@ export class AttachmentService extends BaseService { } else { if (Array.isArray(files?.file)) { for (const { size, mimetype, filename } of files?.file) { - const uploadedFile = await this.repository.create({ + const uploadedFile = await this.create({ size, type: mimetype, name: filename,