mirror of
https://github.com/SoPat712/allstarr.git
synced 2026-02-09 23:55:10 -05:00
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
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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<ILogger<EnvMigrationService>>());
|
||||
migrationService.MigrateEnvFile();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
app.Logger.LogWarning(ex, "Failed to run .env migration");
|
||||
}
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
app.UseExceptionHandler(_ => { }); // Global exception handler
|
||||
|
||||
|
||||
@@ -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))
|
||||
|
||||
59
allstarr/Services/Common/EnvMigrationService.cs
Normal file
59
allstarr/Services/Common/EnvMigrationService.cs
Normal file
@@ -0,0 +1,59 @@
|
||||
namespace allstarr.Services.Common;
|
||||
|
||||
/// <summary>
|
||||
/// Service that runs on startup to migrate old .env file format to new format
|
||||
/// </summary>
|
||||
public class EnvMigrationService
|
||||
{
|
||||
private readonly ILogger<EnvMigrationService> _logger;
|
||||
private readonly string _envFilePath;
|
||||
|
||||
public EnvMigrationService(ILogger<EnvMigrationService> 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");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user