mirror of
https://github.com/C9Glax/tranga.git
synced 2025-10-11 05:09:49 +02:00
Improve CopyCoverFromCacheToDownloadLocation and Manga.FullDirectoryPath usage
This commit is contained in:
@@ -28,8 +28,8 @@ public class Manga : Identifiable
|
|||||||
[StringLength(512)] public string? CoverFileNameInCache { get; internal set; }
|
[StringLength(512)] public string? CoverFileNameInCache { get; internal set; }
|
||||||
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))
|
||||||
|
@@ -225,30 +225,48 @@ public class DownloadChapterFromMangaconnectorWorker(MangaConnectorId<Chapter> c
|
|||||||
|
|
||||||
private async Task CopyCoverFromCacheToDownloadLocation(Manga manga)
|
private async Task CopyCoverFromCacheToDownloadLocation(Manga manga)
|
||||||
{
|
{
|
||||||
//Check if Publication already has a Folder and cover
|
Log.Debug($"Copying cover for {manga}");
|
||||||
string publicationFolder = manga.CreatePublicationFolder();
|
|
||||||
DirectoryInfo dirInfo = new (publicationFolder);
|
manga = await DbContext.MangaIncludeAll().FirstAsync(m => m.Key == manga.Key, CancellationToken);
|
||||||
if (dirInfo.EnumerateFiles().Any(info => info.Name.Contains("cover", StringComparison.InvariantCultureIgnoreCase)))
|
string publicationFolder;
|
||||||
|
try
|
||||||
{
|
{
|
||||||
Log.Debug($"Cover already exists at {publicationFolder}");
|
Log.Debug("Checking Manga directory exists...");
|
||||||
return;
|
//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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
catch (Exception e)
|
||||||
//TODO MangaConnector Selection
|
|
||||||
await DbContext.Entry(manga).Collection(m => m.MangaConnectorIds).LoadAsync(CancellationToken);
|
|
||||||
MangaConnectorId<Manga> mangaConnectorId = manga.MangaConnectorIds.First();
|
|
||||||
if (!Tranga.TryGetMangaConnector(mangaConnectorId.MangaConnectorName, out MangaConnector? mangaConnector))
|
|
||||||
{
|
{
|
||||||
Log.Error($"MangaConnector with name {mangaConnectorId.MangaConnectorName} could not be found");
|
Log.Error(e);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Log.Info($"Copying cover to {publicationFolder}");
|
if (manga.CoverFileNameInCache is not { } coverFileNameInCache)
|
||||||
await DbContext.Entry(mangaConnectorId).Navigation(nameof(MangaConnectorId<Manga>.Obj)).LoadAsync(CancellationToken);
|
{
|
||||||
string? coverFileNameInCache = manga.CoverFileNameInCache ?? mangaConnector.SaveCoverImageToCache(mangaConnectorId);
|
MangaConnectorId<Manga> 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)
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user