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,85 +1,89 @@
import { IncomingMessage } from "node:http";
import { import {
Controller, BadRequestException,
DefaultValuePipe, Controller,
Get, DefaultValuePipe,
Param, Get,
ParseIntPipe, HttpCode,
Post, HttpStatus,
Query, Param,
Req, ParseIntPipe,
Res, Post,
Request, Query,
Response, Req,
StreamableFile, HttpStatus, HttpCode, BadRequestException, UseGuards Request,
} from '@nestjs/common'; Res,
Response,
StreamableFile,
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 {
constructor(private readonly filesService: FilesService) {} constructor(private readonly filesService: FilesService) {}
@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) {
throw new BadRequestException("Header(s) manquant(s)"); throw new BadRequestException("Header(s) manquant(s)");
} }
const machineId = Array(..._machineId); const machineId = Array(..._machineId);
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) => {
}); throw new BadRequestException(err.message);
});
}
req.on('error', (err) => { @Get("find")
throw new BadRequestException(err.message) 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('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(":fileId")
async getFile(@Param("fileId") fileId: string) {
return this.filesService.get(fileId);
}
} }