From 79b2dec9e919b2c5e548615ad2a91c57e1d2df6b Mon Sep 17 00:00:00 2001 From: Mathis Date: Mon, 14 Oct 2024 12:04:12 +0200 Subject: [PATCH] Add file deletion endpoint with admin guard Introduced the deleteFile method in files.service.ts to handle file deletion, considering duplicates. Also updated files.controller.ts to create a new DELETE endpoint protected by the AdminGuard to ensure only admins can delete files. --- apps/backend/src/app/files/files.controller.ts | 15 +++++++++++---- apps/backend/src/app/files/files.service.ts | 6 ++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/apps/backend/src/app/files/files.controller.ts b/apps/backend/src/app/files/files.controller.ts index 2381a41..62ad2f9 100644 --- a/apps/backend/src/app/files/files.controller.ts +++ b/apps/backend/src/app/files/files.controller.ts @@ -2,7 +2,7 @@ import { IncomingMessage } from "node:http"; import { BadRequestException, Controller, - DefaultValuePipe, + DefaultValuePipe, Delete, Get, HttpCode, HttpStatus, @@ -15,9 +15,9 @@ import { Res, Response, StreamableFile, - UseGuards, -} from "@nestjs/common"; -import { InsertAdminState } from "../auth/auth.guard"; + UseGuards +} from '@nestjs/common'; +import { AdminGuard, InsertAdminState } from '../auth/auth.guard'; import { FilesService } from "./files.service"; @Controller("files") @@ -121,4 +121,11 @@ export class FilesController { async getFile(@Param("fileId") fileId: string) { return await this.filesService.get(fileId); } + + @UseGuards(AdminGuard) + @HttpCode(HttpStatus.OK) + @Delete(":fileId") + async deleteFile(@Param("fileId") fileId: string) { + return await this.filesService.deleteFile(fileId); + } } diff --git a/apps/backend/src/app/files/files.service.ts b/apps/backend/src/app/files/files.service.ts index 0dbeb3f..a0ee54f 100644 --- a/apps/backend/src/app/files/files.service.ts +++ b/apps/backend/src/app/files/files.service.ts @@ -68,6 +68,12 @@ export class FilesService { } //TODO DELETE FILE + public async deleteFile(fileId: string) { + //get checksum for fileId + //check if multiple entry for checksum + //if that the case then only remove the entry in database relative to fileId. + //if there is one only entry then remove the file from the storage and the database. + } /** * Searches for files in the database using the specified search field, limit, and offset.