From 30c9c28e3d04ef4c70e3e46ef24cdb680bedda08 Mon Sep 17 00:00:00 2001 From: Mathis Date: Mon, 14 Oct 2024 11:21:50 +0200 Subject: [PATCH] Refactor MIME type check for machine-specific validation Updated the MIME type validation function to handle machine-specific MIME type sets. This ensures that a MIME type must be acceptable across all associated machines. Enhanced clarity and structure by using a map to maintain MIME types for each machine. --- .../src/app/storage/storage.service.ts | 26 +++++++++++++------ 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/apps/backend/src/app/storage/storage.service.ts b/apps/backend/src/app/storage/storage.service.ts index 6dceecb..cefc47a 100644 --- a/apps/backend/src/app/storage/storage.service.ts +++ b/apps/backend/src/app/storage/storage.service.ts @@ -53,13 +53,19 @@ export class StorageService { file: Buffer, ): Promise { /** - * Checks if the current MIME type is allowed based on the given set of allowed MIME types. - * @param {Set} allowedMime - The set of allowed MIME types. - * @param {string} currentMime - The current MIME type to check. - * @return {boolean} - True if the current MIME type is allowed, false otherwise. + * Checks if the given MIME type is present in all machines' MIME type sets. + * + * @param {Map>} mimesForMachines - A map where the key is the machine identifier and the value is a set of MIME types supported by that machine. + * @param {string} currentMime - The MIME type to check for presence in all sets. + * @return {boolean} Returns true if the MIME type is found in all sets, otherwise false. */ - function checkMime(allowedMime: Set, currentMime: string): boolean { - return allowedMime.has(currentMime); + function checkMime(mimesForMachines: Map>, currentMime: string): boolean { + let notFoundCount = 0; + for (const mimesForMachine of mimesForMachines) { + const [key, set] = mimesForMachine + if (!set.has(currentMime)) {notFoundCount++} + } + return notFoundCount === 0; } const fileType = filetypeinfo(file); @@ -67,6 +73,7 @@ export class StorageService { // Array of MIMEs with possible duplicate field const _mimes: Array = []; + const machinesMap: Map> = new Map>() // Fetching MIMEs for the associated machines for (const machineId of machineIds) { console.debug(`Fetching mimeTypes for machine : ${machineId}`); @@ -90,9 +97,12 @@ export class StorageService { ); console.debug(`Total : ${_allowedMime.length}`); // Append each MIME of a machine + const tempSet = new Set() for (const allowedMimeElement of _allowedMime) { - _mimes.push(allowedMimeElement.slug); + tempSet.add(allowedMimeElement.slug) } + machinesMap.set(machineId, tempSet) + tempSet.clear() } //Store the MIMEs without duplicate const mimeSet = new Set(_mimes); @@ -107,7 +117,7 @@ export class StorageService { }); } - if (!checkMime(mimeSet, fileType[0].mime)) + if (!checkMime(machinesMap, fileType[0].mime)) throw new BadRequestException({ cause: "MIME type", description: `Invalid MIME type. Allowed MIME types are: ${[...mimeSet].join(", ")}.`,