Add Galerie Controller
This commit is contained in:
@@ -11,6 +11,7 @@ import { FormularEntity } from './formular/formular.entity';
|
||||
import { FieldEntity } from './formular/fields.entity';
|
||||
import { FormResultEntity } from './formular/result.entity';
|
||||
import { FormResultsEntity } from './formular/results.entity';
|
||||
import { GalerieModule } from './galerie/galerie.module';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
@@ -31,6 +32,7 @@ import { FormResultsEntity } from './formular/results.entity';
|
||||
entities: [EventEntity, FormularEntity,FieldEntity,FormResultEntity,FormResultsEntity]
|
||||
}),
|
||||
FormularModule,
|
||||
GalerieModule
|
||||
|
||||
],
|
||||
controllers: [AppController],
|
||||
|
||||
50
src/galerie/galerie.controller.ts
Normal file
50
src/galerie/galerie.controller.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import { BadRequestException, Controller, Get, Header, Inject, Param, Res } from "@nestjs/common";
|
||||
import { GalerieService } from "./galerie.service";
|
||||
import sharp from "sharp";
|
||||
import path from "path";
|
||||
|
||||
@Controller('/api/galerie')
|
||||
export class GalerieController {
|
||||
constructor(
|
||||
@Inject()
|
||||
private readonly galerieService: GalerieService
|
||||
){}
|
||||
|
||||
@Get("/bilder/:event/:jahr")
|
||||
async getBilder(
|
||||
@Param("event")
|
||||
event: string,
|
||||
@Param("jahr")
|
||||
jahr: string
|
||||
): Promise<string[]> {
|
||||
return this.galerieService.getBilder(event, jahr);
|
||||
}
|
||||
|
||||
|
||||
@Get("/jahre/:event")
|
||||
async getJahre(
|
||||
@Param("event")
|
||||
event: string
|
||||
): Promise<string[]> {
|
||||
return this.galerieService.getJahre(event);
|
||||
}
|
||||
|
||||
@Get("/bild/:event/:jahr/:bild")
|
||||
@Header('Content-Type', 'image/png')
|
||||
async getBild(
|
||||
@Param("event")
|
||||
event: string,
|
||||
@Param("jahr")
|
||||
jahr: string,
|
||||
@Param("bild")
|
||||
bild: string,
|
||||
): Promise<Buffer> {
|
||||
|
||||
const filePath="/bilder/"+path.basename(event)+"/"+path.basename(jahr)+"/"+path.basename(bild);
|
||||
|
||||
|
||||
return sharp(filePath).png().toBuffer();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
17
src/galerie/galerie.module.ts
Normal file
17
src/galerie/galerie.module.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { GalerieController } from './galerie.controller';
|
||||
import { GalerieService } from './galerie.service';
|
||||
|
||||
@Module({
|
||||
imports:[
|
||||
TypeOrmModule.forFeature([])
|
||||
],
|
||||
controllers: [
|
||||
GalerieController
|
||||
],
|
||||
providers: [
|
||||
GalerieService, GalerieController
|
||||
]
|
||||
})
|
||||
export class GalerieModule {}
|
||||
49
src/galerie/galerie.service.ts
Normal file
49
src/galerie/galerie.service.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
import { Injectable } from "@nestjs/common";
|
||||
import { promises as fs } from 'fs';
|
||||
import * as path from 'path';
|
||||
|
||||
@Injectable()
|
||||
export class GalerieService {
|
||||
private readonly imagesPath = "/bilder";
|
||||
|
||||
constructor(
|
||||
){}
|
||||
|
||||
async getBilder(
|
||||
event: string,
|
||||
jahr: string
|
||||
): Promise<string[]> {
|
||||
const files= await fs.readdir(path.join(this.imagesPath,event, jahr));
|
||||
|
||||
// Nur Bilddateien filtern
|
||||
const imageFiles = files.filter(file =>
|
||||
/\.(jpg|jpeg|png|gif|webp)$/i.test(file)
|
||||
);
|
||||
|
||||
return imageFiles;
|
||||
}
|
||||
|
||||
async getJahre(
|
||||
event: string
|
||||
): Promise<string[]> {
|
||||
const files= await fs.readdir(path.join(this.imagesPath,event));
|
||||
|
||||
// Nur Verzeichnisse filtern (Jahre)
|
||||
const directories: string[] = [];
|
||||
for (const file of files) {
|
||||
const filePath = path.join(this.imagesPath, event, file);
|
||||
const stat = await fs.stat(filePath);
|
||||
if (stat.isDirectory()) {
|
||||
directories.push(file);
|
||||
}
|
||||
}
|
||||
return directories;
|
||||
}
|
||||
|
||||
async getBild(
|
||||
|
||||
|
||||
) {
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user