Skip to content

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

C#

Added support for file filtering with wildcard patterns.

nzbdav-devb1796930b268
diff --git a/backend/Config/ConfigManager.cs b/backend/Config/ConfigManager.cs
index 6888e485..2c3c3ce9 100644
--- a/backend/Config/ConfigManager.cs
+++ b/backend/Config/ConfigManager.cs
@@ -204,10 +204,10 @@ public class ConfigManager
return GetConfigValue("api.duplicate-nzb-behavior") ?? defaultValue;
}
- public HashSet<string> GetBlacklistedExtensions()
+ public HashSet<string> GetBlocklistedFiles()
{
- var defaultValue = ".nfo, .par2, .sfv";
- return (GetConfigValue("api.download-extension-blacklist") ?? defaultValue)
+ var defaultValue = "*.nfo, *.par2, *.sfv, *sample.mkv";
+ return (GetConfigValue("api.download-file-blocklist") ?? defaultValue)
.Split(',', StringSplitOptions.RemoveEmptyEntries)
.Select(x => x.Trim())
.Where(x => !string.IsNullOrWhiteSpace(x))
diff --git a/backend/Queue/PostProcessors/BlacklistedExtensionPostProcessor.cs b/backend/Queue/PostProcessors/BlocklistedFilePostProcessor.cs
similarity index 53%
rename from backend/Queue/PostProcessors/BlacklistedExtensionPostProcessor.cs
rename to backend/Queue/PostProcessors/BlocklistedFilePostProcessor.cs
index ddb8c997..49f2a29a 100644
--- a/backend/Queue/PostProcessors/BlacklistedExtensionPostProcessor.cs
+++ b/backend/Queue/PostProcessors/BlocklistedFilePostProcessor.cs
@@ -1,4 +1,5 @@
-using Microsoft.EntityFrameworkCore;
+using System.Text.RegularExpressions;
+using Microsoft.EntityFrameworkCore;
using NzbWebDAV.Config;
using NzbWebDAV.Database;
using NzbWebDAV.Database.Models;
@@ -6,22 +7,37 @@ using Serilog;
namespace NzbWebDAV.Queue.PostProcessors;
-public class BlacklistedExtensionPostProcessor(ConfigManager configManager, DavDatabaseClient dbClient)
+public class BlocklistedFilePostProcessor(ConfigManager configManager, DavDatabaseClient dbClient)
{
- public void RemoveBlacklistedExtensions()
+ public void RemoveBlocklistedFiles()
{
- var blacklistedExtensions = configManager.GetBlacklistedExtensions();
- var blacklistedFiles = dbClient.Ctx.ChangeTracker.Entries<DavItem>()
+ var blocklistPatterns = configManager.GetBlocklistedFiles();
+ var blocklistedFiles = dbClient.Ctx.ChangeTracker.Entries<DavItem>()
.Where(x => x.State == EntityState.Added)
.Select(x => x.Entity)
.Where(x => x.Type != DavItem.ItemType.Directory)
- .Where(x => blacklistedExtensions.Contains(Path.GetExtension(x.Name).ToLower()));
+ .Where(x => MatchesAnyPattern(x.Name, blocklistPatterns));
- foreach (var blacklistedFile in blacklistedFiles)
- RemoveBlacklistedFile(blacklistedFile);
+ foreach (var blocklistedFile in blocklistedFiles)
+ RemoveBlocklistedFile(blocklistedFile);
}
- private void RemoveBlacklistedFile(DavItem davItem)
+ public static bool MatchesAnyPattern(string fileName, HashSet<string> patterns)
+ {
+ var lowerFileName = fileName.ToLower();
+ return patterns.Any(pattern => MatchesPattern(lowerFileName, pattern));
+ }
+
+ private static bool MatchesPattern(string fileName, string pattern)
+ {
+ // Convert pattern to regex:
+ // 1. Escape all regex special characters (this escapes * to \*)
+ // 2. Replace \* with .* to support greedy wildcard matching
+ var regexPattern = Regex.Escape(pattern).Replace("\\*", ".*");
+ return Regex.IsMatch(fileName, $"^{regexPattern}$");
+ }
+
+ private void RemoveBlocklistedFile(DavItem davItem)
{
if (davItem.Type == DavItem.ItemType.NzbFile)
{
@@ -52,10 +68,10 @@ public class BlacklistedExtensionPostProcessor(ConfigManager configManager, DavD
else
{
- Log.Error("Error filtering blacklisted extensions from downloading.");
+ Log.Error("Error filtering blocklisted files.");
return;
}
dbClient.Ctx.Items.Remove(davItem);
}
-}
\ No newline at end of file
+}
diff --git a/backend/Queue/QueueItemProcessor.cs b/backend/Queue/QueueItemProcessor.cs
index e17ad192..03990df7 100644
--- a/backend/Queue/QueueItemProcessor.cs
+++ b/backend/Queue/QueueItemProcessor.cs
@@ -179,7 +179,7 @@ public class QueueItemProcessor(
// post-processing
new RenameDuplicatesPostProcessor(dbClient).RenameDuplicates();
- new BlacklistedExtensionPostProcessor(configManager, dbClient).RemoveBlacklistedExtensions();
+ new BlocklistedFilePostProcessor(configManager, dbClient).RemoveBlocklistedFiles();
// validate video files found
if (configManager.IsEnsureImportableVideoEnabled())
diff --git a/backend/Services/HealthCheckService.cs b/backend/Services/HealthCheckService.cs
index bb2f34e8..fc640e52 100644
--- a/backend/Services/HealthCheckService.cs
+++ b/backend/Services/HealthCheckService.cs
@@ -6,6 +6,7 @@ using NzbWebDAV.Database;
using NzbWebDAV.Database.Models;
using NzbWebDAV.Exceptions;
using NzbWebDAV.Extensions;
+using NzbWebDAV.Queue.PostProcessors;
using NzbWebDAV.Utils;
using NzbWebDAV.Websocket;
using Serilog;
@@ -199,10 +200,10 @@ public class HealthCheckService : BackgroundService
{
try
{
- // if the file extension has been marked as ignored,
+ // if the file pattern has been marked as ignored,
// then don't bother trying to repair it. We can simply delete it.
- var blacklistedExtensions = _configManager.GetBlacklistedExtensions();
- if (blacklistedExtensions.Contains(Path.GetExtension(davItem.Name).ToLower()))
+ var blocklistedFiles = _configManager.GetBlocklistedFiles();
+ if (BlocklistedFilePostProcessor.MatchesAnyPattern(davItem.Name, blocklistedFiles))
{
dbClient.Ctx.Items.Remove(davItem);
dbClient.Ctx.HealthCheckResults.Add(SendStatus(new HealthCheckResult()
@@ -215,7 +216,7 @@ public class HealthCheckService : BackgroundService
RepairStatus = HealthCheckResult.RepairAction.Deleted,
Message = string.Join(" ", [
"File had missing articles.",
- "File extension is marked in settings as ignored (unwanted) file type.",
+ "Filename pattern is marked in settings as an ignored (unwanted) file.",
"Deleted file."
])
}));