Skip to content

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

C#

Added custom nzb parser and removed Usenet dependency.

nzbdav-dev1b6e719b2580
diff --git a/backend/Clients/Usenet/INntpClient.cs b/backend/Clients/Usenet/INntpClient.cs
index 191b6bde..ece2cdf9 100644
--- a/backend/Clients/Usenet/INntpClient.cs
+++ b/backend/Clients/Usenet/INntpClient.cs
@@ -1,6 +1,6 @@
using NzbWebDAV.Clients.Usenet.Models;
+using NzbWebDAV.Models.Nzb;
using NzbWebDAV.Streams;
-using Usenet.Nzb;
using UsenetSharp.Models;
namespace NzbWebDAV.Clients.Usenet;
diff --git a/backend/Clients/Usenet/NntpClient.cs b/backend/Clients/Usenet/NntpClient.cs
index 2ac0aa1d..356ea284 100644
--- a/backend/Clients/Usenet/NntpClient.cs
+++ b/backend/Clients/Usenet/NntpClient.cs
@@ -1,9 +1,8 @@
-using NzbWebDAV.Clients.Usenet.Contexts;
using NzbWebDAV.Clients.Usenet.Models;
using NzbWebDAV.Exceptions;
using NzbWebDAV.Extensions;
+using NzbWebDAV.Models.Nzb;
using NzbWebDAV.Streams;
-using Usenet.Nzb;
using UsenetSharp.Models;
namespace NzbWebDAV.Clients.Usenet;
@@ -85,7 +84,7 @@ public abstract class NntpClient : INntpClient
public virtual async Task<long> GetFileSizeAsync(NzbFile file, CancellationToken ct)
{
if (file.Segments.Count == 0) return 0;
- var headers = await GetYencHeadersAsync(file.Segments[^1].MessageId.Value, ct).ConfigureAwait(false);
+ var headers = await GetYencHeadersAsync(file.Segments[^1].MessageId, ct).ConfigureAwait(false);
return headers!.PartOffset + headers!.PartSize;
}
diff --git a/backend/Extensions/ExceptionExtensions.cs b/backend/Extensions/ExceptionExtensions.cs
index adbccd2f..151672b4 100644
--- a/backend/Extensions/ExceptionExtensions.cs
+++ b/backend/Extensions/ExceptionExtensions.cs
@@ -12,8 +12,7 @@ public static class ExceptionExtensions
public static bool IsNonRetryableDownloadException(this Exception exception)
{
return exception is NonRetryableDownloadException
- or SharpCompress.Common.InvalidFormatException
- or Usenet.Exceptions.InvalidYencDataException;
+ or SharpCompress.Common.InvalidFormatException;
}
public static bool IsCancellationException(this Exception exception)
diff --git a/backend/Extensions/NzbDocumentExtensions.cs b/backend/Extensions/NzbDocumentExtensions.cs
deleted file mode 100644
index 2cb5c845..00000000
--- a/backend/Extensions/NzbDocumentExtensions.cs
+++ /dev/null
@@ -1,18 +0,0 @@
-using Usenet.Nzb;
-
-namespace NzbWebDAV.Extensions;
-
-public static class NzbDocumentExtensions
-{
- public static string? GetMetadataName(this NzbDocument nzbDocument)
- {
- return nzbDocument.MetaData.TryGetValue("name", out var name)
- ? name.FirstOrDefault()
- : null;
- }
-
- public static string GetShortCode(this NzbDocument nzbDocument)
- {
- return nzbDocument.GetHashCode().ToString("x8");
- }
-}
\ No newline at end of file
diff --git a/backend/Extensions/NzbFileExtensions.cs b/backend/Extensions/NzbFileExtensions.cs
deleted file mode 100644
index f290be8d..00000000
--- a/backend/Extensions/NzbFileExtensions.cs
+++ /dev/null
@@ -1,60 +0,0 @@
-using System.Text.RegularExpressions;
-using Usenet.Nzb;
-
-namespace NzbWebDAV.Extensions;
-
-public static class NzbFileExtensions
-{
- public static string[] GetSegmentIds(this NzbFile file)
- {
- return file.Segments
- .Select(x => x.MessageId.Value)
- .ToArray();
- }
-
- public static string[] GetOrderedSegmentIds(this NzbFile file)
- {
- return file.Segments
- .OrderBy(x => x.Number)
- .Select(x => x.MessageId.Value)
- .ToArray();
- }
-
- public static long GetTotalYencodedSize(this NzbFile file)
- {
- return file.Size;
- }
-
- public static string GetSubjectFileName(this NzbFile file)
- {
- return GetFirstValidNonEmptyFilename(
- () => TryParseSubjectFilename1(file),
- () => TryParseSubjectFilename2(file)
- );
- }
-
- private static string TryParseSubjectFilename1(this NzbFile file)
- {
- // The most common format is when filename appears in double quotes
- // example: `[1/8] - "file.mkv" yEnc 12345 (1/54321)`
- var match = Regex.Match(file.Subject, "\\\"(.*)\\\"");
- return match.Success ? match.Groups[1].Value : "";
- }
-
- private static string TryParseSubjectFilename2(this NzbFile file)
- {
- // Otherwise, use sabnzbd's regex
- // https://github.com/sabnzbd/sabnzbd/blob/b6b0d10367fd4960bad73edd1d3812cafa7fc002/sabnzbd/nzbstuff.py#L106
- var match = Regex.Match(file.Subject,
- @"\b([\w\-+()' .,]+(?:\[[\w\-\/+()' .,]*][\w\-+()' .,]*)*\.[A-Za-z0-9]{2,4})\b");
- return match.Success ? match.Groups[1].Value : "";
- }
-
- private static string GetFirstValidNonEmptyFilename(params Func<string>[] funcs)
- {
- return funcs
- .Select(x => x.Invoke())
- .Where(x => x == Path.GetFileName(x))
- .FirstOrDefault(x => x != "") ?? "";
- }
-}
\ No newline at end of file
diff --git a/backend/Models/MultipartFile.cs b/backend/Models/MultipartFile.cs
index 7e689552..12908d37 100644
--- a/backend/Models/MultipartFile.cs
+++ b/backend/Models/MultipartFile.cs
@@ -1,4 +1,4 @@
-using Usenet.Nzb;
+using NzbWebDAV.Models.Nzb;
namespace NzbWebDAV.Models;
diff --git a/backend/Models/Nzb/NzbDocument.cs b/backend/Models/Nzb/NzbDocument.cs
new file mode 100644
index 00000000..134efe5c
--- /dev/null
+++ b/backend/Models/Nzb/NzbDocument.cs
@@ -0,0 +1,119 @@
+using System.Xml;
+
+namespace NzbWebDAV.Models.Nzb;
+
+public class NzbDocument
+{
+ private static readonly XmlReaderSettings XmlSettings = new()
+ {
+ Async = true,
+ DtdProcessing = DtdProcessing.Ignore
+ };
+
+ public Dictionary<string, string> Metadata { get; } = new();
+
+ public List<NzbFile> Files { get; } = [];
+
+ public static async Task<NzbDocument> LoadAsync(Stream stream)
+ {
+ var document = new NzbDocument();
+ using var reader = XmlReader.Create(stream, XmlSettings);
+
+ while (await reader.ReadAsync().ConfigureAwait(false))
+ {
+ if (reader.NodeType != XmlNodeType.Element) continue;
+ switch (reader.Name)
+ {
+ case "head":
+ await ReadHeadAsync(reader, document.Metadata).ConfigureAwait(false);
+ break;
+ case "file":
+ var file = await ReadFileAsync(reader).ConfigureAwait(false);
+ document.Files.Add(file);
+ break;
+ }
+ }
+
+ return document;
+ }
+
+ private static async Task ReadHeadAsync(XmlReader reader, Dictionary<string, string> metadata)
+ {
+ if (reader.IsEmptyElement)
+ return;
+
+ while (true)
+ {
+ if (reader is { NodeType: XmlNodeType.EndElement, Name: "head" })
+ break;
+
+ if (reader is { NodeType: XmlNodeType.Element, Name: "meta" })
+ {
+ var type = reader.GetAttribute("type") ?? string.Empty;
+ var value = await reader.ReadElementContentAsStringAsync().ConfigureAwait(false);
+ metadata.Add(type, value);
+
+ // ReadElementContentAsStringAsync advances the reader - continue to check current position
+ continue;
+ }
+
+ // Only read if we haven't processed an element that advanced us
+ if (!await reader.ReadAsync().ConfigureAwait(false))
+ break;
+ }
+ }
+
+ private static async Task<NzbFile> ReadFileAsync(XmlReader reader)
+ {
+ var file = new NzbFile
+ {
+ Subject = reader.GetAttribute("subject") ?? string.Empty
+ };
+
+ if (reader.IsEmptyElement)
+ return file;
+
+ while (await reader.ReadAsync().ConfigureAwait(false))
+ {
+ if (reader is { NodeType: XmlNodeType.EndElement, Name: "file" })
+ break;
+
+ if (reader is { NodeType: XmlNodeType.Element, Name: "segments" })
+ {
+ await ReadSegmentsAsync(reader, file).ConfigureAwait(false);
+ }
+ }
+
+ return file;
+ }
+
+ private static async Task ReadSegmentsAsync(XmlReader reader, NzbFile file)
+ {
+ if (reader.IsEmptyElement)
+ return;
+
+ while (true)
+ {
+ if (reader is { NodeType: XmlNodeType.EndElement, Name: "segments" })
+ break;
+
+ if (reader is { NodeType: XmlNodeType.Element, Name: "segment" })
+ {
+ var bytesAttr = reader.GetAttribute("bytes");
+ var segment = new NzbSegment
+ {
+ Bytes = long.TryParse(bytesAttr, out var bytes) ? bytes : 0,
+ MessageId = await reader.ReadElementContentAsStringAsync().ConfigureAwait(false)
+ };
+ file.Segments.Add(segment);
+
+ // ReadElementContentAsStringAsync advances the reader - continue to check current position
+ continue;
+ }
+
+ // Only read if we haven't processed an element that advanced us
+ if (!await reader.ReadAsync().ConfigureAwait(false))
+ break;
+ }
+ }
+}
\ No newline at end of file
diff --git a/backend/Models/Nzb/NzbFile.cs b/backend/Models/Nzb/NzbFile.cs
new file mode 100644
index 00000000..fbaf8ecf
--- /dev/null
+++ b/backend/Models/Nzb/NzbFile.cs
@@ -0,0 +1,55 @@
+using System.Text.RegularExpressions;
+
+namespace NzbWebDAV.Models.Nzb;
+
+public class NzbFile
+{
+ public required string Subject { get; init; }
+ public List<NzbSegment> Segments { get; } = [];
+
+ public string[] GetSegmentIds()
+ {
+ return Segments
+ .Select(x => x.MessageId)
+ .ToArray();
+ }
+
+ public long GetTotalYencodedSize()
+ {
+ return Segments
+ .Select(x => x.Bytes)
+ .Sum();
+ }
+
+ public string GetSubjectFileName()
+ {
+ return GetFirstValidNonEmptyFilename(
+ TryParseSubjectFilename1,
+ TryParseSubjectFilename2
+ );
+ }
+
+ private string TryParseSubjectFilename1()
+ {
+ // The most common format is when filename appears in double quotes
+ // example: `[1/8] - "file.mkv" yEnc 12345 (1/54321)`
+ var match = Regex.Match(Subject, "\\\"(.*)\\\"");
+ return match.Success ? match.Groups[1].Value : "";
+ }
+
+ private string TryParseSubjectFilename2()
+ {
+ // Otherwise, use sabnzbd's regex
+ // https://github.com/sabnzbd/sabnzbd/blob/b6b0d10367fd4960bad73edd1d3812cafa7fc002/sabnzbd/nzbstuff.py#L106
+ var match = Regex.Match(Subject, @"\b([\w\-+()' .,]+(?:\[[\w\-\/+()' .,]*][\w\-+()' .,]*)*\.[A-Za-z0-9]{2,4})\b");
+ return match.Success ? match.Groups[1].Value : "";
+ }
+
+ private static string GetFirstValidNonEmptyFilename(params Func<string>[] funcs)
+ {
+ return funcs
+ .Select(x => x.Invoke())
+ .Where(x => x == Path.GetFileName(x))
+ .FirstOrDefault(x => x != "") ?? "";
+ }
+}
\ No newline at end of file
diff --git a/backend/Models/Nzb/NzbHead.cs b/backend/Models/Nzb/NzbHead.cs
new file mode 100644
index 00000000..01ab0ad7
--- /dev/null
+++ b/backend/Models/Nzb/NzbHead.cs
@@ -0,0 +1,6 @@
+namespace NzbWebDAV.Models.Nzb;
+
+public class NzbHead
+{
+ public List<NzbMeta> Meta { get; set; } = [];
+}
diff --git a/backend/Models/Nzb/NzbMeta.cs b/backend/Models/Nzb/NzbMeta.cs
new file mode 100644
index 00000000..5bdd30e7
--- /dev/null
+++ b/backend/Models/Nzb/NzbMeta.cs
@@ -0,0 +1,8 @@
+namespace NzbWebDAV.Models.Nzb;
+
+public class NzbMeta
+{
+ public string Type { get; set; } = string.Empty;
+
+ public string Value { get; set; } = string.Empty;
+}
diff --git a/backend/Models/Nzb/NzbSegment.cs b/backend/Models/Nzb/NzbSegment.cs
new file mode 100644
index 00000000..58af130c
--- /dev/null
+++ b/backend/Models/Nzb/NzbSegment.cs
@@ -0,0 +1,7 @@
+namespace NzbWebDAV.Models.Nzb;
+
+public class NzbSegment
+{
+ public required long Bytes { get; init; }
+ public required string MessageId { get; init; }
+}
diff --git a/backend/NzbWebDAV.csproj b/backend/NzbWebDAV.csproj
index 24b19a98..b4c2469a 100644
--- a/backend/NzbWebDAV.csproj
+++ b/backend/NzbWebDAV.csproj
@@ -18,7 +18,6 @@
<PackageReference Include="Serilog.AspNetCore" Version="10.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="6.1.1" />
<PackageReference Include="SharpCompress" Version="0.39.0" />
- <PackageReference Include="Usenet" Version="3.1.0" />
<PackageReference Include="UsenetSharp" Version="1.0.6" />
</ItemGroup>
diff --git a/backend/Queue/DeobfuscationSteps/1.FetchFirstSegment/FetchFirstSegmentsStep.cs b/backend/Queue/DeobfuscationSteps/1.FetchFirstSegment/FetchFirstSegmentsStep.cs
index 156b72c2..8ba571e4 100644
--- a/backend/Queue/DeobfuscationSteps/1.FetchFirstSegment/FetchFirstSegmentsStep.cs
+++ b/backend/Queue/DeobfuscationSteps/1.FetchFirstSegment/FetchFirstSegmentsStep.cs
@@ -4,7 +4,7 @@ using NzbWebDAV.Clients.Usenet;
using NzbWebDAV.Config;
using NzbWebDAV.Exceptions;
using NzbWebDAV.Extensions;
-using Usenet.Nzb;
+using NzbWebDAV.Models.Nzb;
using UsenetSharp.Models;
namespace NzbWebDAV.Queue.DeobfuscationSteps._1.FetchFirstSegment;
@@ -37,7 +37,7 @@ public static class FetchFirstSegmentsStep
try
{
// get the first article stream
- var firstSegment = nzbFile.Segments[0].MessageId.Value;
+ var firstSegment = nzbFile.Segments[0].MessageId;
var article = await usenetClient.DecodedArticleAsync(firstSegment, cancellationToken).ConfigureAwait(false);
await using var bodyStream = article.Stream!;
diff --git a/backend/Queue/DeobfuscationSteps/3.GetFileInfos/GetFileInfosStep.cs b/backend/Queue/DeobfuscationSteps/3.GetFileInfos/GetFileInfosStep.cs
index 2a7db55d..16790003 100644
--- a/backend/Queue/DeobfuscationSteps/3.GetFileInfos/GetFileInfosStep.cs
+++ b/backend/Queue/DeobfuscationSteps/3.GetFileInfos/GetFileInfosStep.cs
@@ -1,10 +1,10 @@
using System.Security.Cryptography;
using NzbWebDAV.Extensions;
using NzbWebDAV.Models;
+using NzbWebDAV.Models.Nzb;
using NzbWebDAV.Par2Recovery.Packets;
using NzbWebDAV.Queue.DeobfuscationSteps._1.FetchFirstSegment;
using NzbWebDAV.Utils;
-using Usenet.Nzb;
namespace NzbWebDAV.Queue.DeobfuscationSteps._3.GetFileInfos;
diff --git a/backend/Queue/FileProcessors/FileProcessor.cs b/backend/Queue/FileProcessors/FileProcessor.cs
index 951f7db2..f67eb968 100644
--- a/backend/Queue/FileProcessors/FileProcessor.cs
+++ b/backend/Queue/FileProcessors/FileProcessor.cs
@@ -1,9 +1,9 @@
using NzbWebDAV.Clients.Usenet;
using NzbWebDAV.Exceptions;
+using NzbWebDAV.Models.Nzb;
using NzbWebDAV.Queue.DeobfuscationSteps._3.GetFileInfos;
using NzbWebDAV.Utils;
using Serilog;
-using Usenet.Nzb;
namespace NzbWebDAV.Queue.FileProcessors;
diff --git a/backend/Queue/FileProcessors/RarProcessor.cs b/backend/Queue/FileProcessors/RarProcessor.cs
index 2f137ca1..5e40ce5a 100644
--- a/backend/Queue/FileProcessors/RarProcessor.cs
+++ b/backend/Queue/FileProcessors/RarProcessor.cs
@@ -2,11 +2,11 @@
using NzbWebDAV.Clients.Usenet;
using NzbWebDAV.Extensions;
using NzbWebDAV.Models;
+using NzbWebDAV.Models.Nzb;
using NzbWebDAV.Queue.DeobfuscationSteps._3.GetFileInfos;
using NzbWebDAV.Streams;
using NzbWebDAV.Utils;
using SharpCompress.Common.Rar.Headers;
-using Usenet.Nzb;
namespace NzbWebDAV.Queue.FileProcessors;
diff --git a/backend/Queue/QueueItemProcessor.cs b/backend/Queue/QueueItemProcessor.cs
index c17c22a9..a7648507 100644
--- a/backend/Queue/QueueItemProcessor.cs
+++ b/backend/Queue/QueueItemProcessor.cs
@@ -7,6 +7,7 @@ using NzbWebDAV.Config;
using NzbWebDAV.Database;
using NzbWebDAV.Database.Models;
using NzbWebDAV.Extensions;
+using NzbWebDAV.Models.Nzb;
using NzbWebDAV.Queue.DeobfuscationSteps._1.FetchFirstSegment;
using NzbWebDAV.Queue.DeobfuscationSteps._2.GetPar2FileDescriptors;
using NzbWebDAV.Queue.DeobfuscationSteps._3.GetFileInfos;
@@ -17,7 +18,6 @@ using NzbWebDAV.Services;
using NzbWebDAV.Utils;
using NzbWebDAV.Websocket;
using Serilog;
-using Usenet.Nzb;
namespace NzbWebDAV.Queue;
@@ -115,11 +115,11 @@ public class QueueItemProcessor(
// Look for a password in filename and nzb document
// The file name's password takes priority, as an easy override
var archivePassword = FilenameUtil.GetNzbPassword(queueItem.FileName) ??
- nzb.MetaData.GetValueOrDefault("password")?.FirstOrDefault();
+ nzb.Metadata.GetValueOrDefault("password");
// step 0 -- perform article existence pre-check against cache
// https://github.com/nzbdav-dev/nzbdav/issues/101
- var articlesToPrecheck = nzbFiles.SelectMany(x => x.Segments).Select(x => x.MessageId.Value);
+ var articlesToPrecheck = nzbFiles.SelectMany(x => x.Segments).Select(x => x.MessageId);
HealthCheckService.CheckCachedMissingSegmentIds(articlesToPrecheck);
// step 1 -- get name and size of each nzb file