Add Random Image
This commit is contained in:
2
src/dto
2
src/dto
Submodule src/dto updated: 12938c8125...78d9d15d64
@@ -1,50 +1,115 @@
|
|||||||
import { BadRequestException, Controller, Get, Header, Inject, Param, Res } from "@nestjs/common";
|
import { Controller, Get, Header, Inject, Param, Res } from '@nestjs/common';
|
||||||
import { GalerieService } from "./galerie.service";
|
import { GalerieService } from './galerie.service';
|
||||||
import sharp from "sharp";
|
import sharp from 'sharp';
|
||||||
import path from "path";
|
import path from 'path';
|
||||||
|
import { Args, Field, ObjectType, Query, Resolver } from '@nestjs/graphql';
|
||||||
|
import { JahrMitBild } from 'src/dto/galerie.dto';
|
||||||
|
|
||||||
@Controller('/api/galerie')
|
@Controller('/api/galerie')
|
||||||
export class GalerieController {
|
export class GalerieController {
|
||||||
constructor(
|
constructor(
|
||||||
@Inject()
|
@Inject()
|
||||||
private readonly galerieService: GalerieService
|
private readonly galerieService: GalerieService,
|
||||||
){}
|
) {}
|
||||||
|
|
||||||
@Get("/bilder/:event/:jahr")
|
@Get('/bilder/:event/:jahr')
|
||||||
async getBilder(
|
async getBilder(
|
||||||
@Param("event")
|
@Param('event') event: string,
|
||||||
event: string,
|
@Param('jahr') jahr: number,
|
||||||
@Param("jahr")
|
): Promise<string[]> {
|
||||||
jahr: string
|
return this.galerieService.getBilder(event, jahr);
|
||||||
): Promise<string[]> {
|
}
|
||||||
return this.galerieService.getBilder(event, jahr);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
@Get('/jahre/:event')
|
||||||
|
async getJahre(@Param('event') event: string): Promise<JahrMitBild[]> {
|
||||||
|
return this.galerieService.getJahre(event);
|
||||||
|
}
|
||||||
|
|
||||||
@Get("/jahre/:event")
|
@Get('/bild/:event/:jahr/:bild')
|
||||||
async getJahre(
|
@Header('Content-Type', 'image/png')
|
||||||
@Param("event")
|
async getBild(
|
||||||
event: string
|
@Param('event')
|
||||||
): Promise<string[]> {
|
event: string,
|
||||||
return this.galerieService.getJahre(event);
|
@Param('jahr')
|
||||||
}
|
jahr: number,
|
||||||
|
@Param('bild')
|
||||||
|
bild: string,
|
||||||
|
): Promise<Buffer> {
|
||||||
|
const filePath =
|
||||||
|
'/bilder/' +
|
||||||
|
path.basename(event) +
|
||||||
|
'/' +
|
||||||
|
path.basename(jahr.toString()) +
|
||||||
|
'/' +
|
||||||
|
path.basename(bild);
|
||||||
|
|
||||||
@Get("/bild/:event/:jahr/:bild")
|
return sharp(filePath).png().toBuffer();
|
||||||
@Header('Content-Type', 'image/png')
|
}
|
||||||
async getBild(
|
@Get('/randomimage/:event')
|
||||||
@Param("event")
|
@Header('Content-Type', 'image/png')
|
||||||
event: string,
|
async getRandomImageOhneJahr(
|
||||||
@Param("jahr")
|
@Param('event')
|
||||||
jahr: string,
|
event: string,
|
||||||
@Param("bild")
|
): Promise<Buffer> {
|
||||||
bild: string,
|
const jahr=await this.galerieService.getRandomYear(event)
|
||||||
): Promise<Buffer> {
|
const filePath =
|
||||||
|
'/bilder/' +
|
||||||
|
path.basename(event) +
|
||||||
|
'/' +
|
||||||
|
jahr+
|
||||||
|
'/' +
|
||||||
|
await this.galerieService.getRandomImage(event,jahr)
|
||||||
|
|
||||||
const filePath="/bilder/"+path.basename(event)+"/"+path.basename(jahr)+"/"+path.basename(bild);
|
return sharp(filePath).png().toBuffer();
|
||||||
|
}
|
||||||
|
@Get('/randomimage/:event/:jahr')
|
||||||
return sharp(filePath).png().toBuffer();
|
@Header('Content-Type', 'image/png')
|
||||||
|
async getRandomImage(
|
||||||
}
|
@Param('event')
|
||||||
|
event: string,
|
||||||
|
@Param('jahr')
|
||||||
|
jahr: number,
|
||||||
|
): Promise<Buffer> {
|
||||||
|
const filePath =
|
||||||
|
'/bilder/' +
|
||||||
|
path.basename(event) +
|
||||||
|
'/' +
|
||||||
|
path.basename(jahr.toString()) +
|
||||||
|
'/' +
|
||||||
|
await this.galerieService.getRandomImage(event,jahr)
|
||||||
|
|
||||||
|
return sharp(filePath).png().toBuffer();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Resolver()
|
||||||
|
export class GalerieResolver {
|
||||||
|
constructor(private readonly galerieService: GalerieService) {}
|
||||||
|
|
||||||
|
@Query(() => [String])
|
||||||
|
async getBilder(
|
||||||
|
@Args('event', { type: () => String }) event: string,
|
||||||
|
@Args('jahr', { type: () => Number }) jahr: number,
|
||||||
|
): Promise<string[]> {
|
||||||
|
return this.galerieService.getBilder(event, jahr);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Query(() => [JahrMitBildEntity])
|
||||||
|
async bilder(
|
||||||
|
@Args('event', { type: () => String }) event: string,
|
||||||
|
): Promise<JahrMitBildEntity[]> {
|
||||||
|
return this.galerieService.getJahre(event);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@ObjectType()
|
||||||
|
export class JahrMitBildEntity implements JahrMitBild {
|
||||||
|
@Field(() => Number)
|
||||||
|
jahr: number;
|
||||||
|
@Field(() => [String])
|
||||||
|
bild: string[];
|
||||||
|
constructor(jahr: number, bild: string[]) {
|
||||||
|
this.jahr = jahr;
|
||||||
|
this.bild = bild;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { Module } from '@nestjs/common';
|
import { Module } from '@nestjs/common';
|
||||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||||
import { GalerieController } from './galerie.controller';
|
import { GalerieController, GalerieResolver } from './galerie.controller';
|
||||||
import { GalerieService } from './galerie.service';
|
import { GalerieService } from './galerie.service';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
@@ -11,7 +11,7 @@ import { GalerieService } from './galerie.service';
|
|||||||
GalerieController
|
GalerieController
|
||||||
],
|
],
|
||||||
providers: [
|
providers: [
|
||||||
GalerieService, GalerieController
|
GalerieService, GalerieResolver
|
||||||
]
|
]
|
||||||
})
|
})
|
||||||
export class GalerieModule {}
|
export class GalerieModule {}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { Injectable } from "@nestjs/common";
|
import { Injectable } from "@nestjs/common";
|
||||||
import { promises as fs } from 'fs';
|
import { promises as fs } from 'fs';
|
||||||
import * as path from 'path';
|
import * as path from 'path';
|
||||||
|
import { JahrMitBild } from "src/dto/galerie.dto";
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class GalerieService {
|
export class GalerieService {
|
||||||
@@ -11,9 +12,9 @@ export class GalerieService {
|
|||||||
|
|
||||||
async getBilder(
|
async getBilder(
|
||||||
event: string,
|
event: string,
|
||||||
jahr: string
|
jahr: number
|
||||||
): Promise<string[]> {
|
): Promise<string[]> {
|
||||||
const files= await fs.readdir(path.join(this.imagesPath,event, jahr));
|
const files= await fs.readdir(path.join(this.imagesPath,event, jahr.toString()));
|
||||||
|
|
||||||
// Nur Bilddateien filtern
|
// Nur Bilddateien filtern
|
||||||
const imageFiles = files.filter(file =>
|
const imageFiles = files.filter(file =>
|
||||||
@@ -25,25 +26,36 @@ export class GalerieService {
|
|||||||
|
|
||||||
async getJahre(
|
async getJahre(
|
||||||
event: string
|
event: string
|
||||||
): Promise<string[]> {
|
): Promise<JahrMitBild[]> {
|
||||||
const files= await fs.readdir(path.join(this.imagesPath,event));
|
const files= await fs.readdir(path.join(this.imagesPath,event).toString());
|
||||||
|
|
||||||
// Nur Verzeichnisse filtern (Jahre)
|
// Nur Verzeichnisse filtern (Jahre)
|
||||||
const directories: string[] = [];
|
const directories: JahrMitBild[] = [];
|
||||||
for (const file of files) {
|
for (const file of files) {
|
||||||
const filePath = path.join(this.imagesPath, event, file);
|
const filePath = path.join(this.imagesPath, event, file);
|
||||||
const stat = await fs.stat(filePath);
|
const stat = await fs.stat(filePath);
|
||||||
if (stat.isDirectory()) {
|
if (stat.isDirectory()) {
|
||||||
directories.push(file);
|
directories.push(new JahrMitBild(parseInt(file), await this.getBilder(event,parseInt(file))));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return directories;
|
return directories;
|
||||||
}
|
}
|
||||||
|
|
||||||
async getBild(
|
async getRandomImage(event: string, jahr: number): Promise<string>{
|
||||||
|
|
||||||
|
const bilder=await this.getBilder(event,jahr);
|
||||||
|
|
||||||
|
const index = Math.floor(Math.random() * bilder.length);
|
||||||
|
|
||||||
) {
|
return bilder[index];
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
async getRandomYear(event:string): Promise<number>{
|
||||||
|
const jahre=(await this.getJahre(event)).map((jahr)=>jahr.jahr);
|
||||||
|
|
||||||
|
const index = Math.floor(Math.random() * jahre.length);
|
||||||
|
return jahre[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user