From 196ff1733973ceb1bbe3e05db6f33b68c33d67f9 Mon Sep 17 00:00:00 2001 From: glax Date: Mon, 6 Oct 2025 21:35:14 +0200 Subject: [PATCH] Test fix for Chapter FullArchiveFilePath throwing exception --- API/Schema/MangaContext/Chapter.cs | 18 ++++++++++++++-- API/Schema/MangaContext/Manga.cs | 8 +++++-- ...DownloadChapterFromMangaconnectorWorker.cs | 7 ++++++- API/Workers/MoveMangaLibraryWorker.cs | 21 +++++++++++++------ 4 files changed, 43 insertions(+), 11 deletions(-) diff --git a/API/Schema/MangaContext/Chapter.cs b/API/Schema/MangaContext/Chapter.cs index 729d93b..0c0c80c 100644 --- a/API/Schema/MangaContext/Chapter.cs +++ b/API/Schema/MangaContext/Chapter.cs @@ -25,7 +25,10 @@ public class Chapter : Identifiable, IComparable [StringLength(256)] public string FileName { get; private set; } public bool Downloaded { get; internal set; } - [NotMapped] public string FullArchiveFilePath => Path.Join(ParentManga.FullDirectoryPath, FileName); + + /// Library for Manga not loaded + [NotMapped] + public string? FullArchiveFilePath => GetFullFilepath(); private static readonly Regex ChapterNumberRegex = new(@"(?:\d+\.)*\d+", RegexOptions.Compiled); public Chapter(Manga parentManga, string chapterNumber, @@ -89,7 +92,6 @@ public class Chapter : Identifiable, IComparable if (chapter.ParentManga.Library is null) return false; - //TODO Log here this.Downloaded = File.Exists(chapter.FullArchiveFilePath); await context.Sync(token??CancellationToken.None, GetType(), $"CheckDownloaded {this} {this.Downloaded}"); return this.Downloaded; @@ -163,6 +165,18 @@ public class Chapter : Identifiable, IComparable return stringBuilder.ToString(); } + private string? GetFullFilepath() + { + try + { + return Path.Join(ParentManga.FullDirectoryPath, FileName); + } + catch (Exception) + { + return null; + } + } + public class ChapterComparer : IComparer { public int Compare(Chapter? x, Chapter? y) diff --git a/API/Schema/MangaContext/Manga.cs b/API/Schema/MangaContext/Manga.cs index ad90e7d..6cf9197 100644 --- a/API/Schema/MangaContext/Manga.cs +++ b/API/Schema/MangaContext/Manga.cs @@ -29,6 +29,8 @@ public class Manga : Identifiable public uint? Year { get; internal init; } [StringLength(8)] public string? OriginalLanguage { get; internal init; } + + /// Library not loaded [NotMapped] public string FullDirectoryPath => EnsureDirectoryExists(); [NotMapped] public ICollection ChapterIds => Chapters.Select(c => c.Key).ToList(); @@ -110,11 +112,13 @@ public class Manga : Identifiable foreach (Chapter otherChapter in other.Chapters) { - string oldPath = otherChapter.FullArchiveFilePath; + if (otherChapter.FullArchiveFilePath is not { } oldPath) + continue; Chapter newChapter = new(this, otherChapter.ChapterNumber, otherChapter.VolumeNumber, otherChapter.Title); this.Chapters.Add(newChapter); - string newPath = newChapter.FullArchiveFilePath; + if (newChapter.FullArchiveFilePath is not { } newPath) + continue; newJobs.Add(new MoveFileOrFolderWorker(newPath, oldPath)); } diff --git a/API/Workers/MangaDownloadWorkers/DownloadChapterFromMangaconnectorWorker.cs b/API/Workers/MangaDownloadWorkers/DownloadChapterFromMangaconnectorWorker.cs index e1275de..99c2beb 100644 --- a/API/Workers/MangaDownloadWorkers/DownloadChapterFromMangaconnectorWorker.cs +++ b/API/Workers/MangaDownloadWorkers/DownloadChapterFromMangaconnectorWorker.cs @@ -70,7 +70,12 @@ public class DownloadChapterFromMangaconnectorWorker(MangaConnectorId c Log.Error(result.exceptionMessage); return []; } - string saveArchiveFilePath = chapter.FullArchiveFilePath; + + if (chapter.FullArchiveFilePath is not { } saveArchiveFilePath) + { + Log.Error("Failed getting saveArchiveFilePath"); + return []; + } Log.Debug($"Chapter path: {saveArchiveFilePath}"); //Check if Publication Directory already exists diff --git a/API/Workers/MoveMangaLibraryWorker.cs b/API/Workers/MoveMangaLibraryWorker.cs index 2e15ff3..815ab94 100644 --- a/API/Workers/MoveMangaLibraryWorker.cs +++ b/API/Workers/MoveMangaLibraryWorker.cs @@ -14,24 +14,33 @@ public class MoveMangaLibraryWorker(Manga manga, FileLibrary toLibrary, IEnumera protected override async Task DoWorkInternal() { Log.Debug("Moving Manga..."); - // Get Manga (with Chapters and Library) + // Get Manga (with and Library) if (await DbContext.Mangas - .Include(m => m.Chapters) .Include(m => m.Library) .FirstOrDefaultAsync(m => m.Key == MangaId, CancellationToken) is not { } manga) { Log.Error("Could not find Manga."); - return []; //TODO Exception? + return []; } + + if (await DbContext.Chapters + .Include(ch => ch.ParentManga).ThenInclude(m => m.Library) + .Where(ch => ch.ParentMangaId == MangaId) + .ToListAsync(CancellationToken) is not { } chapters) + { + Log.Error("Could not find chapters."); + return []; + } + // Get new Library if (await DbContext.FileLibraries.FirstOrDefaultAsync(l => l.Key == LibraryId, CancellationToken) is not { } toLibrary) { Log.Error("Could not find Library."); - return []; //TODO Exception? + return []; } // Save old Path (to later move chapters) - Dictionary oldPath = manga.Chapters.ToDictionary(c => c, c => c.FullArchiveFilePath); + Dictionary oldPath = manga.Chapters.ToDictionary(c => c.Key, c => c.FullArchiveFilePath).Where(kv => kv.Value is not null).ToDictionary(x => x.Key, x => x.Value)!; // Set new Path manga.Library = toLibrary; @@ -39,7 +48,7 @@ public class MoveMangaLibraryWorker(Manga manga, FileLibrary toLibrary, IEnumera return []; // Create Jobs to move chapters from old to new Path - return manga.Chapters.Select(c => new MoveFileOrFolderWorker(c.FullArchiveFilePath, oldPath[c])).ToArray(); + return manga.Chapters.Select(c => new MoveFileOrFolderWorker(c.FullArchiveFilePath, oldPath[c.Key])).ToArray(); } public override string ToString() => $"{base.ToString()} {MangaId} {LibraryId}";