Remove old covers from ImageCache

This commit is contained in:
Glax 2025-05-18 16:54:53 +02:00
parent aa29c45094
commit 9b251169a5
2 changed files with 18 additions and 0 deletions

View File

@ -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

View File

@ -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<PgsqlContext>();
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)