From 53792a067d7d10dc8819b4957f9ea7b496fcdaed Mon Sep 17 00:00:00 2001 From: Mikal Stordal Date: Mon, 20 Nov 2023 21:38:05 +0100 Subject: [PATCH] feat: add endpoint to get file ids to delete according to the configured file quality preference in the settings. --- .../API/v3/Controllers/WebUIController.cs | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/Shoko.Server/API/v3/Controllers/WebUIController.cs b/Shoko.Server/API/v3/Controllers/WebUIController.cs index e1bd64bd1..0c1b5e4d3 100644 --- a/Shoko.Server/API/v3/Controllers/WebUIController.cs +++ b/Shoko.Server/API/v3/Controllers/WebUIController.cs @@ -10,12 +10,14 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Logging; +using Shoko.Commons.Extensions; using Shoko.Server.API.Annotations; using Shoko.Server.API.ModelBinders; using Shoko.Server.API.v3.Helpers; using Shoko.Server.API.v3.Models.Common; using Shoko.Server.API.v3.Models.Shoko; using Shoko.Server.API.WebUI; +using Shoko.Server.Models; using Shoko.Server.Repositories; using Shoko.Server.Settings; using Shoko.Server.Utilities; @@ -277,6 +279,41 @@ public ActionResult GetSeriesFileSummary( return new WebUISeriesFileSummary(series, type, includeEpisodeDetails, includeMissingFutureEpisodes, includeMissingUnknownEpisodes, groupBy); } + + /// + /// Get the list of file ids to remove according to the file quality + /// preference. + /// + /// Ignore manually toggled variations in the results. + /// Only show finished series. + /// + [HttpGet("Episode/WithMultipleFiles/FilesToDelete")] + public ActionResult> GetFileIdsWithPreference([FromQuery] bool ignoreVariations = true, [FromQuery] bool onlyFinishedSeries = false) + { + IEnumerable enumerable = + RepoFactory.AnimeEpisode.GetEpisodesWithMultipleFiles(ignoreVariations); + if (onlyFinishedSeries) + { + var dictSeriesFinishedAiring = RepoFactory.AnimeSeries.GetAll() + .ToDictionary(a => a.AnimeSeriesID, a => a.GetAnime().GetFinishedAiring()); + enumerable = enumerable.Where(episode => + dictSeriesFinishedAiring.TryGetValue(episode.AnimeSeriesID, out var finishedAiring) && finishedAiring); + } + + return enumerable + .SelectMany(episode => + { + var files = episode.GetVideoLocals(); + files.Sort(FileQualityFilter.CompareTo); + return files + .Skip(FileQualityFilter.Settings.MaxNumberOfFilesToKeep) + .Where(file => !FileQualityFilter.CheckFileKeep(file)) + .Select(file => file.VideoLocalID); + }) + .Distinct() + .ToList(); + } + /// /// Install a fresh copy of the web ui for the selected /// . Will only install if it detects that no