refactor: attachment service uploadFiles method

This commit is contained in:
yassinedorbozgithub 2024-12-09 17:16:27 +01:00
parent a9522d44e0
commit aa96dba18d

View File

@ -148,34 +148,30 @@ 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.repository.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;
} }
/** /**