Usenet streaming with a WebDAV server and a SABnzbd-compatible API
C#
Added more robust identification of RAR files and part numbers.
nzbdav-dev01ca97dfba6b
diff --git a/backend/Extensions/RarHeaderExtensions.cs b/backend/Extensions/RarHeaderExtensions.csindex 9c9e1c16..13c4e84c 100644--- a/backend/Extensions/RarHeaderExtensions.cs+++ b/backend/Extensions/RarHeaderExtensions.cs@@ -33,4 +33,14 @@ public static class RarHeaderExtensions{return (bool)header.GetReflectionProperty("IsDirectory")!;}++ public static int? GetVolumeNumber(this IRarHeader header)+ {+ return (int?)header.GetReflectionProperty("VolumeNumber");+ }++ public static bool GetIsFirstVolume(this IRarHeader header)+ {+ return (bool)header.GetReflectionProperty("IsFirstVolume")!;+ }}\ No newline at end of filediff --git a/backend/Queue/DeobfuscationSteps/1.FetchFirstSegment/FetchFirstSegmentsStep.cs b/backend/Queue/DeobfuscationSteps/1.FetchFirstSegment/FetchFirstSegmentsStep.csindex 264ceda8..2c714e57 100644--- a/backend/Queue/DeobfuscationSteps/1.FetchFirstSegment/FetchFirstSegmentsStep.cs+++ b/backend/Queue/DeobfuscationSteps/1.FetchFirstSegment/FetchFirstSegmentsStep.cs@@ -81,10 +81,22 @@ public static class FetchFirstSegmentsSteppublic class NzbFileWithFirstSegment{+ private static readonly byte[] Rar4Magic = [0x52, 0x61, 0x72, 0x21, 0x1A, 0x07, 0x00];+ private static readonly byte[] Rar5Magic = [0x52, 0x61, 0x72, 0x21, 0x1A, 0x07, 0x01, 0x00];+public required NzbFile NzbFile { get; init; }public required YencHeader? Header { get; init; }public required byte[]? First16KB { get; init; }public required bool MissingFirstSegment { get; init; }public required DateTimeOffset ReleaseDate { get; init; }++ public bool HasRar4Magic() => HasMagic(Rar4Magic);+ public bool HasRar5Magic() => HasMagic(Rar5Magic);++ private bool HasMagic(byte[] sequence)+ {+ return First16KB?.Length >= sequence.Length &&+ First16KB.AsSpan(0, sequence.Length).SequenceEqual(sequence);+ }}}\ No newline at end of filediff --git a/backend/Queue/DeobfuscationSteps/3.GetFileInfos/GetFileInfosStep.cs b/backend/Queue/DeobfuscationSteps/3.GetFileInfos/GetFileInfosStep.csindex fab5ea3a..46711356 100644--- a/backend/Queue/DeobfuscationSteps/3.GetFileInfos/GetFileInfosStep.cs+++ b/backend/Queue/DeobfuscationSteps/3.GetFileInfos/GetFileInfosStep.cs@@ -59,7 +59,8 @@ public static class GetFileInfosStepNzbFile = file.NzbFile,FileName = filename,ReleaseDate = file.ReleaseDate,- FileSize = (long?)fileDesc?.FileLength+ FileSize = (long?)fileDesc?.FileLength,+ IsRar = file.HasRar4Magic() || file.HasRar5Magic(),};}@@ -79,5 +80,6 @@ public static class GetFileInfosSteppublic required string FileName { get; init; }public required DateTimeOffset ReleaseDate { get; init; }public long? FileSize { get; init; }+ public bool IsRar { get; init; }}}\ No newline at end of filediff --git a/backend/Queue/FileProcessors/RarProcessor.cs b/backend/Queue/FileProcessors/RarProcessor.csindex c21f408f..321456a4 100644--- a/backend/Queue/FileProcessors/RarProcessor.cs+++ b/backend/Queue/FileProcessors/RarProcessor.cs@@ -24,13 +24,16 @@ public class RarProcessor({await using var stream = await GetNzbFileStream();await using var cancellableStream = new CancellableStream(stream, ct);+ var headers = await Task.Run(() => GetRarHeaders(cancellableStream));+ var archiveHeader = headers.FirstOrDefault(x => x.HeaderType == HeaderType.Archive);return new Result(){NzbFile = fileInfo.NzbFile,PartSize = cancellableStream.Length,ArchiveName = GetArchiveName(),- PartNumber = GetPartNumber(),- StoredFileSegments = (await Task.Run(() => GetRarHeaders(cancellableStream)))+ PartNumber = GetPartNumber(archiveHeader),+ StoredFileSegments = headers+ .Where(x => x.HeaderType == HeaderType.File).Select(x => new StoredFileSegment(){PathWithinArchive = x.GetFileName(),@@ -54,8 +57,13 @@ public class RarProcessor(return sansExtension;}- private int GetPartNumber()+ private int GetPartNumber(IRarHeader? archiveHeader){+ // read from archive-header if possible+ var archiveVolumeNumber = archiveHeader?.GetVolumeNumber();+ if (archiveVolumeNumber != null) return archiveVolumeNumber!.Value;+ if (archiveHeader?.GetIsFirstVolume() == true) return -1;+// handle the `.partXXX.rar` formatvar partMatch = Regex.Match(fileInfo.FileName, @"\.part(\d+)\.rar$", RegexOptions.IgnoreCase);if (partMatch.Success) return int.Parse(partMatch.Groups[1].Value);@@ -65,7 +73,10 @@ public class RarProcessor(if (rMatch.Success) return int.Parse(rMatch.Groups[1].Value);// handle the `.rar` format.- return -1;+ if (fileInfo.FileName.EndsWith(".rar", StringComparison.OrdinalIgnoreCase)) return -1;++ // we were unable to determine the part number.+ throw new Exception("Could not determine part number for RAR file.");}private List<IRarHeader> GetRarHeaders(Stream stream)@@ -77,6 +88,13 @@ public class RarProcessor({ct.ThrowIfCancellationRequested();+ // Add archive headers+ if (header.HeaderType == HeaderType.Archive)+ {+ headers.Add(header);+ continue;+ }+// we only care about file headersif (header.HeaderType != HeaderType.File || header.IsDirectory() || header.GetFileName() == "QO") continue;diff --git a/backend/Queue/QueueItemProcessor.cs b/backend/Queue/QueueItemProcessor.csindex e99c18e7..114d22e0 100644--- a/backend/Queue/QueueItemProcessor.cs+++ b/backend/Queue/QueueItemProcessor.cs@@ -175,7 +175,7 @@ public class QueueItemProcessor({var groups = fileInfos.DistinctBy(x => x.FileName)- .GroupBy(x => GetGroup(x.FileName));+ .GroupBy(GetGroup);foreach (var group in groups){@@ -196,10 +196,10 @@ public class QueueItemProcessor(yield break;- string GetGroup(string x) => false ? "impossible"- : FilenameUtil.Is7zFile(x) ? "7z"- : FilenameUtil.IsRarFile(x) ? "rar"- : FilenameUtil.IsMultipartMkv(x) ? "multipart-mkv"+ string GetGroup(GetFileInfosStep.FileInfo x) => false ? "impossible"+ : FilenameUtil.Is7zFile(x.FileName) ? "7z"+ : x.IsRar || FilenameUtil.IsRarFile(x.FileName) ? "rar"+ : FilenameUtil.IsMultipartMkv(x.FileName) ? "multipart-mkv": "other";}