[postgres-Server-V2] Formatter fix

This commit is contained in:
Alessandro Benetton 2025-01-12 16:12:51 +01:00
parent dd1d67ac11
commit 7dc34c10e5
No known key found for this signature in database
GPG Key ID: ED63A304CE2C0303
2 changed files with 61 additions and 61 deletions

View File

@ -8,11 +8,29 @@ namespace API.Schema;
[PrimaryKey("ChapterId")] [PrimaryKey("ChapterId")]
public class Chapter : IComparable<Chapter> public class Chapter : IComparable<Chapter>
{ {
[MaxLength(64)] public Chapter(Manga parentManga, string url, string chapterNumber, int? volumeNumber = null, string? title = null)
public string ChapterId { get; init; } = TokenGen.CreateToken(typeof(Chapter), 64); : this(parentManga.MangaId, url, chapterNumber, volumeNumber, title)
{
ParentManga = parentManga;
ArchiveFileName = BuildArchiveFileName();
}
public Chapter(string parentMangaId, string url, string chapterNumber,
int? volumeNumber = null, string? title = null)
{
ParentMangaId = parentMangaId;
Url = url;
ChapterNumber = chapterNumber;
VolumeNumber = volumeNumber;
Title = title;
}
[MaxLength(64)] public string ChapterId { get; init; } = TokenGen.CreateToken(typeof(Chapter), 64);
public int? VolumeNumber { get; private set; } public int? VolumeNumber { get; private set; }
[MaxLength(10)]
public string ChapterNumber { get; private set; } [MaxLength(10)] public string ChapterNumber { get; private set; }
public string Url { get; internal set; } public string Url { get; internal set; }
public string? Title { get; private set; } public string? Title { get; private set; }
public string ArchiveFileName { get; private set; } public string ArchiveFileName { get; private set; }
@ -21,59 +39,52 @@ public class Chapter : IComparable<Chapter>
public string ParentMangaId { get; internal set; } public string ParentMangaId { get; internal set; }
public Manga? ParentManga { get; init; } public Manga? ParentManga { get; init; }
public Chapter(Manga parentManga, string url, string chapterNumber, int? volumeNumber = null, string? title = null) public int CompareTo(Chapter? other)
: this(parentManga.MangaId, url, chapterNumber, volumeNumber, title)
{ {
this.ParentManga = parentManga; if (other is not { } otherChapter)
this.ArchiveFileName = BuildArchiveFileName(); throw new ArgumentException($"{other} can not be compared to {this}");
} return VolumeNumber?.CompareTo(otherChapter.VolumeNumber) switch
{
public Chapter(string parentMangaId, string url, string chapterNumber, < 0 => -1,
int? volumeNumber = null, string? title = null) > 0 => 1,
{ _ => CompareChapterNumbers(ChapterNumber, otherChapter.ChapterNumber)
this.ParentMangaId = parentMangaId; };
this.Url = url;
this.ChapterNumber = chapterNumber;
this.VolumeNumber = volumeNumber;
this.Title = title;
} }
public MoveFileOrFolderJob? UpdateChapterNumber(string chapterNumber) public MoveFileOrFolderJob? UpdateChapterNumber(string chapterNumber)
{ {
this.ChapterNumber = chapterNumber; ChapterNumber = chapterNumber;
return UpdateArchiveFileName(); return UpdateArchiveFileName();
} }
public MoveFileOrFolderJob? UpdateVolumeNumber(int? volumeNumber) public MoveFileOrFolderJob? UpdateVolumeNumber(int? volumeNumber)
{ {
this.VolumeNumber = volumeNumber; VolumeNumber = volumeNumber;
return UpdateArchiveFileName(); return UpdateArchiveFileName();
} }
public MoveFileOrFolderJob? UpdateTitle(string? title) public MoveFileOrFolderJob? UpdateTitle(string? title)
{ {
this.Title = title; Title = title;
return UpdateArchiveFileName(); return UpdateArchiveFileName();
} }
private string BuildArchiveFileName() private string BuildArchiveFileName()
{ {
return $"{this.ParentManga.Name} - Vol.{this.VolumeNumber ?? 0} Ch.{this.ChapterNumber}{(this.Title is null ? "" : $" - {this.Title}")}.cbz"; return
$"{ParentManga.Name} - Vol.{VolumeNumber ?? 0} Ch.{ChapterNumber}{(Title is null ? "" : $" - {Title}")}.cbz";
} }
private MoveFileOrFolderJob? UpdateArchiveFileName() private MoveFileOrFolderJob? UpdateArchiveFileName()
{ {
string oldPath = GetArchiveFilePath(); string oldPath = GetArchiveFilePath();
this.ArchiveFileName = BuildArchiveFileName(); ArchiveFileName = BuildArchiveFileName();
if (Downloaded) if (Downloaded) return new MoveFileOrFolderJob(oldPath, GetArchiveFilePath());
{
return new MoveFileOrFolderJob(oldPath, GetArchiveFilePath());
}
return null; return null;
} }
/// <summary> /// <summary>
/// Creates full file path of chapter-archive /// Creates full file path of chapter-archive
/// </summary> /// </summary>
/// <returns>Filepath</returns> /// <returns>Filepath</returns>
internal string GetArchiveFilePath() internal string GetArchiveFilePath()
@ -89,8 +100,8 @@ public class Chapter : IComparable<Chapter>
private static int CompareChapterNumbers(string ch1, string ch2) private static int CompareChapterNumbers(string ch1, string ch2)
{ {
var ch1Arr = ch1.Split('.').Select(c => int.Parse(c)).ToArray(); int[] ch1Arr = ch1.Split('.').Select(c => int.Parse(c)).ToArray();
var ch2Arr = ch2.Split('.').Select(c => int.Parse(c)).ToArray(); int[] ch2Arr = ch2.Split('.').Select(c => int.Parse(c)).ToArray();
int i = 0, j = 0; int i = 0, j = 0;
@ -107,27 +118,15 @@ public class Chapter : IComparable<Chapter>
return 0; return 0;
} }
public int CompareTo(Chapter? other)
{
if(other is not { } otherChapter)
throw new ArgumentException($"{other} can not be compared to {this}");
return this.VolumeNumber?.CompareTo(otherChapter.VolumeNumber) switch
{
<0 => -1,
>0 => 1,
_ => CompareChapterNumbers(this.ChapterNumber, otherChapter.ChapterNumber)
};
}
internal string GetComicInfoXmlString() internal string GetComicInfoXmlString()
{ {
XElement comicInfo = new XElement("ComicInfo", XElement comicInfo = new("ComicInfo",
new XElement("Tags", string.Join(',', ParentManga.Tags.Select(tag => tag.Tag))), new XElement("Tags", string.Join(',', ParentManga.Tags.Select(tag => tag.Tag))),
new XElement("LanguageISO", ParentManga.OriginalLanguage), new XElement("LanguageISO", ParentManga.OriginalLanguage),
new XElement("Title", this.Title), new XElement("Title", Title),
new XElement("Writer", string.Join(',', ParentManga.Authors.Select(author => author.AuthorName))), new XElement("Writer", string.Join(',', ParentManga.Authors.Select(author => author.AuthorName))),
new XElement("Volume", this.VolumeNumber), new XElement("Volume", VolumeNumber),
new XElement("Number", this.ChapterNumber) new XElement("Number", ChapterNumber)
); );
return comicInfo.ToString(); return comicInfo.ToString();
} }

View File

@ -1,5 +1,6 @@
using API.Schema; using API.Schema;
using API.Schema.Jobs; using API.Schema.Jobs;
using API.Schema.NotificationConnectors;
using log4net; using log4net;
using log4net.Config; using log4net.Config;
@ -20,9 +21,9 @@ public static class Tranga
private static void NotificationSender(object? pgsqlContext) private static void NotificationSender(object? pgsqlContext)
{ {
if (pgsqlContext is null) return; if (pgsqlContext is null) return;
var context = (PgsqlContext)pgsqlContext; PgsqlContext context = (PgsqlContext)pgsqlContext;
var staleNotifications = IQueryable<Notification> staleNotifications =
context.Notifications.Where(n => n.Urgency < NotificationUrgency.Normal); context.Notifications.Where(n => n.Urgency < NotificationUrgency.Normal);
context.Notifications.RemoveRange(staleNotifications); context.Notifications.RemoveRange(staleNotifications);
context.SaveChanges(); context.SaveChanges();
@ -39,14 +40,14 @@ public static class Tranga
private static void SendNotifications(PgsqlContext context, NotificationUrgency urgency) private static void SendNotifications(PgsqlContext context, NotificationUrgency urgency)
{ {
var notifications = context.Notifications.Where(n => n.Urgency == urgency).ToList(); List<Notification> notifications = context.Notifications.Where(n => n.Urgency == urgency).ToList();
if (notifications.Any()) if (notifications.Any())
{ {
var max = notifications.MaxBy(n => n.Date)!.Date; DateTime max = notifications.MaxBy(n => n.Date)!.Date;
if (DateTime.Now.Subtract(max) > TrangaSettings.NotificationUrgencyDelay(urgency)) if (DateTime.Now.Subtract(max) > TrangaSettings.NotificationUrgencyDelay(urgency))
{ {
foreach (var notificationConnector in context.NotificationConnectors) foreach (NotificationConnector notificationConnector in context.NotificationConnectors)
foreach (var notification in notifications) foreach (Notification notification in notifications)
notificationConnector.SendNotification(notification.Title, notification.Message); notificationConnector.SendNotification(notification.Title, notification.Message);
context.Notifications.RemoveRange(notifications); context.Notifications.RemoveRange(notifications);
} }
@ -58,15 +59,15 @@ public static class Tranga
private static void JobStarter(object? pgsqlContext) private static void JobStarter(object? pgsqlContext)
{ {
if (pgsqlContext is null) return; if (pgsqlContext is null) return;
var context = (PgsqlContext)pgsqlContext; PgsqlContext context = (PgsqlContext)pgsqlContext;
var TRANGA = string TRANGA =
"\n\n _______ \n|_ _|.----..---.-..-----..-----..---.-.\n | | | _|| _ || || _ || _ |\n |___| |__| |___._||__|__||___ ||___._|\n |_____| \n\n"; "\n\n _______ \n|_ _|.----..---.-..-----..-----..---.-.\n | | | _|| _ || || _ || _ |\n |___| |__| |___._||__|__||___ ||___._|\n |_____| \n\n";
Log.Info(TRANGA); Log.Info(TRANGA);
while (true) while (true)
{ {
var completedJobs = context.Jobs.Where(j => j.state == JobState.Completed).ToList(); List<Job> completedJobs = context.Jobs.Where(j => j.state == JobState.Completed).ToList();
foreach (var job in completedJobs) foreach (Job job in completedJobs)
if (job.RecurrenceMs <= 0) if (job.RecurrenceMs <= 0)
{ {
context.Jobs.Remove(job); context.Jobs.Remove(job);
@ -80,7 +81,7 @@ public static class Tranga
List<Job> runJobs = context.Jobs.Where(j => j.state <= JobState.Running).AsEnumerable() List<Job> runJobs = context.Jobs.Where(j => j.state <= JobState.Running).AsEnumerable()
.Where(j => j.NextExecution < DateTime.UtcNow).ToList(); .Where(j => j.NextExecution < DateTime.UtcNow).ToList();
foreach (var job in runJobs) foreach (Job job in runJobs)
{ {
// If the job is already running, skip it // If the job is already running, skip it
if (RunningJobs.Values.Any(j => j.JobId == job.JobId)) continue; if (RunningJobs.Values.Any(j => j.JobId == job.JobId)) continue;