Skip to content

Commit

Permalink
Merge pull request #431 from Hexastack/430-issue-refactor-attachment-…
Browse files Browse the repository at this point in the history
…service-uploadfiles-method

refactor: attachment service uploadFiles method
  • Loading branch information
marrouchi authored Dec 13, 2024
2 parents c32c4e1 + ecd6c43 commit dcc8d39
Showing 1 changed file with 19 additions and 23 deletions.
42 changes: 19 additions & 23 deletions api/src/attachment/services/attachment.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,34 +148,30 @@ export class AttachmentService extends BaseService<Attachment> {
* @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.create({
size,
type: mimetype,
name: filename,
channel: {},
location: `/${filename}`,
});
uploadedFiles.push(uploadedFile);
}
}
}

return uploadedFiles;
}

/**
Expand Down

0 comments on commit dcc8d39

Please sign in to comment.