Refactor files.controller.ts and add uploaded_by header

Reformat imports and modify `saveFile` method to include the `uploaded_by` header. Ensure that `_uploadedBy` is checked and added to the parameters alongside other headers.
This commit is contained in:
Mathis H (Avnyr) 2024-10-07 12:00:30 +02:00
parent aecc22a733
commit 534560bae5
Signed by: Mathis
GPG Key ID: DD9E0666A747D126

View File

@ -1,20 +1,24 @@
import { IncomingMessage } from "node:http";
import { import {
BadRequestException,
Controller, Controller,
DefaultValuePipe, DefaultValuePipe,
Get, Get,
HttpCode,
HttpStatus,
Param, Param,
ParseIntPipe, ParseIntPipe,
Post, Post,
Query, Query,
Req, Req,
Res,
Request, Request,
Res,
Response, Response,
StreamableFile, HttpStatus, HttpCode, BadRequestException, UseGuards StreamableFile,
} from '@nestjs/common'; UseGuards,
} from "@nestjs/common";
import { InsertAdminState } from "../auth/auth.guard";
import { FilesService } from "./files.service"; import { FilesService } from "./files.service";
import { IncomingMessage } from 'node:http';
import { InsertAdminState } from '../auth/auth.guard';
@Controller("files") @Controller("files")
export class FilesController { export class FilesController {
@ -22,20 +26,21 @@ export class FilesController {
@UseGuards(InsertAdminState) @UseGuards(InsertAdminState)
@HttpCode(HttpStatus.OK) @HttpCode(HttpStatus.OK)
@Post('new') @Post("new")
async saveFile(@Req() req: IncomingMessage, @Res() res: Response) { async saveFile(@Req() req: IncomingMessage, @Res() res: Response) {
let fileBuffer: Buffer = Buffer.from([]); let fileBuffer: Buffer = Buffer.from([]);
req.on('data', (chunk: Buffer) => { req.on("data", (chunk: Buffer) => {
fileBuffer = Buffer.concat([fileBuffer, chunk]); fileBuffer = Buffer.concat([fileBuffer, chunk]);
}); });
req.on('end', async () => { req.on("end", async () => {
const _fileName = req.headers['file_name'] as string; const _fileName = req.headers["file_name"] as string;
const _groupId = req.headers['group_id'] as string; const _groupId = req.headers["group_id"] as string;
const _machineId = req.headers['machine_id']; const _uploadedBy = req.headers["uploaded_by"] as string;
const _isDocumentation = req.headers['is_documentation'] as string; const _machineId = req.headers["machine_id"];
const _isRestricted = req.headers['is_restricted'] as string; const _isDocumentation = req.headers["is_documentation"] as string;
const _isAdmin = Boolean(req.headers['is_admin'] as string | boolean); 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 // Vérifier que les en-têtes nécessaires sont présents
if (!_fileName || !_groupId || !_machineId) { if (!_fileName || !_groupId || !_machineId) {
@ -46,40 +51,39 @@ export class FilesController {
const Params = new Map() const Params = new Map()
.set("fileName", _fileName.toString()) .set("fileName", _fileName.toString())
.set("groupId", _groupId.toString()) .set("groupId", _groupId.toString())
.set("machinesId", Array(..._machineId)) .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. //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) { if (_isAdmin) {
Params.set("isDocumentation", Boolean(_isDocumentation)) Params.set("isDocumentation", Boolean(_isDocumentation));
Params.set("isRestricted", Boolean(_isRestricted)) Params.set("isRestricted", Boolean(_isRestricted));
} }
//TODO Implement the service //TODO Implement the service
//await this.filesService.save(fileBuffer, Params); //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) => { req.on("error", (err) => {
throw new BadRequestException(err.message) throw new BadRequestException(err.message);
}); });
} }
@Get('find') @Get("find")
async findMany( async findMany(
@Query("limit", new DefaultValuePipe(20), ParseIntPipe) limit: number, @Query("limit", new DefaultValuePipe(20), ParseIntPipe) limit: number,
@Query("offset", new DefaultValuePipe(0), ParseIntPipe) offset: number, @Query("offset", new DefaultValuePipe(0), ParseIntPipe) offset: number,
@Query("search", new DefaultValuePipe("")) search: string, @Query("search", new DefaultValuePipe("")) search: string,
) { ) {}
} @Get(":fileId")
async getFile(@Param("fileId") fileId: string) {
@Get(':fileId')
async getFile(@Param('fileId') fileId: string) {
return this.filesService.get(fileId); return this.filesService.get(fileId);
} }
} }