Skip to content

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

C#

Add support for multipart-mkv files.

nzbdav-dev162c3099161b
diff --git a/backend/Queue/FileAggregators/MultipartMkvAggregator.cs b/backend/Queue/FileAggregators/MultipartMkvAggregator.cs
new file mode 100644
index 00000000..47371276
--- /dev/null
+++ b/backend/Queue/FileAggregators/MultipartMkvAggregator.cs
@@ -0,0 +1,48 @@
+using NzbWebDAV.Database;
+using NzbWebDAV.Database.Models;
+using NzbWebDAV.Queue.FileProcessors;
+using NzbWebDAV.Utils;
+
+namespace NzbWebDAV.Queue.FileAggregators;
+
+public class MultipartMkvAggregator(DavDatabaseClient dbClient, DavItem mountDirectory) : BaseAggregator
+{
+ protected override DavDatabaseClient DBClient => dbClient;
+ protected override DavItem MountDirectory => mountDirectory;
+
+ public override void UpdateDatabase(List<BaseProcessor.Result> processorResults)
+ {
+ var multipartMkvFiles = processorResults
+ .OfType<MultipartMkvProcessor.Result>()
+ .ToList();
+
+ ProcessMultipartMkvFiles(multipartMkvFiles);
+ }
+
+ private void ProcessMultipartMkvFiles(List<MultipartMkvProcessor.Result> multipartMkvFiles)
+ {
+ foreach (var multipartMkvFile in multipartMkvFiles)
+ {
+ var fileParts = multipartMkvFile.Parts;
+ var parentDirectory = MountDirectory;
+ var name = multipartMkvFile.Filename;
+
+ var davItem = DavItem.New(
+ id: Guid.NewGuid(),
+ parent: parentDirectory,
+ name: name,
+ fileSize: fileParts.Sum(x => x.ByteCount),
+ type: DavItem.ItemType.RarFile
+ );
+
+ var davRarFile = new DavRarFile()
+ {
+ Id = davItem.Id,
+ RarParts = fileParts.ToArray(),
+ };
+
+ dbClient.Ctx.Items.Add(davItem);
+ dbClient.Ctx.RarFiles.Add(davRarFile);
+ }
+ }
+}
\ No newline at end of file
diff --git a/backend/Queue/FileProcessors/MultipartMkvProcessor.cs b/backend/Queue/FileProcessors/MultipartMkvProcessor.cs
new file mode 100644
index 00000000..b7d8ca5a
--- /dev/null
+++ b/backend/Queue/FileProcessors/MultipartMkvProcessor.cs
@@ -0,0 +1,67 @@
+using System.Text.RegularExpressions;
+using NzbWebDAV.Clients;
+using NzbWebDAV.Database.Models;
+using NzbWebDAV.Extensions;
+using NzbWebDAV.Queue.DeobfuscationSteps._3.GetFileInfos;
+
+namespace NzbWebDAV.Queue.FileProcessors;
+
+public class MultipartMkvProcessor : BaseProcessor
+{
+ private readonly List<GetFileInfosStep.FileInfo> _fileInfos;
+ private readonly UsenetStreamingClient _client;
+ private readonly CancellationToken _ct;
+
+ public MultipartMkvProcessor
+ (
+ List<GetFileInfosStep.FileInfo> fileInfos,
+ UsenetStreamingClient client,
+ CancellationToken ct
+ )
+ {
+ _fileInfos = fileInfos;
+ _client = client;
+ _ct = ct;
+ }
+
+ public override async Task<BaseProcessor.Result?> ProcessAsync()
+ {
+ var sortedFileInfos = _fileInfos.OrderBy(f => GetPartNumber(f.FileName)).ToList();
+ var fileParts = new List<DavRarFile.RarPart>();
+ foreach (var fileInfo in sortedFileInfos)
+ {
+ var partSize = fileInfo.FileSize ?? await _client.GetFileSizeAsync(fileInfo.NzbFile, _ct);
+ fileParts.Add(new DavRarFile.RarPart
+ {
+ SegmentIds = fileInfo.NzbFile.GetSegmentIds(),
+ PartSize = partSize,
+ Offset = 0,
+ ByteCount = partSize
+ });
+ }
+
+ return new Result
+ {
+ Filename = GetBaseName(sortedFileInfos.First().FileName),
+ Parts = fileParts
+ };
+ }
+
+ private static string GetBaseName(string filename)
+ {
+ var extensionIndex = filename.LastIndexOf(".mkv", StringComparison.Ordinal);
+ return filename[..(extensionIndex + 4)];
+ }
+
+ private static int GetPartNumber(string filename)
+ {
+ var match = Regex.Match(filename, @"\.mkv\.(\d+)?$", RegexOptions.IgnoreCase);
+ return string.IsNullOrEmpty(match.Groups[1].Value) ? -1 : int.Parse(match.Groups[1].Value);
+ }
+
+ public new class Result : BaseProcessor.Result
+ {
+ public required string Filename { 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 2b2b1d41..daac9b54 100644
--- a/backend/Queue/QueueItemProcessor.cs
+++ b/backend/Queue/QueueItemProcessor.cs
@@ -145,6 +145,7 @@ public class QueueItemProcessor(
new RarAggregator(dbClient, mountFolder).UpdateDatabase(fileProcessingResults);
new FileAggregator(dbClient, mountFolder).UpdateDatabase(fileProcessingResults);
new SevenZipAggregator(dbClient, mountFolder).UpdateDatabase(fileProcessingResults);
+ new MultipartMkvAggregator(dbClient, mountFolder).UpdateDatabase(fileProcessingResults);
// validate video files found
if (configManager.IsEnsureImportableVideoEnabled())
@@ -169,6 +170,9 @@ public class QueueItemProcessor(
foreach (var fileInfo in group)
yield return new RarProcessor(fileInfo, usenetClient, ct);
+ else if (group.Key == "multipart-mkv")
+ yield return new MultipartMkvProcessor(group.ToList(), usenetClient, ct);
+
else if (group.Key == "other")
foreach (var fileInfo in group)
yield return new FileProcessor(fileInfo, usenetClient, ct);
@@ -179,6 +183,7 @@ public class QueueItemProcessor(
string GetGroup(string x) => false ? "impossible"
: FilenameUtil.Is7zFile(x) ? "7z"
: FilenameUtil.IsRarFile(x) ? "rar"
+ : FilenameUtil.IsMultipartMkv(x) ? "multipart-mkv"
: "other";
}
diff --git a/backend/Utils/FilenameUtil.cs b/backend/Utils/FilenameUtil.cs
index 96e05b6d..dac049f6 100644
--- a/backend/Utils/FilenameUtil.cs
+++ b/backend/Utils/FilenameUtil.cs
@@ -29,4 +29,10 @@ public class FilenameUtil
if (string.IsNullOrEmpty(filename)) return false;
return Regex.IsMatch(filename, @"\.7z(\.(\d+))?$", RegexOptions.IgnoreCase);
}
+
+ public static bool IsMultipartMkv(string? filename)
+ {
+ if (string.IsNullOrEmpty(filename)) return false;
+ return Regex.IsMatch(filename, @"\.mkv\.(\d+)?$", RegexOptions.IgnoreCase);
+ }
}
\ No newline at end of file