From b84baae99a1ef63274fa032c6716f144dec8ac65 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Tue, 16 Mar 2021 00:24:13 +0100 Subject: [PATCH] Fixing subtitle re-extraction --- Kyoo.Common/Utility.cs | 16 +++++++++++++ Kyoo.CommonAPI/LocalRepository.cs | 23 +++++++++++++++++-- .../Repositories/EpisodeRepository.cs | 15 ++++++------ Kyoo/Tasks/ExtractMetadata.cs | 6 +++-- 4 files changed, 48 insertions(+), 12 deletions(-) diff --git a/Kyoo.Common/Utility.cs b/Kyoo.Common/Utility.cs index 75e2ce3b..1504abf5 100644 --- a/Kyoo.Common/Utility.cs +++ b/Kyoo.Common/Utility.cs @@ -304,6 +304,14 @@ namespace Kyoo action(i); } + public static void ForEach([CanBeNull] this IEnumerable self, Action action) + { + if (self == null) + return; + foreach (object i in self) + action(i); + } + public static async Task ForEachAsync([CanBeNull] this IEnumerable self, Func action) { if (self == null) @@ -319,6 +327,14 @@ namespace Kyoo await foreach (T i in self) action(i); } + + public static async Task ForEachAsync([CanBeNull] this IEnumerable self, Func action) + { + if (self == null) + return; + foreach (object i in self) + await action(i); + } private static MethodInfo GetMethod(Type type, BindingFlags flag, string name, Type[] generics, object[] args) { diff --git a/Kyoo.CommonAPI/LocalRepository.cs b/Kyoo.CommonAPI/LocalRepository.cs index cf6ed4e3..0b6f2572 100644 --- a/Kyoo.CommonAPI/LocalRepository.cs +++ b/Kyoo.CommonAPI/LocalRepository.cs @@ -4,7 +4,6 @@ using System.Linq; using System.Linq.Expressions; using System.Reflection; using System.Threading.Tasks; -using JetBrains.Annotations; using Kyoo.CommonApi; using Kyoo.Models; using Kyoo.Models.Attributes; @@ -119,7 +118,7 @@ namespace Kyoo.Controllers return query.CountAsync(); } - public virtual async Task Create([NotNull] T obj) + public virtual async Task Create(T obj) { if (obj == null) throw new ArgumentNullException(nameof(obj)); @@ -169,6 +168,26 @@ namespace Kyoo.Controllers if (old == null) throw new ItemNotFound($"No resource found with the ID {edited.ID}."); + // foreach (PropertyInfo navigation in typeof(T).GetProperties() + // .Where(x => x.GetCustomAttribute() != null)) + // { + // if (navigation.GetCustomAttribute() == null) + // { + // navigation.SetValue(edited, default); + // continue; + // } + // + // if (resetOld || navigation.GetValue(edited) != default) + // { + // // TODO only works for X To One and not X to Many + // await _library.Value.Load(old, navigation.Name); + // object value = navigation.GetValue(old); + // if (value is IEnumerable list) // TODO handle externalIds & PeopleRoles + // list.ForEach(x => _library.Value.Delete(x)); + // else if (value is IResource resource) + // _library.Value.Delete(resource); + // } + // } foreach (NavigationEntry navigation in Database.Entry(old).Navigations) { if (navigation.Metadata.PropertyInfo.GetCustomAttribute() != null) diff --git a/Kyoo/Controllers/Repositories/EpisodeRepository.cs b/Kyoo/Controllers/Repositories/EpisodeRepository.cs index 7efadd95..ee53f331 100644 --- a/Kyoo/Controllers/Repositories/EpisodeRepository.cs +++ b/Kyoo/Controllers/Repositories/EpisodeRepository.cs @@ -166,13 +166,12 @@ namespace Kyoo.Controllers await base.Validate(resource); - if (resource.Tracks != null) - { - // TODO remove old values - resource.Tracks = await resource.Tracks - .SelectAsync(x => _tracks.CreateIfNotExists(x, true)) - .ToListAsync(); - } + // if (resource.Tracks != null) + // { + // resource.Tracks = await resource.Tracks + // .SelectAsync(x => _tracks.CreateIfNotExists(x, true)) + // .ToListAsync(); + // } if (resource.ExternalIDs != null) { @@ -194,7 +193,7 @@ namespace Kyoo.Controllers throw new ArgumentNullException(nameof(obj)); _database.Entry(obj).State = EntityState.Deleted; - await obj.Tracks.ForEachAsync(x => _tracks.CreateIfNotExists(x, true)); + // await obj.Tracks.ForEachAsync(x => _tracks.CreateIfNotExists(x, true)); if (obj.ExternalIDs != null) foreach (MetadataID entry in obj.ExternalIDs) _database.Entry(entry).State = EntityState.Deleted; diff --git a/Kyoo/Tasks/ExtractMetadata.cs b/Kyoo/Tasks/ExtractMetadata.cs index f78caddf..4dccc70f 100644 --- a/Kyoo/Tasks/ExtractMetadata.cs +++ b/Kyoo/Tasks/ExtractMetadata.cs @@ -100,9 +100,11 @@ namespace Kyoo.Tasks await _thumbnails!.Validate(episode, true); if (subs) { - // TODO handle external subtites. + await _library.Load(episode, x => x.Tracks); episode.Tracks = (await _transcoder!.ExtractInfos(episode.Path)) - .Where(x => x.Type != StreamType.Font).ToArray(); + .Where(x => x.Type != StreamType.Font) + .Concat(episode.Tracks.Where(x => x.IsExternal)) + .ToList(); await _library.EditEpisode(episode, false); } }