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.
This commit is contained in:
parent
2ca13714a4
commit
30c9c28e3d
@ -53,13 +53,19 @@ export class StorageService {
|
|||||||
file: Buffer,
|
file: Buffer,
|
||||||
): Promise<boolean> {
|
): Promise<boolean> {
|
||||||
/**
|
/**
|
||||||
* Checks if the current MIME type is allowed based on the given set of allowed MIME types.
|
* Checks if the given MIME type is present in all machines' MIME type sets.
|
||||||
* @param {Set<string>} allowedMime - The set of allowed MIME types.
|
*
|
||||||
* @param {string} currentMime - The current MIME type to check.
|
* @param {Map<string, Set<string>>} mimesForMachines - A map where the key is the machine identifier and the value is a set of MIME types supported by that machine.
|
||||||
* @return {boolean} - True if the current MIME type is allowed, false otherwise.
|
* @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<string>, currentMime: string): boolean {
|
function checkMime(mimesForMachines: Map<string, Set<string>>, currentMime: string): boolean {
|
||||||
return allowedMime.has(currentMime);
|
let notFoundCount = 0;
|
||||||
|
for (const mimesForMachine of mimesForMachines) {
|
||||||
|
const [key, set] = mimesForMachine
|
||||||
|
if (!set.has(currentMime)) {notFoundCount++}
|
||||||
|
}
|
||||||
|
return notFoundCount === 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
const fileType = filetypeinfo(file);
|
const fileType = filetypeinfo(file);
|
||||||
@ -67,6 +73,7 @@ export class StorageService {
|
|||||||
// Array of MIMEs with possible duplicate field
|
// Array of MIMEs with possible duplicate field
|
||||||
const _mimes: Array<string> = [];
|
const _mimes: Array<string> = [];
|
||||||
|
|
||||||
|
const machinesMap: Map<string, Set<string>> = new Map<string, Set<string>>()
|
||||||
// Fetching MIMEs for the associated machines
|
// Fetching MIMEs for the associated machines
|
||||||
for (const machineId of machineIds) {
|
for (const machineId of machineIds) {
|
||||||
console.debug(`Fetching mimeTypes for machine : ${machineId}`);
|
console.debug(`Fetching mimeTypes for machine : ${machineId}`);
|
||||||
@ -90,9 +97,12 @@ export class StorageService {
|
|||||||
);
|
);
|
||||||
console.debug(`Total : ${_allowedMime.length}`);
|
console.debug(`Total : ${_allowedMime.length}`);
|
||||||
// Append each MIME of a machine
|
// Append each MIME of a machine
|
||||||
|
const tempSet = new Set<string>()
|
||||||
for (const allowedMimeElement of _allowedMime) {
|
for (const allowedMimeElement of _allowedMime) {
|
||||||
_mimes.push(allowedMimeElement.slug);
|
tempSet.add(allowedMimeElement.slug)
|
||||||
}
|
}
|
||||||
|
machinesMap.set(machineId, tempSet)
|
||||||
|
tempSet.clear()
|
||||||
}
|
}
|
||||||
//Store the MIMEs without duplicate
|
//Store the MIMEs without duplicate
|
||||||
const mimeSet = new Set(_mimes);
|
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({
|
throw new BadRequestException({
|
||||||
cause: "MIME type",
|
cause: "MIME type",
|
||||||
description: `Invalid MIME type. Allowed MIME types are: ${[...mimeSet].join(", ")}.`,
|
description: `Invalid MIME type. Allowed MIME types are: ${[...mimeSet].join(", ")}.`,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user