diff --git a/src/app.module.ts b/src/app.module.ts index 4b7e4ee..ffa74a9 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -22,6 +22,7 @@ import { CategoriaModule } from "./categoria/categoria.module"; import { DatosAcademicosModule } from './datos_academicos/datos_academicos.module'; import { LineasInvestigacionModule } from './lineas_investigacion/lineas_investigacion.module'; import { ProyectosAcademicosModule } from './proyectos_academicos/proyectos_academicos.module'; +import { ImagenModule } from './images/images.module'; @Module({ imports: [ @@ -55,6 +56,7 @@ import { ProyectosAcademicosModule } from './proyectos_academicos/proyectos_acad synchronize: true, }), }), + ImagenModule, ProfesorModule, AdscripcionModule, EdificioModule, diff --git a/src/images/images.controller.ts b/src/images/images.controller.ts index 92a6318..5156199 100644 --- a/src/images/images.controller.ts +++ b/src/images/images.controller.ts @@ -4,46 +4,18 @@ import { Body, Put, Param, - BadRequestException, } from '@nestjs/common'; -import { ProfesorService } from '../Profesor/profesor.service' -import * as fs from 'fs'; -import * as path from 'path'; -import * as sharp from 'sharp'; +import { ImagenService } from './images.service'; -@Controller('profesor') -export class ProfesorController { - constructor(private readonly profesorService: ProfesorService) {} +@Controller('imagen') +export class ImagenController { + constructor(private readonly imagenService: ImagenService) {} @Post() async upload(@Body() body) { - const { id_profesor, fotografia,nombre } = body; - - if (!fotografia) { - throw new BadRequestException('No image provided'); - } - - // Extract the image extension - const matches = fotografia.match(/^data:image\/([a-zA-Z]+);base64,/); - if (!matches) { - throw new BadRequestException('Invalid image format'); - } - const ext = matches[1]; - - // 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 imagePath = path.join(__dirname, '..', 'imagenes', imageName); - - await sharp(buffer) - .resize(200, 200) // Resize the image if needed - .toFile(imagePath); - - const imageUrl = `/imagenes/${imageName}`; - - // Update the professor's image URL in the database - await this.profesorService.updateImageURL(id_profesor, imageUrl); + const { fotografia, nombre } = body; + + const imageUrl = await this.imagenService.saveImage(fotografia, nombre); return { message: 'Image uploaded successfully', imageUrl }; } @@ -52,31 +24,7 @@ export class ProfesorController { async modify(@Param('id_profesor') id_profesor: number, @Body() body) { const { fotografia, nombre } = body; - if (!fotografia) { - throw new BadRequestException('No image provided'); - } - - // Extract the image extension - const matches = fotografia.match(/^data:image\/([a-zA-Z]+);base64,/); - if (!matches) { - throw new BadRequestException('Invalid image format'); - } - const ext = matches[1]; - - // 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 imagePath = path.join(__dirname, '..', 'imagenes', imageName); - - await sharp(buffer) - .resize(200, 200) // Resize the image if needed - .toFile(imagePath); - - const imageUrl = `/imagenes/${imageName}`; - - // Update the professor's image URL in the database - await this.profesorService.updateImageURL(id_profesor, imageUrl); + const imageUrl = await this.imagenService.saveImage(fotografia, nombre); return { message: 'Image uploaded successfully', imageUrl }; } diff --git a/src/images/images.module.ts b/src/images/images.module.ts index e69de29..55a1035 100644 --- a/src/images/images.module.ts +++ b/src/images/images.module.ts @@ -0,0 +1,10 @@ +import { Module } from '@nestjs/common'; +import { ImagenController } from './images.controller'; +import { ImagenService } from './images.service'; + +@Module({ + controllers: [ImagenController], + providers: [ImagenService], +}) +export class ImagenModule {} + diff --git a/src/images/images.service.ts b/src/images/images.service.ts index e69de29..5f15110 100644 --- a/src/images/images.service.ts +++ b/src/images/images.service.ts @@ -0,0 +1,37 @@ +import { Injectable, BadRequestException } 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 { + if (!fotografia) { + throw new BadRequestException('No image provided'); + } + + // Extract the image extension + const matches = fotografia.match(/^data:image\/([a-zA-Z]+);base64,/); + if (!matches) { + throw new BadRequestException('Invalid image format'); + } + const ext = matches[1]; + + // 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 imagePath = path.join(__dirname, '..', 'imagenes', imageName); + + // Ensure the directory exists + const dirPath = path.join(__dirname, '..', 'imagenes'); + if (!fs.existsSync(dirPath)) { + fs.mkdirSync(dirPath, { recursive: true }); + } + + await sharp(buffer) + .toFile(imagePath); + + return `dist/imagenes/${imageName}`; + } +} diff --git a/src/main.ts b/src/main.ts index da5451c..949c1e2 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,9 +1,15 @@ import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; +import { NestExpressApplication } from '@nestjs/platform-express'; +import * as path from 'path'; async function bootstrap() { - const app = await NestFactory.create(AppModule); + const app = await NestFactory.create(AppModule); app.enableCors(); + + app.useStaticAssets(path.join(__dirname, '..', 'imagenes'), { + prefix: '/imagenes/', + }); await app.listen(3000); } bootstrap();