Skip to content

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

C#

Removed `extracted` folder. Always extract to download dir (#105)

nzbdav-dev7c4bb9364cb4
diff --git a/backend/Database/Models/DavItem.cs b/backend/Database/Models/DavItem.cs
index fbb4ed03..faf083bc 100644
--- a/backend/Database/Models/DavItem.cs
+++ b/backend/Database/Models/DavItem.cs
@@ -10,10 +10,10 @@ public class DavItem
public string IdPrefix { get; init; }
public DateTime CreatedAt { get; init; }
public Guid? ParentId { get; init; }
- public string Name { get; init; } = null!;
+ public string Name { get; set; } = null!;
public long? FileSize { get; set; }
public ItemType Type { get; init; }
- public string Path { get; init; } = null!;
+ public string Path { get; set; } = null!;
public DateTimeOffset? ReleaseDate { get; set; }
public DateTimeOffset? LastHealthCheck { get; set; }
public DateTimeOffset? NextHealthCheck { get; set; }
diff --git a/backend/Queue/FileAggregators/BaseAggregator.cs b/backend/Queue/FileAggregators/BaseAggregator.cs
index fe2d4369..ef586e03 100644
--- a/backend/Queue/FileAggregators/BaseAggregator.cs
+++ b/backend/Queue/FileAggregators/BaseAggregator.cs
@@ -14,7 +14,6 @@ public abstract class BaseAggregator
{
var pathSegments = pathWithinArchive
.Split(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar)
- .Prepend("extracted")
.ToArray();
var parentDirectory = MountDirectory;
var pathKey = "";
diff --git a/backend/Queue/PostProcessors/RenameDuplicatesPostProcessor.cs b/backend/Queue/PostProcessors/RenameDuplicatesPostProcessor.cs
new file mode 100644
index 00000000..1451e137
--- /dev/null
+++ b/backend/Queue/PostProcessors/RenameDuplicatesPostProcessor.cs
@@ -0,0 +1,39 @@
+using Microsoft.EntityFrameworkCore;
+using NzbWebDAV.Config;
+using NzbWebDAV.Database;
+using NzbWebDAV.Database.Models;
+using Serilog;
+
+namespace NzbWebDAV.Queue.PostProcessors;
+
+public class RenameDuplicatesPostProcessor(DavDatabaseClient dbClient)
+{
+ public void RenameDuplicates()
+ {
+ var groupsOfDuplicates = dbClient.Ctx.ChangeTracker.Entries<DavItem>()
+ .Where(x => x.State == EntityState.Added)
+ .Select(x => x.Entity)
+ .Where(x => x.Type != DavItem.ItemType.Directory)
+ .GroupBy(x => new DuplicateKey(x.ParentId!.Value, x.Name))
+ .Select(g => g.ToList())
+ .Where(g => g.Count > 1);
+
+ foreach (var group in groupsOfDuplicates)
+ RenameDuplicatesInGroup(group);
+ }
+
+ private void RenameDuplicatesInGroup(List<DavItem> duplicates)
+ {
+ var canonicalName = Path.GetFileNameWithoutExtension(duplicates[0].Name);
+ var canonicalExtension = Path.GetExtension(duplicates[0].Name);
+ for (var i = 1; i < duplicates.Count; i++)
+ {
+ var newName = $"{canonicalName} ({i+1}){canonicalExtension}";
+ var parentPath = Path.GetDirectoryName(duplicates[i].Path);
+ duplicates[i].Name = newName;
+ duplicates[i].Path = Path.Join(parentPath, newName);
+ }
+ }
+
+ private record struct DuplicateKey(Guid ParentId, string Name);
+}
\ No newline at end of file
diff --git a/backend/Queue/QueueItemProcessor.cs b/backend/Queue/QueueItemProcessor.cs
index a9a13675..60961f81 100644
--- a/backend/Queue/QueueItemProcessor.cs
+++ b/backend/Queue/QueueItemProcessor.cs
@@ -177,6 +177,7 @@ public class QueueItemProcessor(
new MultipartMkvAggregator(dbClient, mountFolder, checkedFullHealth).UpdateDatabase(fileProcessingResults);
// post-processing
+ new RenameDuplicatesPostProcessor(dbClient).RenameDuplicates();
new BlacklistedExtensionPostProcessor(configManager, dbClient).RemoveBlacklistedExtensions();
// validate video files found