Improve CopyCoverFromCacheToDownloadLocation and Manga.FullDirectoryPath usage

This commit is contained in:
2025-10-03 19:33:58 +02:00
parent 0b44abf4c7
commit 916c08adf8
2 changed files with 39 additions and 21 deletions

View File

@@ -29,7 +29,7 @@ public class Manga : Identifiable
public uint? Year { get; internal init; } public uint? Year { get; internal init; }
[StringLength(8)] public string? OriginalLanguage { 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<string> ChapterIds => Chapters.Select(c => c.Key).ToList(); [NotMapped] public ICollection<string> ChapterIds => Chapters.Select(c => c.Key).ToList();
public ICollection<Chapter> Chapters = null!; public ICollection<Chapter> Chapters = null!;
@@ -79,10 +79,10 @@ public class Manga : Identifiable
this.OriginalLanguage = originalLanguage; this.OriginalLanguage = originalLanguage;
} }
/// <exception cref="DirectoryNotFoundException">Library not loaded</exception>
public string CreatePublicationFolder() private string EnsureDirectoryExists()
{ {
string? publicationFolder = FullDirectoryPath; string? publicationFolder = Library is not null ? Path.Join(Library.BasePath, DirectoryName) : null;
if (publicationFolder is null) if (publicationFolder is null)
throw new DirectoryNotFoundException("Publication folder not found"); throw new DirectoryNotFoundException("Publication folder not found");
if(!Directory.Exists(publicationFolder)) if(!Directory.Exists(publicationFolder))

View File

@@ -225,17 +225,33 @@ public class DownloadChapterFromMangaconnectorWorker(MangaConnectorId<Chapter> c
private async Task CopyCoverFromCacheToDownloadLocation(Manga manga) private async Task CopyCoverFromCacheToDownloadLocation(Manga manga)
{ {
Log.Debug($"Copying cover for {manga}");
manga = await DbContext.MangaIncludeAll().FirstAsync(m => m.Key == manga.Key, CancellationToken);
string publicationFolder;
try
{
Log.Debug("Checking Manga directory exists...");
//Check if Publication already has a Folder and cover //Check if Publication already has a Folder and cover
string publicationFolder = manga.CreatePublicationFolder(); publicationFolder = manga.FullDirectoryPath;
DirectoryInfo dirInfo = new (publicationFolder);
if (dirInfo.EnumerateFiles().Any(info => info.Name.Contains("cover", StringComparison.InvariantCultureIgnoreCase))) 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}"); Log.Debug($"Cover already exists at {publicationFolder}");
return; return;
} }
}
catch (Exception e)
{
Log.Error(e);
return;
}
//TODO MangaConnector Selection if (manga.CoverFileNameInCache is not { } coverFileNameInCache)
await DbContext.Entry(manga).Collection(m => m.MangaConnectorIds).LoadAsync(CancellationToken); {
MangaConnectorId<Manga> mangaConnectorId = manga.MangaConnectorIds.First(); MangaConnectorId<Manga> mangaConnectorId = manga.MangaConnectorIds.First();
if (!Tranga.TryGetMangaConnector(mangaConnectorId.MangaConnectorName, out MangaConnector? mangaConnector)) if (!Tranga.TryGetMangaConnector(mangaConnectorId.MangaConnectorName, out MangaConnector? mangaConnector))
{ {
@@ -243,12 +259,14 @@ public class DownloadChapterFromMangaconnectorWorker(MangaConnectorId<Chapter> c
return; return;
} }
Log.Info($"Copying cover to {publicationFolder}"); coverFileNameInCache = mangaConnector.SaveCoverImageToCache(mangaConnectorId);
await DbContext.Entry(mangaConnectorId).Navigation(nameof(MangaConnectorId<Manga>.Obj)).LoadAsync(CancellationToken); manga.CoverFileNameInCache = coverFileNameInCache;
string? coverFileNameInCache = manga.CoverFileNameInCache ?? mangaConnector.SaveCoverImageToCache(mangaConnectorId); 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) if (coverFileNameInCache is null)
{ {
Log.Error($"File {coverFileNameInCache} does not exist"); Log.Error($"File {coverFileNameInCache} does not exist and failed to download cover");
return; return;
} }