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,
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<string>} allowedMIMEs - An array of allowed MIME types for validation.
* @param {boolean} [isDocumentation] - Optional flag to mark whether the file is documentation.
* @return {Promise<IFileInformation>} - 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<string>} machinesId - An array of machine IDs associated with the file.
* @param {boolean} [isDocumentation] - Optional flag to indicate if the file is documentation.
* @return {Promise<IFileInformation>} A promise that resolves to an object containing information about the file.
*/
public async new(
fileDisplayName: string,
file: Buffer,
allowedMIMEs: Array<string>,
machinesId: Array<string>,
isDocumentation?: boolean,
): Promise<IFileInformation> {
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()"

View File

@ -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<AdditionalData> {
buffer: Buffer;
stream: StreamableFile;
info: IFileInformation;
additionalData?: AdditionalData;
}