diff --git a/back/src/artist/artist.controller.ts b/back/src/artist/artist.controller.ts index 2946e0b..56b4606 100644 --- a/back/src/artist/artist.controller.ts +++ b/back/src/artist/artist.controller.ts @@ -6,12 +6,14 @@ import { DefaultValuePipe, Delete, Get, + InternalServerErrorException, NotFoundException, Param, ParseIntPipe, Post, Query, Req, + StreamableFile, } from '@nestjs/common'; import { Plage } from 'src/models/plage'; import { CreateArtistDto } from './dto/create-artist.dto'; @@ -19,11 +21,12 @@ import { Request } from 'express'; import { ArtistService } from './artist.service'; import { Prisma, Artist } from '@prisma/client'; import { ApiTags } from '@nestjs/swagger'; +import { createReadStream } from 'fs'; @Controller('artist') @ApiTags('artist') export class ArtistController { - constructor(private readonly service: ArtistService) {} + constructor(private readonly service: ArtistService) { } @Post() async create(@Body() dto: CreateArtistDto) { @@ -39,6 +42,20 @@ export class ArtistController { return await this.service.delete({ id }); } + @Get(':id/illustration') + async getIllustration(@Param('id', ParseIntPipe) id: number) { + const artist = await this.service.get({ id }); + if (!artist) throw new NotFoundException('Artist not found'); + const path = `/assets/artists/${artist.name}/illustration.png`; + + try { + const file = createReadStream(path); + return new StreamableFile(file); + } catch { + throw new InternalServerErrorException(); + } + } + @Get() async findAll( @Req() req: Request, diff --git a/back/src/genre/genre.controller.ts b/back/src/genre/genre.controller.ts index 80e5b8d..b349a44 100644 --- a/back/src/genre/genre.controller.ts +++ b/back/src/genre/genre.controller.ts @@ -6,12 +6,14 @@ import { DefaultValuePipe, Delete, Get, + InternalServerErrorException, NotFoundException, Param, ParseIntPipe, Post, Query, Req, + StreamableFile, } from '@nestjs/common'; import { Plage } from 'src/models/plage'; import { CreateGenreDto } from './dto/create-genre.dto'; @@ -19,11 +21,12 @@ import { Request } from 'express'; import { GenreService } from './genre.service'; import { Prisma, Genre } from '@prisma/client'; import { ApiTags } from '@nestjs/swagger'; +import { createReadStream } from 'fs'; @Controller('genre') @ApiTags('genre') export class GenreController { - constructor(private readonly service: GenreService) {} + constructor(private readonly service: GenreService) { } @Post() async create(@Body() dto: CreateGenreDto) { @@ -39,6 +42,20 @@ export class GenreController { return await this.service.delete({ id }); } + @Get(':id/illustration') + async getIllustration(@Param('id', ParseIntPipe) id: number) { + const genre = await this.service.get({ id }); + if (!genre) throw new NotFoundException('Genre not found'); + const path = `/assets/genres/${genre.name}/illustration.png`; + + try { + const file = createReadStream(path); + return new StreamableFile(file); + } catch { + throw new InternalServerErrorException(); + } + } + @Get() async findAll( @Req() req: Request,