Skip to content

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

C#

Added `Path` column to `DavItems` table.

nzbdav-devd7e162273f59
diff --git a/backend/Database/DavDatabaseContext.cs b/backend/Database/DavDatabaseContext.cs
index 8bea79fb..eab9da2a 100644
--- a/backend/Database/DavDatabaseContext.cs
+++ b/backend/Database/DavDatabaseContext.cs
@@ -73,6 +73,9 @@ public sealed class DavDatabaseContext() : DbContext(Options.Value)
.HasConversion<int>()
.IsRequired();
+ e.Property(i => i.Path)
+ .IsRequired();
+
e.HasOne(i => i.Parent)
.WithMany(p => p.Children)
.HasForeignKey(i => i.ParentId)
diff --git a/backend/Database/Migrations/20250819221618_Add-Path-To-DavItem.Designer.cs b/backend/Database/Migrations/20250819221618_Add-Path-To-DavItem.Designer.cs
new file mode 100644
index 00000000..1e2d00fa
--- /dev/null
+++ b/backend/Database/Migrations/20250819221618_Add-Path-To-DavItem.Designer.cs
@@ -0,0 +1,265 @@
+// <auto-generated />
+using System;
+using Microsoft.EntityFrameworkCore;
+using Microsoft.EntityFrameworkCore.Infrastructure;
+using Microsoft.EntityFrameworkCore.Migrations;
+using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
+using NzbWebDAV.Database;
+
+#nullable disable
+
+namespace NzbWebDAV.Database.Migrations
+{
+ [DbContext(typeof(DavDatabaseContext))]
+ [Migration("20250819221618_Add-Path-To-DavItem")]
+ partial class AddPathToDavItem
+ {
+ /// <inheritdoc />
+ protected override void BuildTargetModel(ModelBuilder modelBuilder)
+ {
+#pragma warning disable 612, 618
+ modelBuilder.HasAnnotation("ProductVersion", "9.0.4");
+
+ modelBuilder.Entity("NzbWebDAV.Database.Models.Account", b =>
+ {
+ b.Property<int>("Type")
+ .HasColumnType("INTEGER");
+
+ b.Property<string>("Username")
+ .HasMaxLength(255)
+ .HasColumnType("TEXT");
+
+ b.Property<string>("PasswordHash")
+ .IsRequired()
+ .HasColumnType("TEXT");
+
+ b.Property<string>("RandomSalt")
+ .IsRequired()
+ .HasColumnType("TEXT");
+
+ b.HasKey("Type", "Username");
+
+ b.ToTable("Accounts", (string)null);
+ });
+
+ modelBuilder.Entity("NzbWebDAV.Database.Models.ConfigItem", b =>
+ {
+ b.Property<string>("ConfigName")
+ .HasColumnType("TEXT");
+
+ b.Property<string>("ConfigValue")
+ .IsRequired()
+ .HasColumnType("TEXT");
+
+ b.HasKey("ConfigName");
+
+ b.ToTable("ConfigItems", (string)null);
+ });
+
+ modelBuilder.Entity("NzbWebDAV.Database.Models.DavItem", b =>
+ {
+ b.Property<Guid>("Id")
+ .HasColumnType("TEXT");
+
+ b.Property<DateTime>("CreatedAt")
+ .HasColumnType("TEXT");
+
+ b.Property<long?>("FileSize")
+ .HasColumnType("INTEGER");
+
+ b.Property<string>("Name")
+ .IsRequired()
+ .HasMaxLength(255)
+ .HasColumnType("TEXT");
+
+ b.Property<Guid?>("ParentId")
+ .HasColumnType("TEXT");
+
+ b.Property<string>("Path")
+ .IsRequired()
+ .HasColumnType("TEXT");
+
+ b.Property<int>("Type")
+ .HasColumnType("INTEGER");
+
+ b.HasKey("Id");
+
+ b.HasIndex("ParentId", "Name")
+ .IsUnique();
+
+ b.ToTable("DavItems", (string)null);
+ });
+
+ modelBuilder.Entity("NzbWebDAV.Database.Models.DavNzbFile", b =>
+ {
+ b.Property<Guid>("Id")
+ .HasColumnType("TEXT");
+
+ b.Property<string>("SegmentIds")
+ .IsRequired()
+ .HasColumnType("TEXT");
+
+ b.HasKey("Id");
+
+ b.ToTable("DavNzbFiles", (string)null);
+ });
+
+ modelBuilder.Entity("NzbWebDAV.Database.Models.DavRarFile", b =>
+ {
+ b.Property<Guid>("Id")
+ .HasColumnType("TEXT");
+
+ b.Property<string>("RarParts")
+ .IsRequired()
+ .HasColumnType("TEXT");
+
+ b.HasKey("Id");
+
+ b.ToTable("DavRarFiles", (string)null);
+ });
+
+ modelBuilder.Entity("NzbWebDAV.Database.Models.HistoryItem", b =>
+ {
+ b.Property<Guid>("Id")
+ .HasColumnType("TEXT");
+
+ b.Property<string>("Category")
+ .IsRequired()
+ .HasColumnType("TEXT");
+
+ b.Property<DateTime>("CreatedAt")
+ .HasColumnType("TEXT");
+
+ b.Property<Guid?>("DownloadDirId")
+ .HasColumnType("TEXT");
+
+ b.Property<int>("DownloadStatus")
+ .HasColumnType("INTEGER");
+
+ b.Property<int>("DownloadTimeSeconds")
+ .HasColumnType("INTEGER");
+
+ b.Property<string>("FailMessage")
+ .HasColumnType("TEXT");
+
+ b.Property<string>("FileName")
+ .IsRequired()
+ .HasColumnType("TEXT");
+
+ b.Property<string>("JobName")
+ .IsRequired()
+ .HasColumnType("TEXT");
+
+ b.Property<long>("TotalSegmentBytes")
+ .HasColumnType("INTEGER");
+
+ b.HasKey("Id");
+
+ b.HasIndex("Category");
+
+ b.HasIndex("CreatedAt");
+
+ b.HasIndex("Category", "CreatedAt");
+
+ b.HasIndex("Category", "DownloadDirId");
+
+ b.ToTable("HistoryItems", (string)null);
+ });
+
+ modelBuilder.Entity("NzbWebDAV.Database.Models.QueueItem", b =>
+ {
+ b.Property<Guid>("Id")
+ .HasColumnType("TEXT");
+
+ b.Property<string>("Category")
+ .IsRequired()
+ .HasColumnType("TEXT");
+
+ b.Property<DateTime>("CreatedAt")
+ .HasColumnType("TEXT");
+
+ b.Property<string>("FileName")
+ .IsRequired()
+ .HasColumnType("TEXT");
+
+ b.Property<string>("JobName")
+ .IsRequired()
+ .HasColumnType("TEXT");
+
+ b.Property<string>("NzbContents")
+ .IsRequired()
+ .HasColumnType("TEXT");
+
+ b.Property<long>("NzbFileSize")
+ .HasColumnType("INTEGER");
+
+ b.Property<DateTime?>("PauseUntil")
+ .HasColumnType("TEXT");
+
+ b.Property<int>("PostProcessing")
+ .HasColumnType("INTEGER");
+
+ b.Property<int>("Priority")
+ .HasColumnType("INTEGER");
+
+ b.Property<long>("TotalSegmentBytes")
+ .HasColumnType("INTEGER");
+
+ b.HasKey("Id");
+
+ b.HasIndex("Category");
+
+ b.HasIndex("CreatedAt");
+
+ b.HasIndex("FileName")
+ .IsUnique();
+
+ b.HasIndex("Priority");
+
+ b.HasIndex("Priority", "CreatedAt");
+
+ b.HasIndex("Category", "Priority", "CreatedAt");
+
+ b.ToTable("QueueItems", (string)null);
+ });
+
+ modelBuilder.Entity("NzbWebDAV.Database.Models.DavItem", b =>
+ {
+ b.HasOne("NzbWebDAV.Database.Models.DavItem", "Parent")
+ .WithMany("Children")
+ .HasForeignKey("ParentId")
+ .OnDelete(DeleteBehavior.Cascade);
+
+ b.Navigation("Parent");
+ });
+
+ modelBuilder.Entity("NzbWebDAV.Database.Models.DavNzbFile", b =>
+ {
+ b.HasOne("NzbWebDAV.Database.Models.DavItem", "DavItem")
+ .WithOne()
+ .HasForeignKey("NzbWebDAV.Database.Models.DavNzbFile", "Id")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
+ b.Navigation("DavItem");
+ });
+
+ modelBuilder.Entity("NzbWebDAV.Database.Models.DavRarFile", b =>
+ {
+ b.HasOne("NzbWebDAV.Database.Models.DavItem", "DavItem")
+ .WithOne()
+ .HasForeignKey("NzbWebDAV.Database.Models.DavRarFile", "Id")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
+ b.Navigation("DavItem");
+ });
+
+ modelBuilder.Entity("NzbWebDAV.Database.Models.DavItem", b =>
+ {
+ b.Navigation("Children");
+ });
+#pragma warning restore 612, 618
+ }
+ }
+}
diff --git a/backend/Database/Migrations/20250819221618_Add-Path-To-DavItem.cs b/backend/Database/Migrations/20250819221618_Add-Path-To-DavItem.cs
new file mode 100644
index 00000000..f3d2fd37
--- /dev/null
+++ b/backend/Database/Migrations/20250819221618_Add-Path-To-DavItem.cs
@@ -0,0 +1,58 @@
+using Microsoft.EntityFrameworkCore.Migrations;
+
+#nullable disable
+
+namespace NzbWebDAV.Database.Migrations
+{
+ /// <inheritdoc />
+ public partial class AddPathToDavItem : Migration
+ {
+ /// <inheritdoc />
+ protected override void Up(MigrationBuilder migrationBuilder)
+ {
+ migrationBuilder.AddColumn<string>(
+ name: "Path",
+ table: "DavItems",
+ type: "TEXT",
+ nullable: false,
+ defaultValue: "");
+
+ // Populate the Path column for every existing DavItem
+ // * The root DavItem is given path `/`
+ // * Every other DavItem is given path `{PARENT_PATH}/{NAME}`
+ migrationBuilder.Sql(
+ """
+ WITH RECURSIVE computed(Id, Path) AS (
+ -- base case: the root item
+ SELECT Id, '/'
+ FROM DavItems
+ WHERE Id = '00000000-0000-0000-0000-000000000000'
+
+ UNION ALL
+
+ -- recursive case: all other items
+ SELECT
+ d.Id,
+ CASE
+ WHEN c.Path = '/' THEN '/' || d.Name
+ ELSE c.Path || '/' || d.Name
+ END AS Path
+ FROM DavItems d
+ JOIN computed c ON d.ParentId = c.Id
+ )
+
+ UPDATE DavItems
+ SET Path = (SELECT Path FROM computed WHERE DavItems.Id = computed.Id);
+ """
+ );
+ }
+
+ /// <inheritdoc />
+ protected override void Down(MigrationBuilder migrationBuilder)
+ {
+ migrationBuilder.DropColumn(
+ name: "Path",
+ table: "DavItems");
+ }
+ }
+}
diff --git a/backend/Database/Migrations/DavDatabaseContextModelSnapshot.cs b/backend/Database/Migrations/DavDatabaseContextModelSnapshot.cs
index 8c5e3ec5..009ad603 100644
--- a/backend/Database/Migrations/DavDatabaseContextModelSnapshot.cs
+++ b/backend/Database/Migrations/DavDatabaseContextModelSnapshot.cs
@@ -72,6 +72,10 @@ namespace NzbWebDAV.Database.Migrations
b.Property<Guid?>("ParentId")
.HasColumnType("TEXT");
+ b.Property<string>("Path")
+ .IsRequired()
+ .HasColumnType("TEXT");
+
b.Property<int>("Type")
.HasColumnType("INTEGER");
diff --git a/backend/Database/Models/DavItem.cs b/backend/Database/Models/DavItem.cs
index 15f00e78..9ce281e2 100644
--- a/backend/Database/Models/DavItem.cs
+++ b/backend/Database/Models/DavItem.cs
@@ -10,6 +10,7 @@ public class DavItem
public string Name { get; init; } = null!;
public long? FileSize { get; set; }
public ItemType Type { get; init; }
+ public string Path { get; init; } = null!;
// Important: numerical values cannot be
// changed without a database migration.
@@ -39,6 +40,7 @@ public class DavItem
Name = "/",
FileSize = null,
Type = ItemType.Directory,
+ Path = "/",
};
public static readonly DavItem NzbFolder = new()
@@ -48,6 +50,7 @@ public class DavItem
Name = "nzbs",
FileSize = null,
Type = ItemType.Directory,
+ Path = "/nzbs",
};
public static readonly DavItem ContentFolder = new()
@@ -57,6 +60,7 @@ public class DavItem
Name = "content",
FileSize = null,
Type = ItemType.Directory,
+ Path = "/content",
};
public static readonly DavItem SymlinkFolder = new()
@@ -66,6 +70,7 @@ public class DavItem
Name = "completed-symlinks",
FileSize = null,
Type = ItemType.SymlinkRoot,
+ Path = "/completed-symlinks",
};
public static readonly DavItem IdsFolder = new()
@@ -75,5 +80,6 @@ public class DavItem
Name = ".ids",
FileSize = null,
Type = ItemType.IdsRoot,
+ Path = "/.ids",
};
}
\ No newline at end of file
diff --git a/backend/Queue/FileAggregators/FileAggregator.cs b/backend/Queue/FileAggregators/FileAggregator.cs
index 18cbd602..f74e0adb 100644
--- a/backend/Queue/FileAggregators/FileAggregator.cs
+++ b/backend/Queue/FileAggregators/FileAggregator.cs
@@ -20,7 +20,8 @@ public class FileAggregator(DavDatabaseClient dbClient, DavItem mountDirectory)
ParentId = mountDirectory.Id,
Name = result.FileName,
FileSize = result.FileSize,
- Type = DavItem.ItemType.NzbFile
+ Type = DavItem.ItemType.NzbFile,
+ Path = Path.Join(mountDirectory.Path, result.FileName),
};
var davNzbFile = new DavNzbFile()
diff --git a/backend/Queue/FileAggregators/RarAggregator.cs b/backend/Queue/FileAggregators/RarAggregator.cs
index 09aaa6ca..7d89b56b 100644
--- a/backend/Queue/FileAggregators/RarAggregator.cs
+++ b/backend/Queue/FileAggregators/RarAggregator.cs
@@ -41,15 +41,17 @@ public class RarAggregator(DavDatabaseClient dbClient, DavItem mountDirectory) :
var pathWithinArchive = archiveFile.Key;
var rarParts = archiveFile.Value.ToArray();
var parentDirectory = EnsurePath(pathWithinArchive);
+ var name = Path.GetFileName(pathWithinArchive);
var davItem = new DavItem()
{
Id = Guid.NewGuid(),
CreatedAt = DateTime.Now,
ParentId = parentDirectory.Id,
- Name = Path.GetFileName(pathWithinArchive),
+ Name = name,
FileSize = rarParts.Sum(x => x.ByteCount),
- Type = DavItem.ItemType.RarFile
+ Type = DavItem.ItemType.RarFile,
+ Path = Path.Join(parentDirectory.Path, name),
};
var davRarFile = new DavRarFile()
@@ -91,7 +93,8 @@ public class RarAggregator(DavDatabaseClient dbClient, DavItem mountDirectory) :
CreatedAt = DateTime.Now,
ParentId = parentDirectory.Id,
Name = directoryName,
- Type = DavItem.ItemType.Directory
+ Type = DavItem.ItemType.Directory,
+ Path = Path.Join(parentDirectory.Path, directoryName),
};
_directoryCache.Add(pathKey, directory);
dbClient.Ctx.Items.Add(directory);
diff --git a/backend/Queue/QueueItemProcessor.cs b/backend/Queue/QueueItemProcessor.cs
index 67ead352..6ae58ec9 100644
--- a/backend/Queue/QueueItemProcessor.cs
+++ b/backend/Queue/QueueItemProcessor.cs
@@ -162,6 +162,7 @@ public class QueueItemProcessor(
ParentId = DavItem.ContentFolder.Id,
Name = queueItem.Category,
Type = DavItem.ItemType.Directory,
+ Path = Path.Join(DavItem.ContentFolder.Path, queueItem.Category)
};
dbClient.Ctx.Items.Add(categoryFolder);
return categoryFolder;
@@ -176,6 +177,7 @@ public class QueueItemProcessor(
ParentId = categoryFolder.Id,
Name = queueItem.JobName,
Type = DavItem.ItemType.Directory,
+ Path = Path.Join(categoryFolder.Path, queueItem.JobName)
};
dbClient.Ctx.Items.Add(mountFolder);
return mountFolder;