Compare commits

..

No commits in common. "04a37d19b7849ab3ff87c58f26e56eeb62506008" and "2ca13714a4affc4597587e53d3fa729dc90d5527" have entirely different histories.

3 changed files with 15 additions and 38 deletions

View File

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

View File

@ -23,6 +23,9 @@ export class FilesService {
private readonly database: DbService,
) {}
//TODO
//TODO
/**
* Retrieves a file from the database and storage by its unique identifier.
*
@ -92,14 +95,7 @@ export class FilesService {
}
}
/**
* 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.
*/
//TODO save a file
public async save(file: Buffer, data: Map<string, unknown>) {
const _machineIds = data.get("machineId").toString().split(",");
@ -130,7 +126,6 @@ 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()
@ -162,6 +157,7 @@ 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,
@ -171,19 +167,10 @@ 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,19 +53,13 @@ export class StorageService {
file: Buffer,
): Promise<boolean> {
/**
* 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.
* 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.
*/
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;
function checkMime(allowedMime: Set<string>, currentMime: string): boolean {
return allowedMime.has(currentMime);
}
const fileType = filetypeinfo(file);
@ -73,7 +67,6 @@ 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}`);
@ -97,12 +90,9 @@ export class StorageService {
);
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)
_mimes.push(allowedMimeElement.slug);
}
machinesMap.set(machineId, tempSet)
tempSet.clear()
}
//Store the MIMEs without duplicate
const mimeSet = new Set(_mimes);
@ -117,7 +107,7 @@ export class StorageService {
});
}
if (!checkMime(machinesMap, fileType[0].mime))
if (!checkMime(mimeSet, fileType[0].mime))
throw new BadRequestException({
cause: "MIME type",
description: `Invalid MIME type. Allowed MIME types are: ${[...mimeSet].join(", ")}.`,