Usenet streaming with a WebDAV server and a SABnzbd-compatible API
C#
Add check for full article existence during downloads.
nzbdav-dev290a5af385eb
diff --git a/backend/Api/SabControllers/GetQueue/GetQueueResponse.cs b/backend/Api/SabControllers/GetQueue/GetQueueResponse.csindex f928f1aa..ac99b8fe 100644--- a/backend/Api/SabControllers/GetQueue/GetQueueResponse.cs+++ b/backend/Api/SabControllers/GetQueue/GetQueueResponse.cs@@ -38,6 +38,9 @@ public class GetQueueResponse : SabBaseResponse[JsonPropertyName("percentage")]public string Percentage { get; init; }+ [JsonPropertyName("true_percentage")]+ public string TruePercentage { get; init; }+[JsonPropertyName("status")]public string Status { get; init; }@@ -66,7 +69,8 @@ public class GetQueueResponse : SabBaseResponsePriority = queueItem.Priority.ToString(),Filename = queueItem.FileName,Category = queueItem.Category,- Percentage = progressPercentage.ToString()!,+ Percentage = (progressPercentage % 100).ToString(),+ TruePercentage = progressPercentage.ToString(),Status = status,TimeLeft = TimeSpan.Zero,SizeInMB = FormatSizeMB(queueItem.TotalSegmentBytes),diff --git a/backend/Clients/ThreadSafeNntpClient.cs b/backend/Clients/ThreadSafeNntpClient.csindex 95f6c27b..54fffd3a 100644--- a/backend/Clients/ThreadSafeNntpClient.cs+++ b/backend/Clients/ThreadSafeNntpClient.cs@@ -108,7 +108,7 @@ public class ThreadSafeNntpClient : INntpClientprivate IEnumerable<string> GetArticleBody(string segmentId){return _client.Body(new NntpMessageId(segmentId))?.Article?.Body- ?? throw new UsenetArticleNotFoundException($"Article with message-id {segmentId} not found.");+ ?? throw new UsenetArticleNotFoundException(segmentId);}public void Dispose()diff --git a/backend/Clients/UsenetStreamingClient.cs b/backend/Clients/UsenetStreamingClient.csindex 8d41a484..1cad6928 100644--- a/backend/Clients/UsenetStreamingClient.cs+++ b/backend/Clients/UsenetStreamingClient.cs@@ -59,28 +59,30 @@ public class UsenetStreamingClient};}- public async Task<bool> CheckNzbFileHealth(NzbFile nzbFile, CancellationToken cancellationToken = default)+ public async Task CheckAllSegmentsAsync+ (+ IEnumerable<string> segmentIds,+ int concurrency,+ IProgress<int>? progress = null,+ CancellationToken cancellationToken = default+ ){- var childCt = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);- var tasks = nzbFile.Segments- .Select(x => x.MessageId.Value)- .Select(x => _client.StatAsync(x, childCt.Token))- .ToHashSet();+ using var childCt = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);+ using var _ = childCt.Token.SetScopedContext(cancellationToken.GetContext<object>());+ var token = childCt.Token;- while (tasks.Count > 0)+ var tasks = segmentIds+ .Select(x => _client.StatAsync(x, token))+ .WithConcurrencyAsync(concurrency);++ var processed = 0;+ await foreach (var result in tasks){- var completedTask = await Task.WhenAny(tasks);- tasks.Remove(completedTask);-- var completedResult = await completedTask;- if (completedResult.ResponseType != NntpStatResponseType.ArticleExists)- {- await childCt.CancelAsync();- return false;- }+ progress?.Report(++processed);+ if (result.ResponseType == NntpStatResponseType.ArticleExists) continue;+ await childCt.CancelAsync();+ throw new UsenetArticleNotFoundException(result.MessageId.Value);}-- return true;}public async Task<NzbFileStream> GetFileStream(NzbFile nzbFile, int concurrentConnections, CancellationToken ct)diff --git a/backend/Config/ConfigManager.cs b/backend/Config/ConfigManager.csindex dc8b60e9..fb9fe874 100644--- a/backend/Config/ConfigManager.cs+++ b/backend/Config/ConfigManager.cs@@ -135,6 +135,13 @@ public class ConfigManagerreturn (configValue != null ? bool.Parse(configValue) : defaultValue);}+ public bool IsEnsureArticleExistenceEnabled()+ {+ var defaultValue = false;+ var configValue = StringUtil.EmptyToNull(GetConfigValue("api.ensure-article-existence"));+ return (configValue != null ? bool.Parse(configValue) : defaultValue);+ }+public class ConfigEventArgs : EventArgs{public Dictionary<string, string> ChangedConfig { get; set; } = new();diff --git a/backend/Exceptions/UsenetArticleNotFoundException.cs b/backend/Exceptions/UsenetArticleNotFoundException.csindex 7970eef7..3df9827f 100644--- a/backend/Exceptions/UsenetArticleNotFoundException.cs+++ b/backend/Exceptions/UsenetArticleNotFoundException.cs@@ -1,5 +1,6 @@namespace NzbWebDAV.Exceptions;-public class UsenetArticleNotFoundException(string message) : NonRetryableDownloadException(message)+public class UsenetArticleNotFoundException(string segmentId)+ : NonRetryableDownloadException($"Article with message-id {segmentId} not found."){}\ No newline at end of filediff --git a/backend/Queue/QueueItemProcessor.cs b/backend/Queue/QueueItemProcessor.csindex daac9b54..587abe12 100644--- a/backend/Queue/QueueItemProcessor.cs+++ b/backend/Queue/QueueItemProcessor.cs@@ -6,6 +6,7 @@ using NzbWebDAV.Clients.Connections;using NzbWebDAV.Config;using NzbWebDAV.Database;using NzbWebDAV.Database.Models;+using NzbWebDAV.Exceptions;using NzbWebDAV.Extensions;using NzbWebDAV.Queue.DeobfuscationSteps._1.FetchFirstSegment;using NzbWebDAV.Queue.DeobfuscationSteps._2.GetPar2FileDescriptors;@@ -137,6 +138,20 @@ public class QueueItemProcessor(.Select(x => x!).ToList();+ // part3 -- Optionally check full article existence+ if (configManager.IsEnsureArticleExistenceEnabled())+ {+ var articlesToCheck = fileInfos+ .Where(x => FilenameUtil.IsImportantFileType(x.FileName))+ .SelectMany(x => x.NzbFile.GetSegmentIds())+ .ToList();+ var part3Progress = progress+ .Offset(100)+ .ToPercentage(articlesToCheck.Count);+ var concurrency = configManager.GetMaxQueueConnections();+ await usenetClient.CheckAllSegmentsAsync(articlesToCheck, concurrency, part3Progress, ct);+ }+// update the databaseawait MarkQueueItemCompleted(startTime, error: null, () =>{diff --git a/backend/Utils/FilenameUtil.cs b/backend/Utils/FilenameUtil.csindex dac049f6..e8896897 100644--- a/backend/Utils/FilenameUtil.cs+++ b/backend/Utils/FilenameUtil.cs@@ -12,6 +12,14 @@ public class FilenameUtil".img", ".iso", ".vob", ".mkv", ".mk3d", ".ts", ".wtv", ".m2ts"];+ public static bool IsImportantFileType(string filename)+ {+ return IsVideoFile(filename)+ || IsRarFile(filename)+ || Is7zFile(filename)+ || IsMultipartMkv(filename);+ }+public static bool IsVideoFile(string filename){return VideoExtensions.Contains(Path.GetExtension(filename).ToLower());