Usenet streaming with a WebDAV server and a SABnzbd-compatible API
C#
Repair symlinked sonarr items when unhealthy.
nzbdav-deva770457adc92
diff --git a/backend/Clients/RadarrSonarr/SonarrClient.cs b/backend/Clients/RadarrSonarr/SonarrClient.csindex 49b8c7c3..1dc08135 100644--- a/backend/Clients/RadarrSonarr/SonarrClient.cs+++ b/backend/Clients/RadarrSonarr/SonarrClient.cs@@ -1,14 +1,123 @@-using NzbWebDAV.Clients.RadarrSonarr.SonarrModels;+using System.Net;+using NzbWebDAV.Clients.RadarrSonarr.BaseModels;+using NzbWebDAV.Clients.RadarrSonarr.SonarrModels;+using NzbWebDAV.Utils;namespace NzbWebDAV.Clients.RadarrSonarr;public class SonarrClient(string host, string apiKey) : ArrClient(host, apiKey){+ private static readonly Dictionary<string, int> SeriesPathToSeriesIdCache = new();+ private static readonly Dictionary<string, int> SymlinkToEpisodeFileIdCache = new();+public Task<SonarrQueue> GetSonarrQueueAsync() =>Get<SonarrQueue>($"/queue?protocol=usenet&pageSize=5000");- public override Task<bool> RemoveAndSearch(string symlinkPath)+ public Task<List<SonarrSeries>> GetAllSeries() =>+ Get<List<SonarrSeries>>($"/series");++ public Task<SonarrSeries> GetSeries(int seriesId) =>+ Get<SonarrSeries>($"/series/{seriesId}");++ public Task<SonarrEpisodeFile> GetEpisodeFile(int episodeFileId) =>+ Get<SonarrEpisodeFile>($"/episodefile/{episodeFileId}");++ public Task<List<SonarrEpisodeFile>> GetAllEpisodeFiles(int seriesId) =>+ Get<List<SonarrEpisodeFile>>($"/episodefile?seriesId={seriesId}");++ public Task<List<SonarrEpisode>> GetEpisodesFromEpisodeFileId(int episodeFileId) =>+ Get<List<SonarrEpisode>>($"/episode?episodeFileId={episodeFileId}");++ public Task<HttpStatusCode> DeleteEpisodeFile(int episodeFileId) =>+ Delete($"/episodefile/{episodeFileId}");++ public Task<ArrCommand> SearchEpisodesAsync(List<int> episodeIds) =>+ CommandAsync(new { name = "EpisodeSearch", episodeIds });++ public override async Task<bool> RemoveAndSearch(string symlinkPath){- return Task.FromResult(true);+ // get episode-file-id and episode-ids+ var mediaIds = await GetMediaIds(symlinkPath);+ if (mediaIds == null) return false;++ // delete the episode-file+ if (await DeleteEpisodeFile(mediaIds.Value.episodeFileId) != HttpStatusCode.OK)+ throw new Exception($"Failed to delete episode file `{symlinkPath}` from sonarr instance `{Host}`.");++ // trigger a new search for each episode+ await SearchEpisodesAsync(mediaIds.Value.episodeIds);+ return true;+ }++ private async Task<(int episodeFileId, List<int> episodeIds)?> GetMediaIds(string symlinkPath)+ {+ // get episode-file-id+ var episodeFileId = await GetEpisodeFileId(symlinkPath);+ if (episodeFileId == null) return null;++ // get episode-ids+ var episodes = await GetEpisodesFromEpisodeFileId(episodeFileId.Value);+ var episodeIds = episodes.Select(x => x.Id).ToList();+ if (episodeIds.Count == 0) return null;++ // return+ return (episodeFileId.Value, episodeIds);+ }++ private async Task<int?> GetEpisodeFileId(string symlinkPath)+ {+ // if episode-file-id is found in the cache, verify it and return it+ if (SymlinkToEpisodeFileIdCache.TryGetValue(symlinkPath, out var episodeFileId))+ {+ var episodeFile = await GetEpisodeFile(episodeFileId);+ if (episodeFile.Path == symlinkPath) return episodeFileId;+ }++ // otherwise, find the series-id+ var seriesId = await GetSeriesId(symlinkPath);+ if (seriesId == null) return null;++ // then use it to find all episode-files and repopulate the cache+ int? result = null;+ foreach (var episodeFile in await GetAllEpisodeFiles(seriesId.Value))+ {+ SymlinkToEpisodeFileIdCache[episodeFile.Path!] = episodeFile.Id;+ if (episodeFile.Path == symlinkPath)+ result = episodeFile.Id;+ }++ // return the found episode-file-id+ return result;+ }++ private async Task<int?> GetSeriesId(string symlinkPath)+ {+ // get series-id from cache+ var cachedSeriesId = PathUtil.GetAllParentDirectories(symlinkPath)+ .Where(x => SeriesPathToSeriesIdCache.ContainsKey(x))+ .Select(x => SeriesPathToSeriesIdCache[x])+ .Select(x => (int?)x)+ .FirstOrDefault();++ // if found, verify and return it+ if (cachedSeriesId != null)+ {+ var series = await GetSeries(cachedSeriesId.Value);+ if (symlinkPath.StartsWith(series.Path!))+ return+ cachedSeriesId;+ }++ // otherwise, fetch all series and repopulate the cache+ int? result = null;+ foreach (var series in await GetAllSeries())+ {+ SeriesPathToSeriesIdCache[series.Path!] = series.Id;+ if (symlinkPath.StartsWith(series.Path!))+ result = series.Id;+ }++ // return the found series-id+ return result;}}\ No newline at end of filediff --git a/backend/Clients/RadarrSonarr/SonarrModels/SonarrEpisode.cs b/backend/Clients/RadarrSonarr/SonarrModels/SonarrEpisode.csnew file mode 100644index 00000000..1e25cb56--- /dev/null+++ b/backend/Clients/RadarrSonarr/SonarrModels/SonarrEpisode.cs@@ -0,0 +1,21 @@+using System.Text.Json.Serialization;++namespace NzbWebDAV.Clients.RadarrSonarr.SonarrModels;++public class SonarrEpisode+{+ [JsonPropertyName("id")]+ public int Id { get; set; }++ [JsonPropertyName("seriesId")]+ public int SeriesId { get; set; }++ [JsonPropertyName("seasonNumber")]+ public int SeasonNumber { get; set; }++ [JsonPropertyName("episodeNumber")]+ public int EpisodeNumber { get; set; }++ [JsonPropertyName("title")]+ public string? Title { get; set; }+}\ No newline at end of filediff --git a/backend/Clients/RadarrSonarr/SonarrModels/SonarrEpisodeFile.cs b/backend/Clients/RadarrSonarr/SonarrModels/SonarrEpisodeFile.csnew file mode 100644index 00000000..49ecfd32--- /dev/null+++ b/backend/Clients/RadarrSonarr/SonarrModels/SonarrEpisodeFile.cs@@ -0,0 +1,15 @@+using System.Text.Json.Serialization;++namespace NzbWebDAV.Clients.RadarrSonarr.SonarrModels;++public class SonarrEpisodeFile+{+ [JsonPropertyName("id")]+ public int Id { get; set; }++ [JsonPropertyName("seriesId")]+ public int SeriesId { get; set; }++ [JsonPropertyName("path")]+ public string? Path { get; set; }+}\ No newline at end of filediff --git a/backend/Clients/RadarrSonarr/SonarrModels/SonarrSeries.cs b/backend/Clients/RadarrSonarr/SonarrModels/SonarrSeries.csnew file mode 100644index 00000000..a5a166fc--- /dev/null+++ b/backend/Clients/RadarrSonarr/SonarrModels/SonarrSeries.cs@@ -0,0 +1,15 @@+using System.Text.Json.Serialization;++namespace NzbWebDAV.Clients.RadarrSonarr.SonarrModels;++public class SonarrSeries+{+ [JsonPropertyName("id")]+ public int Id { get; set; }++ [JsonPropertyName("title")]+ public string? Title { get; set; }++ [JsonPropertyName("path")]+ public string? Path { get; set; }+}\ No newline at end of filediff --git a/backend/Services/HealthCheckService.cs b/backend/Services/HealthCheckService.csindex 1ad42cc9..064b1a56 100644--- a/backend/Services/HealthCheckService.cs+++ b/backend/Services/HealthCheckService.cs@@ -211,15 +211,7 @@ public class HealthCheckServicevar rootFolders = await arrClient.GetRootFolders();if (!rootFolders.Any(x => symlink.StartsWith(x.Path!))) continue;- // if we found corresponding sonarr instance,- // then do nothing for now. It is not yet implemented.- if (arrClient is SonarrClient)- {- Log.Warning($"Found corresponding arr instance for repair: {arrClient.Host}");- return;- }-- // if we found a corresponding radarr instance,+ // if we found a corresponding arr instance,// then remove and search.if (await arrClient.RemoveAndSearch(symlink)){diff --git a/backend/Utils/PathUtil.cs b/backend/Utils/PathUtil.csnew file mode 100644index 00000000..0e922bb3--- /dev/null+++ b/backend/Utils/PathUtil.cs@@ -0,0 +1,12 @@+namespace NzbWebDAV.Utils;++public class PathUtil+{+ public static IEnumerable<string> GetAllParentDirectories(string path)+ {+ var directoryName = Path.GetDirectoryName(path);+ return !string.IsNullOrEmpty(directoryName)+ ? GetAllParentDirectories(directoryName).Prepend(directoryName)+ : [];+ }+}\ No newline at end of file