Skip to content

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

C#

Added support for 7z archives.

nzbdav-dev5dc6bd3a8d6e
diff --git a/backend/Exceptions/Unsupported7zCompressionMethodException.cs b/backend/Exceptions/Unsupported7zCompressionMethodException.cs
new file mode 100644
index 00000000..c79ec995
--- /dev/null
+++ b/backend/Exceptions/Unsupported7zCompressionMethodException.cs
@@ -0,0 +1,6 @@
+namespace NzbWebDAV.Exceptions;
+
+// ReSharper disable once InconsistentNaming
+public class Unsupported7zCompressionMethodException(string message) : NonRetryableDownloadException(message)
+{
+}
\ No newline at end of file
diff --git a/backend/Queue/DeobfuscationSteps/3.GetFileInfos/GetFileInfosStep.cs b/backend/Queue/DeobfuscationSteps/3.GetFileInfos/GetFileInfosStep.cs
index 7ccac502..9c1cf707 100644
--- a/backend/Queue/DeobfuscationSteps/3.GetFileInfos/GetFileInfosStep.cs
+++ b/backend/Queue/DeobfuscationSteps/3.GetFileInfos/GetFileInfosStep.cs
@@ -9,7 +9,7 @@ namespace NzbWebDAV.Queue.DeobfuscationSteps._3.GetFileInfos;
public static class GetFileInfosStep
{
- public static Dictionary<NzbFile, FileInfo> GetFileInfos
+ public static List<FileInfo> GetFileInfos
(
List<FetchFirstSegmentsStep.NzbFileWithFirstSegment> files,
List<FileDesc> par2FileDescriptors
@@ -17,12 +17,11 @@ public static class GetFileInfosStep
{
using var md5 = MD5.Create();
var hashToFileDescMap = GetHashToFileDescMap(par2FileDescriptors);
- var fileinfoDictionary = files.ToDictionary(
- x => x.NzbFile,
- x => GetFileInfo(x, hashToFileDescMap, md5)
- );
+ var filesInfos = files
+ .Select(x => GetFileInfo(x, hashToFileDescMap, md5))
+ .ToList();
- return fileinfoDictionary;
+ return filesInfos;
}
private static Dictionary<string, FileDesc> GetHashToFileDescMap(List<FileDesc> par2FileDescriptors)
@@ -57,6 +56,7 @@ public static class GetFileInfosStep
return new FileInfo()
{
+ NzbFile = file.NzbFile,
FileName = filename,
FileSize = (long?)fileDesc?.FileLength
};
@@ -74,6 +74,7 @@ public static class GetFileInfosStep
public class FileInfo
{
+ public required NzbFile NzbFile { get; init; }
public required string FileName { get; init; }
public long? FileSize { get; init; }
}
diff --git a/backend/Queue/FileAggregators/BaseAggregator.cs b/backend/Queue/FileAggregators/BaseAggregator.cs
new file mode 100644
index 00000000..aa660b4e
--- /dev/null
+++ b/backend/Queue/FileAggregators/BaseAggregator.cs
@@ -0,0 +1,47 @@
+using NzbWebDAV.Database;
+using NzbWebDAV.Database.Models;
+using NzbWebDAV.Queue.FileProcessors;
+
+namespace NzbWebDAV.Queue.FileAggregators;
+
+public abstract class BaseAggregator
+{
+ public abstract void UpdateDatabase(List<BaseProcessor.Result> processorResults);
+ protected abstract DavDatabaseClient DBClient { get; }
+ protected abstract DavItem MountDirectory { get; }
+
+ protected DavItem EnsureExtractPath(string pathWithinArchive)
+ {
+ var pathSegments = pathWithinArchive
+ .Split(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar)
+ .Prepend("extracted")
+ .ToArray();
+ var parentDirectory = MountDirectory;
+ var pathKey = "";
+ for (var i = 0; i < pathSegments.Length - 1; i++)
+ {
+ pathKey = Path.Join(pathKey, pathSegments[i]);
+ parentDirectory = EnsureDirectory(parentDirectory, pathSegments[i], pathKey);
+ }
+
+ return parentDirectory;
+ }
+
+ private readonly Dictionary<string, DavItem> _directoryCache = new();
+
+ protected DavItem EnsureDirectory(DavItem parentDirectory, string directoryName, string pathKey)
+ {
+ if (_directoryCache.TryGetValue(pathKey, out var cachedDirectory)) return cachedDirectory;
+
+ var directory = DavItem.New(
+ id: Guid.NewGuid(),
+ parent: parentDirectory,
+ name: directoryName,
+ fileSize: null,
+ type: DavItem.ItemType.Directory
+ );
+ _directoryCache.Add(pathKey, directory);
+ DBClient.Ctx.Items.Add(directory);
+ return directory;
+ }
+}
\ No newline at end of file
diff --git a/backend/Queue/FileAggregators/FileAggregator.cs b/backend/Queue/FileAggregators/FileAggregator.cs
index 9ab6d635..aa84d6f7 100644
--- a/backend/Queue/FileAggregators/FileAggregator.cs
+++ b/backend/Queue/FileAggregators/FileAggregator.cs
@@ -5,9 +5,12 @@ using NzbWebDAV.Queue.FileProcessors;
namespace NzbWebDAV.Queue.FileAggregators;
-public class FileAggregator(DavDatabaseClient dbClient, DavItem mountDirectory) : IAggregator
+public class FileAggregator(DavDatabaseClient dbClient, DavItem mountDirectory) : BaseAggregator
{
- public void UpdateDatabase(List<BaseProcessor.Result> processorResults)
+ protected override DavDatabaseClient DBClient => dbClient;
+ protected override DavItem MountDirectory => mountDirectory;
+
+ public override void UpdateDatabase(List<BaseProcessor.Result> processorResults)
{
foreach (var processorResult in processorResults)
{
diff --git a/backend/Queue/FileAggregators/IAggregator.cs b/backend/Queue/FileAggregators/IAggregator.cs
deleted file mode 100644
index 48a5068f..00000000
--- a/backend/Queue/FileAggregators/IAggregator.cs
+++ /dev/null
@@ -1,8 +0,0 @@
-using NzbWebDAV.Queue.FileProcessors;
-
-namespace NzbWebDAV.Queue.FileAggregators;
-
-public interface IAggregator
-{
- public void UpdateDatabase(List<BaseProcessor.Result> processorResults);
-}
\ No newline at end of file
diff --git a/backend/Queue/FileAggregators/RarAggregator.cs b/backend/Queue/FileAggregators/RarAggregator.cs
index 765f01f7..8adc8090 100644
--- a/backend/Queue/FileAggregators/RarAggregator.cs
+++ b/backend/Queue/FileAggregators/RarAggregator.cs
@@ -6,9 +6,12 @@ using NzbWebDAV.Utils;
namespace NzbWebDAV.Queue.FileAggregators;
-public class RarAggregator(DavDatabaseClient dbClient, DavItem mountDirectory) : IAggregator
+public class RarAggregator(DavDatabaseClient dbClient, DavItem mountDirectory) : BaseAggregator
{
- public void UpdateDatabase(List<BaseProcessor.Result> processorResults)
+ protected override DavDatabaseClient DBClient => dbClient;
+ protected override DavItem MountDirectory => mountDirectory;
+
+ public override void UpdateDatabase(List<BaseProcessor.Result> processorResults)
{
var orderedArchiveParts = processorResults
.OfType<RarProcessor.Result>()
@@ -42,7 +45,7 @@ public class RarAggregator(DavDatabaseClient dbClient, DavItem mountDirectory) :
{
var pathWithinArchive = archiveFile.Key;
var rarParts = archiveFile.Value.ToArray();
- var parentDirectory = EnsurePath(pathWithinArchive);
+ var parentDirectory = EnsureExtractPath(pathWithinArchive);
var name = Path.GetFileName(pathWithinArchive);
// If there is only one file in the archive and the file-name is obfuscated,
@@ -68,39 +71,4 @@ public class RarAggregator(DavDatabaseClient dbClient, DavItem mountDirectory) :
dbClient.Ctx.RarFiles.Add(davRarFile);
}
}
-
- private DavItem EnsurePath(string pathWithinArchive)
- {
- var pathSegments = pathWithinArchive
- .Split(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar)
- .Prepend("extracted")
- .ToArray();
- var parentDirectory = mountDirectory;
- var pathKey = "";
- for (var i = 0; i < pathSegments.Length - 1; i++)
- {
- pathKey = Path.Join(pathKey, pathSegments[i]);
- parentDirectory = EnsureDirectory(parentDirectory, pathSegments[i], pathKey);
- }
-
- return parentDirectory;
- }
-
- private readonly Dictionary<string, DavItem> _directoryCache = new();
-
- private DavItem EnsureDirectory(DavItem parentDirectory, string directoryName, string pathKey)
- {
- if (_directoryCache.TryGetValue(pathKey, out var cachedDirectory)) return cachedDirectory;
-
- var directory = DavItem.New(
- id: Guid.NewGuid(),
- parent: parentDirectory,
- name: directoryName,
- fileSize: null,
- type: DavItem.ItemType.Directory
- );
- _directoryCache.Add(pathKey, directory);
- dbClient.Ctx.Items.Add(directory);
- return directory;
- }
}
\ No newline at end of file
diff --git a/backend/Queue/FileAggregators/SevenZipAggregator.cs b/backend/Queue/FileAggregators/SevenZipAggregator.cs
new file mode 100644
index 00000000..383ea414
--- /dev/null
+++ b/backend/Queue/FileAggregators/SevenZipAggregator.cs
@@ -0,0 +1,55 @@
+using NzbWebDAV.Database;
+using NzbWebDAV.Database.Models;
+using NzbWebDAV.Queue.FileProcessors;
+using NzbWebDAV.Utils;
+
+namespace NzbWebDAV.Queue.FileAggregators;
+
+public class SevenZipAggregator(DavDatabaseClient dbClient, DavItem mountDirectory) : BaseAggregator
+{
+ protected override DavDatabaseClient DBClient => dbClient;
+ protected override DavItem MountDirectory => mountDirectory;
+
+ public override void UpdateDatabase(List<BaseProcessor.Result> processorResults)
+ {
+ var sevenZipFiles = processorResults
+ .OfType<SevenZipProcessor.Result>()
+ .SelectMany(x => x.SevenZipFiles)
+ .ToList();
+
+ ProcessSevenZipFile(sevenZipFiles);
+ }
+
+ private void ProcessSevenZipFile(List<SevenZipProcessor.SevenZipFile> sevenZipFiles)
+ {
+ foreach (var sevenZipFile in sevenZipFiles)
+ {
+ var pathWithinArchive = sevenZipFile.PathWithinArchive;
+ var sevenZipParts = sevenZipFile.Parts;
+ var parentDirectory = EnsureExtractPath(pathWithinArchive);
+ var name = Path.GetFileName(pathWithinArchive);
+
+ // If there is only one file in the archive and the file-name is obfuscated,
+ // then rename the file to the same name as the containing mount directory.
+ if (sevenZipFiles.Count == 1 && ObfuscationUtil.IsProbablyObfuscated(name))
+ name = mountDirectory.Name + Path.GetExtension(name);
+
+ var davItem = DavItem.New(
+ id: Guid.NewGuid(),
+ parent: parentDirectory,
+ name: name,
+ fileSize: sevenZipParts.Sum(x => x.ByteCount),
+ type: DavItem.ItemType.RarFile
+ );
+
+ var davRarFile = new DavRarFile()
+ {
+ Id = davItem.Id,
+ RarParts = sevenZipParts.ToArray(),
+ };
+
+ dbClient.Ctx.Items.Add(davItem);
+ dbClient.Ctx.RarFiles.Add(davRarFile);
+ }
+ }
+}
\ No newline at end of file
diff --git a/backend/Queue/FileProcessors/FileProcessor.cs b/backend/Queue/FileProcessors/FileProcessor.cs
index 53f82360..0cf8a7d5 100644
--- a/backend/Queue/FileProcessors/FileProcessor.cs
+++ b/backend/Queue/FileProcessors/FileProcessor.cs
@@ -8,34 +8,28 @@ using Usenet.Nzb;
namespace NzbWebDAV.Queue.FileProcessors;
public class FileProcessor(
- NzbFile nzbFile,
- GetFileInfosStep.FileInfo fileinfo,
+ GetFileInfosStep.FileInfo fileInfo,
UsenetStreamingClient usenet,
CancellationToken ct
) : BaseProcessor
{
- public static bool CanProcess(string filename)
- {
- return true;
- }
-
public override async Task<BaseProcessor.Result?> ProcessAsync()
{
try
{
return new Result()
{
- NzbFile = nzbFile,
- FileName = fileinfo.FileName,
- FileSize = fileinfo.FileSize ?? await usenet.GetFileSizeAsync(nzbFile, ct),
+ NzbFile = fileInfo.NzbFile,
+ FileName = fileInfo.FileName,
+ FileSize = fileInfo.FileSize ?? await usenet.GetFileSizeAsync(fileInfo.NzbFile, ct),
};
}
// Ignore missing articles if it's not a video file.
// In that case, simply skip the file altogether.
- catch (UsenetArticleNotFoundException) when (!FilenameUtil.IsVideoFile(fileinfo.FileName))
+ catch (UsenetArticleNotFoundException) when (!FilenameUtil.IsVideoFile(fileInfo.FileName))
{
- Log.Warning($"File `{fileinfo.FileName}` has missing articles. Skipping file since it is not a video.");
+ Log.Warning($"File `{fileInfo.FileName}` has missing articles. Skipping file since it is not a video.");
return null;
}
}
diff --git a/backend/Queue/FileProcessors/RarProcessor.cs b/backend/Queue/FileProcessors/RarProcessor.cs
index 44e2b00c..dafa4904 100644
--- a/backend/Queue/FileProcessors/RarProcessor.cs
+++ b/backend/Queue/FileProcessors/RarProcessor.cs
@@ -4,7 +4,6 @@ using NzbWebDAV.Exceptions;
using NzbWebDAV.Extensions;
using NzbWebDAV.Queue.DeobfuscationSteps._3.GetFileInfos;
using NzbWebDAV.Streams;
-using NzbWebDAV.Utils;
using SharpCompress.Common;
using SharpCompress.Common.Rar.Headers;
using SharpCompress.IO;
@@ -14,17 +13,11 @@ using Usenet.Nzb;
namespace NzbWebDAV.Queue.FileProcessors;
public class RarProcessor(
- NzbFile nzbFile,
- GetFileInfosStep.FileInfo fileinfo,
+ GetFileInfosStep.FileInfo fileInfo,
UsenetStreamingClient usenet,
CancellationToken ct
) : BaseProcessor
{
- public static bool CanProcess(string filename)
- {
- return FilenameUtil.IsRarFile(filename);
- }
-
public override async Task<BaseProcessor.Result?> ProcessAsync()
{
try
@@ -33,7 +26,7 @@ public class RarProcessor(
await using var cancellableStream = new CancellableStream(stream, ct);
return new Result()
{
- NzbFile = nzbFile,
+ NzbFile = fileInfo.NzbFile,
PartSize = cancellableStream.Length,
ArchiveName = GetArchiveName(),
PartNumber = GetPartNumber(),
@@ -55,7 +48,7 @@ public class RarProcessor(
private string GetArchiveName()
{
// remove the .rar extension and remove the .partXX if it exists
- var sansExtension = Path.GetFileNameWithoutExtension(fileinfo.FileName);
+ var sansExtension = Path.GetFileNameWithoutExtension(fileInfo.FileName);
sansExtension = Regex.Replace(sansExtension, @"\.part\d+$", "");
return sansExtension;
}
@@ -63,11 +56,11 @@ public class RarProcessor(
private int GetPartNumber()
{
// handle the `.partXXX.rar` format
- var partMatch = Regex.Match(fileinfo.FileName, @"\.part(\d+)\.rar$", RegexOptions.IgnoreCase);
+ var partMatch = Regex.Match(fileInfo.FileName, @"\.part(\d+)\.rar$", RegexOptions.IgnoreCase);
if (partMatch.Success) return int.Parse(partMatch.Groups[1].Value);
// handle the `.rXXX` format
- var rMatch = Regex.Match(fileinfo.FileName, @"\.r(\d+)$", RegexOptions.IgnoreCase);
+ var rMatch = Regex.Match(fileInfo.FileName, @"\.r(\d+)$", RegexOptions.IgnoreCase);
if (rMatch.Success) return int.Parse(rMatch.Groups[1].Value);
// handle the `.rar` format.
@@ -104,8 +97,8 @@ public class RarProcessor(
private async Task<NzbFileStream> GetNzbFileStream()
{
- var filesize = fileinfo.FileSize ?? await usenet.GetFileSizeAsync(nzbFile, ct);
- return usenet.GetFileStream(nzbFile, filesize, concurrentConnections: 1);
+ var filesize = fileInfo.FileSize ?? await usenet.GetFileSizeAsync(fileInfo.NzbFile, ct);
+ return usenet.GetFileStream(fileInfo.NzbFile, filesize, concurrentConnections: 1);
}
public new class Result : BaseProcessor.Result
diff --git a/backend/Queue/FileProcessors/SevenZipProcessor.cs b/backend/Queue/FileProcessors/SevenZipProcessor.cs
new file mode 100644
index 00000000..f2ea7b14
--- /dev/null
+++ b/backend/Queue/FileProcessors/SevenZipProcessor.cs
@@ -0,0 +1,135 @@
+using System.Text.RegularExpressions;
+using NzbWebDAV.Clients;
+using NzbWebDAV.Database.Models;
+using NzbWebDAV.Exceptions;
+using NzbWebDAV.Extensions;
+using NzbWebDAV.Models;
+using NzbWebDAV.Queue.DeobfuscationSteps._3.GetFileInfos;
+using NzbWebDAV.Streams;
+using NzbWebDAV.Utils;
+using SharpCompress.Common;
+
+namespace NzbWebDAV.Queue.FileProcessors;
+
+public class SevenZipProcessor : BaseProcessor
+{
+ private readonly List<GetFileInfosStep.FileInfo> _fileInfos;
+ private readonly UsenetStreamingClient _client;
+ private readonly CancellationToken _ct;
+
+ public SevenZipProcessor
+ (
+ List<GetFileInfosStep.FileInfo> fileInfos,
+ UsenetStreamingClient client,
+ CancellationToken ct
+ )
+ {
+ _fileInfos = fileInfos;
+ _client = client;
+ _ct = ct;
+ }
+
+ public override async Task<BaseProcessor.Result?> ProcessAsync()
+ {
+ var multipartFile = await GetMultipartFile();
+ await using var stream = new MultipartFileStream(multipartFile, _client);
+ var sevenZipEntries = await SevenZipUtil.GetSevenZipEntriesAsync(stream, _ct);
+ if (sevenZipEntries.Any(x => x.CompressionType != CompressionType.None))
+ {
+ const string message = "Only uncompressed 7z files are supported.";
+ throw new Unsupported7zCompressionMethodException(message);
+ }
+
+ return new Result()
+ {
+ SevenZipFiles = sevenZipEntries.Select(x => new SevenZipFile()
+ {
+ PathWithinArchive = x.PathWithinArchive,
+ Parts = GetSevenZipParts(x, multipartFile)
+ }).ToList()
+ };
+ }
+
+ private async Task<MultipartFile> GetMultipartFile()
+ {
+ var sortedFileInfos = _fileInfos.OrderBy(f => GetPartNumber(f.FileName)).ToList();
+ var fileParts = new List<MultipartFile.FilePart>();
+ long startInclusive = 0;
+ foreach (var fileInfo in sortedFileInfos)
+ {
+ var nzbFile = fileInfo.NzbFile;
+ var fileSize = fileInfo.FileSize ?? await _client.GetFileSizeAsync(nzbFile, _ct);
+ var endExclusive = startInclusive + fileSize;
+ fileParts.Add(new MultipartFile.FilePart()
+ {
+ NzbFile = fileInfo.NzbFile,
+ ByteRange = new LongRange(startInclusive, endExclusive),
+ });
+ startInclusive = endExclusive;
+ }
+
+ return new MultipartFile() { FileParts = fileParts };
+ }
+
+ private static int GetPartNumber(string filename)
+ {
+ var match = Regex.Match(filename, @"\.7z(\.(\d+))?$", RegexOptions.IgnoreCase);
+ return string.IsNullOrEmpty(match.Groups[2].Value) ? -1 : int.Parse(match.Groups[2].Value);
+ }
+
+ private List<DavRarFile.RarPart> GetSevenZipParts
+ (
+ SevenZipUtil.SevenZipEntry sevenZipEntry,
+ MultipartFile multipartFile
+ )
+ {
+ var (startIndexInclusive, startIndexByteRange) = InterpolationSearch.Find(
+ sevenZipEntry.ByteRangeWithinArchive.StartInclusive,
+ new LongRange(0, multipartFile.FileParts.Count),
+ new LongRange(0, multipartFile.FileSize),
+ guess => multipartFile.FileParts[guess].ByteRange
+ );
+
+ var (endIndexInclusive, endIndexByteRange) = InterpolationSearch.Find(
+ sevenZipEntry.ByteRangeWithinArchive.EndExclusive - 1,
+ new LongRange(0, multipartFile.FileParts.Count),
+ new LongRange(0, multipartFile.FileSize),
+ guess => multipartFile.FileParts[guess].ByteRange
+ );
+
+ var endIndexExclusive = endIndexInclusive + 1;
+ var indexCount = endIndexExclusive - startIndexInclusive;
+ return Enumerable
+ .Range(startIndexInclusive, indexCount)
+ .Select(index =>
+ {
+ var partStartInclusive = index == startIndexInclusive
+ ? sevenZipEntry.ByteRangeWithinArchive.StartInclusive - startIndexByteRange.StartInclusive
+ : 0;
+ var partEndExclusive = index == endIndexInclusive
+ ? sevenZipEntry.ByteRangeWithinArchive.EndExclusive - endIndexByteRange.StartInclusive
+ : multipartFile.FileParts[index].PartSize;
+ var partByteCount = partEndExclusive - partStartInclusive;
+
+ return new DavRarFile.RarPart()
+ {
+ SegmentIds = multipartFile.FileParts[index].NzbFile.GetSegmentIds(),
+ PartSize = multipartFile.FileParts[index].PartSize,
+ Offset = partStartInclusive,
+ ByteCount = partByteCount
+ };
+ })
+ .ToList();
+ }
+
+ public new class Result : BaseProcessor.Result
+ {
+ public List<SevenZipFile> SevenZipFiles { get; init; } = [];
+ }
+
+ public class SevenZipFile
+ {
+ public required string PathWithinArchive { get; init; }
+ public List<DavRarFile.RarPart> Parts { get; init; } = [];
+ }
+}
\ No newline at end of file
diff --git a/backend/Queue/QueueItemProcessor.cs b/backend/Queue/QueueItemProcessor.cs
index aebbc3a8..165d1b2b 100644
--- a/backend/Queue/QueueItemProcessor.cs
+++ b/backend/Queue/QueueItemProcessor.cs
@@ -13,6 +13,7 @@ using NzbWebDAV.Queue.DeobfuscationSteps._3.GetFileInfos;
using NzbWebDAV.Queue.FileAggregators;
using NzbWebDAV.Queue.FileProcessors;
using NzbWebDAV.Queue.Validators;
+using NzbWebDAV.Utils;
using NzbWebDAV.Websocket;
using Serilog;
using Usenet.Nzb;
@@ -118,15 +119,11 @@ public class QueueItemProcessor(
nzbFiles, usenetClient, configManager, ct, part1Progress);
var par2FileDescriptors = await GetPar2FileDescriptorsStep.GetPar2FileDescriptors(
segments, usenetClient, ct);
- var fileInfoDictionary = GetFileInfosStep.GetFileInfos(
+ var fileInfos = GetFileInfosStep.GetFileInfos(
segments, par2FileDescriptors);
// part 2 -- perform file processing
- var fileProcessors = nzbFiles
- .DistinctBy(x => fileInfoDictionary[x].FileName)
- .Select(x => GetFileProcessor(x, fileInfoDictionary[x]))
- .Where(x => x is not null)
- .ToList();
+ var fileProcessors = GetFileProcessors(fileInfos).ToList();
var part2Progress = progress
.Offset(50)
.Scale(50, 100)
@@ -147,6 +144,7 @@ public class QueueItemProcessor(
var mountFolder = CreateMountFolder(categoryFolder);
new RarAggregator(dbClient, mountFolder).UpdateDatabase(fileProcessingResults);
new FileAggregator(dbClient, mountFolder).UpdateDatabase(fileProcessingResults);
+ new SevenZipAggregator(dbClient, mountFolder).UpdateDatabase(fileProcessingResults);
// validate video files found
if (configManager.IsEnsureImportableVideoEnabled())
@@ -156,11 +154,32 @@ public class QueueItemProcessor(
});
}
- private BaseProcessor? GetFileProcessor(NzbFile nzbFile, GetFileInfosStep.FileInfo fileinfo)
+ private IEnumerable<BaseProcessor> GetFileProcessors(List<GetFileInfosStep.FileInfo> fileInfos)
{
- return RarProcessor.CanProcess(fileinfo.FileName) ? new RarProcessor(nzbFile, fileinfo, usenetClient, ct)
- : FileProcessor.CanProcess(fileinfo.FileName) ? new FileProcessor(nzbFile, fileinfo, usenetClient, ct)
- : null;
+ var groups = fileInfos
+ .DistinctBy(x => x.FileName)
+ .GroupBy(x => GetGroup(x.FileName));
+
+ foreach (var group in groups)
+ {
+ if (group.Key == "7z")
+ yield return new SevenZipProcessor(group.ToList(), usenetClient, ct);
+
+ else if (group.Key == "rar")
+ foreach (var fileInfo in group)
+ yield return new RarProcessor(fileInfo, usenetClient, ct);
+
+ else if (group.Key == "other")
+ foreach (var fileInfo in group)
+ yield return new FileProcessor(fileInfo, usenetClient, ct);
+ }
+
+ yield break;
+
+ string GetGroup(string x) => false ? "impossible"
+ : FilenameUtil.Is7zFile(x) ? "7z"
+ : FilenameUtil.IsRarFile(x) ? "rar"
+ : "other";
}
private async Task<DavItem?> GetMountFolder()
diff --git a/backend/Streams/MultipartFileStream.cs b/backend/Streams/MultipartFileStream.cs
index 744ebea2..1a85f7a5 100644
--- a/backend/Streams/MultipartFileStream.cs
+++ b/backend/Streams/MultipartFileStream.cs
@@ -40,7 +40,7 @@ public class MultipartFileStream : Stream
while (_position < Length && !cancellationToken.IsCancellationRequested)
{
// If we haven't read the first stream, read it.
- _currentStream ??= await GetCurrentStreamAsync(cancellationToken);
+ _currentStream ??= GetCurrentStream();
// read from our current stream
var readCount = await _currentStream.ReadAsync
@@ -60,14 +60,13 @@ public class MultipartFileStream : Stream
return 0;
}
- private async Task<Stream> GetCurrentStreamAsync(CancellationToken cancellationToken)
+ private Stream GetCurrentStream()
{
- var searchResult = await InterpolationSearch.Find(
+ var searchResult = InterpolationSearch.Find(
_position,
new LongRange(0, _multipartFile.FileParts.Count),
new LongRange(0, Length),
- guess => Task.FromResult(_multipartFile.FileParts[guess].ByteRange),
- cancellationToken
+ guess => _multipartFile.FileParts[guess].ByteRange
);
var filePart = _multipartFile.FileParts[searchResult.FoundIndex];
diff --git a/backend/Utils/FilenameUtil.cs b/backend/Utils/FilenameUtil.cs
index 783ce6d3..96e05b6d 100644
--- a/backend/Utils/FilenameUtil.cs
+++ b/backend/Utils/FilenameUtil.cs
@@ -23,4 +23,10 @@ public class FilenameUtil
return filename.EndsWith(".rar", StringComparison.OrdinalIgnoreCase)
|| Regex.IsMatch(filename, @"\.r(\d+)$", RegexOptions.IgnoreCase);
}
+
+ public static bool Is7zFile(string? filename)
+ {
+ if (string.IsNullOrEmpty(filename)) return false;
+ return Regex.IsMatch(filename, @"\.7z(\.(\d+))?$", RegexOptions.IgnoreCase);
+ }
}
\ No newline at end of file
diff --git a/backend/Utils/InterpolationSearch.cs b/backend/Utils/InterpolationSearch.cs
index 3cef3a30..c7dacd80 100644
--- a/backend/Utils/InterpolationSearch.cs
+++ b/backend/Utils/InterpolationSearch.cs
@@ -5,12 +5,29 @@ namespace NzbWebDAV.Utils;
public static class InterpolationSearch
{
+ public static Result Find
+ (
+ long searchByte,
+ LongRange indexRangeToSearch,
+ LongRange byteRangeToSearch,
+ Func<int, LongRange> getByteRangeOfGuessedIndex
+ )
+ {
+ return Find(
+ searchByte,
+ indexRangeToSearch,
+ byteRangeToSearch,
+ guess => new ValueTask<LongRange>(getByteRangeOfGuessedIndex(guess)),
+ CancellationToken.None
+ ).GetAwaiter().GetResult();
+ }
+
public static async Task<Result> Find
(
long searchByte,
LongRange indexRangeToSearch,
LongRange byteRangeToSearch,
- Func<int, Task<LongRange>> getByteRangeOfGuessedIndex,
+ Func<int, ValueTask<LongRange>> getByteRangeOfGuessedIndex,
CancellationToken cancellationToken
)
{