Refactor and log changes in storage and file services

Convert array of machine IDs to sets for uniqueness in methods. Add console logs for debugging and update error handling to improve clarity. Refactor the files controller for better readability.
This commit is contained in:
Mathis H (Avnyr) 2024-10-15 16:50:33 +02:00
parent ff649ebdbf
commit 6f9d25a58b
Signed by: Mathis
GPG Key ID: DD9E0666A747D126
3 changed files with 169 additions and 149 deletions

View File

@ -26,7 +26,7 @@ import { FilesService } from "./files.service";
@Controller("files")
export class FilesController {
constructor(private readonly filesService: FilesService) { }
constructor(private readonly filesService: FilesService) {}
@HttpCode(HttpStatus.OK)
@UseGuards(InsertAdminState)
@ -58,20 +58,22 @@ export class FilesController {
);
// Vérifier que les en-têtes nécessaires sont présents
if (!_fileName || !_machineId) {
if (!_fileName || !_machineId || !_uploadedBy) {
throw new BadRequestException("Header(s) manquant(s)");
}
console.log("Header found !");
const machineId = Array(_machineId);
const Params = new Map()
.set("groupId", null)
.set("fileName", _fileName.toString())
.set("groupId", _groupId.toString() || null)
.set("uploadedBy", _uploadedBy.toString())
.set("machineId", Array(JSON.parse(machineId.toString())))
.set("uploadedBy", String(_uploadedBy))
.set("machineId", Array(machineId))
.set("isDocumentation", false)
.set("isRestricted", false);
if (_groupId) Params.set("groupId", String(_groupId));
console.log("Current params :\n", Params);
//TODO Integrate a verification if the source is an admin, if that the case then it can define isDocumentation and isRestricted else throw in case of presence of those parameters.
@ -123,7 +125,7 @@ export class FilesController {
@HttpCode(HttpStatus.FOUND)
@Get("types")
async getTypes() {
console.log("Performing request")
console.log("Performing request");
return await this.filesService.getAllFilesTypes();
}
@ -138,7 +140,7 @@ export class FilesController {
@UseGuards(AdminGuard)
@Delete("types/:typeId")
async delType(@Param(":typeId", ParseUUIDPipe) typeId: string) {
return await this.filesService.removeFileType(typeId)
return await this.filesService.removeFileType(typeId);
}
@HttpCode(HttpStatus.FOUND)
@ -153,5 +155,4 @@ export class FilesController {
async deleteFile(@Param("fileId", ParseUUIDPipe) fileId: string) {
return await this.filesService.deleteFile(fileId);
}
}

View File

@ -195,7 +195,7 @@ export class FilesService {
.where(eq(FilesGroupTable.uuid, _group))
.prepare("checkGroupExists")
.execute();
if (!_group) console.log("No group to link with the file.");
if (_group && groupExists.length === 0) {
throw new NotFoundException(`Group with ID "${_group}" not found`);
}
@ -203,7 +203,7 @@ export class FilesService {
const saveResult = await this.storage.new(
data.get("fileName") as string,
file,
_machineIds,
machinesIds,
Boolean(data.get("isDocumentation")),
);
console.log(saveResult);
@ -212,7 +212,7 @@ export class FilesService {
.select()
.from(FilesTypesTable)
.where(eq(FilesTypesTable.mime, saveResult.fileType.mime));
console.log(mimeId);
const inserted = await this.database
.use()
.insert(FilesTable)
@ -227,7 +227,9 @@ export class FilesService {
uploadedBy: data.get("uploadedBy") as string,
})
.returning();
if (groupExists[0].uuid) {
console.log(inserted);
if (_group) {
console.log("Adding group ip to file..");
await this.database
.use()
.update(FilesTable)
@ -238,7 +240,7 @@ export class FilesService {
.execute();
}
console.log(inserted);
console.log("File insertion done");
for (const machineId of machinesIds) {
console.log(
@ -299,7 +301,7 @@ export class FilesService {
.from(FilesTypesTable)
.prepare("getAllFilesTypes")
.execute();
console.log(result)
console.log(result);
return result;
}

View File

@ -1,4 +1,3 @@
import * as console from "node:console";
import * as crypto from "node:crypto";
import { readFile, rm, writeFile } from "node:fs/promises";
import { join } from "node:path";
@ -43,13 +42,15 @@ export class StorageService {
}
/**
* Checks if the current MIME type and file size meet the specified conditions.
* @param {Array<string>} machineIds - The IDs of the associated machines.
* @param {Buffer} file - The file to check.
* @return {Promise<boolean>} - A Promise that resolves to true if the conditions are met, false otherwise.
* Checks the conditions for the provided file and machine IDs.
*
* @param {Set<string>} machinesIds - A set containing machine identifiers.
* @param {Buffer} file - The file buffer that needs to be checked.
* @return {Promise<boolean>} Returns a promise that resolves to true if the conditions are met, otherwise it throws an exception.
* @throws {BadRequestException} If the file size exceeds the allowed maximum size or the file MIME type is not allowed.
*/
private async checkConditions(
machineIds: Array<string>,
machinesIds: Set<string>,
file: Buffer,
): Promise<boolean> {
/**
@ -65,6 +66,7 @@ export class StorageService {
): boolean {
let notFoundCount = 0;
for (const mimesForMachine of mimesForMachines) {
console.log(mimesForMachine);
const [key, set] = mimesForMachine;
if (!set.has(currentMime)) {
notFoundCount++;
@ -82,10 +84,12 @@ export class StorageService {
string,
Set<string>
>();
const mimeSet = new Set(_mimes);
// Fetching MIMEs for the associated machines
for (const machineId of machineIds) {
for (const machineId of machinesIds) {
console.debug(`Fetching mimeTypes for machine : ${machineId}`);
// Get MIMEs associated to a machine
/*
const allowedMimeId = this.dbService
.use()
.select()
@ -102,18 +106,31 @@ export class StorageService {
.leftJoin(
allowedMimeId,
eq(FilesTypesTable.id, allowedMimeId.fileTypeId),
);*/
const _allowedMime = await this.dbService
.use()
.select()
.from(FilesTypeForMachine)
.where(eq(FilesTypeForMachine.machineId, machineId))
.leftJoin(
FilesTypesTable,
eq(FilesTypesTable.id, FilesTypeForMachine.fileTypeId),
);
console.log(_allowedMime);
console.debug(`Total : ${_allowedMime.length}`);
// Append each MIME of a machine
const tempSet = new Set<string>();
for (const allowedMimeElement of _allowedMime) {
tempSet.add(allowedMimeElement.slug);
console.debug(
`Adding ${allowedMimeElement.f_types.mime} for verification..`,
);
tempSet.add(allowedMimeElement.f_types.mime);
mimeSet.add(allowedMimeElement.f_types.mime);
}
machinesMap.set(machineId, tempSet);
tempSet.clear();
}
//Store the MIMEs without duplicate
const mimeSet = new Set(_mimes);
console.debug(`Indexed ${mimeSet.size} unique mimeTypes`);
//check file size is less than 2mb
@ -236,7 +253,7 @@ export class StorageService {
public async new(
fileDisplayName: string,
file: Buffer,
machinesId: Array<string>,
machinesId: Set<string>,
isDocumentation?: boolean,
): Promise<IFileInformation> {
try {
@ -246,7 +263,7 @@ export class StorageService {
isDocumentation,
);
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)).toFixed(6)} Mio\n > File format : ${info.fileType.mime}\n`,
);
const condition = await this.checkConditions(machinesId, file);
if (!condition) {