Usenet streaming with a WebDAV server and a SABnzbd-compatible API
C#
feat: backup incoming nzbs to configured directory when enabled.
nzbdav-devc2b369229ae7
diff --git a/backend/Api/SabControllers/AddFile/AddFileController.cs b/backend/Api/SabControllers/AddFile/AddFileController.csindex 08d94428..bcf3fce1 100644--- a/backend/Api/SabControllers/AddFile/AddFileController.cs+++ b/backend/Api/SabControllers/AddFile/AddFileController.cs@@ -38,6 +38,16 @@ public class AddFileController(QueueItem? queueItem;try{+ // backup the nzb file if enabled+ if (configManager.IsNzbBackupEnabled())+ {+ var backupLocation = configManager.GetNzbBackupLocation();+ if (backupLocation != null)+ {+ await BackupNzbAsync(id, request.FileName, request.Category, backupLocation);+ }+ }+// compute the total segment bytesawait using var nzbFileStream = BlobStore.ReadBlob(id);var totalSegmentBytes = ComputeTotalSegmentBytes(nzbFileStream);@@ -99,6 +109,39 @@ public class AddFileController(return Ok(await AddFileAsync(request).ConfigureAwait(false));}+ private static async Task BackupNzbAsync(Guid id, string fileName, string category, string backupLocation)+ {+ try+ {+ if (!Directory.Exists(backupLocation))+ Directory.CreateDirectory(backupLocation);++ var destDir = Path.Combine(backupLocation, category);+ if (!Directory.Exists(destDir))+ Directory.CreateDirectory(destDir);++ var baseName = Path.GetFileNameWithoutExtension(fileName);+ var ext = Path.GetExtension(fileName);+ if (string.IsNullOrEmpty(ext)) ext = ".nzb";++ var destPath = Path.Combine(destDir, $"{baseName}{ext}");+ var counter = 2;+ while (System.IO.File.Exists(destPath))+ {+ destPath = Path.Combine(destDir, $"{baseName} ({counter}){ext}");+ counter++;+ }++ await using var src = BlobStore.ReadBlob(id);+ await using var dst = System.IO.File.Create(destPath);+ await src.CopyToAsync(dst);+ }+ catch (Exception e)+ {+ throw new Exception($"Could not save nzb to `{backupLocation}`", e);+ }+ }+private static long ComputeTotalSegmentBytes(Stream stream){long totalBytes = 0;diff --git a/backend/Config/ConfigManager.cs b/backend/Config/ConfigManager.csindex fd98775c..4b6dcbfb 100644--- a/backend/Config/ConfigManager.cs+++ b/backend/Config/ConfigManager.cs@@ -276,6 +276,18 @@ public class ConfigManagerreturn (configValue != null ? bool.Parse(configValue) : defaultValue);}+ public bool IsNzbBackupEnabled()+ {+ var defaultValue = false;+ var configValue = StringUtil.EmptyToNull(GetConfigValue("api.nzb-backup-enabled"));+ return (configValue != null ? bool.Parse(configValue) : defaultValue);+ }++ public string? GetNzbBackupLocation()+ {+ return StringUtil.EmptyToNull(GetConfigValue("api.nzb-backup-location"));+ }+public class ConfigEventArgs : EventArgs{public required Dictionary<string, string> ChangedConfig { get; init; }