From 916c08adf835d59f848aabf06013069e45da4799 Mon Sep 17 00:00:00 2001 From: glax Date: Fri, 3 Oct 2025 19:33:58 +0200 Subject: [PATCH] Improve CopyCoverFromCacheToDownloadLocation and Manga.FullDirectoryPath usage --- API/Schema/MangaContext/Manga.cs | 10 ++-- ...DownloadChapterFromMangaconnectorWorker.cs | 50 +++++++++++++------ 2 files changed, 39 insertions(+), 21 deletions(-) diff --git a/API/Schema/MangaContext/Manga.cs b/API/Schema/MangaContext/Manga.cs index 9182c47..ad90e7d 100644 --- a/API/Schema/MangaContext/Manga.cs +++ b/API/Schema/MangaContext/Manga.cs @@ -28,8 +28,8 @@ public class Manga : Identifiable [StringLength(512)] public string? CoverFileNameInCache { get; internal set; } public uint? Year { get; internal init; } [StringLength(8)] public string? OriginalLanguage { get; internal init; } - - [NotMapped] public string? FullDirectoryPath => Library is not null ? Path.Join(Library.BasePath, DirectoryName) : null; + + [NotMapped] public string FullDirectoryPath => EnsureDirectoryExists(); [NotMapped] public ICollection ChapterIds => Chapters.Select(c => c.Key).ToList(); public ICollection Chapters = null!; @@ -79,10 +79,10 @@ public class Manga : Identifiable this.OriginalLanguage = originalLanguage; } - - public string CreatePublicationFolder() + /// Library not loaded + private string EnsureDirectoryExists() { - string? publicationFolder = FullDirectoryPath; + string? publicationFolder = Library is not null ? Path.Join(Library.BasePath, DirectoryName) : null; if (publicationFolder is null) throw new DirectoryNotFoundException("Publication folder not found"); if(!Directory.Exists(publicationFolder)) diff --git a/API/Workers/MangaDownloadWorkers/DownloadChapterFromMangaconnectorWorker.cs b/API/Workers/MangaDownloadWorkers/DownloadChapterFromMangaconnectorWorker.cs index a5b9dc4..e1275de 100644 --- a/API/Workers/MangaDownloadWorkers/DownloadChapterFromMangaconnectorWorker.cs +++ b/API/Workers/MangaDownloadWorkers/DownloadChapterFromMangaconnectorWorker.cs @@ -225,30 +225,48 @@ public class DownloadChapterFromMangaconnectorWorker(MangaConnectorId c private async Task CopyCoverFromCacheToDownloadLocation(Manga manga) { - //Check if Publication already has a Folder and cover - string publicationFolder = manga.CreatePublicationFolder(); - DirectoryInfo dirInfo = new (publicationFolder); - if (dirInfo.EnumerateFiles().Any(info => info.Name.Contains("cover", StringComparison.InvariantCultureIgnoreCase))) + Log.Debug($"Copying cover for {manga}"); + + manga = await DbContext.MangaIncludeAll().FirstAsync(m => m.Key == manga.Key, CancellationToken); + string publicationFolder; + try { - Log.Debug($"Cover already exists at {publicationFolder}"); - return; + Log.Debug("Checking Manga directory exists..."); + //Check if Publication already has a Folder and cover + publicationFolder = manga.FullDirectoryPath; + + Log.Debug("Checking cover already exists..."); + DirectoryInfo dirInfo = new(publicationFolder); + if (dirInfo.EnumerateFiles() + .Any(info => info.Name.Contains("cover", StringComparison.InvariantCultureIgnoreCase))) + { + Log.Debug($"Cover already exists at {publicationFolder}"); + return; + } } - - //TODO MangaConnector Selection - await DbContext.Entry(manga).Collection(m => m.MangaConnectorIds).LoadAsync(CancellationToken); - MangaConnectorId mangaConnectorId = manga.MangaConnectorIds.First(); - if (!Tranga.TryGetMangaConnector(mangaConnectorId.MangaConnectorName, out MangaConnector? mangaConnector)) + catch (Exception e) { - Log.Error($"MangaConnector with name {mangaConnectorId.MangaConnectorName} could not be found"); + Log.Error(e); return; } - Log.Info($"Copying cover to {publicationFolder}"); - await DbContext.Entry(mangaConnectorId).Navigation(nameof(MangaConnectorId.Obj)).LoadAsync(CancellationToken); - string? coverFileNameInCache = manga.CoverFileNameInCache ?? mangaConnector.SaveCoverImageToCache(mangaConnectorId); + if (manga.CoverFileNameInCache is not { } coverFileNameInCache) + { + MangaConnectorId mangaConnectorId = manga.MangaConnectorIds.First(); + if (!Tranga.TryGetMangaConnector(mangaConnectorId.MangaConnectorName, out MangaConnector? mangaConnector)) + { + Log.Error($"MangaConnector with name {mangaConnectorId.MangaConnectorName} could not be found"); + return; + } + + coverFileNameInCache = mangaConnector.SaveCoverImageToCache(mangaConnectorId); + manga.CoverFileNameInCache = coverFileNameInCache; + if (await DbContext.Sync(CancellationToken, reason: "Update cover filename") is { success: false } result) + Log.Error($"Couldn't update cover filename {result.exceptionMessage}"); + } if (coverFileNameInCache is null) { - Log.Error($"File {coverFileNameInCache} does not exist"); + Log.Error($"File {coverFileNameInCache} does not exist and failed to download cover"); return; }