Skip to content

Commit

Permalink
Cleanup and Add FilePathSelector
Browse files Browse the repository at this point in the history
  • Loading branch information
da3dsoul committed Dec 3, 2023
1 parent a400f51 commit 5ed005a
Show file tree
Hide file tree
Showing 31 changed files with 111 additions and 31 deletions.
16 changes: 8 additions & 8 deletions Shoko.Server/API/v3/Models/Shoko/ScrobblingFileResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,22 @@ public ScrobblingFileResult(SVR_VideoLocal videoLocal, int userID, string fileNa
public override async Task ExecuteResultAsync(ActionContext context)
{
await base.ExecuteResultAsync(context);
var (_, end) = GetRange(context.HttpContext, VideoLocal.FileSize);
var end = GetRange(context.HttpContext, VideoLocal.FileSize);
if (end != VideoLocal.FileSize) return;
#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
Task.Factory.StartNew(() => VideoLocal.ToggleWatchedStatus(true, UserID), new CancellationToken(), TaskCreationOptions.LongRunning,
TaskScheduler.Default);
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
}

private static (long start, long end) GetRange(HttpContext context, long length)
private static long GetRange(HttpContext context, long length)
{
if (length == 0) return (0, 0);
if (length == 0) return 0;
var requestHeaders = context.Request.GetTypedHeaders();
var rangeHeader = requestHeaders.Range;
if (rangeHeader == null) return (0, length);
if (rangeHeader == null) return length;
var ranges = rangeHeader.Ranges;
if (ranges.Count == 0) return (0, length);
if (ranges.Count == 0) return length;

var range = ranges.First();
var start = range.From;
Expand All @@ -57,7 +57,7 @@ private static (long start, long end) GetRange(HttpContext context, long length)
if (start.Value >= length)
{
// Not satisfiable, skip/discard.
return (0, length);
return length;
}
if (!end.HasValue || end.Value >= length)
{
Expand All @@ -70,14 +70,14 @@ private static (long start, long end) GetRange(HttpContext context, long length)
if (end.Value == 0)
{
// Not satisfiable, skip/discard.
return (0, length);
return length;
}

var bytes = Math.Min(end.Value, length);
start = length - bytes;
end = start + bytes - 1;
}

return (start ?? 0, end ?? length);
return end ?? length;
}
}
9 changes: 7 additions & 2 deletions Shoko.Server/Filters/FilterExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ public static Filterable ToFilterable(this SVR_AnimeSeries series, ILookup<int,
},
ResolutionsDelegate = () =>
series.GetVideoLocals().Select(a => MediaInfoUtils.GetStandardResolution(Tuple.Create(a.Media.VideoStream.Width, a.Media.VideoStream.Height)))
.ToHashSet(),
FilePathsDelegate = () =>
series.GetVideoLocals().Select(a => a.GetBestVideoLocalPlace().FilePath)
.ToHashSet()
};

Expand Down Expand Up @@ -223,7 +226,8 @@ public static Filterable ToFilterable(this SVR_AnimeGroup group, ILookup<int, Cr
}).ToHashSet(),
HasTvDBLinkDelegate = () => series.Any(a => RepoFactory.CrossRef_AniDB_TvDB.GetByAnimeID(a.AniDB_ID).Any()),
HasMissingTvDbLinkDelegate = () => HasMissingTvDBLink(group),
HasTMDbLinkDelegate = () => movieDBLookup != null ? series.All(a => movieDBLookup.Contains(a.AniDB_ID)) : group.Contract?.Stat_HasMovieDBLink ?? false,
HasTMDbLinkDelegate =
() => movieDBLookup != null ? series.All(a => movieDBLookup.Contains(a.AniDB_ID)) : group.Contract?.Stat_HasMovieDBLink ?? false,
HasMissingTMDbLinkDelegate = () => HasMissingTMDbLink(series, movieDBLookup),
HasTraktLinkDelegate = () => series.Any(a => RepoFactory.CrossRef_AniDB_TraktV2.GetByAnimeID(a.AniDB_ID).Any()),
HasMissingTraktLinkDelegate = () => series.Any(a => !RepoFactory.CrossRef_AniDB_TraktV2.GetByAnimeID(a.AniDB_ID).Any()),
Expand Down Expand Up @@ -265,7 +269,8 @@ public static Filterable ToFilterable(this SVR_AnimeGroup group, ILookup<int, Cr
return subtitles;
},
ResolutionsDelegate = () => series.SelectMany(a => a.GetVideoLocals()).Select(a =>
MediaInfoUtils.GetStandardResolution(Tuple.Create(a.Media.VideoStream.Width, a.Media.VideoStream.Height))).ToHashSet()
MediaInfoUtils.GetStandardResolution(Tuple.Create(a.Media.VideoStream.Width, a.Media.VideoStream.Height))).ToHashSet(),
FilePathsDelegate = () => series.SelectMany(s => s.GetVideoLocals().Select(a => a.GetBestVideoLocalPlace().FilePath)).ToHashSet()
};

return filterable;
Expand Down
8 changes: 8 additions & 0 deletions Shoko.Server/Filters/Filterable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class Filterable : IFilterable
private readonly Lazy<int> _missingEpisodesCollecting;
private readonly Lazy<string> _name;
private readonly Lazy<IReadOnlySet<string>> _resolutions;
private readonly Lazy<IReadOnlySet<string>> _filePaths;
private readonly Lazy<IReadOnlySet<(int year, AnimeSeason season)>> _seasons;
private readonly Lazy<int> _seriesCount;
private readonly Lazy<IReadOnlySet<string>> _sharedAudioLanguages;
Expand Down Expand Up @@ -273,4 +274,11 @@ public Func<IReadOnlySet<string>> ResolutionsDelegate
_resolutions = new Lazy<IReadOnlySet<string>>(value);
}
}

public IReadOnlySet<string> FilePaths => _filePaths.Value;

public Func<IReadOnlySet<string>> FilePathsDelegate
{
init => _filePaths = new Lazy<IReadOnlySet<string>>(value);
}
}
5 changes: 5 additions & 0 deletions Shoko.Server/Filters/Interfaces/IFilterable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,9 @@ public interface IFilterable
/// Resolutions
/// </summary>
IReadOnlySet<string> Resolutions { get; }

/// <summary>
/// Relative File Paths
/// </summary>
IReadOnlySet<string> FilePaths { get; }
}
2 changes: 2 additions & 0 deletions Shoko.Server/Filters/Legacy/LegacyConditionConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
using Shoko.Server.Filters.Logic.Expressions;
using Shoko.Server.Filters.Logic.Numbers;
using Shoko.Server.Filters.Selectors;
using Shoko.Server.Filters.Selectors.DateSelectors;
using Shoko.Server.Filters.Selectors.NumberSelectors;
using Shoko.Server.Filters.SortingSelectors;
using Shoko.Server.Filters.User;
using Shoko.Server.Models;
Expand Down
2 changes: 2 additions & 0 deletions Shoko.Server/Filters/Legacy/LegacyMappings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
using Shoko.Server.Filters.Logic.Expressions;
using Shoko.Server.Filters.Logic.Numbers;
using Shoko.Server.Filters.Selectors;
using Shoko.Server.Filters.Selectors.DateSelectors;
using Shoko.Server.Filters.Selectors.NumberSelectors;
using Shoko.Server.Filters.User;

namespace Shoko.Server.Filters.Legacy;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using Shoko.Server.Filters.Interfaces;

namespace Shoko.Server.Filters.Selectors;
namespace Shoko.Server.Filters.Selectors.DateSelectors;

public class AddedDateSelector : FilterExpression<DateTime?>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using Shoko.Server.Filters.Interfaces;

namespace Shoko.Server.Filters.Selectors;
namespace Shoko.Server.Filters.Selectors.DateSelectors;

public class AirDateSelector : FilterExpression<DateTime?>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using Shoko.Server.Filters.Interfaces;

namespace Shoko.Server.Filters.Selectors;
namespace Shoko.Server.Filters.Selectors.DateSelectors;

public class LastAddedDateSelector : FilterExpression<DateTime?>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using Shoko.Server.Filters.Interfaces;

namespace Shoko.Server.Filters.Selectors;
namespace Shoko.Server.Filters.Selectors.DateSelectors;

public class LastAirDateSelector : FilterExpression<DateTime?>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using Shoko.Server.Filters.Interfaces;

namespace Shoko.Server.Filters.Selectors;
namespace Shoko.Server.Filters.Selectors.DateSelectors;

public class LastWatchedDateSelector : FilterExpression<DateTime?>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using Shoko.Server.Filters.Interfaces;

namespace Shoko.Server.Filters.Selectors;
namespace Shoko.Server.Filters.Selectors.DateSelectors;

public class WatchedDateSelector : FilterExpression<DateTime?>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Shoko.Server.Filters.Interfaces;

namespace Shoko.Server.Filters.Selectors;
namespace Shoko.Server.Filters.Selectors.NumberSelectors;

public class AudioLanguageCountSelector : FilterExpression<double>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using Shoko.Server.Filters.Interfaces;

namespace Shoko.Server.Filters.Selectors;
namespace Shoko.Server.Filters.Selectors.NumberSelectors;

public class AverageAniDBRatingSelector : FilterExpression<double>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Shoko.Server.Filters.Interfaces;

namespace Shoko.Server.Filters.Selectors;
namespace Shoko.Server.Filters.Selectors.NumberSelectors;

public class EpisodeCountSelector : FilterExpression<double>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using Shoko.Server.Filters.Interfaces;

namespace Shoko.Server.Filters.Selectors;
namespace Shoko.Server.Filters.Selectors.NumberSelectors;

public class HighestAniDBRatingSelector : FilterExpression<double>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using Shoko.Server.Filters.Interfaces;

namespace Shoko.Server.Filters.Selectors;
namespace Shoko.Server.Filters.Selectors.NumberSelectors;

public class HighestUserRatingSelector : FilterExpression<double>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using Shoko.Server.Filters.Interfaces;

namespace Shoko.Server.Filters.Selectors;
namespace Shoko.Server.Filters.Selectors.NumberSelectors;

public class LowestAniDBRatingSelector : FilterExpression<double>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using Shoko.Server.Filters.Interfaces;

namespace Shoko.Server.Filters.Selectors;
namespace Shoko.Server.Filters.Selectors.NumberSelectors;

public class LowestUserRatingSelector : FilterExpression<double>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Shoko.Server.Filters.Interfaces;

namespace Shoko.Server.Filters.Selectors;
namespace Shoko.Server.Filters.Selectors.NumberSelectors;

public class MissingEpisodeCollectingCountSelector : FilterExpression<double>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Shoko.Server.Filters.Interfaces;

namespace Shoko.Server.Filters.Selectors;
namespace Shoko.Server.Filters.Selectors.NumberSelectors;

public class MissingEpisodeCountSelector : FilterExpression<double>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Shoko.Server.Filters.Interfaces;

namespace Shoko.Server.Filters.Selectors;
namespace Shoko.Server.Filters.Selectors.NumberSelectors;

public class SeriesCountSelector : FilterExpression<double>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Shoko.Server.Filters.Interfaces;

namespace Shoko.Server.Filters.Selectors;
namespace Shoko.Server.Filters.Selectors.NumberSelectors;

public class SubtitleLanguageCountSelector : FilterExpression<double>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Shoko.Server.Filters.Interfaces;

namespace Shoko.Server.Filters.Selectors;
namespace Shoko.Server.Filters.Selectors.NumberSelectors;

public class TotalEpisodeCountSelector : FilterExpression<double>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using Shoko.Server.Filters.Interfaces;

namespace Shoko.Server.Filters.Selectors;
namespace Shoko.Server.Filters.Selectors.NumberSelectors;

public class UnwatchedEpisodeCountSelector : FilterExpression<double>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using Shoko.Server.Filters.Interfaces;

namespace Shoko.Server.Filters.Selectors;
namespace Shoko.Server.Filters.Selectors.NumberSelectors;

public class WatchedEpisodeCountSelector : FilterExpression<double>
{
Expand Down
55 changes: 55 additions & 0 deletions Shoko.Server/Filters/Selectors/StringSelectors/FilePathSelector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using Shoko.Server.Filters.Interfaces;

namespace Shoko.Server.Filters.Selectors.StringSelectors;

public class FilePathSelector : FilterExpression<string>
{
public override bool TimeDependent => false;
public override bool UserDependent => false;
public override string HelpDescription => "This returns a comma separated list of the file paths in a filterable";

public override string Evaluate(IFilterable filterable, IFilterableUserInfo userInfo)
{
return string.Join(",", filterable.FilePaths);
}

protected bool Equals(FilePathSelector other)
{
return base.Equals(other);
}

public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj))
{
return false;
}

if (ReferenceEquals(this, obj))
{
return true;
}

if (obj.GetType() != this.GetType())
{
return false;
}

return Equals((FilePathSelector)obj);
}

public override int GetHashCode()
{
return GetType().FullName!.GetHashCode();
}

public static bool operator ==(FilePathSelector left, FilePathSelector right)
{
return Equals(left, right);
}

public static bool operator !=(FilePathSelector left, FilePathSelector right)
{
return !Equals(left, right);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Shoko.Server.Filters.Interfaces;

namespace Shoko.Server.Filters.Selectors;
namespace Shoko.Server.Filters.Selectors.StringSelectors;

public class NameSelector : FilterExpression<string>
{
Expand Down
1 change: 1 addition & 0 deletions Shoko.Server/Repositories/Cached/FilterPresetRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Shoko.Server.Filters.Logic.DateTimes;
using Shoko.Server.Filters.Logic.Expressions;
using Shoko.Server.Filters.Selectors;
using Shoko.Server.Filters.Selectors.DateSelectors;
using Shoko.Server.Filters.SortingSelectors;
using Shoko.Server.Filters.User;
using Shoko.Server.Models;
Expand Down
1 change: 1 addition & 0 deletions Shoko.Tests/Shoko.Tests/FilterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Shoko.Server.Filters.Logic.DateTimes;
using Shoko.Server.Filters.Logic.Expressions;
using Shoko.Server.Filters.Selectors;
using Shoko.Server.Filters.Selectors.DateSelectors;
using Shoko.Server.Filters.User;
using Xunit;

Expand Down
1 change: 1 addition & 0 deletions Shoko.Tests/Shoko.Tests/TestFilterable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,5 @@ public class TestFilterable : IFilterable
public IReadOnlySet<string> SubtitleLanguages { get; init; }
public IReadOnlySet<string> SharedSubtitleLanguages { get; init; }
public IReadOnlySet<string> Resolutions { get; init; }
public IReadOnlySet<string> FilePaths { get; init; }
}

0 comments on commit 5ed005a

Please sign in to comment.