From 3ab76006afaeeb0d5e6536b8ffcf641700686559 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Tue, 25 Apr 2023 00:37:21 +0900 Subject: [PATCH] Create a proxy for the trasncoder --- back/src/Kyoo.Core/CoreModule.cs | 3 +- back/src/Kyoo.Core/Kyoo.Core.csproj | 1 + back/src/Kyoo.Core/Views/Watch/ProxyApi.cs | 48 +++++++ back/src/Kyoo.Core/Views/Watch/VideoApi.cs | 146 --------------------- docker-compose.dev.yml | 2 + docker-compose.prod.yml | 2 + docker-compose.yml | 2 + 7 files changed, 57 insertions(+), 147 deletions(-) create mode 100644 back/src/Kyoo.Core/Views/Watch/ProxyApi.cs delete mode 100644 back/src/Kyoo.Core/Views/Watch/VideoApi.cs diff --git a/back/src/Kyoo.Core/CoreModule.cs b/back/src/Kyoo.Core/CoreModule.cs index 5e808c9a..e1613c5c 100644 --- a/back/src/Kyoo.Core/CoreModule.cs +++ b/back/src/Kyoo.Core/CoreModule.cs @@ -16,9 +16,9 @@ // You should have received a copy of the GNU General Public License // along with Kyoo. If not, see . -using System; using System.Collections.Generic; using System.Linq; +using AspNetCore.Proxy; using Autofac; using Kyoo.Abstractions; using Kyoo.Abstractions.Controllers; @@ -113,6 +113,7 @@ namespace Kyoo.Core x.EnableForHttps = true; }); + services.AddProxies(); services.AddHttpClient(); } diff --git a/back/src/Kyoo.Core/Kyoo.Core.csproj b/back/src/Kyoo.Core/Kyoo.Core.csproj index 8f113039..7342f457 100644 --- a/back/src/Kyoo.Core/Kyoo.Core.csproj +++ b/back/src/Kyoo.Core/Kyoo.Core.csproj @@ -12,6 +12,7 @@ + diff --git a/back/src/Kyoo.Core/Views/Watch/ProxyApi.cs b/back/src/Kyoo.Core/Views/Watch/ProxyApi.cs new file mode 100644 index 00000000..207700a1 --- /dev/null +++ b/back/src/Kyoo.Core/Views/Watch/ProxyApi.cs @@ -0,0 +1,48 @@ +// Kyoo - A portable and vast media library solution. +// Copyright (c) Kyoo. +// +// See AUTHORS.md and LICENSE file in the project root for full license information. +// +// Kyoo is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// any later version. +// +// Kyoo is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Kyoo. If not, see . + +using System.Threading.Tasks; +using AspNetCore.Proxy; +using Kyoo.Abstractions.Models.Permissions; +using Microsoft.AspNetCore.Mvc; + +namespace Kyoo.Core.Api +{ + /// + /// Proxy to other services + /// + [ApiController] + public class ProxyApi : Controller + { + /// + /// Transcoder proxy + /// + /// + /// Simply proxy requests to the transcoder + /// + /// The path of the transcoder. + /// The return value of the transcoder. + [Route("video/{**rest}")] + [Permission("video", Kind.Read)] + public Task Proxy(string rest) + { + // TODO: Use an env var to configure transcoder:7666. + return this.HttpProxyAsync($"http://transcoder:7666/{rest}"); + } + } +} diff --git a/back/src/Kyoo.Core/Views/Watch/VideoApi.cs b/back/src/Kyoo.Core/Views/Watch/VideoApi.cs deleted file mode 100644 index 40f8c881..00000000 --- a/back/src/Kyoo.Core/Views/Watch/VideoApi.cs +++ /dev/null @@ -1,146 +0,0 @@ -// Kyoo - A portable and vast media library solution. -// Copyright (c) Kyoo. -// -// See AUTHORS.md and LICENSE file in the project root for full license information. -// -// Kyoo is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// any later version. -// -// Kyoo is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with Kyoo. If not, see . - -using System.IO; -using System.Threading.Tasks; -using Kyoo.Abstractions.Controllers; -using Kyoo.Abstractions.Models; -using Kyoo.Abstractions.Models.Attributes; -using Kyoo.Abstractions.Models.Permissions; -using Kyoo.Abstractions.Models.Utils; -using Kyoo.Core.Models.Options; -using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.Mvc.Filters; -using Microsoft.Extensions.Options; -using static Kyoo.Abstractions.Models.Utils.Constants; - -namespace Kyoo.Core.Api -{ - /// - /// Get the video in a raw format or transcoded in the codec you want. - /// - [Route("videos")] - [Route("video", Order = AlternativeRoute)] - [ApiController] - [ApiDefinition("Videos", Group = WatchGroup)] - public class VideoApi : Controller - { - /// - /// The library manager used to modify or retrieve information in the data store. - /// - private readonly ILibraryManager _libraryManager; - - /// - /// The file system used to send video files. - /// - private readonly IFileSystem _files; - - /// - /// Create a new . - /// - /// The library manager used to retrieve episodes. - /// The file manager used to send video files. - public VideoApi(ILibraryManager libraryManager, - IFileSystem files) - { - _libraryManager = libraryManager; - _files = files; - } - - /// - /// - /// Disabling the cache prevent an issue on firefox that skip the last 30 seconds of HLS files - /// - public override void OnActionExecuted(ActionExecutedContext ctx) - { - base.OnActionExecuted(ctx); - ctx.HttpContext.Response.Headers.Add("Cache-Control", "no-cache, no-store, must-revalidate"); - ctx.HttpContext.Response.Headers.Add("Pragma", "no-cache"); - ctx.HttpContext.Response.Headers.Add("Expires", "0"); - } - - /// - /// Direct video - /// - /// - /// Retrieve the raw video stream, in the same container as the one on the server. No transcoding or - /// transmuxing is done. - /// - /// The identifier of the episode to retrieve. - /// The raw video stream - /// No episode exists for the given identifier. - // TODO enable the following line, this is disabled since the web app can't use bearers. [Permission("video", Kind.Read)] - [HttpGet("direct/{identifier:id}")] - [HttpGet("{identifier:id}", Order = AlternativeRoute)] - [ProducesResponseType(StatusCodes.Status200OK)] - [ProducesResponseType(StatusCodes.Status404NotFound)] - public async Task Direct(Identifier identifier) - { - Episode episode = await identifier.Match( - id => _libraryManager.GetOrDefault(id), - slug => _libraryManager.GetOrDefault(slug) - ); - return _files.FileResult(episode?.Path, true); - } - - /// - /// Transmux video - /// - /// - /// Change the container of the video to hls but don't re-encode the video or audio. This doesn't require mutch - /// resources from the server. - /// - /// The identifier of the episode to retrieve. - /// The transmuxed video stream - /// No episode exists for the given identifier. - [HttpGet("transmux/{identifier:id}/master.m3u8")] - [Permission("video", Kind.Read)] - [ProducesResponseType(StatusCodes.Status200OK)] - [ProducesResponseType(StatusCodes.Status404NotFound)] - public async Task Transmux(Identifier identifier) - { - Episode episode = await identifier.Match( - id => _libraryManager.GetOrDefault(id), - slug => _libraryManager.GetOrDefault(slug) - ); - return _files.Transmux(episode); - } - - /// - /// Transmuxed chunk - /// - /// - /// Retrieve a chunk of a transmuxed video. - /// - /// The identifier of the episode. - /// The identifier of the chunk to retrieve. - /// The options used to retrieve the path of the segments. - /// A transmuxed video chunk. - [HttpGet("transmux/{episodeLink}/segments/{chunk}", Order = AlternativeRoute)] - [Permission("video", Kind.Read)] - [ProducesResponseType(StatusCodes.Status200OK)] - public IActionResult GetTransmuxedChunk(string episodeLink, string chunk, - [FromServices] IOptions options) - { - string path = Path.GetFullPath(Path.Combine(options.Value.TransmuxPath, episodeLink)); - path = Path.Combine(path, "segments", chunk); - return PhysicalFile(path, "video/MP2T"); - } - } -} diff --git a/docker-compose.dev.yml b/docker-compose.dev.yml index e27b7d65..b510fa5b 100644 --- a/docker-compose.dev.yml +++ b/docker-compose.dev.yml @@ -57,6 +57,8 @@ services: ports: - "7666:7666" restart: on-failure + env_file: + - ./.env volumes: - ./transcoder:/app - ${LIBRARY_ROOT}:/video diff --git a/docker-compose.prod.yml b/docker-compose.prod.yml index 74b42dc8..7e72a154 100644 --- a/docker-compose.prod.yml +++ b/docker-compose.prod.yml @@ -37,6 +37,8 @@ services: transcoder: image: zoriya/kyoo_transcoder:edge restart: on-failure + env_file: + - ./.env volumes: - ${LIBRARY_ROOT}:/video - ${CACHE_ROOT}:/cache diff --git a/docker-compose.yml b/docker-compose.yml index b5d9fafa..1885e9e5 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -37,6 +37,8 @@ services: transcoder: build: ./transcoder restart: on-failure + env_file: + - ./.env volumes: - ${LIBRARY_ROOT}:/video - ${CACHE_ROOT}:/cache