From 0a07804833dee743ba0ab1b1210265a0165e26f0 Mon Sep 17 00:00:00 2001 From: Josh Patra Date: Mon, 9 Feb 2026 12:29:57 -0500 Subject: [PATCH] feat: standardize download path configuration and auto-migrate .env - Change DOWNLOAD_PATH to Library__DownloadPath (ASP.NET Core standard) - Add EnvMigrationService to automatically update old .env files on startup - Update .env.example with new variable name - Ensures cache cleanup and downloads use consistent path configuration - No manual intervention needed - migration happens automatically --- .env.example | 2 +- allstarr/Program.cs | 11 ++++ .../Services/Common/CacheCleanupService.cs | 2 +- .../Services/Common/EnvMigrationService.cs | 59 +++++++++++++++++++ .../Services/Local/LocalLibraryService.cs | 32 +++++----- 5 files changed, 88 insertions(+), 18 deletions(-) create mode 100644 allstarr/Services/Common/EnvMigrationService.cs diff --git a/.env.example b/.env.example index de90e3a..dc1b120 100644 --- a/.env.example +++ b/.env.example @@ -40,7 +40,7 @@ MUSIC_SERVICE=SquidWTF # - downloads/permanent/ - Permanently downloaded tracks (STORAGE_MODE=Permanent) # - downloads/cache/ - Temporarily cached tracks (STORAGE_MODE=Cache) # - downloads/kept/ - Favorited external tracks (always permanent) -DOWNLOAD_PATH=./downloads +Library__DownloadPath=./downloads # ===== SQUIDWTF CONFIGURATION ===== # Different quality options for SquidWTF. Only FLAC supported right now diff --git a/allstarr/Program.cs b/allstarr/Program.cs index 1e97e68..70b47ed 100644 --- a/allstarr/Program.cs +++ b/allstarr/Program.cs @@ -626,6 +626,17 @@ builder.Services.AddCors(options => var app = builder.Build(); +// Migrate old .env file format on startup +try +{ + var migrationService = new EnvMigrationService(app.Services.GetRequiredService>()); + migrationService.MigrateEnvFile(); +} +catch (Exception ex) +{ + app.Logger.LogWarning(ex, "Failed to run .env migration"); +} + // Configure the HTTP request pipeline. app.UseExceptionHandler(_ => { }); // Global exception handler diff --git a/allstarr/Services/Common/CacheCleanupService.cs b/allstarr/Services/Common/CacheCleanupService.cs index 0676009..f60922e 100644 --- a/allstarr/Services/Common/CacheCleanupService.cs +++ b/allstarr/Services/Common/CacheCleanupService.cs @@ -67,7 +67,7 @@ public class CacheCleanupService : BackgroundService private async Task CleanupOldCachedFilesAsync(CancellationToken cancellationToken) { // Get the actual cache path used by download services - var downloadPath = _configuration["DOWNLOAD_PATH"] ?? "downloads"; + var downloadPath = _configuration["Library:DownloadPath"] ?? "downloads"; var cachePath = Path.Combine(downloadPath, "cache"); if (!Directory.Exists(cachePath)) diff --git a/allstarr/Services/Common/EnvMigrationService.cs b/allstarr/Services/Common/EnvMigrationService.cs new file mode 100644 index 0000000..a959f0a --- /dev/null +++ b/allstarr/Services/Common/EnvMigrationService.cs @@ -0,0 +1,59 @@ +namespace allstarr.Services.Common; + +/// +/// Service that runs on startup to migrate old .env file format to new format +/// +public class EnvMigrationService +{ + private readonly ILogger _logger; + private readonly string _envFilePath; + + public EnvMigrationService(ILogger logger) + { + _logger = logger; + _envFilePath = Path.Combine(Directory.GetCurrentDirectory(), ".env"); + } + + public void MigrateEnvFile() + { + if (!File.Exists(_envFilePath)) + { + _logger.LogDebug("No .env file found, skipping migration"); + return; + } + + try + { + var lines = File.ReadAllLines(_envFilePath); + var modified = false; + + for (int i = 0; i < lines.Length; i++) + { + var line = lines[i].Trim(); + + // Skip comments and empty lines + if (string.IsNullOrWhiteSpace(line) || line.StartsWith("#")) + continue; + + // Migrate DOWNLOAD_PATH to Library__DownloadPath + if (line.StartsWith("DOWNLOAD_PATH=")) + { + var value = line.Substring("DOWNLOAD_PATH=".Length); + lines[i] = $"Library__DownloadPath={value}"; + modified = true; + _logger.LogInformation("Migrated DOWNLOAD_PATH to Library__DownloadPath in .env file"); + } + } + + if (modified) + { + File.WriteAllLines(_envFilePath, lines); + _logger.LogInformation("✅ .env file migration completed successfully"); + } + } + catch (Exception ex) + { + _logger.LogWarning(ex, "Failed to migrate .env file - please manually update DOWNLOAD_PATH to Library__DownloadPath"); + } + } +} diff --git a/allstarr/Services/Local/LocalLibraryService.cs b/allstarr/Services/Local/LocalLibraryService.cs index 527a368..ead7837 100644 --- a/allstarr/Services/Local/LocalLibraryService.cs +++ b/allstarr/Services/Local/LocalLibraryService.cs @@ -1,19 +1,19 @@ -using System.Text.Json; -using Microsoft.Extensions.Options; -using allstarr.Models.Domain; -using allstarr.Models.Settings; -using allstarr.Models.Download; -using allstarr.Models.Search; -using allstarr.Models.Subsonic; -using allstarr.Services; - -namespace allstarr.Services.Local; - -/// -/// Local library service implementation -/// Uses a simple JSON file to store mappings (can be replaced with a database) -/// -public class LocalLibraryService : ILocalLibraryService +using System.Text.Json; +using Microsoft.Extensions.Options; +using allstarr.Models.Domain; +using allstarr.Models.Settings; +using allstarr.Models.Download; +using allstarr.Models.Search; +using allstarr.Models.Subsonic; +using allstarr.Services; + +namespace allstarr.Services.Local; + +/// +/// Local library service implementation +/// Uses a simple JSON file to store mappings (can be replaced with a database) +/// +public class LocalLibraryService : ILocalLibraryService { private readonly string _mappingFilePath; private readonly string _downloadDirectory;