Skip to content

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

C#

Backend: Extract archive password from NZB filenames (#215)

Root-Coree3904efade99
diff --git a/backend/Api/SabControllers/AddFile/AddFileController.cs b/backend/Api/SabControllers/AddFile/AddFileController.cs
index 5e8625e5..b4a4753c 100644
--- a/backend/Api/SabControllers/AddFile/AddFileController.cs
+++ b/backend/Api/SabControllers/AddFile/AddFileController.cs
@@ -7,6 +7,7 @@ using NzbWebDAV.Database;
using NzbWebDAV.Database.Models;
using NzbWebDAV.Extensions;
using NzbWebDAV.Queue;
+using NzbWebDAV.Utils;
using NzbWebDAV.Websocket;
using Usenet.Nzb;
@@ -34,7 +35,7 @@ public class AddFileController(
Id = Guid.NewGuid(),
CreatedAt = DateTime.Now,
FileName = request.FileName,
- JobName = Path.GetFileNameWithoutExtension(request.FileName),
+ JobName = FilenameUtil.GetJobName(request.FileName),
NzbFileSize = documentBytes.Length,
TotalSegmentBytes = document.Files.SelectMany(x => x.Segments).Select(x => x.Size).Sum(),
Category = request.Category,
diff --git a/backend/Queue/QueueItemProcessor.cs b/backend/Queue/QueueItemProcessor.cs
index c9610686..6476b3a3 100644
--- a/backend/Queue/QueueItemProcessor.cs
+++ b/backend/Queue/QueueItemProcessor.cs
@@ -111,9 +111,13 @@ public class QueueItemProcessor(
var documentBytes = Encoding.UTF8.GetBytes(queueNzbContents.NzbContents);
using var stream = new MemoryStream(documentBytes);
var nzb = await NzbDocument.LoadAsync(stream).ConfigureAwait(false);
- var archivePassword = nzb.MetaData.GetValueOrDefault("password")?.FirstOrDefault();
var nzbFiles = nzb.Files.Where(x => x.Segments.Count > 0).ToList();
+ // Look for a password in filename and nzb document
+ // The file name's password takes priority, as an easy override
+ var archivePassword = FilenameUtil.GetNzbPassword(queueItem.FileName) ??
+ nzb.MetaData.GetValueOrDefault("password")?.FirstOrDefault();
+
// step 0 -- perform article existence pre-check against cache
// https://github.com/nzbdav-dev/nzbdav/issues/101
var articlesToPrecheck = nzbFiles.SelectMany(x => x.Segments).Select(x => x.MessageId.Value);
diff --git a/backend/Utils/FilenameUtil.cs b/backend/Utils/FilenameUtil.cs
index e8896897..6f847537 100644
--- a/backend/Utils/FilenameUtil.cs
+++ b/backend/Utils/FilenameUtil.cs
@@ -2,8 +2,14 @@
namespace NzbWebDAV.Utils;
-public class FilenameUtil
+public partial class FilenameUtil
{
+ // Group `pw` contains the password,
+ // Group `rm` contains the part of the filename that should be removed to create a clean job name
+ // Group `br` ensures that the brackets are closed / is used for back reference
+ [GeneratedRegex(@"(?<rm>[\s-]*(?:(?<br>{{)|password=)(?<pw>\w+)(?(br)}}))\.nzb$", RegexOptions.IgnoreCase)]
+ public static partial Regex PasswordRegex { get; }
+
private static readonly HashSet<string> VideoExtensions =
[
".webm", ".m4v", ".3gp", ".nsv", ".ty", ".strm", ".rm", ".rmvb", ".m3u", ".ifo", ".mov", ".qt", ".divx",
@@ -43,4 +49,20 @@ public class FilenameUtil
if (string.IsNullOrEmpty(filename)) return false;
return Regex.IsMatch(filename, @"\.mkv\.(\d+)?$", RegexOptions.IgnoreCase);
}
+
+ public static string GetJobName(string filename)
+ {
+ var passMatch = PasswordRegex.Match(filename);
+ return Path.GetFileNameWithoutExtension(
+ passMatch.Success ?
+ filename.Replace(passMatch.Groups["rm"].Value, "") :
+ filename
+ );
+ }
+
+ public static string? GetNzbPassword(string filename)
+ {
+ var passMatch = PasswordRegex.Match(filename);
+ return passMatch.Success ? passMatch.Groups["pw"].Value : null;
+ }
}
\ No newline at end of file