diff --git a/Kyoo.Common/Controllers/IFileManager.cs b/Kyoo.Common/Controllers/IFileManager.cs index 8e2d52fb..aa845f82 100644 --- a/Kyoo.Common/Controllers/IFileManager.cs +++ b/Kyoo.Common/Controllers/IFileManager.cs @@ -1,3 +1,4 @@ +using System.Collections.Generic; using System.IO; using JetBrains.Annotations; using Microsoft.AspNetCore.Mvc; @@ -6,11 +7,15 @@ namespace Kyoo.Controllers { public interface IFileManager { - public IActionResult FileResult([NotNull] string path, bool rangeSupport = false); + public IActionResult FileResult([CanBeNull] string path, bool rangeSupport = false); 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. // 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); } } \ No newline at end of file diff --git a/Kyoo.Common/Controllers/ILibraryManager.cs b/Kyoo.Common/Controllers/ILibraryManager.cs index 374f012a..ae1d0ccb 100644 --- a/Kyoo.Common/Controllers/ILibraryManager.cs +++ b/Kyoo.Common/Controllers/ILibraryManager.cs @@ -35,6 +35,7 @@ namespace Kyoo.Controllers Task GetTrack(int id); Task GetStudio(int id); Task GetPeople(int id); + Task GetProvider(int id); // Get by slug Task GetLibrary(string slug); @@ -49,6 +50,7 @@ namespace Kyoo.Controllers Task GetGenre(string slug); Task GetStudio(string slug); Task GetPeople(string slug); + Task GetProvider(string slug); // Get by predicate Task GetLibrary(Expression> where); diff --git a/Kyoo.Common/Controllers/IThumbnailsManager.cs b/Kyoo.Common/Controllers/IThumbnailsManager.cs index 32666e84..603a0f6f 100644 --- a/Kyoo.Common/Controllers/IThumbnailsManager.cs +++ b/Kyoo.Common/Controllers/IThumbnailsManager.cs @@ -1,6 +1,7 @@ using Kyoo.Models; using System.Collections.Generic; using System.Threading.Tasks; +using JetBrains.Annotations; namespace Kyoo.Controllers { @@ -11,5 +12,13 @@ namespace Kyoo.Controllers Task Validate(Episode episode, bool alwaysDownload = false); Task Validate(People actors, bool alwaysDownload = false); Task Validate(ProviderID actors, bool alwaysDownload = false); + + Task GetShowPoster([NotNull] Show show); + Task GetShowLogo([NotNull] Show show); + Task GetShowBackdrop([NotNull] Show show); + Task GetSeasonPoster([NotNull] Season season); + Task GetEpisodeThumb([NotNull] Episode episode); + Task GetPeoplePoster([NotNull] People people); + Task GetProviderLogo([NotNull] ProviderID provider); } } diff --git a/Kyoo.Common/Controllers/Implementations/LibraryManager.cs b/Kyoo.Common/Controllers/Implementations/LibraryManager.cs index 9a54b7af..7cae9b4d 100644 --- a/Kyoo.Common/Controllers/Implementations/LibraryManager.cs +++ b/Kyoo.Common/Controllers/Implementations/LibraryManager.cs @@ -131,6 +131,11 @@ namespace Kyoo.Controllers return PeopleRepository.Get(id); } + public Task GetProvider(int id) + { + return ProviderRepository.Get(id); + } + public Task GetLibrary(string slug) { return LibraryRepository.Get(slug); @@ -190,6 +195,11 @@ namespace Kyoo.Controllers { return PeopleRepository.Get(slug); } + + public Task GetProvider(string slug) + { + return ProviderRepository.Get(slug); + } public Task GetLibrary(Expression> where) { diff --git a/Kyoo/Controllers/FileManager.cs b/Kyoo/Controllers/FileManager.cs index e6be6f6d..a9e52478 100644 --- a/Kyoo/Controllers/FileManager.cs +++ b/Kyoo/Controllers/FileManager.cs @@ -1,11 +1,12 @@ using System; +using System.Collections.Generic; using System.IO; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.StaticFiles; namespace Kyoo.Controllers { - public class FileManager : ControllerBase, IFileManager + public class FileManager : IFileManager { private FileExtensionContentTypeProvider _provider; @@ -19,16 +20,19 @@ namespace Kyoo.Controllers if (_provider.TryGetContentType(path, out string contentType)) return contentType; - return "video/mp4"; + throw new NotImplementedException($"Can't get the content type of the file at: {path}"); } public IActionResult FileResult(string path, bool range) { if (path == null) - throw new ArgumentNullException(nameof(path)); - if (!System.IO.File.Exists(path)) - return NotFound(); - return PhysicalFile(path, _GetContentType(path), range); + return new NotFoundResult(); + if (!File.Exists(path)) + return new NotFoundResult(); + return new PhysicalFileResult(Path.GetFullPath(path), _GetContentType(path)) + { + EnableRangeProcessing = range + }; } public StreamReader GetReader(string path) @@ -37,5 +41,19 @@ namespace Kyoo.Controllers throw new ArgumentNullException(nameof(path)); 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) + { + if (path == null) + throw new ArgumentNullException(nameof(path)); + return Directory.GetFiles(path); + } } } \ No newline at end of file diff --git a/Kyoo/Controllers/ThumbnailsManager.cs b/Kyoo/Controllers/ThumbnailsManager.cs index a2f0f7ac..b2dc08c7 100644 --- a/Kyoo/Controllers/ThumbnailsManager.cs +++ b/Kyoo/Controllers/ThumbnailsManager.cs @@ -11,10 +11,16 @@ namespace Kyoo.Controllers public class ThumbnailsManager : IThumbnailsManager { private readonly IConfiguration _config; + private readonly IFileManager _files; + private readonly string _peoplePath; + private readonly string _providerPath; - public ThumbnailsManager(IConfiguration configuration) + public ThumbnailsManager(IConfiguration configuration, IFileManager files) { _config = configuration; + _files = files; + _peoplePath = Path.GetFullPath(configuration.GetValue("peoplePath")); + _providerPath = Path.GetFullPath(configuration.GetValue("providerPath")); } private static async Task DownloadImage(string url, string localPath, string what) @@ -34,22 +40,23 @@ namespace Kyoo.Controllers { if (show?.Path == null) return default; + string basePath = _files.GetExtraDirectory(show.Path); if (show.Poster != null) { - string posterPath = Path.Combine(show.Path, "poster.jpg"); + string posterPath = Path.Combine(basePath, "poster.jpg"); if (alwaysDownload || !File.Exists(posterPath)) await DownloadImage(show.Poster, posterPath, $"The poster of {show.Title}"); } if (show.Logo != null) { - string logoPath = Path.Combine(show.Path, "logo.png"); + string logoPath = Path.Combine(basePath, "logo.png"); if (alwaysDownload || !File.Exists(logoPath)) await DownloadImage(show.Logo, logoPath, $"The logo of {show.Title}"); } if (show.Backdrop != null) { - string backdropPath = Path.Combine(show.Path, "backdrop.jpg"); + string backdropPath = Path.Combine(basePath, "backdrop.jpg"); if (alwaysDownload || !File.Exists(backdropPath)) await DownloadImage(show.Backdrop, backdropPath, $"The backdrop of {show.Title}"); } @@ -81,7 +88,8 @@ namespace Kyoo.Controllers if (season.Poster != null) { - string localPath = Path.Combine(season.Show.Path, $"season-{season.SeasonNumber}.jpg"); + string basePath = _files.GetExtraDirectory(season.Show.Path); + string localPath = Path.Combine(basePath, $"season-{season.SeasonNumber}.jpg"); if (alwaysDownload || !File.Exists(localPath)) await DownloadImage(season.Poster, localPath, $"The poster of {season.Show.Title}'s season {season.SeasonNumber}"); } @@ -95,7 +103,10 @@ namespace Kyoo.Controllers if (episode.Thumb != null) { - string localPath = Path.ChangeExtension(episode.Path, "jpg"); + string localPath = Path.Combine( + _files.GetExtraDirectory(Path.GetDirectoryName(episode.Path)), + "Thumbnails", + $"{Path.GetFileNameWithoutExtension(episode.Path)}.jpg"); if (alwaysDownload || !File.Exists(localPath)) await DownloadImage(episode.Thumb, localPath, $"The thumbnail of {episode.Slug}"); } @@ -115,7 +126,67 @@ namespace Kyoo.Controllers await DownloadImage(provider.Logo, localPath, $"The logo of {provider.Slug}"); return provider; } + + public Task GetShowBackdrop(Show show) + { + if (show?.Path == null) + throw new ArgumentNullException(nameof(show)); + return Task.FromResult(Path.Combine(_files.GetExtraDirectory(show.Path), "backdrop.jpg")); + } - //TODO add get thumbs here + 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")); + } + + 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")); + } + + 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); + } + + 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)), + "Thumbnails", + $"{Path.GetFileNameWithoutExtension(path)}.jpg")); + } + + public Task GetPeoplePoster(People people) + { + if (people == null) + throw new ArgumentNullException(nameof(people)); + string thumbPath = Path.GetFullPath(Path.Combine(_peoplePath, $"{people.Slug}.jpg")); + if (!thumbPath.StartsWith(_peoplePath) || File.Exists(thumbPath)) + return Task.FromResult(null); + return Task.FromResult(thumbPath); + } + + public Task GetProviderLogo(ProviderID provider) + { + if (provider == null) + throw new ArgumentNullException(nameof(provider)); + string thumbPath = Path.GetFullPath(Path.Combine(_providerPath, $"{provider.Slug}.jpg")); + return Task.FromResult(thumbPath.StartsWith(_providerPath) ? thumbPath : null); + } } } diff --git a/Kyoo/Controllers/Transcoder.cs b/Kyoo/Controllers/Transcoder.cs index 25e1722b..26a05e0e 100644 --- a/Kyoo/Controllers/Transcoder.cs +++ b/Kyoo/Controllers/Transcoder.cs @@ -70,12 +70,14 @@ namespace Kyoo.Controllers return tracks; } } - + + private readonly IFileManager _files; private readonly string _transmuxPath; private readonly string _transcodePath; - public Transcoder(IConfiguration config) + public Transcoder(IConfiguration config, IFileManager files) { + _files = files; _transmuxPath = Path.GetFullPath(config.GetValue("transmuxTempPath")); _transcodePath = Path.GetFullPath(config.GetValue("transcodeTempPath")); @@ -85,9 +87,10 @@ namespace Kyoo.Controllers public Task ExtractInfos(string path, bool reextract) { - string dir = Path.GetDirectoryName(path); + string dir = _files.GetExtraDirectory(path); 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), diff --git a/Kyoo/Startup.cs b/Kyoo/Startup.cs index 475f8691..7f25909c 100644 --- a/Kyoo/Startup.cs +++ b/Kyoo/Startup.cs @@ -158,7 +158,7 @@ namespace Kyoo services.AddScoped(); services.AddScoped(); - services.AddScoped(); + services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); diff --git a/Kyoo/Views/API/EpisodeApi.cs b/Kyoo/Views/API/EpisodeApi.cs index de9ae424..c0c01a18 100644 --- a/Kyoo/Views/API/EpisodeApi.cs +++ b/Kyoo/Views/API/EpisodeApi.cs @@ -2,7 +2,6 @@ using Kyoo.Models; using Microsoft.AspNetCore.Mvc; using System.Collections.Generic; -using System.IO; using System.Linq; using System.Threading.Tasks; using Kyoo.CommonApi; @@ -18,11 +17,18 @@ namespace Kyoo.Api public class EpisodeApi : CrudApi { private readonly ILibraryManager _libraryManager; + private readonly IThumbnailsManager _thumbnails; + private readonly IFileManager _files; - public EpisodeApi(ILibraryManager libraryManager, IConfiguration configuration) + public EpisodeApi(ILibraryManager libraryManager, + IConfiguration configuration, + IFileManager files, + IThumbnailsManager thumbnails) : base(libraryManager.EpisodeRepository, configuration) { _libraryManager = libraryManager; + _files = files; + _thumbnails = thumbnails; } [HttpGet("{episodeID:int}/show")] @@ -156,30 +162,20 @@ namespace Kyoo.Api [Authorize(Policy="Read")] public async Task GetThumb(int id) { - string path = (await _libraryManager.GetEpisode(id))?.Path; - if (path == null) + Episode episode = await _libraryManager.GetEpisode(id); + if (episode == null) return NotFound(); - - string thumb = Path.ChangeExtension(path, "jpg"); - - if (System.IO.File.Exists(thumb)) - return new PhysicalFileResult(Path.GetFullPath(thumb), "image/jpg"); - return NotFound(); + return _files.FileResult(await _thumbnails.GetEpisodeThumb(episode)); } - [HttpGet("{showSlug}-s{seasonNumber:int}e{episodeNumber:int}/thumb")] + [HttpGet("{slug}/thumb")] [Authorize(Policy="Read")] - public async Task GetThumb(string showSlug, int seasonNumber, int episodeNumber) + public async Task GetThumb(string slug) { - string path = (await _libraryManager.GetEpisode(showSlug, seasonNumber, episodeNumber))?.Path; - if (path == null) + Episode episode = await _libraryManager.GetEpisode(slug); + if (episode == null) return NotFound(); - - string thumb = Path.ChangeExtension(path, "jpg"); - - if (System.IO.File.Exists(thumb)) - return new PhysicalFileResult(Path.GetFullPath(thumb), "image/jpg"); - return NotFound(); + return _files.FileResult(await _thumbnails.GetEpisodeThumb(episode)); } } } \ No newline at end of file diff --git a/Kyoo/Views/API/PeopleApi.cs b/Kyoo/Views/API/PeopleApi.cs index 602c022c..bfcb8a29 100644 --- a/Kyoo/Views/API/PeopleApi.cs +++ b/Kyoo/Views/API/PeopleApi.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.IO; using System.Threading.Tasks; using Kyoo.CommonApi; using Kyoo.Controllers; @@ -17,13 +16,18 @@ namespace Kyoo.Api public class PeopleApi : CrudApi { private readonly ILibraryManager _libraryManager; - private readonly string _peoplePath; + private readonly IFileManager _files; + private readonly IThumbnailsManager _thumbs; - public PeopleApi(ILibraryManager libraryManager, IConfiguration configuration) + public PeopleApi(ILibraryManager libraryManager, + IConfiguration configuration, + IFileManager files, + IThumbnailsManager thumbs) : base(libraryManager.PeopleRepository, configuration) { _libraryManager = libraryManager; - _peoplePath = Path.GetFullPath(configuration.GetValue("peoplePath")); + _files = files; + _thumbs = thumbs; } [HttpGet("{id:int}/role")] @@ -86,19 +90,16 @@ namespace Kyoo.Api [Authorize(Policy="Read")] public async Task GetPeopleIcon(int id) { - string slug = (await _libraryManager.GetPeople(id)).Slug; - return GetPeopleIcon(slug); + People people = await _libraryManager.GetPeople(id); + return _files.FileResult(await _thumbs.GetPeoplePoster(people)); } [HttpGet("{slug}/poster")] [Authorize(Policy="Read")] - public IActionResult GetPeopleIcon(string slug) + public async Task GetPeopleIcon(string slug) { - string thumbPath = Path.GetFullPath(Path.Combine(_peoplePath, slug + ".jpg")); - if (!thumbPath.StartsWith(_peoplePath) || !System.IO.File.Exists(thumbPath)) - return NotFound(); - - return new PhysicalFileResult(Path.GetFullPath(thumbPath), "image/jpg"); + People people = await _libraryManager.GetPeople(slug); + return _files.FileResult(await _thumbs.GetPeoplePoster(people)); } } } \ No newline at end of file diff --git a/Kyoo/Views/API/ProviderApi.cs b/Kyoo/Views/API/ProviderApi.cs index 6d1cbb86..2d3aab3d 100644 --- a/Kyoo/Views/API/ProviderApi.cs +++ b/Kyoo/Views/API/ProviderApi.cs @@ -1,5 +1,3 @@ -using System.Collections.Generic; -using System.IO; using System.Threading.Tasks; using Kyoo.CommonApi; using Kyoo.Controllers; @@ -15,33 +13,35 @@ namespace Kyoo.Api [ApiController] public class ProviderAPI : CrudApi { + private readonly IThumbnailsManager _thumbnails; private readonly ILibraryManager _libraryManager; - private readonly string _providerPath; + private readonly IFileManager _files; - public ProviderAPI(ILibraryManager libraryManager, IConfiguration config) + public ProviderAPI(ILibraryManager libraryManager, + IConfiguration config, + IFileManager files, + IThumbnailsManager thumbnails) : base(libraryManager.ProviderRepository, config) { _libraryManager = libraryManager; - _providerPath = Path.GetFullPath(config.GetValue("providerPath")); + _files = files; + _thumbnails = thumbnails; } [HttpGet("{id:int}/logo")] [Authorize(Policy="Read")] public async Task GetLogo(int id) { - string slug = (await _libraryManager.GetPeople(id)).Slug; - return GetLogo(slug); + ProviderID provider = await _libraryManager.GetProvider(id); + return _files.FileResult(await _thumbnails.GetProviderLogo(provider)); } [HttpGet("{slug}/logo")] [Authorize(Policy="Read")] - public IActionResult GetLogo(string slug) + public async Task GetLogo(string slug) { - string thumbPath = Path.GetFullPath(Path.Combine(_providerPath, slug + ".jpg")); - if (!thumbPath.StartsWith(_providerPath) || !System.IO.File.Exists(thumbPath)) - return NotFound(); - - return new PhysicalFileResult(Path.GetFullPath(thumbPath), "image/jpg"); + ProviderID provider = await _libraryManager.GetProvider(slug); + return _files.FileResult(await _thumbnails.GetProviderLogo(provider)); } } } \ No newline at end of file diff --git a/Kyoo/Views/API/SeasonApi.cs b/Kyoo/Views/API/SeasonApi.cs index 4bedf5ec..bc43cad5 100644 --- a/Kyoo/Views/API/SeasonApi.cs +++ b/Kyoo/Views/API/SeasonApi.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.IO; using System.Threading.Tasks; using Kyoo.CommonApi; using Kyoo.Controllers; @@ -18,11 +17,18 @@ namespace Kyoo.Api public class SeasonApi : CrudApi { private readonly ILibraryManager _libraryManager; + private readonly IThumbnailsManager _thumbs; + private readonly IFileManager _files; - public SeasonApi(ILibraryManager libraryManager, IConfiguration configuration) + public SeasonApi(ILibraryManager libraryManager, + IConfiguration configuration, + IThumbnailsManager thumbs, + IFileManager files) : base(libraryManager.SeasonRepository, configuration) { _libraryManager = libraryManager; + _thumbs = thumbs; + _files = files; } [HttpGet("{seasonID:int}/episode")] @@ -131,31 +137,18 @@ namespace Kyoo.Api [Authorize(Policy="Read")] public async Task GetThumb(int id) { - // TODO remove the next lambda and use a Season.Path (should exit for seasons in a different folder) - string path = (await _libraryManager.GetShow(x => x.Seasons.Any(y => y.ID == id)))?.Path; - int seasonNumber = (await _libraryManager.GetSeason(id)).SeasonNumber; - if (path == null) - return NotFound(); - - string thumb = Path.Combine(path, $"season-{seasonNumber}.jpg"); - if (System.IO.File.Exists(thumb)) - return new PhysicalFileResult(Path.GetFullPath(thumb), "image/jpg"); - return NotFound(); + Season season = await _libraryManager.GetSeason(id); + await _libraryManager.Load(season, x => x.Show); + return _files.FileResult(await _thumbs.GetSeasonPoster(season)); } - [HttpGet("{showSlug}-s{seasonNumber:int}/thumb")] + [HttpGet("{slug}/thumb")] [Authorize(Policy="Read")] - public async Task GetThumb(string showSlug, int seasonNumber) + public async Task GetThumb(string slug) { - // TODO use a season.Path - string path = (await _libraryManager.GetShow(showSlug))?.Path; - if (path == null) - return NotFound(); - - string thumb = Path.Combine(path, $"season-{seasonNumber}.jpg"); - if (System.IO.File.Exists(thumb)) - return new PhysicalFileResult(Path.GetFullPath(thumb), "image/jpg"); - return NotFound(); + Season season = await _libraryManager.GetSeason(slug); + await _libraryManager.Load(season, x => x.Show); + return _files.FileResult(await _thumbs.GetSeasonPoster(season)); } } } \ No newline at end of file diff --git a/Kyoo/Views/API/ShowApi.cs b/Kyoo/Views/API/ShowApi.cs index 28b6ef94..5ab63a00 100644 --- a/Kyoo/Views/API/ShowApi.cs +++ b/Kyoo/Views/API/ShowApi.cs @@ -9,7 +9,6 @@ using Kyoo.CommonApi; using Kyoo.Controllers; using Kyoo.Models.Exceptions; using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.StaticFiles; using Microsoft.Extensions.Configuration; namespace Kyoo.Api @@ -20,12 +19,18 @@ namespace Kyoo.Api public class ShowApi : CrudApi { private readonly ILibraryManager _libraryManager; - private FileExtensionContentTypeProvider _provider; + private readonly IFileManager _files; + private readonly IThumbnailsManager _thumbs; - public ShowApi(ILibraryManager libraryManager, IConfiguration configuration) + public ShowApi(ILibraryManager libraryManager, + IFileManager files, + IThumbnailsManager thumbs, + IConfiguration configuration) : base(libraryManager.ShowRepository, configuration) { _libraryManager = libraryManager; + _files = files; + _thumbs = thumbs; } [HttpGet("{showID:int}/season")] @@ -374,10 +379,8 @@ namespace Kyoo.Api string path = (await _libraryManager.GetShow(slug))?.Path; if (path == null) return NotFound(); - path = Path.Combine(path, "Subtitles", "fonts"); - if (!Directory.Exists(path)) - return new Dictionary(); - return Directory.GetFiles(path) + path = Path.Combine(_files.GetExtraDirectory(path), "Attachment"); + return _files.ListFiles(path) .ToDictionary(Path.GetFileNameWithoutExtension, x => $"{BaseURL}/api/shows/{slug}/fonts/{Path.GetFileName(x)}"); } @@ -385,64 +388,43 @@ namespace Kyoo.Api [HttpGet("{showSlug}/font/{slug}")] [HttpGet("{showSlug}/fonts/{slug}")] [Authorize(Policy = "Read")] - public async Task GetFont(string showSlug, string slug) + public async Task GetFont(string showSlug, string slug) { string path = (await _libraryManager.GetShow(showSlug))?.Path; if (path == null) return NotFound(); - string fontPath = Path.Combine(path, "Subtitles", "fonts", slug); - if (!System.IO.File.Exists(fontPath)) - return NotFound(); - - if (_provider == null) - _provider = new FileExtensionContentTypeProvider(); - _provider.TryGetContentType(path, out string contentType); - return PhysicalFile(fontPath, contentType ?? "application/x-font-ttf"); + path = Path.Combine(_files.GetExtraDirectory(path), "Attachment", slug); + return _files.FileResult(path); } [HttpGet("{slug}/poster")] [Authorize(Policy = "Read")] - public async Task GetPoster(string slug) + public async Task GetPoster(string slug) { - string path = (await _libraryManager.GetShow(slug))?.Path; - if (path == null) + Show show = await _libraryManager.GetShow(slug); + if (show == null) return NotFound(); - - string poster = Path.Combine(path, "poster.jpg"); - - if (System.IO.File.Exists(poster)) - return new PhysicalFileResult(Path.GetFullPath(poster), "image/jpg"); - return NotFound(); + return _files.FileResult(await _thumbs.GetShowPoster(show)); } [HttpGet("{slug}/logo")] [Authorize(Policy="Read")] public async Task GetLogo(string slug) { - string path = (await _libraryManager.GetShow(slug))?.Path; - if (path == null) + Show show = await _libraryManager.GetShow(slug); + if (show == null) return NotFound(); - - string logo = Path.Combine(path, "logo.png"); - - if (System.IO.File.Exists(logo)) - return new PhysicalFileResult(Path.GetFullPath(logo), "image/png"); - return NotFound(); + return _files.FileResult(await _thumbs.GetShowLogo(show)); } [HttpGet("{slug}/backdrop")] [Authorize(Policy="Read")] public async Task GetBackdrop(string slug) { - string path = (await _libraryManager.GetShow(slug))?.Path; - if (path == null) + Show show = await _libraryManager.GetShow(slug); + if (show == null) return NotFound(); - - string thumb = Path.Combine(path, "backdrop.jpg"); - - if (System.IO.File.Exists(thumb)) - return new PhysicalFileResult(Path.GetFullPath(thumb), "image/jpg"); - return NotFound(); + return _files.FileResult(await _thumbs.GetShowBackdrop(show)); } } } diff --git a/Kyoo/Views/API/StudioApi.cs b/Kyoo/Views/API/StudioApi.cs index e244575d..a0cf872e 100644 --- a/Kyoo/Views/API/StudioApi.cs +++ b/Kyoo/Views/API/StudioApi.cs @@ -5,7 +5,6 @@ using System.Threading.Tasks; using Kyoo.CommonApi; using Kyoo.Controllers; using Kyoo.Models; -using Kyoo.Models.Exceptions; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration;