From 52d0489a1b71584c92f0aabc050f997a526aeea2 Mon Sep 17 00:00:00 2001 From: Glax Date: Mon, 16 Sep 2024 22:47:55 +0200 Subject: [PATCH] Fix duplicate mangas on startup --- Tranga/Jobs/JobBoss.cs | 39 +++++++++++++-------------------------- 1 file changed, 13 insertions(+), 26 deletions(-) diff --git a/Tranga/Jobs/JobBoss.cs b/Tranga/Jobs/JobBoss.cs index 610d70c..91d3870 100644 --- a/Tranga/Jobs/JobBoss.cs +++ b/Tranga/Jobs/JobBoss.cs @@ -148,7 +148,7 @@ public class JobBoss : GlobalBase Regex idRex = new (@"(.*)\.json"); //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), @@ -163,6 +163,7 @@ public class JobBoss : GlobalBase { Log($"Adding Job {job}"); this.jobs.Add(job); + UpdateJobFile(job, file.Name); } } @@ -188,42 +189,28 @@ public class JobBoss : GlobalBase internal void UpdateJobFile(Job job, string? oldFile = null) { string newJobFilePath = Path.Join(TrangaSettings.jobsFolderPath, $"{job.id}.json"); - - if (!this.jobs.Any(jjob => jjob.id == job.id)) + string oldFilePath = Path.Join(TrangaSettings.jobsFolderPath, oldFile); + + if (File.Exists(oldFilePath)) { + Log($"Deleting Job-file {oldFilePath}"); try { - Log($"Deleting Job-file {newJobFilePath}"); - while(IsFileInUse(newJobFilePath)) + while(IsFileInUse(oldFilePath)) Thread.Sleep(10); - File.Delete(newJobFilePath); + File.Delete(oldFilePath); } catch (Exception e) { Log(e.ToString()); } } - else - { - Log($"Exporting Job {newJobFilePath}"); - string jobStr = JsonConvert.SerializeObject(job, Formatting.Indented); - while(IsFileInUse(newJobFilePath)) - Thread.Sleep(10); - File.WriteAllText(newJobFilePath, jobStr); - } - if(oldFile is not null) - try - { - Log($"Deleting old Job-file {oldFile}"); - while(IsFileInUse(oldFile)) - Thread.Sleep(10); - File.Delete(oldFile); - } - catch (Exception e) - { - Log(e.ToString()); - } + Log($"Exporting Job {newJobFilePath}"); + string jobStr = JsonConvert.SerializeObject(job, Formatting.Indented); + while(IsFileInUse(newJobFilePath)) + Thread.Sleep(10); + File.WriteAllText(newJobFilePath, jobStr); } private void UpdateAllJobFiles()