From dd250622eb116208fcb06c411cc6c3095c93664c Mon Sep 17 00:00:00 2001 From: jurgis Date: Sun, 11 Dec 2022 19:51:48 +0200 Subject: [PATCH] all files: add titles, seasons and files list --- src/KodiApi/AllFiles/all-files.controller.ts | 19 ++--- src/KodiApi/AllFiles/all-files.service.ts | 74 +++++++++++++++++++- 2 files changed, 83 insertions(+), 10 deletions(-) diff --git a/src/KodiApi/AllFiles/all-files.controller.ts b/src/KodiApi/AllFiles/all-files.controller.ts index 95ae5c1..1a5cd5e 100644 --- a/src/KodiApi/AllFiles/all-files.controller.ts +++ b/src/KodiApi/AllFiles/all-files.controller.ts @@ -22,18 +22,19 @@ export class AllFilesController { return this.allFilesService.getListOfTitles(TitleTypeEnum.movie); } - @Get('play') - play() { - return { will: 'return a playable stream' }; + @Get('play/:fileId') + play(@Param('fileId') fileId: string) { + return { will: 'return a playable stream of ' + fileId }; } @Get(':titleId/:seasonId?') getTitle( - @Param('titleId') titleId: number, - @Param('seasonId') seasonId: number, - ) { - return { - will: 'show title videos list title ' + titleId + ', season ' + seasonId, - }; + @Param('titleId') titleId: string, //actually a number, how to cast? + @Param('seasonId') seasonId: string, //actually a number, how to cast? + ): Promise { + return this.allFilesService.loadTitle( + parseInt(titleId), + parseInt(seasonId), + ); } } diff --git a/src/KodiApi/AllFiles/all-files.service.ts b/src/KodiApi/AllFiles/all-files.service.ts index 5891414..2e80b6e 100644 --- a/src/KodiApi/AllFiles/all-files.service.ts +++ b/src/KodiApi/AllFiles/all-files.service.ts @@ -48,9 +48,81 @@ export class AllFilesService { .createItem() .setLabel(entity.title) .setToFolder() - .setPlot(entity.files.length + ' files'); + .setPlot(entity.files.length + ' files') + .setPath('all/' + entity.id); }); return response; } + + async loadTitle(titleId: number, seasonId: number): Promise { + const titleEntity = await this.titleRepository.findOne({ + relations: { files: true }, + where: { id: titleId }, + }); + + if (titleEntity === null) { + return this.koiApiResponseFactory + .createApiResponse() + .setTitle('Not found ' + titleId); + } + + if (titleEntity.type === TitleTypeEnum.movie) { + return this.buildFilesResponse(titleEntity.title, titleEntity.files); + } + + if (seasonId) { + return this.buildFilesResponse( + titleEntity.title, + titleEntity.files.filter( + (fileEntity) => fileEntity.season === seasonId, + ), + ); + } + + return this.buildSeasonsResponse(titleEntity); + } + + private buildFilesResponse( + title: string, + fileEntities: Array, + ): ApiResponse { + const apiResponse = this.koiApiResponseFactory.createApiResponse(); + apiResponse.setTitle(title); + for (const fileEntity of fileEntities) { + apiResponse + .createItem() + .setLabel(fileEntity.fileName) + .setPath('/api?path=all/play/' + fileEntity.id) + .setToPlayable() + .setPlot('Size ' + fileEntity.size) + .setSize(fileEntity.size) + .setDuration(fileEntity.duration); + } + + return apiResponse; + } + + private buildSeasonsResponse(titleEntity: TitleEntity): ApiResponse { + const apiResponse = this.koiApiResponseFactory.createApiResponse(); + apiResponse.setTitle(titleEntity.title); + + //todo - add num files count + const seasons = []; + for (const fileEntity of titleEntity.files) { + if (seasons.indexOf(fileEntity.season) === -1) { + seasons.push(fileEntity.season); + } + } + + for (const season of seasons) { + apiResponse + .createItem() + .setLabel(season + ' Sezonas') + .setToFolder() + .setPath(`all/${titleEntity.id}/${season}`); + } + + return apiResponse; + } }