Usenet streaming with a WebDAV server and a SABnzbd-compatible API
C#
Added accessed DavItem to HttpContext.
nzbdav-devaad043f33164
diff --git a/backend/WebDav/DatabaseStore.cs b/backend/WebDav/DatabaseStore.csindex ce8591b1..ad4bb4fa 100644--- a/backend/WebDav/DatabaseStore.cs+++ b/backend/WebDav/DatabaseStore.cs@@ -1,4 +1,5 @@-using NWebDav.Server.Stores;+using Microsoft.AspNetCore.Http;+using NWebDav.Server.Stores;using NzbWebDAV.Clients;using NzbWebDAV.Config;using NzbWebDAV.Database;@@ -8,6 +9,7 @@ using NzbWebDAV.Queue;namespace NzbWebDAV.WebDav;public class DatabaseStore(+ IHttpContextAccessor httpContextAccessor,DavDatabaseClient dbClient,ConfigManager configManager,UsenetStreamingClient usenetClient,@@ -16,6 +18,7 @@ public class DatabaseStore({private readonly DatabaseStoreCollection _root = new(DavItem.Root,+ httpContextAccessor.HttpContext!,dbClient,configManager,usenetClient,diff --git a/backend/WebDav/DatabaseStoreCollection.cs b/backend/WebDav/DatabaseStoreCollection.csindex e78997de..56b887dd 100644--- a/backend/WebDav/DatabaseStoreCollection.cs+++ b/backend/WebDav/DatabaseStoreCollection.cs@@ -1,4 +1,5 @@-using NWebDav.Server;+using Microsoft.AspNetCore.Http;+using NWebDav.Server;using NWebDav.Server.Stores;using NzbWebDAV.Clients;using NzbWebDAV.Config;@@ -13,6 +14,7 @@ namespace NzbWebDAV.WebDav;public class DatabaseStoreCollection(DavItem davDirectory,+ HttpContext httpContext,DavDatabaseClient dbClient,ConfigManager configManager,UsenetStreamingClient usenetClient,@@ -97,15 +99,15 @@ public class DatabaseStoreCollection(return davItem.Type switch{DavItem.ItemType.Directory when davItem.Id == DavItem.NzbFolder.Id =>- new DatabaseStoreWatchFolder(davItem, dbClient, configManager, usenetClient, queueManager),+ new DatabaseStoreWatchFolder(davItem, httpContext, dbClient, configManager, usenetClient, queueManager),DavItem.ItemType.Directory =>- new DatabaseStoreCollection(davItem, dbClient, configManager, usenetClient, queueManager),+ new DatabaseStoreCollection(davItem, httpContext, dbClient, configManager, usenetClient, queueManager),DavItem.ItemType.SymlinkRoot =>new DatabaseStoreSymlinkCollection(davItem, dbClient, configManager),DavItem.ItemType.NzbFile =>- new DatabaseStoreNzbFile(davItem, dbClient, usenetClient, configManager),+ new DatabaseStoreNzbFile(davItem, httpContext, dbClient, usenetClient, configManager),DavItem.ItemType.RarFile =>- new DatabaseStoreRarFile(davItem, dbClient, usenetClient, configManager),+ new DatabaseStoreRarFile(davItem, httpContext, dbClient, usenetClient, configManager),_ => throw new ArgumentException("Unrecognized directory child type.")};}diff --git a/backend/WebDav/DatabaseStoreNzbFile.cs b/backend/WebDav/DatabaseStoreNzbFile.csindex 08cc9731..f4fb5888 100644--- a/backend/WebDav/DatabaseStoreNzbFile.cs+++ b/backend/WebDav/DatabaseStoreNzbFile.cs@@ -1,4 +1,5 @@-using NWebDav.Server;+using Microsoft.AspNetCore.Http;+using NWebDav.Server;using NWebDav.Server.Stores;using NzbWebDAV.Clients;using NzbWebDAV.Config;@@ -12,6 +13,7 @@ namespace NzbWebDAV.WebDav;public class DatabaseStoreNzbFile(DavItem davNzbFile,+ HttpContext httpContext,DavDatabaseClient dbClient,UsenetStreamingClient usenetClient,ConfigManager configManager@@ -24,6 +26,10 @@ public class DatabaseStoreNzbFile(public override async Task<Stream> GetReadableStreamAsync(CancellationToken cancellationToken){+ // store the DavItem being accessed in the http context+ httpContext.Items["DavItem"] = davNzbFile;++ // return the streamvar id = davNzbFile.Id;var file = await dbClient.GetNzbFileAsync(id, cancellationToken);if (file is null) throw new FileNotFoundException($"Could not find nzb file with id: {id}");diff --git a/backend/WebDav/DatabaseStoreRarFile.cs b/backend/WebDav/DatabaseStoreRarFile.csindex ea3d136d..6409898f 100644--- a/backend/WebDav/DatabaseStoreRarFile.cs+++ b/backend/WebDav/DatabaseStoreRarFile.cs@@ -1,4 +1,5 @@-using Microsoft.EntityFrameworkCore;+using Microsoft.AspNetCore.Http;+using Microsoft.EntityFrameworkCore;using NWebDav.Server;using NWebDav.Server.Stores;using NzbWebDAV.Clients;@@ -14,6 +15,7 @@ namespace NzbWebDAV.WebDav;public class DatabaseStoreRarFile(DavItem davRarFile,+ HttpContext httpContext,DavDatabaseClient dbClient,UsenetStreamingClient usenetClient,ConfigManager configManager@@ -26,6 +28,10 @@ public class DatabaseStoreRarFile(public override async Task<Stream> GetReadableStreamAsync(CancellationToken ct){+ // store the DavItem being accessed in the http context+ httpContext.Items["DavItem"] = davRarFile;++ // return the streamvar id = davRarFile.Id;var rarFile = await dbClient.Ctx.RarFiles.Where(x => x.Id == id).FirstOrDefaultAsync(ct);if (rarFile is null) throw new FileNotFoundException($"Could not find nzb file with id: {id}");diff --git a/backend/WebDav/DatabaseStoreWatchFolder.cs b/backend/WebDav/DatabaseStoreWatchFolder.csindex 0f06fa84..b233cc7a 100644--- a/backend/WebDav/DatabaseStoreWatchFolder.cs+++ b/backend/WebDav/DatabaseStoreWatchFolder.cs@@ -1,4 +1,5 @@-using Microsoft.EntityFrameworkCore;+using Microsoft.AspNetCore.Http;+using Microsoft.EntityFrameworkCore;using NWebDav.Server;using NWebDav.Server.Stores;using NzbWebDAV.Api.SabControllers.AddFile;@@ -14,11 +15,12 @@ namespace NzbWebDAV.WebDav;public class DatabaseStoreWatchFolder(DavItem davDirectory,+ HttpContext httpContext,DavDatabaseClient dbClient,ConfigManager configManager,UsenetStreamingClient usenetClient,QueueManager queueManager-) : DatabaseStoreCollection(davDirectory, dbClient, configManager, usenetClient, queueManager)+) : DatabaseStoreCollection(davDirectory, httpContext, dbClient, configManager, usenetClient, queueManager){protected override async Task<IStoreItem?> GetItemAsync(GetItemRequest request){