diff --git a/API/Program.cs b/API/Program.cs index ef4da11..9be2ed9 100644 --- a/API/Program.cs +++ b/API/Program.cs @@ -149,6 +149,7 @@ using (IServiceScope scope = app.Services.CreateScope()) TrangaSettings.Load(); Tranga.StartLogger(); +Tranga.RemoveStaleFiles(app.Services); Tranga.JobStarterThread.Start(app.Services); //Tranga.NotificationSenderThread.Start(app.Services); //TODO RE-ENABLE diff --git a/API/Tranga.cs b/API/Tranga.cs index 6ef394b..86c9793 100644 --- a/API/Tranga.cs +++ b/API/Tranga.cs @@ -32,6 +32,23 @@ public static class Tranga Log.Info(TRANGA); } + internal static void RemoveStaleFiles(IServiceProvider serviceProvider) + { + Log.Info($"Removing stale files..."); + using IServiceScope scope = serviceProvider.CreateScope(); + PgsqlContext context = scope.ServiceProvider.GetRequiredService(); + string[] usedFiles = context.Mangas.Select(m => m.CoverFileNameInCache).Where(s => s != null).ToArray()!; + string[] extraneousFiles = new DirectoryInfo(TrangaSettings.coverImageCache).GetFiles() + .Where(f => usedFiles.Contains(f.FullName) == false) + .Select(f => f.FullName) + .ToArray(); + foreach (string path in extraneousFiles) + { + Log.Info($"Deleting {path}"); + File.Delete(path); + } + } + private static void NotificationSender(object? serviceProviderObj) { if (serviceProviderObj is null)