Cleanup code and merge conflict left in PR.

This commit is contained in:
Joseph Milazzo 2024-10-26 06:19:42 -05:00
parent 180b49b8ea
commit 87b1a807ea
5 changed files with 1337 additions and 109 deletions

View file

@ -16,9 +16,7 @@
<Delete Files="../openapi.json" />
<Exec Command="swagger tofile &#45;&#45;output ../openapi.json bin/$(Configuration)/$(TargetFramework)/$(AssemblyName).dll v1" />
</Target>
=======
>>>>>>> fc7f84f2bffadf9a0127d35fa01166c847b20dc0
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<DebugSymbols>false</DebugSymbols>
<ApplicationIcon>../favicon.ico</ApplicationIcon>

View file

@ -22,8 +22,8 @@ public class MangaFile : IEntityDate
public required string FilePath { get; set; }
/// <summary>
/// A hash of the document using Koreader's unique hashing algorithm
/// <remark> KoreaderHash is only available for epub types </remark>
/// </summary>
/// <remark> KoreaderHash is only available for epub types </remark>
public string? KoreaderHash { get; set; }
/// <summary>
/// Number of pages for the given file

View file

@ -1,4 +1,4 @@
using System;
using System;
using System.IO;
using API.Entities;
using API.Entities.Enums;
@ -61,12 +61,15 @@ public class MangaFileBuilder : IEntityBuilder<MangaFile>
return this;
}
/// <summary>
/// Generate the Hash on the underlying file
/// </summary>
/// <remarks>Only applicable to Epubs</remarks>
public MangaFileBuilder WithHash()
{
if (_mangaFile.Format == MangaFormat.Epub)
{
_mangaFile.KoreaderHash = KoreaderHelper.HashContents(_mangaFile.FilePath);
}
if (_mangaFile.Format != MangaFormat.Epub) return this;
_mangaFile.KoreaderHash = KoreaderHelper.HashContents(_mangaFile.FilePath);
return this;
}

View file

@ -1,12 +1,8 @@
using API.DTOs.Koreader;
using API.DTOs.Progress;
using API.Services;
using System;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
namespace API.Helpers;
@ -21,17 +17,17 @@ public static class KoreaderHelper
/// <param name="filePath">The path to the file to hash</param>
public static string HashContents(string filePath)
{
if (string.IsNullOrEmpty(filePath))
if (string.IsNullOrEmpty(filePath) || !File.Exists(filePath))
{
return null;
}
using var file = File.OpenRead(filePath);
var step = 1024;
var size = 1024;
MD5 md5 = MD5.Create();
byte[] buffer = new byte[size];
const int step = 1024;
const int size = 1024;
var md5 = MD5.Create();
var buffer = new byte[size];
for (var i = -1; i < 10; i++)
{
@ -48,15 +44,14 @@ public static class KoreaderHelper
}
file.Close();
md5.TransformFinalBlock(new byte[0], 0, 0);
return BitConverter.ToString(md5.Hash).Replace("-", string.Empty).ToUpper();
md5.TransformFinalBlock(Array.Empty<byte>(), 0, 0);
return md5.Hash == null ? null : BitConverter.ToString(md5.Hash).Replace("-", string.Empty).ToUpper();
}
/// <summary>
/// Koreader can identitfy documents based on contents or title.
/// For now we only support by contents.
/// Koreader can identify documents based on contents or title.
/// For now, we only support by contents.
/// </summary>
public static string HashTitle(string filePath)
{
@ -74,16 +69,18 @@ public static class KoreaderHelper
{
return;
}
var docNumber = path[2].Replace("DocFragment[", string.Empty).Replace("]", string.Empty);
progress.PageNum = Int32.Parse(docNumber) - 1;
progress.PageNum = int.Parse(docNumber) - 1;
var lastTag = path[5].ToUpper();
if (lastTag == "A")
{
progress.BookScrollId = null;
}
else
{
// The format that Kavita accpets as a progress string. It tells Kavita where Koreader last left off.
// The format that Kavita accepts as a progress string. It tells Kavita where Koreader last left off.
progress.BookScrollId = $"//html[1]/BODY/APP-ROOT[1]/DIV[1]/DIV[1]/DIV[1]/APP-BOOK-READER[1]/DIV[1]/DIV[2]/DIV[1]/DIV[1]/DIV[1]/{lastTag}";
}
}
@ -99,7 +96,8 @@ public static class KoreaderHelper
}
else
{
lastTag = progressDto.BookScrollId.Split('/').Last().ToLower();
var tokens = progressDto.BookScrollId.Split('/');
lastTag = tokens[^1].ToLower();
}
// The format that Koreader accepts as a progress string. It tells Koreader where Kavita last left off.
return $"/body/DocFragment[{koreaderPageNumber}]/body/div/{lastTag}";

File diff suppressed because it is too large Load diff