mirror of
https://github.com/C9Glax/tranga.git
synced 2025-06-17 08:47:53 +02:00
MangaConnectors do not have to return an Object with 6 Parameters.
Job-Start Logic readable and optimized More robust Database design
This commit is contained in:
@ -2,7 +2,7 @@
|
||||
using System.IO.Compression;
|
||||
using System.Runtime.InteropServices;
|
||||
using API.MangaDownloadClients;
|
||||
using API.Schema.MangaConnectors;
|
||||
using Newtonsoft.Json;
|
||||
using SixLabors.ImageSharp;
|
||||
using SixLabors.ImageSharp.Formats.Jpeg;
|
||||
using SixLabors.ImageSharp.Processing;
|
||||
@ -11,45 +11,37 @@ using static System.IO.UnixFileMode;
|
||||
|
||||
namespace API.Schema.Jobs;
|
||||
|
||||
public class DownloadSingleChapterJob(string chapterId, string? parentJobId = null, ICollection<string>? dependsOnJobsIds = null)
|
||||
: Job(TokenGen.CreateToken(typeof(DownloadSingleChapterJob)), JobType.DownloadSingleChapterJob, 0, parentJobId, dependsOnJobsIds)
|
||||
public class DownloadSingleChapterJob : Job
|
||||
{
|
||||
[StringLength(64)]
|
||||
[Required]
|
||||
public string ChapterId { get; init; } = chapterId;
|
||||
[StringLength(64)] [Required] public string ChapterId { get; init; }
|
||||
|
||||
[JsonIgnore] public Chapter Chapter { get; init; } = null!;
|
||||
|
||||
public DownloadSingleChapterJob(Chapter chapter, Job? parentJob = null, ICollection<Job>? dependsOnJobs = null)
|
||||
: base(TokenGen.CreateToken(typeof(DownloadSingleChapterJob)), JobType.DownloadSingleChapterJob, 0, parentJob, dependsOnJobs)
|
||||
{
|
||||
this.ChapterId = chapter.ChapterId;
|
||||
this.Chapter = chapter;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// EF ONLY!!!
|
||||
/// </summary>
|
||||
public DownloadSingleChapterJob(string chapterId, string? parentJobId = null)
|
||||
: base(TokenGen.CreateToken(typeof(DownloadSingleChapterJob)), JobType.DownloadSingleChapterJob, 0)
|
||||
{
|
||||
this.ChapterId = chapterId;
|
||||
}
|
||||
|
||||
protected override IEnumerable<Job> RunInternal(PgsqlContext context)
|
||||
{
|
||||
Chapter? chapter = context.Chapters.Find(ChapterId);
|
||||
if (chapter is null)
|
||||
{
|
||||
Log.Error("Chapter is null.");
|
||||
return [];
|
||||
}
|
||||
Manga? manga = context.Mangas.Find(chapter.ParentMangaId) ?? chapter.ParentManga;
|
||||
if (manga is null)
|
||||
{
|
||||
Log.Error("Manga is null.");
|
||||
return [];
|
||||
}
|
||||
MangaConnector? connector = context.MangaConnectors.Find(manga.MangaConnectorId) ?? manga.MangaConnector;
|
||||
if (connector is null)
|
||||
{
|
||||
Log.Error("Connector is null.");
|
||||
return [];
|
||||
}
|
||||
string[] imageUrls = connector.GetChapterImageUrls(chapter);
|
||||
string[] imageUrls = Chapter.ParentManga.MangaConnector.GetChapterImageUrls(Chapter);
|
||||
if (imageUrls.Length < 1)
|
||||
{
|
||||
Log.Info($"No imageUrls for chapter {ChapterId}");
|
||||
return [];
|
||||
}
|
||||
string? saveArchiveFilePath = chapter.FullArchiveFilePath;
|
||||
if (saveArchiveFilePath is null)
|
||||
{
|
||||
Log.Error("saveArchiveFilePath is null.");
|
||||
return [];
|
||||
}
|
||||
string saveArchiveFilePath = Chapter.FullArchiveFilePath;
|
||||
|
||||
//Check if Publication Directory already exists
|
||||
string directoryPath = Path.GetDirectoryName(saveArchiveFilePath)!;
|
||||
@ -88,10 +80,10 @@ public class DownloadSingleChapterJob(string chapterId, string? parentJobId = nu
|
||||
}
|
||||
}
|
||||
|
||||
CopyCoverFromCacheToDownloadLocation(manga);
|
||||
CopyCoverFromCacheToDownloadLocation(Chapter.ParentManga);
|
||||
|
||||
Log.Debug($"Creating ComicInfo.xml {ChapterId}");
|
||||
File.WriteAllText(Path.Join(tempFolder, "ComicInfo.xml"), chapter.GetComicInfoXmlString());
|
||||
File.WriteAllText(Path.Join(tempFolder, "ComicInfo.xml"), Chapter.GetComicInfoXmlString());
|
||||
|
||||
Log.Debug($"Packaging images to archive {ChapterId}");
|
||||
//ZIP-it and ship-it
|
||||
@ -100,10 +92,10 @@ public class DownloadSingleChapterJob(string chapterId, string? parentJobId = nu
|
||||
File.SetUnixFileMode(saveArchiveFilePath, UserRead | UserWrite | UserExecute | GroupRead | GroupWrite | GroupExecute | OtherRead | OtherExecute);
|
||||
Directory.Delete(tempFolder, true); //Cleanup
|
||||
|
||||
chapter.Downloaded = true;
|
||||
Chapter.Downloaded = true;
|
||||
context.SaveChanges();
|
||||
|
||||
return [new UpdateFilesDownloadedJob(0, manga.MangaId, this.JobId)];
|
||||
return [new UpdateFilesDownloadedJob(Chapter.ParentManga, 0, this)];
|
||||
}
|
||||
|
||||
private void ProcessImage(string imagePath)
|
||||
@ -138,7 +130,7 @@ public class DownloadSingleChapterJob(string chapterId, string? parentJobId = nu
|
||||
}
|
||||
|
||||
Log.Info($"Copying cover to {publicationFolder}");
|
||||
string? fileInCache = manga.CoverFileNameInCache ?? manga.SaveCoverImageToCache();
|
||||
string? fileInCache = manga.CoverFileNameInCache ?? manga.MangaConnector.SaveCoverImageToCache(manga);
|
||||
if (fileInCache is null)
|
||||
{
|
||||
Log.Error($"File {fileInCache} does not exist");
|
||||
|
Reference in New Issue
Block a user