diff --git a/Tranga/Chapter.cs b/Tranga/Chapter.cs index 6e40c6b9..928591af 100644 --- a/Tranga/Chapter.cs +++ b/Tranga/Chapter.cs @@ -89,13 +89,15 @@ internal bool CheckChapterIsDownloaded() return false; FileInfo? mangaArchive = null; string markerPath = Path.Join(mangaDirectory, $".{id}"); - if (this.id is not null - && File.Exists(markerPath) - && File.Exists(File.ReadAllText(markerPath))) + if (this.id is not null && File.Exists(markerPath)) { - mangaArchive = new FileInfo(File.ReadAllText(markerPath)); + if(File.Exists(File.ReadAllText(markerPath))) + mangaArchive = new FileInfo(File.ReadAllText(markerPath)); + else + File.Delete(markerPath); } - else + + if(mangaArchive is null) { FileInfo[] archives = new DirectoryInfo(mangaDirectory).GetFiles("*.cbz"); Regex volChRex = new(@"(?:Vol(?:ume)?\.([0-9]+)\D*)?Ch(?:apter)?\.([0-9]+(?:\.[0-9]+)*)"); @@ -110,6 +112,7 @@ internal bool CheckChapterIsDownloaded() return m.Groups[2].Value == t.chapterNumber; }); } + string correctPath = GetArchiveFilePath(); if(mangaArchive is not null && mangaArchive.FullName != correctPath) mangaArchive.MoveTo(correctPath, true); diff --git a/Tranga/Jobs/JobBoss.cs b/Tranga/Jobs/JobBoss.cs index 8c4542dc..5fde472f 100644 --- a/Tranga/Jobs/JobBoss.cs +++ b/Tranga/Jobs/JobBoss.cs @@ -1,6 +1,8 @@ -using System.Text.RegularExpressions; +using System.Runtime.InteropServices; +using System.Text.RegularExpressions; using Newtonsoft.Json; using Tranga.MangaConnectors; +using static System.IO.UnixFileMode; namespace Tranga.Jobs; @@ -17,7 +19,7 @@ public JobBoss(GlobalBase clone, HashSet connectors) : base(clon Log($"Next job in {jobs.MinBy(job => job.nextExecution)?.nextExecution.Subtract(DateTime.Now)} {jobs.MinBy(job => job.nextExecution)?.id}"); } - public bool AddJob(Job job) + public bool AddJob(Job job, string? jobFile = null) { if (ContainsJobLike(job)) { @@ -27,10 +29,11 @@ public bool AddJob(Job job) else { Log($"Added {job}"); - this.jobs.Add(job); - UpdateJobFile(job); - return true; + if (!this.jobs.Add(job)) + return false; + UpdateJobFile(job, jobFile); } + return true; } public void AddJobs(IEnumerable jobsToAdd) @@ -140,18 +143,18 @@ private void AddJobsToQueue(IEnumerable newJobs) private void LoadJobsList(HashSet connectors) { + Directory.CreateDirectory(TrangaSettings.jobsFolderPath); + if(RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + File.SetUnixFileMode(TrangaSettings.jobsFolderPath, UserRead | UserWrite | UserExecute | GroupRead | OtherRead); if (!Directory.Exists(TrangaSettings.jobsFolderPath)) //No jobs to load - { - Directory.CreateDirectory(TrangaSettings.jobsFolderPath); return; - } Regex idRex = new (@"(.*)\.json"); //Load Manga-Files ImportManga(); //Load json-job-files - foreach (FileInfo file in new DirectoryInfo(TrangaSettings.jobsFolderPath).EnumerateFiles().Where(fileInfo => idRex.IsMatch(fileInfo.Name))) + foreach (FileInfo file in new DirectoryInfo(TrangaSettings.jobsFolderPath).EnumerateFiles().Where(fileInfo => idRex.IsMatch(fileInfo.Name))) { Log($"Adding {file.Name}"); Job? job = JsonConvert.DeserializeObject(File.ReadAllText(file.FullName), @@ -165,8 +168,8 @@ private void LoadJobsList(HashSet connectors) else { Log($"Adding Job {job}"); - this.jobs.Add(job); - UpdateJobFile(job, file.Name); + if(!AddJob(job, file.Name)) //If we detect a duplicate, delete the file. + file.Delete(); } } @@ -218,7 +221,8 @@ internal void UpdateJobFile(Job job, string? oldFile = null) } catch (Exception e) { - Log(e.ToString()); + Log($"Error deleting {oldFilePath} job {job.id}\n{e}"); + return; //Don't export a new file when we haven't actually deleted the old one } } @@ -230,6 +234,8 @@ internal void UpdateJobFile(Job job, string? oldFile = null) while(IsFileInUse(newJobFilePath)) Thread.Sleep(10); File.WriteAllText(newJobFilePath, jobStr); + if(RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + File.SetUnixFileMode(newJobFilePath, UserRead | UserWrite | GroupRead | OtherRead); } } diff --git a/Tranga/MangaConnectors/ManhuaPlus.cs b/Tranga/MangaConnectors/ManhuaPlus.cs index 67cca628..db2e265a 100644 --- a/Tranga/MangaConnectors/ManhuaPlus.cs +++ b/Tranga/MangaConnectors/ManhuaPlus.cs @@ -108,9 +108,10 @@ private Manga ParseSinglePublicationFromHtml(HtmlDocument document, string publi Log("No genres found"); } - string yearNodeStr = document.DocumentNode - .SelectSingleNode("//aside//i[contains(concat(' ',normalize-space(@class),' '),' fa-clock ')]/../span").InnerText.Replace("\n", ""); - int year = int.Parse(yearNodeStr.Split(' ')[0].Split('/')[^1]); + Regex yearRex = new(@"(?:[0-9]{1,2}\/){2}([0-9]{2,4}) [0-9]{1,2}:[0-9]{1,2}"); + HtmlNode yearNode = document.DocumentNode.SelectSingleNode("//aside//i[contains(concat(' ',normalize-space(@class),' '),' fa-clock ')]/../span"); + Match match = yearRex.Match(yearNode.InnerText); + int year = match.Success && match.Groups[1].Success ? int.Parse(match.Groups[1].Value) : 1960; status = document.DocumentNode.SelectSingleNode("//aside//i[contains(concat(' ',normalize-space(@class),' '),' fa-rss ')]/../span").InnerText.Replace("\n", ""); switch (status.ToLower())