From 53bd76c78d5d82f42f260437e15e4bc9e0d9c04c Mon Sep 17 00:00:00 2001 From: IO Date: Sun, 18 Aug 2024 22:09:32 -0600 Subject: [PATCH] new limit and image fixed --- src/Profesor/dto/profesorDto.dto.ts | 5 +++++ src/Profesor/profesor.controller.ts | 22 +++++++++++++++++++- src/Profesor/profesor.service.ts | 8 ++++++++ src/images/images.service.ts | 31 ++++++++++++----------------- src/main.ts | 5 +++++ 5 files changed, 52 insertions(+), 19 deletions(-) diff --git a/src/Profesor/dto/profesorDto.dto.ts b/src/Profesor/dto/profesorDto.dto.ts index e4d3b2f..6e6a548 100644 --- a/src/Profesor/dto/profesorDto.dto.ts +++ b/src/Profesor/dto/profesorDto.dto.ts @@ -121,4 +121,9 @@ export class LineasInvestigacion{ @IsOptional() @Length(0,300) lineas_inv_html: string; +} + +export class UpdateProfesorImageDto { + @IsString() + fotografia: string; } \ No newline at end of file diff --git a/src/Profesor/profesor.controller.ts b/src/Profesor/profesor.controller.ts index 5943759..56c0551 100644 --- a/src/Profesor/profesor.controller.ts +++ b/src/Profesor/profesor.controller.ts @@ -3,6 +3,7 @@ import { Controller, Delete, Get, + HttpException, Logger, Param, ParseIntPipe, @@ -12,10 +13,11 @@ import { UseGuards } from "@nestjs/common"; import { ProfesorService } from "./profesor.service"; -import { profesorDto } from "./dto/profesorDto.dto"; +import { profesorDto, UpdateProfesorImageDto } from "./dto/profesorDto.dto"; import { Roles } from "../permissions/roles.decorator"; import { RolesGuard } from "../permissions/roles.guard"; import { Role } from "../permissions/role.enum"; +import { Profesor } from "./entities/profesor.entity"; @Controller("profesor") @UseGuards(RolesGuard) @@ -30,6 +32,24 @@ export class ProfesorController { return this.profesorService.register(data); } + @Put(':id_profesor/image') + async updateImage( + @Param('id_profesor') id_profesor: number, + @Body() updateProfesorImageDto: UpdateProfesorImageDto, + ): Promise { + const { fotografia } = updateProfesorImageDto; + + const profesor = await this.profesorService.findOne(id_profesor); + + if (!profesor) { + throw new HttpException('Profesor not found', 404); + } + + profesor.fotografia = fotografia; + + return this.profesorService.update(profesor); + } + @Get() findAll() { Logger.debug("findAllProfesors"); diff --git a/src/Profesor/profesor.service.ts b/src/Profesor/profesor.service.ts index f472293..27a5343 100644 --- a/src/Profesor/profesor.service.ts +++ b/src/Profesor/profesor.service.ts @@ -27,6 +27,14 @@ export class ProfesorService { ) { } + async findOne(id_profesor: number): Promise { + return this.profesorRepository.findOne({ where: { id_profesor } }); + } + + async update(profesor: Profesor): Promise { + return this.profesorRepository.save(profesor); + } + private async findAvailableId(): Promise { let id = 1; let exists = true; diff --git a/src/images/images.service.ts b/src/images/images.service.ts index 160007f..43a468d 100644 --- a/src/images/images.service.ts +++ b/src/images/images.service.ts @@ -1,11 +1,10 @@ -import { Injectable, BadRequestException, NotFoundException, Logger } from '@nestjs/common'; +import { Injectable, BadRequestException, Logger } from '@nestjs/common'; import * as fs from 'fs'; import * as path from 'path'; -import * as sharp from 'sharp'; @Injectable() export class ImagenService { - async saveImage(fotografia: string, nombre: string): Promise { + async saveImage(fotografia: string, id: string): Promise { if (!fotografia) { throw new BadRequestException('No image provided'); } @@ -19,8 +18,7 @@ export class ImagenService { // Decode base64 string and save the image const buffer = Buffer.from(fotografia.split(',')[1], 'base64'); - const sanitizedFileName = nombre.replace(/\s+/g, '_').toLowerCase(); - const imageName = `${sanitizedFileName}.${ext}`; + const imageName = `${id}.${ext}`; // Save image to the 'imagenes' directory in the root of the project const imagePath = path.join(process.cwd(), 'imagenes', imageName); @@ -31,16 +29,15 @@ export class ImagenService { fs.mkdirSync(dirPath, { recursive: true }); } - await sharp(buffer).toFile(imagePath); + fs.writeFileSync(imagePath, buffer); - return `/imagenes/${imageName}`; + return imageName; } - async deleteImage(nombre: string): Promise { + async deleteImage(id: string): Promise { Logger.debug('delete image'); - const sanitizedFileName = nombre.replace(/\s+/g, '_').toLowerCase(); const imageDir = path.join(process.cwd(), 'imagenes'); - const imagePath = path.join(imageDir, sanitizedFileName); + const imagePath = path.join(imageDir, id); const extensions = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp', 'avif']; @@ -53,19 +50,18 @@ export class ImagenService { break; } } - + if (!fileFound) { console.log('Image not found, but continuing to save the new image.'); - // No se lanza excepción } } - async modify(fotografia: string, nombre: string): Promise { + async modify(fotografia: string, id: string): Promise { if (!fotografia) { throw new BadRequestException('No image provided'); } - await this.deleteImage(nombre); + await this.deleteImage(id); // Extract the image extension const matches = fotografia.match(/^data:image\/([a-zA-Z]+);base64,/); @@ -76,8 +72,7 @@ export class ImagenService { // Decode base64 string and save the image const buffer = Buffer.from(fotografia.split(',')[1], 'base64'); - const sanitizedFileName = nombre.replace(/\s+/g, '_').toLowerCase(); - const imageName = `${sanitizedFileName}.${ext}`; + const imageName = `${id}.${ext}`; // Save image to the 'imagenes' directory in the root of the project const imagePath = path.join(process.cwd(), 'imagenes', imageName); @@ -88,8 +83,8 @@ export class ImagenService { fs.mkdirSync(dirPath, { recursive: true }); } - await sharp(buffer).toFile(imagePath); + fs.writeFileSync(imagePath, buffer); - return `/imagenes/${imageName}`; + return imageName; } } diff --git a/src/main.ts b/src/main.ts index 949c1e2..68a6e1b 100644 --- a/src/main.ts +++ b/src/main.ts @@ -2,14 +2,19 @@ import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; import { NestExpressApplication } from '@nestjs/platform-express'; import * as path from 'path'; +import * as bodyParser from 'body-parser'; async function bootstrap() { const app = await NestFactory.create(AppModule); app.enableCors(); + app.use(bodyParser.json({ limit: '1mb' })); + app.use(bodyParser.urlencoded({ limit: '1mb', extended: true })); + app.useStaticAssets(path.join(__dirname, '..', 'imagenes'), { prefix: '/imagenes/', }); + await app.listen(3000); } bootstrap();