Usenet streaming with a WebDAV server and a SABnzbd-compatible API
C#
Added `DownloadDirId` column to `HistoryItems` table.
nzbdav-dev998adf916c76
diff --git a/backend/Database/DavDatabaseContext.cs b/backend/Database/DavDatabaseContext.csindex bbc9a626..8bea79fb 100644--- a/backend/Database/DavDatabaseContext.cs+++ b/backend/Database/DavDatabaseContext.cs@@ -227,6 +227,9 @@ public sealed class DavDatabaseContext() : DbContext(Options.Value)e.Property(i => i.FailMessage).IsRequired(false);+ e.Property(i => i.DownloadDirId)+ .IsRequired(false);+e.HasIndex(i => new { i.CreatedAt }).IsUnique(false);@@ -235,6 +238,9 @@ public sealed class DavDatabaseContext() : DbContext(Options.Value)e.HasIndex(i => new { i.Category, i.CreatedAt }).IsUnique(false);++ e.HasIndex(i => new { i.Category, i.DownloadDirId })+ .IsUnique(false);});// ConfigItemdiff --git a/backend/Database/Migrations/20250818232259_Add-DownloadDirId-To-HistoryItem.Designer.cs b/backend/Database/Migrations/20250818232259_Add-DownloadDirId-To-HistoryItem.Designer.csnew file mode 100644index 00000000..50a847a1--- /dev/null+++ b/backend/Database/Migrations/20250818232259_Add-DownloadDirId-To-HistoryItem.Designer.cs@@ -0,0 +1,261 @@+// <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("20250818232259_Add-DownloadDirId-To-HistoryItem")]+ partial class AddDownloadDirIdToHistoryItem+ {+ /// <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<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/20250818232259_Add-DownloadDirId-To-HistoryItem.cs b/backend/Database/Migrations/20250818232259_Add-DownloadDirId-To-HistoryItem.csnew file mode 100644index 00000000..e6a725f1--- /dev/null+++ b/backend/Database/Migrations/20250818232259_Add-DownloadDirId-To-HistoryItem.cs@@ -0,0 +1,56 @@+using System;+using Microsoft.EntityFrameworkCore.Migrations;++#nullable disable++namespace NzbWebDAV.Database.Migrations+{+ /// <inheritdoc />+ public partial class AddDownloadDirIdToHistoryItem : Migration+ {+ /// <inheritdoc />+ protected override void Up(MigrationBuilder migrationBuilder)+ {+ migrationBuilder.AddColumn<Guid>(+ name: "DownloadDirId",+ table: "HistoryItems",+ type: "TEXT",+ nullable: true);++ migrationBuilder.CreateIndex(+ name: "IX_HistoryItems_Category_DownloadDirId",+ table: "HistoryItems",+ columns: new[] { "Category", "DownloadDirId" });++ // Populate DownloadDirId for completed history items based on+ // DavItems hierarchy under /content/{Category}/{JobName}+ migrationBuilder.Sql(+ """+ UPDATE HistoryItems+ SET DownloadDirId = (+ SELECT child.Id+ FROM DavItems AS child+ JOIN DavItems AS parent ON child.ParentId = parent.Id+ WHERE parent.ParentId = '00000000-0000-0000-0000-000000000002' -- DavItem.ContentFolder.Id+ AND parent.Name = HistoryItems.Category+ AND child.Name = HistoryItems.JobName+ AND child.Type = 1 -- ItemType.Directory+ )+ WHERE DownloadStatus = 1; -- HistoryItem.DownloadStatusOption.Completed+ """+ );+ }++ /// <inheritdoc />+ protected override void Down(MigrationBuilder migrationBuilder)+ {+ migrationBuilder.DropIndex(+ name: "IX_HistoryItems_Category_DownloadDirId",+ table: "HistoryItems");++ migrationBuilder.DropColumn(+ name: "DownloadDirId",+ table: "HistoryItems");+ }+ }+}diff --git a/backend/Database/Migrations/DavDatabaseContextModelSnapshot.cs b/backend/Database/Migrations/DavDatabaseContextModelSnapshot.csindex 56ee5ccd..8c5e3ec5 100644--- a/backend/Database/Migrations/DavDatabaseContextModelSnapshot.cs+++ b/backend/Database/Migrations/DavDatabaseContextModelSnapshot.cs@@ -123,6 +123,9 @@ namespace NzbWebDAV.Database.Migrationsb.Property<DateTime>("CreatedAt").HasColumnType("TEXT");+ b.Property<Guid?>("DownloadDirId")+ .HasColumnType("TEXT");+b.Property<int>("DownloadStatus").HasColumnType("INTEGER");@@ -151,6 +154,8 @@ namespace NzbWebDAV.Database.Migrationsb.HasIndex("Category", "CreatedAt");+ b.HasIndex("Category", "DownloadDirId");+b.ToTable("HistoryItems", (string)null);});diff --git a/backend/Database/Models/HistoryItem.cs b/backend/Database/Models/HistoryItem.csindex 3fb97ac0..defa01ae 100644--- a/backend/Database/Models/HistoryItem.cs+++ b/backend/Database/Models/HistoryItem.cs@@ -11,6 +11,7 @@ public class HistoryItempublic long TotalSegmentBytes { get; set; }public int DownloadTimeSeconds { get; set; }public string? FailMessage { get; set; }+ public Guid? DownloadDirId { get; set; }public enum DownloadStatusOption{diff --git a/backend/Queue/QueueItemProcessor.cs b/backend/Queue/QueueItemProcessor.csindex f7ee1461..67ead352 100644--- a/backend/Queue/QueueItemProcessor.cs+++ b/backend/Queue/QueueItemProcessor.cs@@ -1,4 +1,4 @@-using System.Text;+using System.Text;using Microsoft.EntityFrameworkCore;using NzbWebDAV.Clients;using NzbWebDAV.Config;@@ -73,11 +73,14 @@ public class QueueItemProcessor(private async Task ProcessQueueItemAsync(DateTime startTime){- // This NZB is already processed and mounted- if (await IsAlreadyDownloaded())+ // if the mount folder already exists,+ // then we've already downloaded this nzb and we can mark the job as completed.+ var existingMountFolder = await GetMountFolder();+ var isAlreadyDownloaded = existingMountFolder is not null;+ if (isAlreadyDownloaded){Log.Information($"Nzb `{queueItem.JobName}` is a duplicate. Skipping and marking complete.");- await MarkQueueItemCompleted(startTime);+ await MarkQueueItemCompleted(startTime, error: null, () => existingMountFolder);return;}@@ -118,6 +121,8 @@ public class QueueItemProcessor(// validate video files foundif (configManager.IsEnsureImportableVideoEnabled())new EnsureImportableVideoValidator(dbClient).ThrowIfValidationFails();++ return mountFolder;});}@@ -128,17 +133,17 @@ public class QueueItemProcessor(: null;}- private async Task<bool> IsAlreadyDownloaded()+ private async Task<DavItem?> GetMountFolder(){var query = from mountFolder in dbClient.Ctx.Itemsjoin categoryFolder in dbClient.Ctx.Items on mountFolder.ParentId equals categoryFolder.Idwhere mountFolder.Name == queueItem.JobName- && mountFolder.ParentId != null- && categoryFolder.Name == queueItem.Category- && categoryFolder.ParentId == DavItem.ContentFolder.Id+ && mountFolder.ParentId != null+ && categoryFolder.Name == queueItem.Category+ && categoryFolder.ParentId == DavItem.ContentFolder.Idselect mountFolder;- return await query.AnyAsync();+ return await query.FirstOrDefaultAsync(ct);}private DavItem GetOrCreateCategoryFolder()@@ -176,7 +181,7 @@ public class QueueItemProcessor(return mountFolder;}- private HistoryItem CreateHistoryItem(DateTime jobStartTime, string? errorMessage = null)+ private HistoryItem CreateHistoryItem(DavItem? mountFolder, DateTime jobStartTime, string? errorMessage = null){return new HistoryItem(){@@ -190,7 +195,8 @@ public class QueueItemProcessor(: HistoryItem.DownloadStatusOption.Failed,TotalSegmentBytes = queueItem.TotalSegmentBytes,DownloadTimeSeconds = (int)(DateTime.Now - jobStartTime).TotalSeconds,- FailMessage = errorMessage+ FailMessage = errorMessage,+ DownloadDirId = mountFolder?.Id,};}@@ -198,13 +204,13 @@ public class QueueItemProcessor((DateTime startTime,string? error = null,- Action? databaseOperations = null+ Func<DavItem?>? databaseOperations = null){dbClient.Ctx.ChangeTracker.Clear();- databaseOperations?.Invoke();+ var mountFolder = databaseOperations?.Invoke();dbClient.Ctx.QueueItems.Remove(queueItem);- dbClient.Ctx.HistoryItems.Add(CreateHistoryItem(startTime, error));+ dbClient.Ctx.HistoryItems.Add(CreateHistoryItem(mountFolder, startTime, error));await dbClient.Ctx.SaveChangesAsync(ct);}}\ No newline at end of file