From dc2e87615c42947994c38685c0164197b61ecdbf Mon Sep 17 00:00:00 2001 From: Mathis Date: Mon, 30 Sep 2024 14:33:57 +0200 Subject: [PATCH] Switch file read to streamable and update method signatures Replaced Buffer with StreamableFile for more efficient file handling by streaming the content instead of reading it into memory. Additionally, updated the method signatures and parameters to improve clarity and align with the new changes. --- .../src/app/storage/storage.service.ts | 31 +++++++++---------- apps/backend/src/app/storage/storage.types.ts | 3 +- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/apps/backend/src/app/storage/storage.service.ts b/apps/backend/src/app/storage/storage.service.ts index 34947fd..534b31e 100644 --- a/apps/backend/src/app/storage/storage.service.ts +++ b/apps/backend/src/app/storage/storage.service.ts @@ -7,6 +7,7 @@ import { Injectable, InternalServerErrorException, NotFoundException, + StreamableFile, } from "@nestjs/common"; import { DbService } from "apps/backend/src/app/db/db.service"; import { @@ -189,34 +190,35 @@ export class StorageService { public async read( checksum: string, extension: string, - isDocumentation: boolean + isDocumentation: boolean, ) { try { const fileName = `${isDocumentation ? "doc" : "file"}-${checksum}.${extension.toLowerCase()}`; console.log(`Fetching file "${fileName}" from storage...`); - const file = await this.getFile(fileName) - console.log(`Got a ${file.byteLength / (1024 * 1024)}MiB file from the storage.`) + const file = await this.getFile(fileName); + console.log( + `Got a ${file.byteLength / (1024 * 1024)}MiB file from the storage.`, + ); return file; } catch (err) { - console.log("File not found.") + console.log("File not found."); throw new NotFoundException(err); } } /** - * Processes and saves a new file after validating its properties and conditions. + * Processes and saves a new file with the provided information. * * @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. + * @param {Buffer} file - The file buffer to be processed. + * @param {Array} machinesId - An array of machine IDs associated with the file. + * @param {boolean} [isDocumentation] - Optional flag to indicate if the file is documentation. + * @return {Promise} A promise that resolves to an object containing information about the file. */ public async new( fileDisplayName: string, file: Buffer, - allowedMIMEs: Array, + machinesId: Array, isDocumentation?: boolean, ): Promise { try { @@ -228,17 +230,14 @@ export class StorageService { console.log( `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( - allowedMIMEs, - file, - ); + const condition = await this.checkConditions(machinesId, file); if (!condition) { console.warn( `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}` + `File "${info.fileDisplayName}" passed the files requirement successfully.\n${info.fileChecksum}`, ); // Save the file with "saveFile()" diff --git a/apps/backend/src/app/storage/storage.types.ts b/apps/backend/src/app/storage/storage.types.ts index 888a151..51ef87c 100644 --- a/apps/backend/src/app/storage/storage.types.ts +++ b/apps/backend/src/app/storage/storage.types.ts @@ -1,3 +1,4 @@ +import { StreamableFile } from "@nestjs/common"; import FileType from "file-type"; export interface IFileInformation { @@ -10,7 +11,7 @@ export interface IFileInformation { } export interface IFileWithInformation { - buffer: Buffer; + stream: StreamableFile; info: IFileInformation; additionalData?: AdditionalData; }