From 4d3ae0e19ae34075e5f029d52a23580de5fa77f4 Mon Sep 17 00:00:00 2001 From: whimsical-c4lic0 Date: Thu, 19 Feb 2026 17:01:15 -0600 Subject: [PATCH] Move OS and system environment checks to a helper class --- AGENTS.md | 3 ++- OF DL.Core/Helpers/EnvironmentHelper.cs | 14 ++++++++++++++ OF DL.Core/Services/AuthService.cs | 3 ++- OF DL.Core/Services/DownloadService.cs | 4 ++-- OF DL.Core/Services/StartupService.cs | 11 +++++------ OF DL.Gui/Program.cs | 9 +++++---- OF DL.Gui/Views/FaqWindow.axaml.cs | 8 +++----- OF DL.Gui/Views/MainWindow.axaml.cs | 4 ++-- OF DL/Program.cs | 8 ++++---- 9 files changed, 39 insertions(+), 25 deletions(-) create mode 100644 OF DL.Core/Helpers/EnvironmentHelper.cs diff --git a/AGENTS.md b/AGENTS.md index c4f4876..6e21e46 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -29,7 +29,7 @@ most important change points. entities, and mapping helpers. - `OF DL.Core/Widevine/` implements Widevine CDM handling and key derivation. - `OF DL.Core/Helpers/`, `OF DL.Core/Utils/`, `OF DL.Core/Crypto/`, `OF DL.Core/Enumerations/` contain shared core - logic. + logic. `OF DL.Core/Helpers/EnvironmentHelper.cs` centralizes environment checks (Docker and Windows). - `docs/` and `mkdocs.yml` define the documentation site. - `site/` is generated MkDocs output and should not be edited by hand. - `docker/` contains container entrypoint and supervisor configuration. @@ -239,6 +239,7 @@ cookies/user-agent. Output is written to `{filename}_source.mp4`, then moved and - `OF DL.Core/Widevine/` for CDM key generation and license parsing. - `OF DL.Core/Models/Config/Config.cs` and `OF DL.Core/Services/ConfigService.cs` for config shape and parsing. - `OF DL.Core/Services/AuthService.cs` for user-facing authentication behavior and browser login flow. +- `OF DL.Core/Helpers/EnvironmentHelper.cs` for shared Docker/Windows runtime checks. - `docs/` for public documentation; update docs whenever user-facing behavior or configuration changes. ## Documentation updates for common changes: diff --git a/OF DL.Core/Helpers/EnvironmentHelper.cs b/OF DL.Core/Helpers/EnvironmentHelper.cs new file mode 100644 index 0000000..42abf47 --- /dev/null +++ b/OF DL.Core/Helpers/EnvironmentHelper.cs @@ -0,0 +1,14 @@ +namespace OF_DL.Helpers; + +public static class EnvironmentHelper +{ + private const string DockerEnvironmentVariableName = "OFDL_DOCKER"; + + public static bool IsRunningInDocker() + { + string? dockerValue = Environment.GetEnvironmentVariable(DockerEnvironmentVariableName); + return string.Equals(dockerValue, "true", StringComparison.OrdinalIgnoreCase); + } + + public static bool IsRunningOnWindows() => OperatingSystem.IsWindows(); +} diff --git a/OF DL.Core/Services/AuthService.cs b/OF DL.Core/Services/AuthService.cs index 5851fe1..d235da5 100644 --- a/OF DL.Core/Services/AuthService.cs +++ b/OF DL.Core/Services/AuthService.cs @@ -2,6 +2,7 @@ using System.Text.RegularExpressions; using Microsoft.Extensions.DependencyInjection; using Microsoft.Playwright; using Newtonsoft.Json; +using OF_DL.Helpers; using OF_DL.Models; using Serilog; using UserEntities = OF_DL.Models.Entities.Users; @@ -79,7 +80,7 @@ public class AuthService(IServiceProvider serviceProvider) : IAuthService try { - bool runningInDocker = Environment.GetEnvironmentVariable("OFDL_DOCKER") != null; + bool runningInDocker = EnvironmentHelper.IsRunningInDocker(); await SetupBrowser(runningInDocker); } catch (Exception ex) diff --git a/OF DL.Core/Services/DownloadService.cs b/OF DL.Core/Services/DownloadService.cs index c47f6b1..2a9dfcc 100644 --- a/OF DL.Core/Services/DownloadService.cs +++ b/OF DL.Core/Services/DownloadService.cs @@ -412,14 +412,14 @@ public class DownloadService( { string firstFullPath = Path.GetFullPath(firstPath); string secondFullPath = Path.GetFullPath(secondPath); - StringComparison comparison = OperatingSystem.IsWindows() + StringComparison comparison = EnvironmentHelper.IsRunningOnWindows() ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal; return string.Equals(firstFullPath, secondFullPath, comparison); } catch { - StringComparison comparison = OperatingSystem.IsWindows() + StringComparison comparison = EnvironmentHelper.IsRunningOnWindows() ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal; return string.Equals(firstPath, secondPath, comparison); diff --git a/OF DL.Core/Services/StartupService.cs b/OF DL.Core/Services/StartupService.cs index 04f8c46..881493f 100644 --- a/OF DL.Core/Services/StartupService.cs +++ b/OF DL.Core/Services/StartupService.cs @@ -1,6 +1,5 @@ using System.Diagnostics; using System.Reflection; -using System.Runtime.InteropServices; using Newtonsoft.Json; using OF_DL.Helpers; using OF_DL.Models; @@ -24,9 +23,9 @@ public class StartupService(IConfigService configService, IAuthService authServi // OS validation OperatingSystem os = Environment.OSVersion; result.OsVersionString = os.VersionString; - Log.Debug($"Operating system information: {os.VersionString}"); + Log.Debug("Operating system information: {OsVersionString}", os.VersionString); - if (os.Platform == PlatformID.Win32NT && os.Version.Major < 10) + if (EnvironmentHelper.IsRunningOnWindows() && os.Version.Major < 10) { result.IsWindowsVersionValid = false; Log.Error("Windows version prior to 10.x: {0}", os.VersionString); @@ -40,7 +39,7 @@ public class StartupService(IConfigService configService, IAuthService authServi if (result is { FfmpegFound: true, FfmpegPath: not null }) { // Escape backslashes for Windows - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && + if (EnvironmentHelper.IsRunningOnWindows() && result.FfmpegPath.Contains(@":\") && !result.FfmpegPath.Contains(@":\\")) { @@ -55,7 +54,7 @@ public class StartupService(IConfigService configService, IAuthService authServi if (result is { FfprobeFound: true, FfprobePath: not null }) { // Escape backslashes for Windows - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && + if (EnvironmentHelper.IsRunningOnWindows() && result.FfprobePath.Contains(@":\") && !result.FfprobePath.Contains(@":\\")) { @@ -211,7 +210,7 @@ public class StartupService(IConfigService configService, IAuthService authServi string? ffmpegDirectory = Path.GetDirectoryName(result.FfmpegPath); if (!string.IsNullOrEmpty(ffmpegDirectory)) { - string ffprobeFileName = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) + string ffprobeFileName = EnvironmentHelper.IsRunningOnWindows() ? "ffprobe.exe" : "ffprobe"; string inferredFfprobePath = Path.Combine(ffmpegDirectory, ffprobeFileName); diff --git a/OF DL.Gui/Program.cs b/OF DL.Gui/Program.cs index 39d54fe..599a7b9 100644 --- a/OF DL.Gui/Program.cs +++ b/OF DL.Gui/Program.cs @@ -1,5 +1,6 @@ using Avalonia; using Microsoft.Extensions.DependencyInjection; +using OF_DL.Helpers; using OF_DL.Services; using Serilog; @@ -21,11 +22,11 @@ public static class Program Log.Information("Starting OF DL GUI"); - // Check if running in Docker and print message - string? ofdlDocker = Environment.GetEnvironmentVariable("OFDL_DOCKER"); - if (string.Equals(ofdlDocker, "true", StringComparison.OrdinalIgnoreCase)) + // Check if running in Docker and print a message + if (EnvironmentHelper.IsRunningInDocker()) { - Console.WriteLine("In your web browser, navigate to the port forwarded from your docker container. For instance, if your docker run command included \"-p 8080:8080\", open your web browser to \"http://localhost:8080\"."); + Console.WriteLine( + "In your web browser, navigate to the port forwarded from your docker container. For instance, if your docker run command included \"-p 8080:8080\", open your web browser to \"http://localhost:8080\"."); } BuildAvaloniaApp() diff --git a/OF DL.Gui/Views/FaqWindow.axaml.cs b/OF DL.Gui/Views/FaqWindow.axaml.cs index 221a1d4..5696c75 100644 --- a/OF DL.Gui/Views/FaqWindow.axaml.cs +++ b/OF DL.Gui/Views/FaqWindow.axaml.cs @@ -3,6 +3,7 @@ using System.Diagnostics; using Avalonia.Controls; using Avalonia.Interactivity; using Avalonia.Platform; +using OF_DL.Helpers; namespace OF_DL.Gui.Views; @@ -65,11 +66,8 @@ public partial class FaqWindow : Window { Entries.Clear(); - bool isDocker = string.Equals( - Environment.GetEnvironmentVariable("OFDL_DOCKER"), - "true", - StringComparison.OrdinalIgnoreCase); - bool isWindows = OperatingSystem.IsWindows(); + bool isDocker = EnvironmentHelper.IsRunningInDocker(); + bool isWindows = EnvironmentHelper.IsRunningOnWindows(); Entries.Add(new FaqEntry( "Why are some users missing from the users list?", diff --git a/OF DL.Gui/Views/MainWindow.axaml.cs b/OF DL.Gui/Views/MainWindow.axaml.cs index 90e5936..f51b0a4 100644 --- a/OF DL.Gui/Views/MainWindow.axaml.cs +++ b/OF DL.Gui/Views/MainWindow.axaml.cs @@ -7,6 +7,7 @@ using Avalonia.Platform; using Avalonia.Platform.Storage; using Avalonia.VisualTree; using Avalonia.Threading; +using OF_DL.Helpers; using OF_DL.Gui.ViewModels; namespace OF_DL.Gui.Views; @@ -28,8 +29,7 @@ public partial class MainWindow : Window Icon = new WindowIcon(AssetLoader.Open(new Uri("avares://OF DL.Gui/Assets/icon.ico"))); // Start maximized if running in Docker - string? ofdlDocker = Environment.GetEnvironmentVariable("OFDL_DOCKER"); - if (string.Equals(ofdlDocker, "true", StringComparison.OrdinalIgnoreCase)) + if (EnvironmentHelper.IsRunningInDocker()) { WindowState = WindowState.Maximized; } diff --git a/OF DL/Program.cs b/OF DL/Program.cs index a487dfe..476a625 100644 --- a/OF DL/Program.cs +++ b/OF DL/Program.cs @@ -3,6 +3,7 @@ using Microsoft.Extensions.DependencyInjection; using OF_DL.CLI; using OF_DL.Models; using OF_DL.Enumerations; +using OF_DL.Helpers; using OF_DL.Models.Config; using OF_DL.Models.Downloads; using OF_DL.Models.Entities.Users; @@ -17,14 +18,13 @@ public class Program(IServiceProvider serviceProvider) private async Task LoadAuthFromBrowser() { IAuthService authService = serviceProvider.GetRequiredService(); - bool runningInDocker = Environment.GetEnvironmentVariable("OFDL_DOCKER") != null; // Show the initial message AnsiConsole.MarkupLine("[yellow]Downloading dependencies. Please wait ...[/]"); // Show instructions based on the environment await Task.Delay(5000); - if (runningInDocker) + if (EnvironmentHelper.IsRunningInDocker()) { AnsiConsole.MarkupLine( "[yellow]In your web browser, navigate to the port forwarded from your docker container.[/]"); @@ -846,8 +846,8 @@ public class Program(IServiceProvider serviceProvider) private static void DisplayStartupResult(StartupResult result) { // OS - if (result.IsWindowsVersionValid && result.OsVersionString != null && - Environment.OSVersion.Platform == PlatformID.Win32NT) + if (result is { IsWindowsVersionValid: true, OsVersionString: not null } && + EnvironmentHelper.IsRunningOnWindows()) { AnsiConsole.Markup("[green]Valid version of Windows found.\n[/]"); }