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.
This commit is contained in:
Mathis H (Avnyr) 2024-09-30 14:33:57 +02:00
parent 040dada16c
commit dc2e87615c
Signed by: Mathis
GPG Key ID: DD9E0666A747D126
2 changed files with 17 additions and 17 deletions

View File

@ -7,6 +7,7 @@ import {
Injectable, Injectable,
InternalServerErrorException, InternalServerErrorException,
NotFoundException, NotFoundException,
StreamableFile,
} from "@nestjs/common"; } from "@nestjs/common";
import { DbService } from "apps/backend/src/app/db/db.service"; import { DbService } from "apps/backend/src/app/db/db.service";
import { import {
@ -189,34 +190,35 @@ export class StorageService {
public async read( public async read(
checksum: string, checksum: string,
extension: string, extension: string,
isDocumentation: boolean isDocumentation: boolean,
) { ) {
try { try {
const fileName = `${isDocumentation ? "doc" : "file"}-${checksum}.${extension.toLowerCase()}`; const fileName = `${isDocumentation ? "doc" : "file"}-${checksum}.${extension.toLowerCase()}`;
console.log(`Fetching file "${fileName}" from storage...`); console.log(`Fetching file "${fileName}" from storage...`);
const file = await this.getFile(fileName) const file = await this.getFile(fileName);
console.log(`Got a ${file.byteLength / (1024 * 1024)}MiB file from the storage.`) console.log(
`Got a ${file.byteLength / (1024 * 1024)}MiB file from the storage.`,
);
return file; return file;
} catch (err) { } catch (err) {
console.log("File not found.") console.log("File not found.");
throw new NotFoundException(err); 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 {string} fileDisplayName - The display name of the file.
* @param {Buffer} file - The buffer representing the file content. * @param {Buffer} file - The file buffer to be processed.
* @param {Array<string>} allowedMIMEs - An array of allowed MIME types for validation. * @param {Array<string>} machinesId - An array of machine IDs associated with the file.
* @param {boolean} [isDocumentation] - Optional flag to mark whether the file is documentation. * @param {boolean} [isDocumentation] - Optional flag to indicate if the file is documentation.
* @return {Promise<IFileInformation>} - Returns a promise resolving to the file information object upon successful operation. * @return {Promise<IFileInformation>} A promise that resolves to an object containing information about the file.
* @throws {BadRequestException} If an error occurs during the file processing or saving.
*/ */
public async new( public async new(
fileDisplayName: string, fileDisplayName: string,
file: Buffer, file: Buffer,
allowedMIMEs: Array<string>, machinesId: Array<string>,
isDocumentation?: boolean, isDocumentation?: boolean,
): Promise<IFileInformation> { ): Promise<IFileInformation> {
try { try {
@ -228,17 +230,14 @@ export class StorageService {
console.log( 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`, `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( const condition = await this.checkConditions(machinesId, file);
allowedMIMEs,
file,
);
if (!condition) { if (!condition) {
console.warn( console.warn(
`File "${info.fileDisplayName}" did not pass the files requirement.\n${info.fileChecksum}`, `File "${info.fileDisplayName}" did not pass the files requirement.\n${info.fileChecksum}`,
); );
} }
console.log( 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()" // Save the file with "saveFile()"

View File

@ -1,3 +1,4 @@
import { StreamableFile } from "@nestjs/common";
import FileType from "file-type"; import FileType from "file-type";
export interface IFileInformation { export interface IFileInformation {
@ -10,7 +11,7 @@ export interface IFileInformation {
} }
export interface IFileWithInformation<AdditionalData> { export interface IFileWithInformation<AdditionalData> {
buffer: Buffer; stream: StreamableFile;
info: IFileInformation; info: IFileInformation;
additionalData?: AdditionalData; additionalData?: AdditionalData;
} }