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; }
[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();
public ICollection<Chapter> Chapters = null!;
@@ -79,10 +79,10 @@ public class Manga : Identifiable
this.OriginalLanguage = originalLanguage;
}
public string CreatePublicationFolder()
/// <exception cref="DirectoryNotFoundException">Library not loaded</exception>
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))

View File

@@ -225,17 +225,33 @@ public class DownloadChapterFromMangaconnectorWorker(MangaConnectorId<Chapter> c
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
string publicationFolder = manga.CreatePublicationFolder();
DirectoryInfo dirInfo = new (publicationFolder);
if (dirInfo.EnumerateFiles().Any(info => info.Name.Contains("cover", StringComparison.InvariantCultureIgnoreCase)))
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;
}
}
catch (Exception e)
{
Log.Error(e);
return;
}
//TODO MangaConnector Selection
await DbContext.Entry(manga).Collection(m => m.MangaConnectorIds).LoadAsync(CancellationToken);
if (manga.CoverFileNameInCache is not { } coverFileNameInCache)
{
MangaConnectorId<Manga> mangaConnectorId = manga.MangaConnectorIds.First();
if (!Tranga.TryGetMangaConnector(mangaConnectorId.MangaConnectorName, out MangaConnector? mangaConnector))
{
@@ -243,12 +259,14 @@ public class DownloadChapterFromMangaconnectorWorker(MangaConnectorId<Chapter> c
return;
}
Log.Info($"Copying cover to {publicationFolder}");
await DbContext.Entry(mangaConnectorId).Navigation(nameof(MangaConnectorId<Manga>.Obj)).LoadAsync(CancellationToken);
string? coverFileNameInCache = manga.CoverFileNameInCache ?? mangaConnector.SaveCoverImageToCache(mangaConnectorId);
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;
}