Skip to content

Usenet streaming with a WebDAV server and a SABnzbd-compatible API

C#

Refactored OrganizedSymlinksUtil to its own class.

nzbdav-devfee05ad655aa
diff --git a/backend/Services/HealthCheckService.cs b/backend/Services/HealthCheckService.cs
index 746b7d9e..77660f93 100644
--- a/backend/Services/HealthCheckService.cs
+++ b/backend/Services/HealthCheckService.cs
@@ -124,6 +124,16 @@ public class HealthCheckService
// update the database
davItem.LastHealthCheck = DateTimeOffset.UtcNow;
davItem.NextHealthCheck = davItem.ReleaseDate + 2 * (davItem.LastHealthCheck - davItem.ReleaseDate);
+ dbClient.Ctx.HealthCheckResults.Add(new HealthCheckResult()
+ {
+ Id = new Guid(),
+ DavItemId = davItem.Id,
+ Path = davItem.Path,
+ CreatedAt = DateTimeOffset.UtcNow,
+ Result = HealthCheckResult.HealthResult.Healthy,
+ RepairStatus = HealthCheckResult.RepairAction.None,
+ Message = null
+ });
await dbClient.Ctx.SaveChangesAsync(ct);
return true;
}
@@ -166,84 +176,26 @@ public class HealthCheckService
private async Task Repair(DavItem davItem, DavDatabaseClient dbClient, CancellationToken ct)
{
Log.Warning($"Repairing database item: {davItem.Path}");
- var symlink = OrganizedSymlinks.GetSymlink(davItem, _configManager);
+ var symlink = OrganizedSymlinksUtil.GetSymlink(davItem, _configManager);
// for unlinked files, we can simply delete the unhealthy item
if (symlink == null)
{
- Log.Warning($"Item is not linked to organized media library. Deleting: {davItem.Path}");
- // dbClient.Ctx.Items.Remove(davItem);
+ dbClient.Ctx.Items.Remove(davItem);
+ dbClient.Ctx.HealthCheckResults.Add(new HealthCheckResult()
+ {
+ Id = new Guid(),
+ DavItemId = davItem.Id,
+ Path = davItem.Path,
+ CreatedAt = DateTimeOffset.UtcNow,
+ Result = HealthCheckResult.HealthResult.Unhealthy,
+ RepairStatus = HealthCheckResult.RepairAction.Deleted,
+ Message = null
+ });
+ await dbClient.Ctx.SaveChangesAsync(ct);
return;
}
Log.Warning($"Symlink found in organized media library: {symlink}");
}
-
- private static class OrganizedSymlinks
- {
- private static readonly Dictionary<Guid, string> Cache = new();
-
- public static string? GetSymlink(DavItem targetDavItem, ConfigManager configManager)
- {
- return !TryGetSymlinkFromCache(targetDavItem, configManager, out var symlinkFromCache)
- ? SearchForSymlink(targetDavItem, configManager)
- : symlinkFromCache;
- }
-
- private static bool TryGetSymlinkFromCache
- (
- DavItem targetDavItem,
- ConfigManager configManager,
- out string? symlink
- )
- {
- return Cache.TryGetValue(targetDavItem.Id, out symlink)
- && Verify(symlink, targetDavItem, configManager);
- }
-
- private static bool Verify(string symlink, DavItem targetDavItem, ConfigManager configManager)
- {
- return GetSymlinkTargets([symlink], configManager)
- .Select(x => x.Target)
- .FirstOrDefault() == targetDavItem.Id;
- }
-
- private static string? SearchForSymlink(DavItem targetDavItem, ConfigManager configManager)
- {
- var libraryRoot = configManager.GetLibraryDir()!;
- var allSymlinkPaths = Directory.EnumerateFileSystemEntries(libraryRoot, "*", SearchOption.AllDirectories);
- var allSymlinks = GetSymlinkTargets(allSymlinkPaths, configManager);
-
- string? result = null;
- foreach (var symlink in allSymlinks)
- {
- Cache[targetDavItem.Id] = symlink.FileInfo.FullName;
- if (symlink.Target == targetDavItem.Id)
- result = symlink.FileInfo.FullName;
- }
-
- return result;
- }
-
- private static IEnumerable<(FileInfo FileInfo, Guid Target)> GetSymlinkTargets
- (
- IEnumerable<string> symlinkPaths,
- ConfigManager configManager
- )
- {
- var mountDir = configManager.GetRcloneMountDir();
- return symlinkPaths
- .Select(x => new FileInfo(x))
- .Where(x => x.Attributes.HasFlag(FileAttributes.ReparsePoint))
- .Select(x => (FileInfo: x, Target: x.LinkTarget))
- .Where(x => x.Target is not null)
- .Select(x => (x.FileInfo, Target: x.Target!))
- .Where(x => x.Target.StartsWith(mountDir))
- .Select(x => (x.FileInfo, Target: x.Target.RemovePrefix(mountDir)))
- .Select(x => (x.FileInfo, Target: x.Target.StartsWith("/") ? x.Target : $"/{x.Target}"))
- .Where(x => x.Target.StartsWith("/.ids"))
- .Select(x => (x.FileInfo, Target: Path.GetFileNameWithoutExtension(x.Target)))
- .Select(x => (x.FileInfo, Target: Guid.Parse(x.Target)));
- }
- }
}
\ No newline at end of file
diff --git a/backend/Tasks/RemoveUnlinkedFilesTask.cs b/backend/Tasks/RemoveUnlinkedFilesTask.cs
index 1538091b..8ed0f1b6 100644
--- a/backend/Tasks/RemoveUnlinkedFilesTask.cs
+++ b/backend/Tasks/RemoveUnlinkedFilesTask.cs
@@ -3,6 +3,7 @@ using NzbWebDAV.Config;
using NzbWebDAV.Database;
using NzbWebDAV.Database.Models;
using NzbWebDAV.Extensions;
+using NzbWebDAV.Utils;
using NzbWebDAV.Websocket;
using Serilog;
@@ -105,20 +106,8 @@ public class RemoveUnlinkedFilesTask(
private HashSet<Guid> GetLinkedIds()
{
- var mountDir = configManager.GetRcloneMountDir();
- var libraryRoot = configManager.GetLibraryDir();
- return Directory.EnumerateFileSystemEntries(libraryRoot, "*", SearchOption.AllDirectories)
- .Select(x => new FileInfo(x))
- .Where(x => x.Attributes.HasFlag(FileAttributes.ReparsePoint))
- .Select(x => x.LinkTarget)
- .Where(x => x is not null)
- .Select(x => x!)
- .Where(x => x.StartsWith(mountDir))
- .Select(x => x.RemovePrefix(mountDir))
- .Select(x => x.StartsWith("/") ? x : $"/{x}")
- .Where(x => x.StartsWith("/.ids"))
- .Select(Path.GetFileNameWithoutExtension)
- .Select(Guid.Parse)
+ return OrganizedSymlinksUtil.GetLibrarySymlinkTargets(configManager)
+ .Select(x => x.Target)
.ToHashSet();
}
diff --git a/backend/Utils/OrganizedSymlinksUtil.cs b/backend/Utils/OrganizedSymlinksUtil.cs
new file mode 100644
index 00000000..941c66be
--- /dev/null
+++ b/backend/Utils/OrganizedSymlinksUtil.cs
@@ -0,0 +1,87 @@
+using NzbWebDAV.Config;
+using NzbWebDAV.Database.Models;
+using NzbWebDAV.Extensions;
+
+namespace NzbWebDAV.Utils;
+
+public static class OrganizedSymlinksUtil
+{
+ private static readonly Dictionary<Guid, string> Cache = new();
+
+ /// <summary>
+ /// Searches organized media library for a symlink pointing to the given target
+ /// </summary>
+ /// <param name="targetDavItem">The given target</param>
+ /// <param name="configManager">The application config</param>
+ /// <returns>The path to a symlink in the organized media library that points to the given target.</returns>
+ public static string? GetSymlink(DavItem targetDavItem, ConfigManager configManager)
+ {
+ return !TryGetSymlinkFromCache(targetDavItem, configManager, out var symlinkFromCache)
+ ? SearchForSymlink(targetDavItem, configManager)
+ : symlinkFromCache;
+ }
+
+ /// <summary>
+ /// Enumerates all symlinks within the organized media library that point to nzbdav dav-items.
+ /// </summary>
+ /// <param name="configManager">The application config</param>
+ /// <returns>All symlinks within the organized media library that point to nzbdav dav-items.</returns>
+ public static IEnumerable<(FileInfo FileInfo, Guid Target)> GetLibrarySymlinkTargets(ConfigManager configManager)
+ {
+ var libraryRoot = configManager.GetLibraryDir()!;
+ var allSymlinkPaths = Directory.EnumerateFileSystemEntries(libraryRoot, "*", SearchOption.AllDirectories);
+ return GetSymlinkTargets(allSymlinkPaths, configManager);
+ }
+
+ private static bool TryGetSymlinkFromCache
+ (
+ DavItem targetDavItem,
+ ConfigManager configManager,
+ out string? symlink
+ )
+ {
+ return Cache.TryGetValue(targetDavItem.Id, out symlink)
+ && Verify(symlink, targetDavItem, configManager);
+ }
+
+ private static bool Verify(string symlink, DavItem targetDavItem, ConfigManager configManager)
+ {
+ return GetSymlinkTargets([symlink], configManager)
+ .Select(x => x.Target)
+ .FirstOrDefault() == targetDavItem.Id;
+ }
+
+ private static string? SearchForSymlink(DavItem targetDavItem, ConfigManager configManager)
+ {
+ string? result = null;
+ foreach (var symlink in GetLibrarySymlinkTargets(configManager))
+ {
+ Cache[targetDavItem.Id] = symlink.FileInfo.FullName;
+ if (symlink.Target == targetDavItem.Id)
+ result = symlink.FileInfo.FullName;
+ }
+
+ return result;
+ }
+
+ private static IEnumerable<(FileInfo FileInfo, Guid Target)> GetSymlinkTargets
+ (
+ IEnumerable<string> symlinkPaths,
+ ConfigManager configManager
+ )
+ {
+ var mountDir = configManager.GetRcloneMountDir();
+ return symlinkPaths
+ .Select(x => new FileInfo(x))
+ .Where(x => x.Attributes.HasFlag(FileAttributes.ReparsePoint))
+ .Select(x => (FileInfo: x, Target: x.LinkTarget))
+ .Where(x => x.Target is not null)
+ .Select(x => (x.FileInfo, Target: x.Target!))
+ .Where(x => x.Target.StartsWith(mountDir))
+ .Select(x => (x.FileInfo, Target: x.Target.RemovePrefix(mountDir)))
+ .Select(x => (x.FileInfo, Target: x.Target.StartsWith("/") ? x.Target : $"/{x.Target}"))
+ .Where(x => x.Target.StartsWith("/.ids"))
+ .Select(x => (x.FileInfo, Target: Path.GetFileNameWithoutExtension(x.Target)))
+ .Select(x => (x.FileInfo, Target: Guid.Parse(x.Target)));
+ }
+}
\ No newline at end of file