Skip to content

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

C#

Switched to reading queue nzb stream from blob-store.

nzbdav-devf617da785047
diff --git a/backend/Api/SabControllers/AddFile/AddFileController.cs b/backend/Api/SabControllers/AddFile/AddFileController.cs
index bbcdf7d3..16f181df 100644
--- a/backend/Api/SabControllers/AddFile/AddFileController.cs
+++ b/backend/Api/SabControllers/AddFile/AddFileController.cs
@@ -57,18 +57,8 @@ public class AddFileController(
PauseUntil = request.PauseUntil
};
- // create the nzb-content record
- nzbFileStream.Seek(0, SeekOrigin.Begin);
- using var streamReader = new StreamReader(nzbFileStream);
- var queueNzbContents = new QueueNzbContents()
- {
- Id = queueItem.Id,
- NzbContents = await streamReader.ReadToEndAsync(),
- };
-
// save
dbClient.Ctx.QueueItems.Add(queueItem);
- dbClient.Ctx.QueueNzbContents.Add(queueNzbContents);
await dbClient.Ctx.SaveChangesAsync(request.CancellationToken).ConfigureAwait(false);
}
catch
diff --git a/backend/Database/DavDatabaseClient.cs b/backend/Database/DavDatabaseClient.cs
index a3a15f3a..94f26827 100644
--- a/backend/Database/DavDatabaseClient.cs
+++ b/backend/Database/DavDatabaseClient.cs
@@ -1,3 +1,4 @@
+using System.Text;
using Microsoft.EntityFrameworkCore;
using NzbWebDAV.Database.Models;
@@ -76,11 +77,12 @@ public sealed class DavDatabaseClient(DavDatabaseContext ctx)
}
// queue
- public async Task<(QueueItem? queueItem, QueueNzbContents? queueNzbContents)> GetTopQueueItem
+ public async Task<(QueueItem? queueItem, Stream? queueNzbStream)> GetTopQueueItem
(
CancellationToken ct = default
)
{
+ // read queue item from database
var nowTime = DateTime.Now;
var queueItem = await Ctx.QueueItems
.OrderByDescending(q => q.Priority)
@@ -89,10 +91,26 @@ public sealed class DavDatabaseClient(DavDatabaseContext ctx)
.Skip(0)
.Take(1)
.FirstOrDefaultAsync(ct).ConfigureAwait(false);
- var queueNzbContents = queueItem != null
- ? await Ctx.QueueNzbContents.FirstOrDefaultAsync(q => q.Id == queueItem.Id, ct).ConfigureAwait(false)
+
+ // attempt to read nzb contents from blob-store.
+ var queueNzbStream = queueItem != null
+ ? BlobStore.ReadBlob(queueItem.Id)
: null;
- return (queueItem, queueNzbContents);
+
+ // otherwise, read nzb contents from database.
+ if (queueItem != null && queueNzbStream == null)
+ {
+ var queueNzbContents = await Ctx.QueueNzbContents
+ .FirstOrDefaultAsync(q => q.Id == queueItem.Id, ct)
+ .ConfigureAwait(false);
+
+ queueNzbStream = queueNzbContents != null
+ ? new MemoryStream(Encoding.UTF8.GetBytes(queueNzbContents.NzbContents))
+ : null;
+ }
+
+ // return
+ return (queueItem, queueNzbStream);
}
public Task<QueueItem[]> GetQueueItems
diff --git a/backend/Queue/QueueItemProcessor.cs b/backend/Queue/QueueItemProcessor.cs
index a7648507..e17ad192 100644
--- a/backend/Queue/QueueItemProcessor.cs
+++ b/backend/Queue/QueueItemProcessor.cs
@@ -23,7 +23,7 @@ namespace NzbWebDAV.Queue;
public class QueueItemProcessor(
QueueItem queueItem,
- QueueNzbContents queueNzbContents,
+ Stream queueNzbStream,
DavDatabaseClient dbClient,
INntpClient usenetClient,
ConfigManager configManager,
@@ -107,9 +107,7 @@ public class QueueItemProcessor(
}
// read the nzb document
- var documentBytes = Encoding.UTF8.GetBytes(queueNzbContents.NzbContents);
- using var stream = new MemoryStream(documentBytes);
- var nzb = await NzbDocument.LoadAsync(stream).ConfigureAwait(false);
+ var nzb = await NzbDocument.LoadAsync(queueNzbStream).ConfigureAwait(false);
var nzbFiles = nzb.Files.Where(x => x.Segments.Count > 0).ToList();
// Look for a password in filename and nzb document
diff --git a/backend/Queue/QueueManager.cs b/backend/Queue/QueueManager.cs
index fafec06c..acd0da06 100644
--- a/backend/Queue/QueueManager.cs
+++ b/backend/Queue/QueueManager.cs
@@ -81,7 +81,7 @@ public class QueueManager : IDisposable
await using var dbContext = new DavDatabaseContext();
var dbClient = new DavDatabaseClient(dbContext);
var topItem = await LockAsync(() => dbClient.GetTopQueueItem(ct)).ConfigureAwait(false);
- if (topItem.queueItem is null || topItem.queueNzbContents is null)
+ if (topItem.queueItem is null || topItem.queueNzbStream is null)
{
try
{
@@ -106,12 +106,13 @@ public class QueueManager : IDisposable
using var cachingUsenetClient = new ArticleCachingNntpClient(_usenetClient);
// process the queue-item
+ await using var queueNzbStream = topItem.queueNzbStream;
using var queueItemCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(ct);
await LockAsync(() =>
{
// ReSharper disable twice AccessToDisposedClosure
_inProgressQueueItem = BeginProcessingQueueItem(dbClient, cachingUsenetClient,
- topItem.queueItem, topItem.queueNzbContents, queueItemCancellationTokenSource);
+ topItem.queueItem, queueNzbStream, queueItemCancellationTokenSource);
}).ConfigureAwait(false);
await (_inProgressQueueItem?.ProcessingTask ?? Task.CompletedTask).ConfigureAwait(false);
}
@@ -131,13 +132,13 @@ public class QueueManager : IDisposable
DavDatabaseClient dbClient,
INntpClient usenetClient,
QueueItem queueItem,
- QueueNzbContents queueNzbContents,
+ Stream queueNzbStream,
CancellationTokenSource cts
)
{
var progressHook = new Progress<int>();
var task = new QueueItemProcessor(
- queueItem, queueNzbContents, dbClient, usenetClient,
+ queueItem, queueNzbStream, dbClient, usenetClient,
_configManager, _websocketManager, progressHook, cts.Token
).ProcessAsync();
var inProgressQueueItem = new InProgressQueueItem()