This commit is contained in:
IO
2024-07-05 19:27:20 -06:00
parent 3c67e8e61c
commit 6f8cff7c2d
5 changed files with 64 additions and 61 deletions
+2
View File
@@ -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,
+8 -60
View File
@@ -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 };
}
+10
View File
@@ -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 {}
+37
View File
@@ -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<string> {
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}`;
}
}
+7 -1
View File
@@ -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<NestExpressApplication>(AppModule);
app.enableCors();
app.useStaticAssets(path.join(__dirname, '..', 'imagenes'), {
prefix: '/imagenes/',
});
await app.listen(3000);
}
bootstrap();