From e788f4945e7d308a56239b129ab94729ee0c1372 Mon Sep 17 00:00:00 2001 From: Mathis Date: Mon, 30 Sep 2024 11:10:36 +0200 Subject: [PATCH] Add detailed processing and validation for new files Enhanced the "new" method to include detailed documentation and parameters for file processing and validation. Introduced allowed MIME types as a parameter and logging for clearer file management. --- .../src/app/storage/storage.service.ts | 28 +++++++++++++++---- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/apps/backend/src/app/storage/storage.service.ts b/apps/backend/src/app/storage/storage.service.ts index 50ae43e..9ee57b7 100644 --- a/apps/backend/src/app/storage/storage.service.ts +++ b/apps/backend/src/app/storage/storage.service.ts @@ -177,11 +177,22 @@ export class StorageService { }; } + /** + * Processes and saves a new file after validating its properties and conditions. + * + * @param {string} fileDisplayName - The display name of the file. + * @param {Buffer} file - The buffer representing the file content. + * @param {Array} allowedMIMEs - An array of allowed MIME types for validation. + * @param {boolean} [isDocumentation] - Optional flag to mark whether the file is documentation. + * @return {Promise} - Returns a promise resolving to the file information object upon successful operation. + * @throws {BadRequestException} If an error occurs during the file processing or saving. + */ public async new( fileDisplayName: string, file: Buffer, + allowedMIMEs: Array, isDocumentation?: boolean, - ) { + ): Promise { try { const info = await this.generateInformation( file, @@ -192,9 +203,7 @@ export class StorageService { `Trying to append a new file : "${info.fileDisplayName}"...\n > Checksum SHA-256 : ${info.fileChecksum}\n > Size : ${info.fileSize / (1024 * 1024)}Mio\n > File format : ${info.fileType.mime}\n`, ); const condition = await this.checkConditions( - [ - /* TODO import autorized file format */ - ], + allowedMIMEs, file, ); if (!condition) { @@ -202,8 +211,17 @@ export class StorageService { `File "${info.fileDisplayName}" did not pass the files requirement.\n${info.fileChecksum}`, ); } + console.log( + `File "${info.fileDisplayName}" passed the files requirement successfully.\n${info.fileChecksum}` + ); - //TODO Append in DB and save to storage + // Save the file with "saveFile()" + console.log(`Saving file "${info.fileName}"...`); + console.log(`Nom d'affichage : ${info.fileDisplayName}`); + await this.saveFile(info.fileName, file); + + // All good we return data about the file to append it to the db. + return info; } catch (err) { throw new BadRequestException(err); }