Fix Chapter already exists check

This commit is contained in:
2025-09-05 00:05:14 +02:00
parent aae5c88ec9
commit ee3789fdfd
4 changed files with 35 additions and 5 deletions

View File

@@ -66,14 +66,26 @@ public class Chapter : Identifiable, IComparable<Chapter>
};
}
/// <summary>
/// Checks the filesystem if an archive at the ArchiveFilePath exists
/// </summary>
/// <param name="context"></param>
/// <param name="token"></param>
/// <returns>True if archive exists on disk</returns>
public bool CheckDownloaded()
/// <exception cref="KeyNotFoundException">Unable to load Chapter, Parent or Library</exception>
public async Task<bool> CheckDownloaded(MangaContext context, CancellationToken? token = null)
{
if(await context.Chapters
.Include(c => c.ParentManga)
.ThenInclude(p => p.Library)
.FirstOrDefaultAsync(c => c.Key == this.Key, token??CancellationToken.None) is not { } chapter)
throw new KeyNotFoundException("Unable to find chapter");
//TODO Log here
return File.Exists(FullArchiveFilePath);
this.Downloaded = File.Exists(chapter.FullArchiveFilePath);
await context.Sync(token??CancellationToken.None);
return this.Downloaded;
}
/// Placeholders:

View File

@@ -148,7 +148,7 @@ public static class Tranga
if (RunningWorkers.TryGetValue(worker, out Task<BaseWorker[]>? task))
{
BaseWorker[] newWorkers = task.Result;
Log.Debug($"Resulted in {newWorkers.Length} new Workers.");
Log.Debug($"{worker} created {newWorkers.Length} new Workers.");
AddWorkers(newWorkers);
}
RunningWorkers.Remove(worker, out _);

View File

@@ -35,6 +35,14 @@ public class DownloadChapterFromMangaconnectorWorker(MangaConnectorId<Chapter> c
Log.Error("Could not get MangaConnectorId.");
return []; //TODO Exception?
}
// Check if Chapter already exists...
if (await mangaConnectorId.Obj.CheckDownloaded(DbContext, CancellationToken))
{
Log.Warn("Chapter already exists!");
return [];
}
if (!Tranga.TryGetMangaConnector(mangaConnectorId.MangaConnectorName, out MangaConnector? mangaConnector))
{
Log.Error("Could not get MangaConnector.");

View File

@@ -14,9 +14,19 @@ public class UpdateChaptersDownloadedWorker(TimeSpan? interval = null, IEnumerab
protected override async Task<BaseWorker[]> DoWorkInternal()
{
Log.Debug("Checking chapter files...");
List<Chapter> chapters = await DbContext.Chapters.Include(c => c.ParentManga).ToListAsync(CancellationToken);
List<Chapter> chapters = await DbContext.Chapters.ToListAsync(CancellationToken);
Log.Debug($"Checking {chapters.Count} chapters...");
chapters.ForEach(chapter => DbContext.Entry(chapter).Property(c => c.Downloaded).CurrentValue = chapter.CheckDownloaded());
chapters.ForEach(async void (chapter) =>
{
try
{
chapter.Downloaded = await chapter.CheckDownloaded(DbContext, CancellationToken);
}
catch (Exception exception)
{
Log.Error(exception);
}
});
if(await DbContext.Sync(CancellationToken) is { success: false } e)
Log.Error($"Failed to save database changes: {e.exceptionMessage}");