Skip to content

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

C#

Refactored `Environment.GetEnvironmentVariable` -> `EnvironmentUtil.GetEnvironmentVariable`.

nzbdav-devae0dfe32a493
diff --git a/backend/Api/Controllers/GetDatabaseBackup/GetDatabaseBackupController.cs b/backend/Api/Controllers/GetDatabaseBackup/GetDatabaseBackupController.cs
index 4ee07120..79531db3 100644
--- a/backend/Api/Controllers/GetDatabaseBackup/GetDatabaseBackupController.cs
+++ b/backend/Api/Controllers/GetDatabaseBackup/GetDatabaseBackupController.cs
@@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Mvc;
using NzbWebDAV.Database;
+using NzbWebDAV.Utils;
namespace NzbWebDAV.Api.Controllers.GetDatabaseBackup;
@@ -11,7 +12,7 @@ public class GetDatabaseBackupController() : BaseApiController
{
// This endpoint allows downloading a backup of the database.
// It is disabled by default and can only be enabled by the env variable below.
- if (Environment.GetEnvironmentVariable("DANGEROUS_ENABLE_DATABASE_DOWNLOAD_ENDPOINT") != "true")
+ if (!EnvironmentUtil.IsVariableTrue("DANGEROUS_ENABLE_DATABASE_DOWNLOAD_ENDPOINT"))
return Forbid("This endpoint is not enabled.");
var filepath = DavDatabaseContext.DatabaseFilePath;
diff --git a/backend/Config/ConfigManager.cs b/backend/Config/ConfigManager.cs
index 66155d7a..d9af25de 100644
--- a/backend/Config/ConfigManager.cs
+++ b/backend/Config/ConfigManager.cs
@@ -57,7 +57,7 @@ public class ConfigManager
public string GetRcloneMountDir()
{
var mountDir = StringUtil.EmptyToNull(GetConfigValue("rclone.mount-dir"))
- ?? StringUtil.EmptyToNull(Environment.GetEnvironmentVariable("MOUNT_DIR"))
+ ?? EnvironmentUtil.GetEnvironmentVariable("MOUNT_DIR")
?? "/mnt/nzbdav";
if (mountDir.EndsWith('/')) mountDir = mountDir.TrimEnd('/');
return mountDir;
@@ -78,7 +78,7 @@ public class ConfigManager
public string GetApiCategories()
{
return StringUtil.EmptyToNull(GetConfigValue("api.categories"))
- ?? StringUtil.EmptyToNull(Environment.GetEnvironmentVariable("CATEGORIES"))
+ ?? EnvironmentUtil.GetEnvironmentVariable("CATEGORIES")
?? "audio,software,tv,movies";
}
@@ -91,7 +91,7 @@ public class ConfigManager
public string? GetWebdavUser()
{
return StringUtil.EmptyToNull(GetConfigValue("webdav.user"))
- ?? StringUtil.EmptyToNull(Environment.GetEnvironmentVariable("WEBDAV_USER"))
+ ?? EnvironmentUtil.GetEnvironmentVariable("WEBDAV_USER")
?? "admin";
}
@@ -99,7 +99,7 @@ public class ConfigManager
{
var hashedPass = StringUtil.EmptyToNull(GetConfigValue("webdav.pass"));
if (hashedPass != null) return hashedPass;
- var pass = Environment.GetEnvironmentVariable("WEBDAV_PASSWORD");
+ var pass = EnvironmentUtil.GetEnvironmentVariable("WEBDAV_PASSWORD");
if (pass != null) return PasswordUtil.Hash(pass);
return null;
}
diff --git a/backend/Database/DavDatabaseContext.cs b/backend/Database/DavDatabaseContext.cs
index b518fe7d..d1af9a82 100644
--- a/backend/Database/DavDatabaseContext.cs
+++ b/backend/Database/DavDatabaseContext.cs
@@ -3,12 +3,13 @@ using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using NzbWebDAV.Database.Interceptors;
using NzbWebDAV.Database.Models;
+using NzbWebDAV.Utils;
namespace NzbWebDAV.Database;
public sealed class DavDatabaseContext() : DbContext(Options.Value)
{
- public static string ConfigPath => Environment.GetEnvironmentVariable("CONFIG_PATH") ?? "/config";
+ public static string ConfigPath => EnvironmentUtil.GetEnvironmentVariable("CONFIG_PATH") ?? "/config";
public static string DatabaseFilePath => Path.Join(ConfigPath, "db.sqlite");
private static readonly Lazy<DbContextOptions<DavDatabaseContext>> Options = new(
diff --git a/backend/Program.cs b/backend/Program.cs
index dd5abaff..0ef38f54 100644
--- a/backend/Program.cs
+++ b/backend/Program.cs
@@ -36,7 +36,7 @@ class Program
// Initialize logger
var defaultLevel = LogEventLevel.Information;
- var envLevel = Environment.GetEnvironmentVariable("LOG_LEVEL");
+ var envLevel = EnvironmentUtil.GetEnvironmentVariable("LOG_LEVEL");
var level = Enum.TryParse<LogEventLevel>(envLevel, true, out var parsed) ? parsed : defaultLevel;
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Is(level)
@@ -57,7 +57,9 @@ class Program
{
var argIndex = args.ToList().IndexOf("--db-migration");
var targetMigration = args.Length > argIndex + 1 ? args[argIndex + 1] : null;
- await databaseContext.Database.MigrateAsync(targetMigration, SigtermUtil.GetCancellationToken()).ConfigureAwait(false);
+ await databaseContext.Database
+ .MigrateAsync(targetMigration, SigtermUtil.GetCancellationToken())
+ .ConfigureAwait(false);
return;
}
diff --git a/backend/Utils/EnvironmentUtil.cs b/backend/Utils/EnvironmentUtil.cs
index 0fc7239e..f8810d31 100644
--- a/backend/Utils/EnvironmentUtil.cs
+++ b/backend/Utils/EnvironmentUtil.cs
@@ -2,6 +2,11 @@
public static class EnvironmentUtil
{
+ public static string? GetEnvironmentVariable(string envVariable)
+ {
+ return StringUtil.EmptyToNull(Environment.GetEnvironmentVariable(envVariable));
+ }
+
public static string GetRequiredVariable(string envVariable)
{
return Environment.GetEnvironmentVariable(envVariable) ??
diff --git a/backend/Utils/StringUtil.cs b/backend/Utils/StringUtil.cs
index f2c5fece..cafecdbb 100644
--- a/backend/Utils/StringUtil.cs
+++ b/backend/Utils/StringUtil.cs
@@ -4,6 +4,6 @@ public static class StringUtil
{
public static string? EmptyToNull(string? value)
{
- return value == "" ? null : value;
+ return string.IsNullOrWhiteSpace(value) ? null : value;
}
}
\ No newline at end of file