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 {
BadRequestException,
Controller,
DefaultValuePipe,
Get,
HttpCode,
HttpStatus,
Param,
ParseIntPipe,
Post,
Query,
Req,
Res,
Request,
Res,
Response,
StreamableFile, HttpStatus, HttpCode, BadRequestException, UseGuards
} from '@nestjs/common';
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 {
@ -22,20 +26,21 @@ export class FilesController {
@UseGuards(InsertAdminState)
@HttpCode(HttpStatus.OK)
@Post('new')
@Post("new")
async saveFile(@Req() req: IncomingMessage, @Res() res: Response) {
let fileBuffer: Buffer = Buffer.from([]);
req.on('data', (chunk: Buffer) => {
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) {
@ -46,40 +51,39 @@ export class FilesController {
const Params = new Map()
.set("fileName", _fileName.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.
if (_isAdmin) {
Params.set("isDocumentation", Boolean(_isDocumentation))
Params.set("isRestricted", Boolean(_isRestricted))
Params.set("isDocumentation", Boolean(_isDocumentation));
Params.set("isRestricted", Boolean(_isRestricted));
}
//TODO Implement the service
//await this.filesService.save(fileBuffer, Params);
// 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')
@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) {
@Get(":fileId")
async getFile(@Param("fileId") fileId: string) {
return this.filesService.get(fileId);
}
}