From 6498aa7fe5c197eeaa866ced93327aba99284fbb Mon Sep 17 00:00:00 2001 From: Joseph Milazzo Date: Mon, 2 Jun 2025 17:23:39 -0500 Subject: [PATCH] Ensure Koreader hash is generated for existing files. --- API/Helpers/KoreaderHelper.cs | 10 ++++++---- API/Services/Tasks/Scanner/ProcessSeries.cs | 2 ++ 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/API/Helpers/KoreaderHelper.cs b/API/Helpers/KoreaderHelper.cs index 7912edf74..e779cd911 100644 --- a/API/Helpers/KoreaderHelper.cs +++ b/API/Helpers/KoreaderHelper.cs @@ -3,6 +3,7 @@ using System; using System.IO; using System.Security.Cryptography; using System.Text; +using API.Services.Tasks.Scanner.Parser; namespace API.Helpers; @@ -15,13 +16,14 @@ public static class KoreaderHelper /// /// Hashes the document according to a custom Koreader hashing algorithm. /// Look at the util.partialMD5 method in the attached link. + /// Note: Only applies to epub files /// /// The hashing algorithm is relatively quick as it only hashes ~10,000 bytes for the biggest of files. /// /// The path to the file to hash public static string HashContents(string filePath) { - if (string.IsNullOrEmpty(filePath) || !File.Exists(filePath)) + if (string.IsNullOrEmpty(filePath) || !File.Exists(filePath) || !Parser.IsEpub(filePath)) { return null; } @@ -48,9 +50,9 @@ public static class KoreaderHelper } file.Close(); - md5.TransformFinalBlock(Array.Empty(), 0, 0); + md5.TransformFinalBlock([], 0, 0); - return md5.Hash == null ? null : BitConverter.ToString(md5.Hash).Replace("-", string.Empty).ToUpper(); + return md5.Hash == null ? null : Convert.ToHexString(md5.Hash).ToUpper(); } /// @@ -63,7 +65,7 @@ public static class KoreaderHelper var fileNameBytes = Encoding.ASCII.GetBytes(fileName); var bytes = MD5.HashData(fileNameBytes); - return BitConverter.ToString(bytes).Replace("-", string.Empty); + return Convert.ToHexString(bytes); } public static void UpdateProgressDto(ProgressDto progress, string koreaderPosition) diff --git a/API/Services/Tasks/Scanner/ProcessSeries.cs b/API/Services/Tasks/Scanner/ProcessSeries.cs index c65f0fc7b..cf3a9f3fb 100644 --- a/API/Services/Tasks/Scanner/ProcessSeries.cs +++ b/API/Services/Tasks/Scanner/ProcessSeries.cs @@ -880,6 +880,8 @@ public class ProcessSeries : IProcessSeries existingFile.FileName = Parser.Parser.RemoveExtensionIfSupported(existingFile.FilePath); existingFile.FilePath = Parser.Parser.NormalizePath(existingFile.FilePath); existingFile.Bytes = fileInfo.Length; + existingFile.KoreaderHash = KoreaderHelper.HashContents(existingFile.FilePath); + // We skip updating DB here with last modified time so that metadata refresh can do it } else