Skip to content

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

C#

Added indexed IdPrefix column to DavItems table.

nzbdav-deva028d7a7be92
diff --git a/backend/Database/DavDatabaseClient.cs b/backend/Database/DavDatabaseClient.cs
index 8cba2391..6910403c 100644
--- a/backend/Database/DavDatabaseClient.cs
+++ b/backend/Database/DavDatabaseClient.cs
@@ -17,7 +17,7 @@ public sealed class DavDatabaseClient(DavDatabaseContext ctx)
public Task<List<DavItem>> GetFilesByIdPrefix(string prefix)
{
return ctx.Items
- .Where(i => EF.Functions.Like(i.Id.ToString(), $"{prefix}%"))
+ .Where(i => i.IdPrefix == prefix)
.Where(i => i.Type == DavItem.ItemType.NzbFile || i.Type == DavItem.ItemType.RarFile)
.ToListAsync();
}
diff --git a/backend/Database/DavDatabaseContext.cs b/backend/Database/DavDatabaseContext.cs
index eab9da2a..8c5a25ab 100644
--- a/backend/Database/DavDatabaseContext.cs
+++ b/backend/Database/DavDatabaseContext.cs
@@ -76,6 +76,9 @@ public sealed class DavDatabaseContext() : DbContext(Options.Value)
e.Property(i => i.Path)
.IsRequired();
+ e.Property(i => i.IdPrefix)
+ .IsRequired();
+
e.HasOne(i => i.Parent)
.WithMany(p => p.Children)
.HasForeignKey(i => i.ParentId)
@@ -83,6 +86,8 @@ public sealed class DavDatabaseContext() : DbContext(Options.Value)
e.HasIndex(i => new { i.ParentId, i.Name })
.IsUnique();
+
+ e.HasIndex(i => new { i.IdPrefix, i.Type });
});
// DavNzbFile
diff --git a/backend/Database/Migrations/20250824100609_Add-IdPrefix-To-DavItems.Designer.cs b/backend/Database/Migrations/20250824100609_Add-IdPrefix-To-DavItems.Designer.cs
new file mode 100644
index 00000000..1b332209
--- /dev/null
+++ b/backend/Database/Migrations/20250824100609_Add-IdPrefix-To-DavItems.Designer.cs
@@ -0,0 +1,271 @@
+// <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("20250824100609_Add-IdPrefix-To-DavItems")]
+ partial class AddIdPrefixToDavItems
+ {
+ /// <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>("IdPrefix")
+ .IsRequired()
+ .HasColumnType("TEXT");
+
+ 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("IdPrefix", "Type");
+
+ 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/20250824100609_Add-IdPrefix-To-DavItems.cs b/backend/Database/Migrations/20250824100609_Add-IdPrefix-To-DavItems.cs
new file mode 100644
index 00000000..5fca2810
--- /dev/null
+++ b/backend/Database/Migrations/20250824100609_Add-IdPrefix-To-DavItems.cs
@@ -0,0 +1,46 @@
+using Microsoft.EntityFrameworkCore.Migrations;
+
+#nullable disable
+
+namespace NzbWebDAV.Database.Migrations
+{
+ /// <inheritdoc />
+ public partial class AddIdPrefixToDavItems : Migration
+ {
+ /// <inheritdoc />
+ protected override void Up(MigrationBuilder migrationBuilder)
+ {
+ migrationBuilder.AddColumn<string>(
+ name: "IdPrefix",
+ table: "DavItems",
+ type: "TEXT",
+ nullable: false,
+ defaultValue: "");
+
+ migrationBuilder.Sql(
+ """
+ UPDATE DavItems
+ SET IdPrefix = lower(substr(Id, 1, 5));
+ """
+ );
+
+ migrationBuilder.CreateIndex(
+ name: "IX_DavItems_IdPrefix_Type",
+ table: "DavItems",
+ columns: new[] { "IdPrefix", "Type" });
+
+ }
+
+ /// <inheritdoc />
+ protected override void Down(MigrationBuilder migrationBuilder)
+ {
+ migrationBuilder.DropIndex(
+ name: "IX_DavItems_IdPrefix_Type",
+ table: "DavItems");
+
+ migrationBuilder.DropColumn(
+ name: "IdPrefix",
+ table: "DavItems");
+ }
+ }
+}
diff --git a/backend/Database/Migrations/DavDatabaseContextModelSnapshot.cs b/backend/Database/Migrations/DavDatabaseContextModelSnapshot.cs
index 009ad603..48795ffa 100644
--- a/backend/Database/Migrations/DavDatabaseContextModelSnapshot.cs
+++ b/backend/Database/Migrations/DavDatabaseContextModelSnapshot.cs
@@ -64,6 +64,10 @@ namespace NzbWebDAV.Database.Migrations
b.Property<long?>("FileSize")
.HasColumnType("INTEGER");
+ b.Property<string>("IdPrefix")
+ .IsRequired()
+ .HasColumnType("TEXT");
+
b.Property<string>("Name")
.IsRequired()
.HasMaxLength(255)
@@ -81,6 +85,8 @@ namespace NzbWebDAV.Database.Migrations
b.HasKey("Id");
+ b.HasIndex("IdPrefix", "Type");
+
b.HasIndex("ParentId", "Name")
.IsUnique();
diff --git a/backend/Database/Models/DavItem.cs b/backend/Database/Models/DavItem.cs
index 9ce281e2..23e38c6b 100644
--- a/backend/Database/Models/DavItem.cs
+++ b/backend/Database/Models/DavItem.cs
@@ -4,7 +4,10 @@ namespace NzbWebDAV.Database.Models;
public class DavItem
{
+ public const int IdPrefixLength = 5;
+
public Guid Id { get; init; }
+ public string IdPrefix { get; init; }
public DateTime CreatedAt { get; init; }
public Guid? ParentId { get; init; }
public string Name { get; init; } = null!;
@@ -12,6 +15,28 @@ public class DavItem
public ItemType Type { get; init; }
public string Path { get; init; } = null!;
+ public static DavItem New
+ (
+ Guid id,
+ DavItem parent,
+ string name,
+ long? fileSize,
+ ItemType type
+ )
+ {
+ return new DavItem()
+ {
+ Id = id,
+ IdPrefix = id.ToString()[..5],
+ CreatedAt = DateTime.Now,
+ ParentId = parent.Id,
+ Name = name,
+ FileSize = fileSize,
+ Type = type,
+ Path = System.IO.Path.Join(parent.Path, name)
+ };
+ }
+
// Important: numerical values cannot be
// changed without a database migration.
public enum ItemType
diff --git a/backend/Queue/FileAggregators/FileAggregator.cs b/backend/Queue/FileAggregators/FileAggregator.cs
index f74e0adb..99006fb2 100644
--- a/backend/Queue/FileAggregators/FileAggregator.cs
+++ b/backend/Queue/FileAggregators/FileAggregator.cs
@@ -13,16 +13,13 @@ public class FileAggregator(DavDatabaseClient dbClient, DavItem mountDirectory)
if (processorResult is not FileProcessor.Result result) continue;
if (result.FileName == "") continue; // skip files whose name we can't determine
- var davItem = new DavItem()
- {
- Id = Guid.NewGuid(),
- CreatedAt = DateTime.Now,
- ParentId = mountDirectory.Id,
- Name = result.FileName,
- FileSize = result.FileSize,
- Type = DavItem.ItemType.NzbFile,
- Path = Path.Join(mountDirectory.Path, result.FileName),
- };
+ var davItem = DavItem.New(
+ id: Guid.NewGuid(),
+ parent: mountDirectory,
+ name: result.FileName,
+ fileSize: result.FileSize,
+ type: DavItem.ItemType.NzbFile
+ );
var davNzbFile = new DavNzbFile()
{
diff --git a/backend/Queue/FileAggregators/RarAggregator.cs b/backend/Queue/FileAggregators/RarAggregator.cs
index 7d89b56b..188811c2 100644
--- a/backend/Queue/FileAggregators/RarAggregator.cs
+++ b/backend/Queue/FileAggregators/RarAggregator.cs
@@ -43,16 +43,13 @@ public class RarAggregator(DavDatabaseClient dbClient, DavItem mountDirectory) :
var parentDirectory = EnsurePath(pathWithinArchive);
var name = Path.GetFileName(pathWithinArchive);
- var davItem = new DavItem()
- {
- Id = Guid.NewGuid(),
- CreatedAt = DateTime.Now,
- ParentId = parentDirectory.Id,
- Name = name,
- FileSize = rarParts.Sum(x => x.ByteCount),
- Type = DavItem.ItemType.RarFile,
- Path = Path.Join(parentDirectory.Path, name),
- };
+ var davItem = DavItem.New(
+ id: Guid.NewGuid(),
+ parent: parentDirectory,
+ name: name,
+ fileSize: rarParts.Sum(x => x.ByteCount),
+ type: DavItem.ItemType.RarFile
+ );
var davRarFile = new DavRarFile()
{
@@ -87,15 +84,14 @@ public class RarAggregator(DavDatabaseClient dbClient, DavItem mountDirectory) :
private DavItem EnsureDirectory(DavItem parentDirectory, string directoryName, string pathKey)
{
if (_directoryCache.TryGetValue(pathKey, out var cachedDirectory)) return cachedDirectory;
- var directory = new DavItem()
- {
- Id = Guid.NewGuid(),
- CreatedAt = DateTime.Now,
- ParentId = parentDirectory.Id,
- Name = directoryName,
- Type = DavItem.ItemType.Directory,
- Path = Path.Join(parentDirectory.Path, directoryName),
- };
+
+ var directory = DavItem.New(
+ id: Guid.NewGuid(),
+ parent: parentDirectory,
+ name: directoryName,
+ fileSize: null,
+ type: DavItem.ItemType.Directory
+ );
_directoryCache.Add(pathKey, directory);
dbClient.Ctx.Items.Add(directory);
return directory;
diff --git a/backend/Queue/QueueItemProcessor.cs b/backend/Queue/QueueItemProcessor.cs
index 63f04b15..15ada3a3 100644
--- a/backend/Queue/QueueItemProcessor.cs
+++ b/backend/Queue/QueueItemProcessor.cs
@@ -161,30 +161,26 @@ public class QueueItemProcessor(
return categoryFolder;
// otherwise, create it
- categoryFolder = new DavItem()
- {
- Id = Guid.NewGuid(),
- CreatedAt = DateTime.Now,
- ParentId = DavItem.ContentFolder.Id,
- Name = queueItem.Category,
- Type = DavItem.ItemType.Directory,
- Path = Path.Join(DavItem.ContentFolder.Path, queueItem.Category)
- };
+ categoryFolder = DavItem.New(
+ id: Guid.NewGuid(),
+ parent: DavItem.ContentFolder,
+ name: queueItem.Category,
+ fileSize: null,
+ type: DavItem.ItemType.Directory
+ );
dbClient.Ctx.Items.Add(categoryFolder);
return categoryFolder;
}
private DavItem CreateMountFolder(DavItem categoryFolder)
{
- var mountFolder = new DavItem()
- {
- Id = Guid.NewGuid(),
- CreatedAt = DateTime.Now,
- ParentId = categoryFolder.Id,
- Name = queueItem.JobName,
- Type = DavItem.ItemType.Directory,
- Path = Path.Join(categoryFolder.Path, queueItem.JobName)
- };
+ var mountFolder = DavItem.New(
+ id: Guid.NewGuid(),
+ parent: categoryFolder,
+ name: queueItem.JobName,
+ fileSize: null,
+ type: DavItem.ItemType.Directory
+ );
dbClient.Ctx.Items.Add(mountFolder);
return mountFolder;
}
diff --git a/backend/WebDav/DatabaseStoreIdsCollection.cs b/backend/WebDav/DatabaseStoreIdsCollection.cs
index 79628cc3..eb9b2083 100644
--- a/backend/WebDav/DatabaseStoreIdsCollection.cs
+++ b/backend/WebDav/DatabaseStoreIdsCollection.cs
@@ -4,6 +4,7 @@ using NWebDav.Server.Stores;
using NzbWebDAV.Clients;
using NzbWebDAV.Config;
using NzbWebDAV.Database;
+using NzbWebDAV.Database.Models;
using NzbWebDAV.WebDav.Base;
using NzbWebDAV.WebDav.Requests;
@@ -22,7 +23,6 @@ public class DatabaseStoreIdsCollection(
public override string UniqueKey => currentPath;
public override DateTime CreatedAt => default;
- public const int FanningDepth = 5;
private const string Alphabet = "0123456789abcdef";
private readonly string[] _currentPathParts = currentPath.Split(
@@ -39,7 +39,7 @@ public class DatabaseStoreIdsCollection(
protected override async Task<IStoreItem?> GetItemAsync(GetItemRequest request)
{
var (dir, ctx, db, usenet, config) = (request.Name, httpContext, dbClient, usenetClient, configManager);
- if (_currentPathParts.Length < FanningDepth)
+ if (_currentPathParts.Length < DavItem.IdPrefixLength)
{
if (request.Name.Length != 1) return null;
if (!Alphabet.Contains(request.Name[0])) return null;
@@ -53,7 +53,7 @@ public class DatabaseStoreIdsCollection(
protected override async Task<IStoreItem[]> GetAllItemsAsync(CancellationToken cancellationToken)
{
var (ctx, db, usenet, config) = (httpContext, dbClient, usenetClient, configManager);
- if (_currentPathParts.Length < FanningDepth)
+ if (_currentPathParts.Length < DavItem.IdPrefixLength)
return Alphabet
.Select(x => x.ToString())
.Select(x => new DatabaseStoreIdsCollection(x, Path.Join(currentPath, x), ctx, db, usenet, config))
diff --git a/backend/WebDav/DatabaseStoreSymlinkFile.cs b/backend/WebDav/DatabaseStoreSymlinkFile.cs
index f8955d72..a71fba8c 100644
--- a/backend/WebDav/DatabaseStoreSymlinkFile.cs
+++ b/backend/WebDav/DatabaseStoreSymlinkFile.cs
@@ -42,9 +42,8 @@ public class DatabaseStoreSymlinkFile(DavItem davFile, string parentPath, Config
public static string GetTargetPath(DavItem davFile, string mountDir)
{
- var pathParts = davFile.Id.ToString()
+ var pathParts = davFile.IdPrefix
.Select(x => x.ToString())
- .Take(DatabaseStoreIdsCollection.FanningDepth)
.Prepend(DavItem.IdsFolder.Name)
.Prepend(mountDir)
.Append(davFile.Id.ToString())