Merge pull request #431 from Hexastack/430-issue-refactor-attachment-service-uploadfiles-method

refactor: attachment service uploadFiles method
This commit is contained in:
Med Marrouchi 2024-12-13 09:43:50 +01:00 committed by GitHub
commit dcc8d3929d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -148,35 +148,31 @@ export class AttachmentService extends BaseService<Attachment> {
* @returns A promise that resolves to an array of uploaded attachments. * @returns A promise that resolves to an array of uploaded attachments.
*/ */
async uploadFiles(files: { file: Express.Multer.File[] }) { async uploadFiles(files: { file: Express.Multer.File[] }) {
const uploadedFiles: Attachment[] = [];
if (this.getStoragePlugin()) { if (this.getStoragePlugin()) {
const dtos = await Promise.all( for (const file of files?.file) {
files.file.map((file) => { const dto = await this.getStoragePlugin().upload(file);
return this.getStoragePlugin().upload(file); const uploadedFile = await this.create(dto);
}), uploadedFiles.push(uploadedFile);
); }
const uploadedFiles = await Promise.all(
dtos.map((dto) => {
return this.create(dto);
}),
);
return uploadedFiles;
} else { } else {
if (Array.isArray(files?.file)) { if (Array.isArray(files?.file)) {
const uploadedFiles = await Promise.all( for (const { size, mimetype, filename } of files?.file) {
files?.file?.map(async ({ size, filename, mimetype }) => { const uploadedFile = await this.create({
return await this.create({
size, size,
type: mimetype, type: mimetype,
name: filename, name: filename,
channel: {}, channel: {},
location: `/${filename}`, location: `/${filename}`,
}); });
}), uploadedFiles.push(uploadedFile);
); }
}
}
return uploadedFiles; return uploadedFiles;
} }
}
}
/** /**
* Downloads an attachment identified by the provided parameters. * Downloads an attachment identified by the provided parameters.