#168 Multiple Base-Paths (Libraries) Support
Some checks failed
Docker Image CI / build (push) Has been cancelled

This commit is contained in:
2025-03-16 16:48:19 +01:00
parent 5012bbb2eb
commit 5b03befbf1
64 changed files with 668 additions and 16084 deletions

View File

@ -1,4 +1,5 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Xml.Linq;
using API.Schema.Jobs;
using Microsoft.EntityFrameworkCore;
@ -13,7 +14,6 @@ public class Chapter : IComparable<Chapter>
: this(parentManga.MangaId, url, chapterNumber, volumeNumber, title)
{
ParentManga = parentManga;
ArchiveFileName = BuildArchiveFileName();
}
public Chapter(string parentMangaId, string url, string chapterNumber,
@ -25,6 +25,7 @@ public class Chapter : IComparable<Chapter>
ChapterNumber = chapterNumber;
VolumeNumber = volumeNumber;
Title = title;
FileName = GetArchiveFilePath();
}
[StringLength(64)]
@ -43,7 +44,10 @@ public class Chapter : IComparable<Chapter>
public string? Title { get; private set; }
[StringLength(256)]
[Required]
public string ArchiveFileName { get; private set; }
public string FileName { get; private set; }
[JsonIgnore]
[NotMapped]
public string? FullArchiveFilePath => ParentManga is { } m ? Path.Join(m.FullDirectoryPath, FileName) : null;
[Required]
public bool Downloaded { get; internal set; } = false;
[Required]
@ -82,26 +86,14 @@ public class Chapter : IComparable<Chapter>
return UpdateArchiveFileName();
}
private string BuildArchiveFileName()
{
return
$"{ParentManga.Name} - Vol.{VolumeNumber ?? 0} Ch.{ChapterNumber}{(Title is null ? "" : $" - {Title}")}.cbz";
}
private MoveFileOrFolderJob? UpdateArchiveFileName()
{
string oldPath = GetArchiveFilePath();
ArchiveFileName = BuildArchiveFileName();
return Downloaded ? new MoveFileOrFolderJob(oldPath, GetArchiveFilePath()) : null;
}
/// <summary>
/// Creates full file path of chapter-archive
/// </summary>
/// <returns>Filepath</returns>
internal string GetArchiveFilePath()
{
return Path.Join(TrangaSettings.downloadLocation, ParentManga.FolderName, ArchiveFileName);
string? oldPath = FullArchiveFilePath;
if (oldPath is null)
return null;
string newPath = GetArchiveFilePath();
FileName = newPath;
return Downloaded ? new MoveFileOrFolderJob(oldPath, newPath) : null;
}
/// <summary>
@ -114,6 +106,11 @@ public class Chapter : IComparable<Chapter>
return File.Exists(path);
}
private string GetArchiveFilePath()
{
return $"{ParentManga!.Name} - Vol.{VolumeNumber ?? 0} Ch.{ChapterNumber}{(Title is null ? "" : $" - {Title}")}.cbz";
}
private static int CompareChapterNumbers(string ch1, string ch2)
{
int[] ch1Arr = ch1.Split('.').Select(c => int.TryParse(c, out int result) ? result : -1).ToArray();

View File

@ -14,7 +14,7 @@ public class DownloadMangaCoverJob(string mangaId, string? parentJobId = null, I
protected override IEnumerable<Job> RunInternal(PgsqlContext context)
{
Manga? manga = Manga ?? context.Manga.Find(this.MangaId);
Manga? manga = Manga ?? context.Mangas.Find(this.MangaId);
if (manga is null)
return [];

View File

@ -25,10 +25,10 @@ public class DownloadSingleChapterJob(string chapterId, string? parentJobId = nu
protected override IEnumerable<Job> RunInternal(PgsqlContext context)
{
Chapter chapter = Chapter ?? context.Chapters.Find(ChapterId)!;
Manga manga = chapter.ParentManga ?? context.Manga.Find(chapter.ParentMangaId)!;
Manga manga = chapter.ParentManga ?? context.Mangas.Find(chapter.ParentMangaId)!;
MangaConnector connector = manga.MangaConnector ?? context.MangaConnectors.Find(manga.MangaConnectorId)!;
string[] imageUrls = connector.GetChapterImageUrls(chapter);
string saveArchiveFilePath = chapter.GetArchiveFilePath();
string saveArchiveFilePath = chapter.FullArchiveFilePath;
//Check if Publication Directory already exists
string directoryPath = Path.GetDirectoryName(saveArchiveFilePath)!;

View File

@ -9,5 +9,6 @@ public enum JobType : byte
MoveFileOrFolderJob = 3,
DownloadMangaCoverJob = 4,
RetrieveChaptersJob = 5,
UpdateFilesDownloadedJob = 6
UpdateFilesDownloadedJob = 6,
MoveMangaLibraryJob = 7
}

View File

@ -0,0 +1,30 @@
using System.ComponentModel.DataAnnotations;
namespace API.Schema.Jobs;
public class MoveMangaLibraryJob(string mangaId, string toLibraryId, string? parentJobId = null, ICollection<string>? dependsOnJobsIds = null)
: Job(TokenGen.CreateToken(typeof(MoveMangaLibraryJob)), JobType.MoveMangaLibraryJob, 0, parentJobId, dependsOnJobsIds)
{
[StringLength(64)]
[Required]
public string MangaId { get; init; } = mangaId;
[StringLength(64)]
[Required]
public string ToLibraryId { get; init; } = toLibraryId;
protected override IEnumerable<Job> RunInternal(PgsqlContext context)
{
Manga? manga = context.Mangas.Find(MangaId);
if(manga is null)
throw new KeyNotFoundException();
LocalLibrary? library = context.LocalLibraries.Find(ToLibraryId);
if(library is null)
throw new KeyNotFoundException();
Chapter[] chapters = context.Chapters.Where(c => c.ParentMangaId == MangaId).ToArray();
Dictionary<Chapter, string> oldPath = chapters.ToDictionary(c => c, c => c.FullArchiveFilePath!);
manga.Library = library;
context.SaveChanges();
return chapters.Select(c => new MoveFileOrFolderJob(oldPath[c], c.FullArchiveFilePath!));
}
}

View File

@ -21,7 +21,7 @@ public class RetrieveChaptersJob(ulong recurrenceMs, string mangaId, string? par
* Manga as a new entity and Postgres throws a Duplicate PK exception.
* m.MangaConnector does not have this issue (IDK why).
*/
Manga m = context.Manga.Find(MangaId)!;
Manga m = context.Mangas.Find(MangaId)!;
MangaConnector connector = context.MangaConnectors.Find(m.MangaConnectorId)!;
// This gets all chapters that are not downloaded
Chapter[] allNewChapters = connector.GetNewChapters(m).DistinctBy(c => c.ChapterId).ToArray();

View File

@ -0,0 +1,17 @@
using System.ComponentModel.DataAnnotations;
namespace API.Schema;
public class LocalLibrary(string basePath, string libraryName)
{
[StringLength(64)]
[Required]
public string LocalLibraryId { get; init; } = TokenGen.CreateToken(typeof(LocalLibrary), basePath);
[StringLength(256)]
[Required]
public string BasePath { get; internal set; } = basePath;
[StringLength(512)]
[Required]
public string LibraryName { get; internal set; } = libraryName;
}

View File

@ -44,8 +44,16 @@ public class Manga
public string? OriginalLanguage { get; internal set; }
[Required]
public MangaReleaseStatus ReleaseStatus { get; internal set; }
[StringLength(256)]
[Required]
public string FolderName { get; private set; }
public string DirectoryName { get; private set; }
public LocalLibrary? Library { get; internal set; }
[JsonIgnore]
[NotMapped]
public string LibraryPath => Library is null ? TrangaSettings.downloadLocation : Library.BasePath;
[JsonIgnore]
[NotMapped]
public string FullDirectoryPath => Path.Join(LibraryPath, DirectoryName);
[Required]
public float IgnoreChapterBefore { get; internal set; }
[StringLength(64)]
@ -81,7 +89,8 @@ public class Manga
public Manga(string idOnConnectorSite, string name, string description, string websiteUrl, string coverUrl,
string? coverFileNameInCache, uint year, string? originalLanguage, MangaReleaseStatus releaseStatus,
float ignoreChapterBefore, MangaConnector mangaConnector, ICollection<Author> authors,
ICollection<MangaTag> mangaTags, ICollection<Link> links, ICollection<MangaAltTitle> altTitles)
ICollection<MangaTag> mangaTags, ICollection<Link> links, ICollection<MangaAltTitle> altTitles,
LocalLibrary? library = null)
: this(idOnConnectorSite, name, description, websiteUrl, coverUrl, coverFileNameInCache, year, originalLanguage,
releaseStatus, ignoreChapterBefore, mangaConnector.Name)
{
@ -89,6 +98,7 @@ public class Manga
this.MangaTags = mangaTags;
this.Links = links;
this.AltTitles = altTitles;
this.Library = library;
}
public Manga(string idOnConnectorSite, string name, string description, string websiteUrl, string coverUrl,
@ -107,14 +117,14 @@ public class Manga
ReleaseStatus = releaseStatus;
IgnoreChapterBefore = ignoreChapterBefore;
MangaConnectorId = mangaConnectorId;
FolderName = BuildFolderName(name);
DirectoryName = BuildFolderName(name);
}
public MoveFileOrFolderJob UpdateFolderName(string downloadLocation, string newName)
{
string oldName = this.FolderName;
this.FolderName = newName;
return new MoveFileOrFolderJob(Path.Join(downloadLocation, oldName), Path.Join(downloadLocation, this.FolderName));
string oldName = this.DirectoryName;
this.DirectoryName = newName;
return new MoveFileOrFolderJob(Path.Join(downloadLocation, oldName), Path.Join(downloadLocation, this.DirectoryName));
}
internal void UpdateWithInfo(Manga other)
@ -164,7 +174,7 @@ public class Manga
public string CreatePublicationFolder()
{
string publicationFolder = Path.Join(TrangaSettings.downloadLocation, this.FolderName);
string publicationFolder = Path.Join(LibraryPath, this.DirectoryName);
if(!Directory.Exists(publicationFolder))
Directory.CreateDirectory(publicationFolder);
if(RuntimeInformation.IsOSPlatform(OSPlatform.Linux))

View File

@ -10,10 +10,11 @@ public class PgsqlContext(DbContextOptions<PgsqlContext> options) : DbContext(op
{
public DbSet<Job> Jobs { get; set; }
public DbSet<MangaConnector> MangaConnectors { get; set; }
public DbSet<Manga> Manga { get; set; }
public DbSet<Manga> Mangas { get; set; }
public DbSet<LocalLibrary> LocalLibraries { get; set; }
public DbSet<Chapter> Chapters { get; set; }
public DbSet<Author> Authors { get; set; }
public DbSet<Link> Link { get; set; }
public DbSet<Link> Links { get; set; }
public DbSet<MangaTag> Tags { get; set; }
public DbSet<MangaAltTitle> AltTitles { get; set; }
public DbSet<LibraryConnector> LibraryConnectors { get; set; }
@ -73,6 +74,13 @@ public class PgsqlContext(DbContextOptions<PgsqlContext> options) : DbContext(op
modelBuilder.Entity<Manga>()
.Navigation(m => m.MangaConnector)
.AutoInclude();
modelBuilder.Entity<Manga>()
.HasOne<LocalLibrary>(m => m.Library)
.WithMany()
.OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<Manga>()
.Navigation(m => m.Library)
.AutoInclude();
modelBuilder.Entity<Manga>()
.HasMany<Author>(m => m.Authors)
.WithMany();