diff --git a/apps/backend/src/app/files/files.controller.ts b/apps/backend/src/app/files/files.controller.ts index 31a9e9f..9186a8c 100644 --- a/apps/backend/src/app/files/files.controller.ts +++ b/apps/backend/src/app/files/files.controller.ts @@ -1,85 +1,89 @@ +import { IncomingMessage } from "node:http"; import { - Controller, - DefaultValuePipe, - Get, - Param, - ParseIntPipe, - Post, - Query, - Req, - Res, - Request, - Response, - StreamableFile, HttpStatus, HttpCode, BadRequestException, UseGuards -} from '@nestjs/common'; + BadRequestException, + Controller, + DefaultValuePipe, + Get, + HttpCode, + HttpStatus, + Param, + ParseIntPipe, + Post, + Query, + Req, + Request, + Res, + Response, + StreamableFile, + UseGuards, +} from "@nestjs/common"; +import { InsertAdminState } from "../auth/auth.guard"; import { FilesService } from "./files.service"; -import { IncomingMessage } from 'node:http'; -import { InsertAdminState } from '../auth/auth.guard'; @Controller("files") export class FilesController { constructor(private readonly filesService: FilesService) {} - @UseGuards(InsertAdminState) - @HttpCode(HttpStatus.OK) - @Post('new') - async saveFile(@Req() req: IncomingMessage, @Res() res: Response) { - let fileBuffer: Buffer = Buffer.from([]); - req.on('data', (chunk: Buffer) => { - fileBuffer = Buffer.concat([fileBuffer, chunk]); - }); + @UseGuards(InsertAdminState) + @HttpCode(HttpStatus.OK) + @Post("new") + async saveFile(@Req() req: IncomingMessage, @Res() res: Response) { + let fileBuffer: Buffer = Buffer.from([]); + req.on("data", (chunk: Buffer) => { + fileBuffer = Buffer.concat([fileBuffer, chunk]); + }); - req.on('end', async () => { - const _fileName = req.headers['file_name'] as string; - const _groupId = req.headers['group_id'] as string; - const _machineId = req.headers['machine_id']; - const _isDocumentation = req.headers['is_documentation'] as string; - const _isRestricted = req.headers['is_restricted'] as string; - const _isAdmin = Boolean(req.headers['is_admin'] as string | boolean); + req.on("end", async () => { + const _fileName = req.headers["file_name"] as string; + const _groupId = req.headers["group_id"] as string; + const _uploadedBy = req.headers["uploaded_by"] as string; + const _machineId = req.headers["machine_id"]; + const _isDocumentation = req.headers["is_documentation"] as string; + const _isRestricted = req.headers["is_restricted"] as string; + const _isAdmin = Boolean(req.headers["is_admin"] as string | boolean); - // Vérifier que les en-têtes nécessaires sont présents - if (!_fileName || !_groupId || !_machineId) { - throw new BadRequestException("Header(s) manquant(s)"); - } - const machineId = Array(..._machineId); + // Vérifier que les en-têtes nécessaires sont présents + if (!_fileName || !_groupId || !_machineId) { + throw new BadRequestException("Header(s) manquant(s)"); + } + const machineId = Array(..._machineId); - const Params = new Map() - .set("fileName", _fileName.toString()) - .set("groupId", _groupId.toString()) - .set("machinesId", Array(..._machineId)) + const Params = new Map() + .set("fileName", _fileName.toString()) + .set("groupId", _groupId.toString()) + .set("uploadedBy", _uploadedBy.toString()) + .set("machineId", Array(..._machineId)) + .set("isDocumentation", false) + .set("isRestricted", false); - //TODO Integrate a verification if the source is an admin, if that the case then it can define isDocumentation and isRestricted else throw in case of presence of those parameters. - if (_isAdmin) { - Params.set("isDocumentation", Boolean(_isDocumentation)) - Params.set("isRestricted", Boolean(_isRestricted)) - } + //TODO Integrate a verification if the source is an admin, if that the case then it can define isDocumentation and isRestricted else throw in case of presence of those parameters. + if (_isAdmin) { + Params.set("isDocumentation", Boolean(_isDocumentation)); + Params.set("isRestricted", Boolean(_isRestricted)); + } - //TODO Implement the service - //await this.filesService.save(fileBuffer, Params); + //TODO Implement the service + //await this.filesService.save(fileBuffer, Params); + // TODO logique de sauvegarde du fichier et des données - // TODO logique de sauvegarde du fichier et des données + return { message: "Fichier sauvegardé avec succès" }; + }); - return { message: 'Fichier sauvegardé avec succès' } - }); + req.on("error", (err) => { + throw new BadRequestException(err.message); + }); + } - req.on('error', (err) => { - throw new BadRequestException(err.message) - }); - } - - @Get('find') - async findMany( - @Query("limit", new DefaultValuePipe(20), ParseIntPipe) limit: number, - @Query("offset", new DefaultValuePipe(0), ParseIntPipe) offset: number, - @Query("search", new DefaultValuePipe("")) search: string, - ) { - - } - - @Get(':fileId') - async getFile(@Param('fileId') fileId: string) { - return this.filesService.get(fileId); - } + @Get("find") + async findMany( + @Query("limit", new DefaultValuePipe(20), ParseIntPipe) limit: number, + @Query("offset", new DefaultValuePipe(0), ParseIntPipe) offset: number, + @Query("search", new DefaultValuePipe("")) search: string, + ) {} + @Get(":fileId") + async getFile(@Param("fileId") fileId: string) { + return this.filesService.get(fileId); + } }