Usenet streaming with a WebDAV server and a SABnzbd-compatible API
C#
Throw inner `missing-articles` exception when encountered during rar processing.
nzbdav-dev72d7eb6798eb
diff --git a/backend/Extensions/ExceptionExtensions.cs b/backend/Extensions/ExceptionExtensions.csindex d9e49a9a..54e6d33d 100644--- a/backend/Extensions/ExceptionExtensions.cs+++ b/backend/Extensions/ExceptionExtensions.cs@@ -20,4 +20,24 @@ public static class ExceptionExtensions{return exception is TaskCanceledException or OperationCanceledException;}++ public static bool TryGetInnerException<T>(this Exception exception, out T? exceptionType) where T : Exception+ {+ ArgumentNullException.ThrowIfNull(exception);+ var current = exception.InnerException;++ while (current != null)+ {+ if (current is T matching)+ {+ exceptionType = matching;+ return true;+ }++ current = current.InnerException;+ }++ exceptionType = null;+ return false;+ }}\ No newline at end of filediff --git a/backend/Utils/RarUtil.cs b/backend/Utils/RarUtil.csindex 1fa8e705..1275850c 100644--- a/backend/Utils/RarUtil.cs+++ b/backend/Utils/RarUtil.cs@@ -1,6 +1,7 @@using NzbWebDAV.Exceptions;using NzbWebDAV.Extensions;using NzbWebDAV.Streams;+using Serilog;using SharpCompress.Common.Rar.Headers;using SharpCompress.IO;using SharpCompress.Readers;@@ -22,47 +23,54 @@ public static class RarUtilprivate static List<IRarHeader> GetRarHeaders(Stream stream, string? password){- var readerOptions = new ReaderOptions() { Password = password };- var headerFactory = new RarHeaderFactory(StreamingMode.Seekable, readerOptions);- var headers = new List<IRarHeader>();- foreach (var header in headerFactory.ReadHeaders(stream))+ try{- // add archive headers- if (header.HeaderType is HeaderType.Archive or HeaderType.EndArchive)+ var readerOptions = new ReaderOptions() { Password = password };+ var headerFactory = new RarHeaderFactory(StreamingMode.Seekable, readerOptions);+ var headers = new List<IRarHeader>();+ foreach (var header in headerFactory.ReadHeaders(stream)){- headers.Add(header);- continue;- }+ // add archive headers+ if (header.HeaderType is HeaderType.Archive or HeaderType.EndArchive)+ {+ headers.Add(header);+ continue;+ }- // skip comments- if (header.HeaderType == HeaderType.Service)- {- if (header.GetFileName() == "CMT")+ // skip comments+ if (header.HeaderType == HeaderType.Service){- var buffer = new byte[header.GetCompressedSize()];- _ = stream.Read(buffer, 0, buffer.Length);+ if (header.GetFileName() == "CMT")+ {+ var buffer = new byte[header.GetCompressedSize()];+ _ = stream.Read(buffer, 0, buffer.Length);+ }++ continue;}- continue;- }+ // we only care about file headers+ if (header.HeaderType != HeaderType.File || header.IsDirectory() ||+ header.GetFileName() == "QO") continue;- // we only care about file headers- if (header.HeaderType != HeaderType.File || header.IsDirectory() ||- header.GetFileName() == "QO") continue;+ // we only support stored files (compression method m0).+ if (header.GetCompressionMethod() != 0)+ throw new UnsupportedRarCompressionMethodException(+ "Only rar files with compression method m0 are supported.");- // we only support stored files (compression method m0).- if (header.GetCompressionMethod() != 0)- throw new UnsupportedRarCompressionMethodException(- "Only rar files with compression method m0 are supported.");+ // TODO: support solid archives+ if (header.GetIsEncrypted() && header.GetIsSolid())+ throw new Exception("Password-protected rar archives cannot be solid.");- // TODO: support solid archives- if (header.GetIsEncrypted() && header.GetIsSolid())- throw new Exception("Password-protected rar archives cannot be solid.");+ // add the headers+ headers.Add(header);+ }- // add the headers- headers.Add(header);+ return headers;+ }+ catch (Exception e) when (e.TryGetInnerException(out UsenetArticleNotFoundException missingArticleException))+ {+ throw missingArticleException;}-- return headers;}}\ No newline at end of file