Usenet streaming with a WebDAV server and a SABnzbd-compatible API
C#
Update HistoryCleanupService to deleted mounted files when needed.
nzbdav-dev212653e79c8b
diff --git a/backend/Database/DavDatabaseClient.cs b/backend/Database/DavDatabaseClient.csindex 91494c4c..7a5e3574 100644--- a/backend/Database/DavDatabaseClient.cs+++ b/backend/Database/DavDatabaseClient.cs@@ -202,10 +202,20 @@ public sealed class DavDatabaseClient(DavDatabaseContext ctx)foreach (var historyItem in historyItems.Where(h => h.DownloadDirId is not null))Ctx.Items.Remove(new DavItem() { Id = historyItem.DownloadDirId!.Value });Ctx.HistoryItems.RemoveRange(historyItems);+ Ctx.HistoryCleanupItems.AddRange(historyItems.Select(x => new HistoryCleanupItem+ {+ Id = x.Id,+ DeleteMountedFiles = deleteFiles+ }));return;}Ctx.HistoryItems.RemoveRange(ids.Select(id => new HistoryItem() { Id = id }));+ Ctx.HistoryCleanupItems.AddRange(ids.Select(x => new HistoryCleanupItem+ {+ Id = x,+ DeleteMountedFiles = deleteFiles+ }));}private class FileSizeResultdiff --git a/backend/Database/DavDatabaseContext.cs b/backend/Database/DavDatabaseContext.csindex 345c213c..cdfb6485 100644--- a/backend/Database/DavDatabaseContext.cs+++ b/backend/Database/DavDatabaseContext.cs@@ -131,11 +131,11 @@ public sealed class DavDatabaseContext() : DbContext(Options.Value).IsUnique();e.HasIndex(i => new { i.IdPrefix, i.Type });-+e.HasIndex(i => new { i.Type, i.HistoryItemId, i.NextHealthCheck, i.ReleaseDate, i.Id });-+e.HasIndex(i => new { i.HistoryItemId, i.Type, i.CreatedAt });-+e.HasIndex(i => new { i.HistoryItemId, i.SubType, i.CreatedAt });});@@ -447,6 +447,9 @@ public sealed class DavDatabaseContext() : DbContext(Options.Value)e.Property(i => i.Id).ValueGeneratedNever();++ e.Property(i => i.DeleteMountedFiles)+ .IsRequired();});// DavCleanupItemdiff --git a/backend/Database/Migrations/20260203205014_Add-DeleteMountedFiles-To-HistoryCleanupItems-Table.Designer.cs b/backend/Database/Migrations/20260203205014_Add-DeleteMountedFiles-To-HistoryCleanupItems-Table.Designer.csnew file mode 100644index 00000000..4fad726e--- /dev/null+++ b/backend/Database/Migrations/20260203205014_Add-DeleteMountedFiles-To-HistoryCleanupItems-Table.Designer.cs@@ -0,0 +1,417 @@+// <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("20260203205014_Add-DeleteMountedFiles-To-HistoryCleanupItems-Table")]+ partial class AddDeleteMountedFilesToHistoryCleanupItemsTable+ {+ /// <inheritdoc />+ protected override void BuildTargetModel(ModelBuilder modelBuilder)+ {+#pragma warning disable 612, 618+ modelBuilder.HasAnnotation("ProductVersion", "10.0.1");++ 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.BlobCleanupItem", b =>+ {+ b.Property<Guid>("Id")+ .HasColumnType("TEXT");++ b.HasKey("Id");++ b.ToTable("BlobCleanupItems", (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.DavCleanupItem", b =>+ {+ b.Property<Guid>("Id")+ .HasColumnType("TEXT");++ b.HasKey("Id");++ b.ToTable("DavCleanupItems", (string)null);+ });++ modelBuilder.Entity("NzbWebDAV.Database.Models.DavItem", b =>+ {+ b.Property<Guid>("Id")+ .HasColumnType("TEXT");++ b.Property<DateTime>("CreatedAt")+ .HasColumnType("TEXT");++ b.Property<Guid?>("FileBlobId")+ .HasColumnType("TEXT");++ b.Property<long?>("FileSize")+ .HasColumnType("INTEGER");++ b.Property<Guid?>("HistoryItemId")+ .HasColumnType("TEXT");++ b.Property<string>("IdPrefix")+ .IsRequired()+ .HasColumnType("TEXT");++ b.Property<long?>("LastHealthCheck")+ .HasColumnType("INTEGER");++ b.Property<string>("Name")+ .IsRequired()+ .HasMaxLength(255)+ .HasColumnType("TEXT");++ b.Property<long?>("NextHealthCheck")+ .HasColumnType("INTEGER");++ b.Property<Guid?>("ParentId")+ .HasColumnType("TEXT");++ b.Property<string>("Path")+ .IsRequired()+ .HasColumnType("TEXT");++ b.Property<long?>("ReleaseDate")+ .HasColumnType("INTEGER");++ b.Property<int>("SubType")+ .HasColumnType("INTEGER");++ b.Property<int>("Type")+ .HasColumnType("INTEGER");++ b.HasKey("Id");++ b.HasIndex("IdPrefix", "Type");++ b.HasIndex("ParentId", "Name")+ .IsUnique();++ b.HasIndex("HistoryItemId", "SubType", "CreatedAt");++ b.HasIndex("HistoryItemId", "Type", "CreatedAt");++ b.HasIndex("Type", "HistoryItemId", "NextHealthCheck", "ReleaseDate", "Id");++ b.ToTable("DavItems", (string)null);+ });++ modelBuilder.Entity("NzbWebDAV.Database.Models.DavMultipartFile", b =>+ {+ b.Property<Guid>("Id")+ .HasColumnType("TEXT");++ b.Property<string>("Metadata")+ .IsRequired()+ .HasColumnType("TEXT");++ b.HasKey("Id");++ b.ToTable("DavMultipartFiles", (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.HealthCheckResult", b =>+ {+ b.Property<Guid>("Id")+ .HasColumnType("TEXT");++ b.Property<long>("CreatedAt")+ .HasColumnType("INTEGER");++ b.Property<Guid>("DavItemId")+ .HasColumnType("TEXT");++ b.Property<string>("Message")+ .HasColumnType("TEXT");++ b.Property<string>("Path")+ .IsRequired()+ .HasColumnType("TEXT");++ b.Property<int>("RepairStatus")+ .HasColumnType("INTEGER");++ b.Property<int>("Result")+ .HasColumnType("INTEGER");++ b.HasKey("Id");++ b.HasIndex("CreatedAt");++ b.HasIndex("DavItemId")+ .HasFilter("\"RepairStatus\" = 3");++ b.HasIndex("Result", "RepairStatus", "CreatedAt");++ b.ToTable("HealthCheckResults", (string)null);+ });++ modelBuilder.Entity("NzbWebDAV.Database.Models.HealthCheckStat", b =>+ {+ b.Property<long>("DateStartInclusive")+ .HasColumnType("INTEGER");++ b.Property<long>("DateEndExclusive")+ .HasColumnType("INTEGER");++ b.Property<int>("Result")+ .HasColumnType("INTEGER");++ b.Property<int>("RepairStatus")+ .HasColumnType("INTEGER");++ b.Property<int>("Count")+ .HasColumnType("INTEGER");++ b.HasKey("DateStartInclusive", "DateEndExclusive", "Result", "RepairStatus");++ b.ToTable("HealthCheckStats", (string)null);+ });++ modelBuilder.Entity("NzbWebDAV.Database.Models.HistoryCleanupItem", b =>+ {+ b.Property<Guid>("Id")+ .HasColumnType("TEXT");++ b.Property<bool>("DeleteMountedFiles")+ .HasColumnType("INTEGER");++ b.HasKey("Id");++ b.ToTable("HistoryCleanupItems", (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<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.QueueNzbContents", b =>+ {+ b.Property<Guid>("Id")+ .HasColumnType("TEXT");++ b.Property<string>("NzbContents")+ .IsRequired()+ .HasColumnType("TEXT");++ b.HasKey("Id");++ b.ToTable("QueueNzbContents", (string)null);+ });++ modelBuilder.Entity("NzbWebDAV.Database.Models.DavMultipartFile", b =>+ {+ b.HasOne("NzbWebDAV.Database.Models.DavItem", "DavItem")+ .WithOne()+ .HasForeignKey("NzbWebDAV.Database.Models.DavMultipartFile", "Id")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();++ b.Navigation("DavItem");+ });++ 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.QueueNzbContents", b =>+ {+ b.HasOne("NzbWebDAV.Database.Models.QueueItem", "QueueItem")+ .WithOne()+ .HasForeignKey("NzbWebDAV.Database.Models.QueueNzbContents", "Id")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();++ b.Navigation("QueueItem");+ });+#pragma warning restore 612, 618+ }+ }+}diff --git a/backend/Database/Migrations/20260203205014_Add-DeleteMountedFiles-To-HistoryCleanupItems-Table.cs b/backend/Database/Migrations/20260203205014_Add-DeleteMountedFiles-To-HistoryCleanupItems-Table.csnew file mode 100644index 00000000..d647ed65--- /dev/null+++ b/backend/Database/Migrations/20260203205014_Add-DeleteMountedFiles-To-HistoryCleanupItems-Table.cs@@ -0,0 +1,42 @@+using Microsoft.EntityFrameworkCore.Migrations;++#nullable disable++namespace NzbWebDAV.Database.Migrations+{+ /// <inheritdoc />+ public partial class AddDeleteMountedFilesToHistoryCleanupItemsTable : Migration+ {+ /// <inheritdoc />+ protected override void Up(MigrationBuilder migrationBuilder)+ {+ migrationBuilder.Sql("DROP TRIGGER IF EXISTS TR_HistoryItems_Delete_AddHistoryCleanup;");++ migrationBuilder.AddColumn<bool>(+ name: "DeleteMountedFiles",+ table: "HistoryCleanupItems",+ type: "INTEGER",+ nullable: false,+ defaultValue: false);+ }++ /// <inheritdoc />+ protected override void Down(MigrationBuilder migrationBuilder)+ {+ migrationBuilder.DropColumn(+ name: "DeleteMountedFiles",+ table: "HistoryCleanupItems");++ migrationBuilder.Sql(+ """+ CREATE TRIGGER TR_HistoryItems_Delete_AddHistoryCleanup+ AFTER DELETE ON HistoryItems+ BEGIN+ INSERT INTO HistoryCleanupItems (Id)+ VALUES (OLD.Id);+ END+ """+ );+ }+ }+}diff --git a/backend/Database/Migrations/DavDatabaseContextModelSnapshot.cs b/backend/Database/Migrations/DavDatabaseContextModelSnapshot.csindex 516ff24a..92de4181 100644--- a/backend/Database/Migrations/DavDatabaseContextModelSnapshot.cs+++ b/backend/Database/Migrations/DavDatabaseContextModelSnapshot.cs@@ -242,6 +242,9 @@ namespace NzbWebDAV.Database.Migrationsb.Property<Guid>("Id").HasColumnType("TEXT");+ b.Property<bool>("DeleteMountedFiles")+ .HasColumnType("INTEGER");+b.HasKey("Id");b.ToTable("HistoryCleanupItems", (string)null);diff --git a/backend/Database/Models/HistoryCleanupItem.cs b/backend/Database/Models/HistoryCleanupItem.csindex 2f449b6b..a2e58706 100644--- a/backend/Database/Models/HistoryCleanupItem.cs+++ b/backend/Database/Models/HistoryCleanupItem.cs@@ -3,4 +3,5 @@ namespace NzbWebDAV.Database.Models;public class HistoryCleanupItem{public Guid Id { get; set; }+ public bool DeleteMountedFiles { get; set; }}diff --git a/backend/Services/HistoryCleanupService.cs b/backend/Services/HistoryCleanupService.csindex d3d506cd..754a629f 100644--- a/backend/Services/HistoryCleanupService.cs+++ b/backend/Services/HistoryCleanupService.cs@@ -27,13 +27,23 @@ public class HistoryCleanupService : BackgroundServicecontinue;}- // Mark the corresponding dav-items as no longer in History- await dbContext.Items- .Where(x => x.HistoryItemId == cleanupItem.Id)- .ExecuteUpdateAsync(- x => x.SetProperty(p => p.HistoryItemId, (Guid?)null),- stoppingToken- );+ if (cleanupItem.DeleteMountedFiles)+ {+ // Delete the corresponding dav-items+ await dbContext.Items+ .Where(x => x.HistoryItemId == cleanupItem.Id)+ .ExecuteDeleteAsync(stoppingToken);+ }+ else+ {+ // Mark the corresponding dav-items as no longer in History+ await dbContext.Items+ .Where(x => x.HistoryItemId == cleanupItem.Id)+ .ExecuteUpdateAsync(+ x => x.SetProperty(p => p.HistoryItemId, (Guid?)null),+ stoppingToken+ );+ }// Remove the cleanup item from the databasedbContext.HistoryCleanupItems.Remove(cleanupItem);