Add Random Image
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { Injectable } from "@nestjs/common";
|
||||
import { promises as fs } from 'fs';
|
||||
import * as path from 'path';
|
||||
import { JahrMitBild } from "src/dto/galerie.dto";
|
||||
|
||||
@Injectable()
|
||||
export class GalerieService {
|
||||
@@ -11,9 +12,9 @@ export class GalerieService {
|
||||
|
||||
async getBilder(
|
||||
event: string,
|
||||
jahr: string
|
||||
jahr: number
|
||||
): 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
|
||||
const imageFiles = files.filter(file =>
|
||||
@@ -25,25 +26,36 @@ export class GalerieService {
|
||||
|
||||
async getJahre(
|
||||
event: string
|
||||
): Promise<string[]> {
|
||||
const files= await fs.readdir(path.join(this.imagesPath,event));
|
||||
): Promise<JahrMitBild[]> {
|
||||
const files= await fs.readdir(path.join(this.imagesPath,event).toString());
|
||||
|
||||
// Nur Verzeichnisse filtern (Jahre)
|
||||
const directories: string[] = [];
|
||||
const directories: JahrMitBild[] = [];
|
||||
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);
|
||||
directories.push(new JahrMitBild(parseInt(file), await this.getBilder(event,parseInt(file))));
|
||||
}
|
||||
}
|
||||
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