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.
This commit is contained in:
Mathis H (Avnyr) 2024-09-30 11:10:36 +02:00
parent fd8ad47cf7
commit e788f4945e
Signed by: Mathis
GPG Key ID: DD9E0666A747D126

View File

@ -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<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.
*/
public async new( public async new(
fileDisplayName: string, fileDisplayName: string,
file: Buffer, file: Buffer,
allowedMIMEs: Array<string>,
isDocumentation?: boolean, isDocumentation?: boolean,
) { ): Promise<IFileInformation> {
try { try {
const info = await this.generateInformation( const info = await this.generateInformation(
file, 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`, `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(
[ allowedMIMEs,
/* TODO import autorized file format */
],
file, file,
); );
if (!condition) { if (!condition) {
@ -202,8 +211,17 @@ export class StorageService {
`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(
`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) { } catch (err) {
throw new BadRequestException(err); throw new BadRequestException(err);
} }