mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-05-24 02:02:36 -04:00
Cleaning up path handling in extractors
This commit is contained in:
parent
365fd1e79f
commit
1ad84174f2
@ -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<string> ListFiles([NotNull] string path);
|
||||
// TODO implement a List for directorys, a Exist to check existance and all.
|
||||
public Task<ICollection<string>> ListFiles([NotNull] string path);
|
||||
|
||||
public Task<bool> 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);
|
||||
}
|
||||
}
|
@ -5,7 +5,7 @@ namespace Kyoo.Controllers
|
||||
{
|
||||
public interface ITranscoder
|
||||
{
|
||||
Task<Track[]> ExtractInfos(string path, bool reextract);
|
||||
Task<Track[]> ExtractInfos(Episode episode, bool reextract);
|
||||
Task<string> Transmux(Episode episode);
|
||||
Task<string> Transcode(Episode episode);
|
||||
}
|
||||
|
@ -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<string> ListFiles(string path)
|
||||
public Task<ICollection<string>> ListFiles(string path)
|
||||
{
|
||||
if (path == null)
|
||||
throw new ArgumentNullException(nameof(path));
|
||||
return Directory.GetFiles(path);
|
||||
return Task.FromResult<ICollection<string>>(Directory.GetFiles(path));
|
||||
}
|
||||
|
||||
public Task<bool> 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;
|
||||
}
|
||||
}
|
||||
}
|
@ -38,25 +38,21 @@ namespace Kyoo.Controllers
|
||||
|
||||
public async Task<Show> 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<string> 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<string> 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<string> 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<string>(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<string> 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<string> GetPeoplePoster(People people)
|
||||
|
@ -85,15 +85,13 @@ namespace Kyoo.Controllers
|
||||
throw new BadTranscoderException();
|
||||
}
|
||||
|
||||
public Task<Track[]> ExtractInfos(string path, bool reextract)
|
||||
public Task<Track[]> 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);
|
||||
}
|
||||
|
||||
|
@ -359,7 +359,7 @@ namespace Kyoo.Controllers
|
||||
|
||||
private async Task<ICollection<Track>> 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;
|
||||
|
@ -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();
|
||||
|
@ -376,11 +376,11 @@ namespace Kyoo.Api
|
||||
[Authorize(Policy = "Read")]
|
||||
public async Task<ActionResult<Dictionary<string, string>>> 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<IActionResult> 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);
|
||||
}
|
||||
|
||||
|
@ -1 +1 @@
|
||||
Subproject commit 9acd635aca92ad81f1de562e34b2c7c270bade29
|
||||
Subproject commit 1902defd32fa98227acad02dabe7f90ee546ec5b
|
Loading…
x
Reference in New Issue
Block a user