diff --git a/Kyoo.Common/Controllers/IFileManager.cs b/Kyoo.Common/Controllers/IFileManager.cs index aa845f82..03d13d5d 100644 --- a/Kyoo.Common/Controllers/IFileManager.cs +++ b/Kyoo.Common/Controllers/IFileManager.cs @@ -1,6 +1,8 @@ using System.Collections.Generic; using System.IO; +using System.Threading.Tasks; using JetBrains.Annotations; +using Kyoo.Models; using Microsoft.AspNetCore.Mvc; namespace Kyoo.Controllers @@ -11,11 +13,16 @@ namespace Kyoo.Controllers public StreamReader GetReader([NotNull] string path); - public ICollection ListFiles([NotNull] string path); - // TODO implement a List for directorys, a Exist to check existance and all. + public Task> ListFiles([NotNull] string path); + + public Task Exists([NotNull] string path); // TODO replace every use of System.IO with this to allow custom paths (like uptobox://path) // TODO find a way to handle Transmux/Transcode with this system. - public string GetExtraDirectory(string showPath); + public string GetExtraDirectory(Show show); + + public string GetExtraDirectory(Season season); + + public string GetExtraDirectory(Episode episode); } } \ No newline at end of file diff --git a/Kyoo.Common/Controllers/ITranscoder.cs b/Kyoo.Common/Controllers/ITranscoder.cs index afaa524f..4fc109d1 100644 --- a/Kyoo.Common/Controllers/ITranscoder.cs +++ b/Kyoo.Common/Controllers/ITranscoder.cs @@ -5,7 +5,7 @@ namespace Kyoo.Controllers { public interface ITranscoder { - Task ExtractInfos(string path, bool reextract); + Task ExtractInfos(Episode episode, bool reextract); Task Transmux(Episode episode); Task Transcode(Episode episode); } diff --git a/Kyoo/Controllers/FileManager.cs b/Kyoo/Controllers/FileManager.cs index a9e52478..71dfbb13 100644 --- a/Kyoo/Controllers/FileManager.cs +++ b/Kyoo/Controllers/FileManager.cs @@ -1,6 +1,8 @@ using System; using System.Collections.Generic; using System.IO; +using System.Threading.Tasks; +using Kyoo.Models; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.StaticFiles; @@ -16,6 +18,8 @@ namespace Kyoo.Controllers { _provider = new FileExtensionContentTypeProvider(); _provider.Mappings[".mkv"] = "video/x-matroska"; + _provider.Mappings[".ass"] = "text/x-ssa"; + _provider.Mappings[".srt"] = "application/x-subrip"; } if (_provider.TryGetContentType(path, out string contentType)) @@ -23,6 +27,7 @@ namespace Kyoo.Controllers throw new NotImplementedException($"Can't get the content type of the file at: {path}"); } + // TODO add a way to force content type public IActionResult FileResult(string path, bool range) { if (path == null) @@ -42,18 +47,40 @@ namespace Kyoo.Controllers return new StreamReader(path); } - public string GetExtraDirectory(string showPath) - { - string path = Path.Combine(showPath, "Extra"); - Directory.CreateDirectory(path); - return path; - } - - public ICollection ListFiles(string path) + public Task> ListFiles(string path) { if (path == null) throw new ArgumentNullException(nameof(path)); - return Directory.GetFiles(path); + return Task.FromResult>(Directory.GetFiles(path)); + } + + public Task Exists(string path) + { + return Task.FromResult(File.Exists(path)); + } + + public string GetExtraDirectory(Show show) + { + string path = Path.Combine(show.Path, "Extra"); + Directory.CreateDirectory(path); + return path; + } + + public string GetExtraDirectory(Season season) + { + if (season.Show == null) + throw new NotImplementedException("Can't get season's extra directory when season.Show == null."); + // TODO use a season.Path here. + string path = Path.Combine(season.Show.Path, "Extra"); + Directory.CreateDirectory(path); + return path; + } + + public string GetExtraDirectory(Episode episode) + { + string path = Path.Combine(Path.GetDirectoryName(episode.Path)!, "Extra"); + Directory.CreateDirectory(path); + return path; } } } \ No newline at end of file diff --git a/Kyoo/Controllers/ThumbnailsManager.cs b/Kyoo/Controllers/ThumbnailsManager.cs index b2dc08c7..04e01ff4 100644 --- a/Kyoo/Controllers/ThumbnailsManager.cs +++ b/Kyoo/Controllers/ThumbnailsManager.cs @@ -38,25 +38,21 @@ namespace Kyoo.Controllers public async Task Validate(Show show, bool alwaysDownload) { - if (show?.Path == null) - return default; - string basePath = _files.GetExtraDirectory(show.Path); - if (show.Poster != null) { - string posterPath = Path.Combine(basePath, "poster.jpg"); + string posterPath = await GetShowPoster(show); if (alwaysDownload || !File.Exists(posterPath)) await DownloadImage(show.Poster, posterPath, $"The poster of {show.Title}"); } if (show.Logo != null) { - string logoPath = Path.Combine(basePath, "logo.png"); + string logoPath = await GetShowLogo(show); if (alwaysDownload || !File.Exists(logoPath)) await DownloadImage(show.Logo, logoPath, $"The logo of {show.Title}"); } if (show.Backdrop != null) { - string backdropPath = Path.Combine(basePath, "backdrop.jpg"); + string backdropPath = await GetShowBackdrop(show); if (alwaysDownload || !File.Exists(backdropPath)) await DownloadImage(show.Backdrop, backdropPath, $"The backdrop of {show.Title}"); } @@ -88,8 +84,7 @@ namespace Kyoo.Controllers if (season.Poster != null) { - string basePath = _files.GetExtraDirectory(season.Show.Path); - string localPath = Path.Combine(basePath, $"season-{season.SeasonNumber}.jpg"); + string localPath = await GetSeasonPoster(season); if (alwaysDownload || !File.Exists(localPath)) await DownloadImage(season.Poster, localPath, $"The poster of {season.Show.Title}'s season {season.SeasonNumber}"); } @@ -103,10 +98,7 @@ namespace Kyoo.Controllers if (episode.Thumb != null) { - string localPath = Path.Combine( - _files.GetExtraDirectory(Path.GetDirectoryName(episode.Path)), - "Thumbnails", - $"{Path.GetFileNameWithoutExtension(episode.Path)}.jpg"); + string localPath = await GetEpisodeThumb(episode); if (alwaysDownload || !File.Exists(localPath)) await DownloadImage(episode.Thumb, localPath, $"The thumbnail of {episode.Slug}"); } @@ -131,44 +123,36 @@ namespace Kyoo.Controllers { if (show?.Path == null) throw new ArgumentNullException(nameof(show)); - return Task.FromResult(Path.Combine(_files.GetExtraDirectory(show.Path), "backdrop.jpg")); + return Task.FromResult(Path.Combine(_files.GetExtraDirectory(show), "backdrop.jpg")); } public Task GetShowLogo(Show show) { if (show?.Path == null) throw new ArgumentNullException(nameof(show)); - return Task.FromResult(Path.Combine(_files.GetExtraDirectory(show.Path), "logo.png")); + return Task.FromResult(Path.Combine(_files.GetExtraDirectory(show), "logo.png")); } public Task GetShowPoster(Show show) { if (show?.Path == null) throw new ArgumentNullException(nameof(show)); - return Task.FromResult(Path.Combine(_files.GetExtraDirectory(show.Path), "poster.jpg")); + return Task.FromResult(Path.Combine(_files.GetExtraDirectory(show), "poster.jpg")); } public Task GetSeasonPoster(Season season) { if (season == null) throw new ArgumentNullException(nameof(season)); - // TODO Use a season.Path (for season's folder) - string path = season.Show.Poster; - if (path == null) - return Task.FromResult(null); - - string thumb = Path.Combine(_files.GetExtraDirectory(path), $"season-{season.SeasonNumber}.jpg"); - return Task.FromResult(File.Exists(thumb) ? Path.GetFullPath(thumb) : null); + return Task.FromResult(Path.Combine(_files.GetExtraDirectory(season), $"season-{season.SeasonNumber}.jpg")); } public Task GetEpisodeThumb(Episode episode) { - string path = episode.Path; - // TODO use show's path for get extra directory. If seasons folder are used, episodes may not be directly in the show folder. return Task.FromResult(Path.Combine( - _files.GetExtraDirectory(Path.GetDirectoryName(path)), + _files.GetExtraDirectory(episode), "Thumbnails", - $"{Path.GetFileNameWithoutExtension(path)}.jpg")); + $"{Path.GetFileNameWithoutExtension(episode.Path)}.jpg")); } public Task GetPeoplePoster(People people) diff --git a/Kyoo/Controllers/Transcoder.cs b/Kyoo/Controllers/Transcoder.cs index 26a05e0e..898f7ba4 100644 --- a/Kyoo/Controllers/Transcoder.cs +++ b/Kyoo/Controllers/Transcoder.cs @@ -85,15 +85,13 @@ namespace Kyoo.Controllers throw new BadTranscoderException(); } - public Task ExtractInfos(string path, bool reextract) + public Task ExtractInfos(Episode episode, bool reextract) { - string dir = _files.GetExtraDirectory(path); + string dir = _files.GetExtraDirectory(episode); if (dir == null) throw new ArgumentException("Invalid path."); - // TODO invalid path here. - dir = Path.Combine(dir, "Extra"); return Task.Factory.StartNew( - () => TranscoderAPI.ExtractInfos(path, dir, reextract), + () => TranscoderAPI.ExtractInfos(episode.Path, dir, reextract), TaskCreationOptions.LongRunning); } diff --git a/Kyoo/Tasks/Crawler.cs b/Kyoo/Tasks/Crawler.cs index 853ea5a9..71da1381 100644 --- a/Kyoo/Tasks/Crawler.cs +++ b/Kyoo/Tasks/Crawler.cs @@ -359,7 +359,7 @@ namespace Kyoo.Controllers private async Task> GetTracks(Episode episode) { - episode.Tracks = (await _transcoder.ExtractInfos(episode.Path, false)) + episode.Tracks = (await _transcoder.ExtractInfos(episode, false)) .Where(x => x.Type != StreamType.Attachment) .ToArray(); return episode.Tracks; diff --git a/Kyoo/Tasks/ExtractMetadata.cs b/Kyoo/Tasks/ExtractMetadata.cs index a36659f4..73ed031b 100644 --- a/Kyoo/Tasks/ExtractMetadata.cs +++ b/Kyoo/Tasks/ExtractMetadata.cs @@ -101,7 +101,7 @@ namespace Kyoo.Tasks if (subs) { await _library.Load(episode, x => x.Tracks); - episode.Tracks = (await _transcoder!.ExtractInfos(episode.Path, true)) + episode.Tracks = (await _transcoder!.ExtractInfos(episode, true)) .Where(x => x.Type != StreamType.Attachment) .Concat(episode.Tracks.Where(x => x.IsExternal)) .ToList(); diff --git a/Kyoo/Views/API/ShowApi.cs b/Kyoo/Views/API/ShowApi.cs index 5ab63a00..703f8c45 100644 --- a/Kyoo/Views/API/ShowApi.cs +++ b/Kyoo/Views/API/ShowApi.cs @@ -376,11 +376,11 @@ namespace Kyoo.Api [Authorize(Policy = "Read")] public async Task>> GetFonts(string slug) { - string path = (await _libraryManager.GetShow(slug))?.Path; - if (path == null) + Show show = await _libraryManager.GetShow(slug); + if (show == null) return NotFound(); - path = Path.Combine(_files.GetExtraDirectory(path), "Attachment"); - return _files.ListFiles(path) + string path = Path.Combine(_files.GetExtraDirectory(show), "Attachments"); + return (await _files.ListFiles(path)) .ToDictionary(Path.GetFileNameWithoutExtension, x => $"{BaseURL}/api/shows/{slug}/fonts/{Path.GetFileName(x)}"); } @@ -390,10 +390,10 @@ namespace Kyoo.Api [Authorize(Policy = "Read")] public async Task GetFont(string showSlug, string slug) { - string path = (await _libraryManager.GetShow(showSlug))?.Path; - if (path == null) + Show show = await _libraryManager.GetShow(showSlug); + if (show == null) return NotFound(); - path = Path.Combine(_files.GetExtraDirectory(path), "Attachment", slug); + string path = Path.Combine(_files.GetExtraDirectory(show), "Attachments", slug); return _files.FileResult(path); } diff --git a/transcoder b/transcoder index 9acd635a..1902defd 160000 --- a/transcoder +++ b/transcoder @@ -1 +1 @@ -Subproject commit 9acd635aca92ad81f1de562e34b2c7c270bade29 +Subproject commit 1902defd32fa98227acad02dabe7f90ee546ec5b