Compare commits

...

2 Commits

Author SHA1 Message Date
04a37d19b7
Add file saving functionality and update group ID handling
Implemented the save method to store files and associate them with machines, including handling optional group associations. Modified the database schema to allow null as a default for group_id and added logic to update group ID after file insertion.
2024-10-14 11:53:54 +02:00
30c9c28e3d
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.
2024-10-14 11:21:50 +02:00
3 changed files with 38 additions and 15 deletions

View File

@ -73,7 +73,7 @@ export const FilesTable = pgTable("files", {
})
.notNull(),
groupId: p.uuid("group_id").references(() => FilesGroupTable.uuid),
groupId: p.uuid("group_id").default(null).references(() => FilesGroupTable.uuid),
fileSize: p.integer("file_size").notNull(),

View File

@ -23,9 +23,6 @@ export class FilesService {
private readonly database: DbService,
) {}
//TODO
//TODO
/**
* Retrieves a file from the database and storage by its unique identifier.
*
@ -95,7 +92,14 @@ export class FilesService {
}
}
//TODO save a file
/**
* Saves a file and associates it with machines and an optional group in the database.
*
* @param {Buffer} file - The file data to be saved.
* @param {Map<string, unknown>} data - A map containing file and association metadata.
* @throws {NotFoundException} If a machine or group specified in the data does not exist in the database.
* @return {Promise<Object>} The inserted file record.
*/
public async save(file: Buffer, data: Map<string, unknown>) {
const _machineIds = data.get("machineId").toString().split(",");
@ -126,6 +130,7 @@ export class FilesService {
/*if (!_group) {
throw new NotFoundException(`Group with ID "${_group}" not found`);
}*/
// verify that the group exist in the database
const groupExists = await this.database
.use()
@ -157,7 +162,6 @@ export class FilesService {
.insert(FilesTable)
.values({
fileName: data.get("fileName") as string,
groupId: groupExists[0].uuid || null,
checksum: saveResult.fileChecksum,
extension: saveResult.fileType.extension,
fileSize: saveResult.fileSize,
@ -167,10 +171,19 @@ export class FilesService {
uploadedBy: data.get("uploadedBy") as string,
})
.returning();
if (groupExists[0].uuid) {
await this.database.use()
.update(FilesTable)
// @ts-ignore TODO FIX
.set({groupId: groupExists[0].uuid})
.where(eq(FilesTable.uuid, inserted[0].uuid))
.prepare("addGroupToFile")
.execute()
}
console.log(inserted);
for (const machineId of machinesIds) {
//TODO insert a link betwen fileId and MachineIds[]
console.log(
`Append file ${inserted[0].fileName} for machine : "${machineId}"`,
);

View File

@ -53,13 +53,19 @@ export class StorageService {
file: Buffer,
): Promise<boolean> {
/**
* Checks if the current MIME type is allowed based on the given set of allowed MIME types.
* @param {Set<string>} 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<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.
* @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 {
return allowedMime.has(currentMime);
function checkMime(mimesForMachines: Map<string, Set<string>>, 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<string> = [];
const machinesMap: Map<string, Set<string>> = new Map<string, Set<string>>()
// 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<string>()
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(", ")}.`,