Fixing subtitle re-extraction

This commit is contained in:
Zoe Roux 2021-03-16 00:24:13 +01:00
parent d2a38c9b3d
commit b84baae99a
4 changed files with 48 additions and 12 deletions

View File

@ -304,6 +304,14 @@ namespace Kyoo
action(i);
}
public static void ForEach([CanBeNull] this IEnumerable self, Action<object> action)
{
if (self == null)
return;
foreach (object i in self)
action(i);
}
public static async Task ForEachAsync<T>([CanBeNull] this IEnumerable<T> self, Func<T, Task> 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<object, Task> 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)
{

View File

@ -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<T> Create([NotNull] T obj)
public virtual async Task<T> 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<LoadableRelationAttribute>() != null))
// {
// if (navigation.GetCustomAttribute<EditableRelationAttribute>() == 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<EditableRelationAttribute>() != null)

View File

@ -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;

View File

@ -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);
}
}