Fixing sort limit with native types (non boxed ones)

This commit is contained in:
Zoe Roux 2020-08-01 18:22:46 +02:00
parent 24355bac84
commit d77cae6d2b
19 changed files with 70 additions and 33 deletions

View File

@ -68,7 +68,7 @@ namespace Kyoo.Controllers
} }
} }
public interface IRepository<T> : IDisposable, IAsyncDisposable where T : IRessource public interface IRepository<T> : IDisposable, IAsyncDisposable where T : IResource
{ {
Task<T> Get(int id); Task<T> Get(int id);
Task<T> Get(string slug); Task<T> Get(string slug);

View File

@ -10,7 +10,7 @@ namespace Kyoo.Models
Collection Collection
} }
public class LibraryItem : IRessource public class LibraryItem : IResource
{ {
public int ID { get; set; } public int ID { get; set; }
public string Slug { get; set; } public string Slug { get; set; }

View File

@ -3,7 +3,7 @@ using System.Linq;
namespace Kyoo.Models namespace Kyoo.Models
{ {
public class Page<T> where T : IRessource public class Page<T> where T : IResource
{ {
public string This { get; set; } public string This { get; set; }
public string First { get; set; } public string First { get; set; }

View File

@ -3,7 +3,7 @@ using Newtonsoft.Json;
namespace Kyoo.Models namespace Kyoo.Models
{ {
public class PeopleLink : IRessource public class PeopleLink : IResource
{ {
[JsonIgnore] public int ID { get; set; } [JsonIgnore] public int ID { get; set; }
[JsonIgnore] public int PeopleID { get; set; } [JsonIgnore] public int PeopleID { get; set; }

View File

@ -5,7 +5,7 @@ using Kyoo.Models.Attributes;
namespace Kyoo.Models namespace Kyoo.Models
{ {
public class Collection : IRessource public class Collection : IResource
{ {
[JsonIgnore] public int ID { get; set; } [JsonIgnore] public int ID { get; set; }
public string Slug { get; set; } public string Slug { get; set; }

View File

@ -5,7 +5,7 @@ using Kyoo.Models.Attributes;
namespace Kyoo.Models namespace Kyoo.Models
{ {
public class Episode : IRessource, IOnMerge public class Episode : IResource, IOnMerge
{ {
[JsonIgnore] public int ID { get; set; } [JsonIgnore] public int ID { get; set; }
[JsonIgnore] public int ShowID { get; set; } [JsonIgnore] public int ShowID { get; set; }

View File

@ -5,7 +5,7 @@ using Newtonsoft.Json;
namespace Kyoo.Models namespace Kyoo.Models
{ {
public class Genre : IRessource public class Genre : IResource
{ {
[JsonIgnore] public int ID { get; set; } [JsonIgnore] public int ID { get; set; }
public string Slug { get; set; } public string Slug { get; set; }

View File

@ -1,6 +1,6 @@
namespace Kyoo.Models namespace Kyoo.Models
{ {
public interface IRessource public interface IResource
{ {
public int ID { get; set; } public int ID { get; set; }
public string Slug { get; } public string Slug { get; }

View File

@ -5,7 +5,7 @@ using Newtonsoft.Json;
namespace Kyoo.Models namespace Kyoo.Models
{ {
public class Library : IRessource public class Library : IResource
{ {
[JsonIgnore] public int ID { get; set; } [JsonIgnore] public int ID { get; set; }
public string Slug { get; set; } public string Slug { get; set; }

View File

@ -4,7 +4,7 @@ using Newtonsoft.Json;
namespace Kyoo.Models namespace Kyoo.Models
{ {
public class People : IRessource public class People : IResource
{ {
public int ID { get; set; } public int ID { get; set; }
public string Slug { get; set; } public string Slug { get; set; }

View File

@ -2,7 +2,7 @@ using Newtonsoft.Json;
namespace Kyoo.Models namespace Kyoo.Models
{ {
public class ProviderID : IRessource public class ProviderID : IResource
{ {
[JsonIgnore] public int ID { get; set; } [JsonIgnore] public int ID { get; set; }
public string Slug { get; set; } public string Slug { get; set; }

View File

@ -3,7 +3,7 @@ using Newtonsoft.Json;
namespace Kyoo.Models namespace Kyoo.Models
{ {
public class Season : IRessource public class Season : IResource
{ {
[JsonIgnore] public int ID { get; set; } [JsonIgnore] public int ID { get; set; }
[JsonIgnore] public int ShowID { get; set; } [JsonIgnore] public int ShowID { get; set; }

View File

@ -5,7 +5,7 @@ using Kyoo.Models.Attributes;
namespace Kyoo.Models namespace Kyoo.Models
{ {
public class Show : IRessource, IOnMerge public class Show : IResource, IOnMerge
{ {
[JsonIgnore] public int ID { get; set; } [JsonIgnore] public int ID { get; set; }

View File

@ -3,7 +3,7 @@ using Newtonsoft.Json;
namespace Kyoo.Models namespace Kyoo.Models
{ {
public class Studio : IRessource public class Studio : IResource
{ {
[JsonIgnore] public int ID { get; set; } [JsonIgnore] public int ID { get; set; }
public string Slug { get; set; } public string Slug { get; set; }

View File

@ -53,7 +53,7 @@ namespace Kyoo.Models
} }
} }
public class Track : Stream, IRessource public class Track : Stream, IResource
{ {
[JsonIgnore] public int ID { get; set; } [JsonIgnore] public int ID { get; set; }
[JsonIgnore] public int EpisodeID { get; set; } [JsonIgnore] public int EpisodeID { get; set; }

View File

@ -12,7 +12,7 @@ using Microsoft.Extensions.Configuration;
namespace Kyoo.CommonApi namespace Kyoo.CommonApi
{ {
[ApiController] [ApiController]
public class CrudApi<T> : ControllerBase where T : IRessource public class CrudApi<T> : ControllerBase where T : IResource
{ {
private readonly IRepository<T> _repository; private readonly IRepository<T> _repository;
private readonly string _baseURL; private readonly string _baseURL;
@ -73,7 +73,7 @@ namespace Kyoo.CommonApi
} }
protected Page<TResult> Page<TResult>(ICollection<TResult> ressources, int limit) protected Page<TResult> Page<TResult>(ICollection<TResult> ressources, int limit)
where TResult : IRessource where TResult : IResource
{ {
return new Page<TResult>(ressources, return new Page<TResult>(ressources,
_baseURL + Request.Path, _baseURL + Request.Path,

View File

@ -11,7 +11,7 @@ using Npgsql;
namespace Kyoo.Controllers namespace Kyoo.Controllers
{ {
public abstract class LocalRepository<T> : IRepository<T> where T : class, IRessource public abstract class LocalRepository<T> : IRepository<T> where T : class, IResource
{ {
private readonly DbContext _database; private readonly DbContext _database;
@ -76,9 +76,12 @@ namespace Kyoo.Controllers
if (limit.AfterID != 0) if (limit.AfterID != 0)
{ {
TValue after = await get(limit.AfterID); TValue after = await get(limit.AfterID);
object afterObj = sortKey.Compile()(after); Expression sortExpression = sortKey.Body.NodeType == ExpressionType.Convert
? ((UnaryExpression)sortKey.Body).Operand
: sortKey.Body;
Expression key = Expression.Constant(sortKey.Compile()(after), sortExpression.Type);
query = query.Where(Expression.Lambda<Func<TValue, bool>>( query = query.Where(Expression.Lambda<Func<TValue, bool>>(
ApiHelper.StringCompatibleExpression(Expression.GreaterThan, sortKey.Body, Expression.Constant(afterObj)), ApiHelper.StringCompatibleExpression(Expression.GreaterThan, sortExpression, key),
sortKey.Parameters.First() sortKey.Parameters.First()
)); ));
} }

View File

@ -309,7 +309,9 @@ namespace Kyoo.Controllers
private async Task<IEnumerable<Track>> GetTracks(Episode episode) private async Task<IEnumerable<Track>> GetTracks(Episode episode)
{ {
IEnumerable<Track> tracks = await _transcoder.GetTrackInfo(episode.Path); IEnumerable<Track> tracks = await _transcoder.GetTrackInfo(episode.Path);
List<Track> epTracks = tracks.Where(x => x.Type != StreamType.Subtitle).Concat(GetExtractedSubtitles(episode)).ToList(); List<Track> epTracks = tracks.Where(x => x.Type != StreamType.Subtitle)
.Concat(GetExtractedSubtitles(episode))
.ToList();
if (epTracks.Count(x => !x.IsExternal) < tracks.Count()) if (epTracks.Count(x => !x.IsExternal) < tracks.Count())
epTracks.AddRange(await _transcoder.ExtractSubtitles(episode.Path)); epTracks.AddRange(await _transcoder.ExtractSubtitles(episode.Path));
episode.Tracks = epTracks; episode.Tracks = epTracks;
@ -329,26 +331,58 @@ namespace Kyoo.Controllers
foreach (string sub in Directory.EnumerateFiles(path, "", SearchOption.AllDirectories)) foreach (string sub in Directory.EnumerateFiles(path, "", SearchOption.AllDirectories))
{ {
string episodeLink = Path.GetFileNameWithoutExtension(episode.Path); string episodeLink = Path.GetFileNameWithoutExtension(episode.Path);
string subName = Path.GetFileName(sub);
if (!sub.Contains(episodeLink!))
if (episodeLink == null
|| subName?.Contains(episodeLink) == false
|| subName.Length < episodeLink.Length + 5)
continue; continue;
string language = sub.Substring(Path.GetDirectoryName(sub).Length + episodeLink.Length + 2, 3); string language = subName.Substring(episodeLink.Length + 2, 3);
bool isDefault = sub.Contains("default"); bool isDefault = sub.Contains("default");
bool isForced = sub.Contains("forced"); bool isForced = sub.Contains("forced");
Track track = new Track(StreamType.Subtitle, null, language, isDefault, isForced, null, false, sub) { EpisodeID = episode.ID }; Track track = new Track(StreamType.Subtitle, null, language, isDefault, isForced, null, false, sub)
{
EpisodeID = episode.ID,
Codec = Path.GetExtension(sub) switch
{
".ass" => "ass",
".srt" => "subrip",
_ => null
}
};
if (Path.GetExtension(sub) == ".ass")
track.Codec = "ass";
else if (Path.GetExtension(sub) == ".srt")
track.Codec = "subrip";
else
track.Codec = null;
tracks.Add(track); tracks.Add(track);
} }
return tracks; return tracks;
} }
private static readonly string[] VideoExtensions = { ".webm", ".mkv", ".flv", ".vob", ".ogg", ".ogv", ".avi", ".mts", ".m2ts", ".ts", ".mov", ".qt", ".asf", ".mp4", ".m4p", ".m4v", ".mpg", ".mp2", ".mpeg", ".mpe", ".mpv", ".m2v", ".3gp", ".3g2" }; private static readonly string[] VideoExtensions =
{
".webm",
".mkv",
".flv",
".vob",
".ogg",
".ogv",
".avi",
".mts",
".m2ts",
".ts",
".mov",
".qt",
".asf",
".mp4",
".m4p",
".m4v",
".mpg",
".mp2",
".mpeg",
".mpe",
".mpv",
".m2v",
".3gp",
".3g2"
};
private static bool IsVideo(string filePath) private static bool IsVideo(string filePath)
{ {

@ -1 +1 @@
Subproject commit fffb6690fc5db161767753d1fc554be04eb732d4 Subproject commit 45afdbb44ed28b5ee408923b6d4d13534e284517