almacenar img (sin terminar)
This commit is contained in:
@@ -19,6 +19,7 @@ import { RolesModule } from './features/roles/roles.module';
|
||||
import { UserRolesModule } from './features/user-roles/user-roles.module';
|
||||
import { SurveysModule } from './features/surveys/surveys.module';
|
||||
import {InventoryModule} from './features/inventory/inventory.module'
|
||||
import { PicturesModule } from './features/pictures/pictures.module';
|
||||
|
||||
@Module({
|
||||
providers: [
|
||||
@@ -59,7 +60,8 @@ import {InventoryModule} from './features/inventory/inventory.module'
|
||||
ConfigurationsModule,
|
||||
SurveysModule,
|
||||
LocationModule,
|
||||
InventoryModule
|
||||
InventoryModule,
|
||||
PicturesModule
|
||||
],
|
||||
})
|
||||
export class AppModule {}
|
||||
|
||||
15
apps/api/src/features/pictures/pictures.controller.ts
Normal file
15
apps/api/src/features/pictures/pictures.controller.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
|
||||
import { Controller, Post, UploadedFiles, UseInterceptors } from '@nestjs/common';
|
||||
import { FilesInterceptor } from '@nestjs/platform-express';
|
||||
import { PicturesService } from './pictures.service';
|
||||
|
||||
@Controller('pictures')
|
||||
export class PicturesController {
|
||||
constructor(private readonly picturesService: PicturesService) {}
|
||||
|
||||
@Post('upload')
|
||||
@UseInterceptors(FilesInterceptor('files'))
|
||||
async uploadFile(@UploadedFiles() files: Express.Multer.File[]) {
|
||||
return this.picturesService.saveImages(files);
|
||||
}
|
||||
}
|
||||
10
apps/api/src/features/pictures/pictures.module.ts
Normal file
10
apps/api/src/features/pictures/pictures.module.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
|
||||
import { Module } from '@nestjs/common';
|
||||
import { PicturesController } from './pictures.controller';
|
||||
import { PicturesService } from './pictures.service';
|
||||
|
||||
@Module({
|
||||
controllers: [PicturesController],
|
||||
providers: [PicturesService],
|
||||
})
|
||||
export class PicturesModule {}
|
||||
43
apps/api/src/features/pictures/pictures.service.ts
Normal file
43
apps/api/src/features/pictures/pictures.service.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { writeFile } from 'fs/promises';
|
||||
import { join } from 'path';
|
||||
|
||||
@Injectable()
|
||||
export class PicturesService {
|
||||
/**
|
||||
* Guarda una imagen en el directorio de imágenes.
|
||||
* @param file - El archivo de imagen a guardar.
|
||||
* @returns La ruta de la imagen guardada.
|
||||
*/
|
||||
async saveImages(file: Express.Multer.File[]): Promise<string[]> {
|
||||
|
||||
const picturesPath = join(__dirname, '..', '..', 'pictures');
|
||||
|
||||
let images : string[] = [];
|
||||
|
||||
|
||||
file.forEach(async (pic) => {
|
||||
const fileName = `${Date.now()}-${pic.originalname}`;
|
||||
const filePath = join(picturesPath, fileName);
|
||||
await writeFile(filePath, pic.buffer);
|
||||
images.push(`/pictures/${fileName}`);
|
||||
});
|
||||
|
||||
return images;
|
||||
|
||||
|
||||
// // Construye la ruta al directorio de imágenes.
|
||||
// const picturesPath = join(__dirname, '..', '..', 'pictures');
|
||||
// // Crea un nombre de archivo único para la imagen.
|
||||
// const fileName = `${Date.now()}-${file.originalname}`;
|
||||
// // Construye la ruta completa al archivo de imagen.
|
||||
// const filePath = join(picturesPath, fileName);
|
||||
|
||||
// // Escribe el archivo de imagen en el disco.
|
||||
// await writeFile(filePath, file.buffer);
|
||||
|
||||
// // Devuelve la ruta de la imagen guardada.
|
||||
// return `/pictures/${fileName}`;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user