Merge branch 'develop' into bugfix/cleanup
This commit is contained in:
commit
2c00f1f124
40 changed files with 5980 additions and 809 deletions
118
API/Controllers/KoreaderController.cs
Normal file
118
API/Controllers/KoreaderController.cs
Normal file
|
|
@ -0,0 +1,118 @@
|
|||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using API.Data;
|
||||
using API.Data.Repositories;
|
||||
using API.DTOs.Koreader;
|
||||
using API.Entities;
|
||||
using API.Services;
|
||||
using Kavita.Common;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using static System.Net.WebRequestMethods;
|
||||
|
||||
namespace API.Controllers;
|
||||
#nullable enable
|
||||
|
||||
/// <summary>
|
||||
/// The endpoint to interface with Koreader's Progress Sync plugin.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Koreader uses a different form of authentication. It stores the username and password in headers.
|
||||
/// https://github.com/koreader/koreader/blob/master/plugins/kosync.koplugin/KOSyncClient.lua
|
||||
/// </remarks>
|
||||
[AllowAnonymous]
|
||||
public class KoreaderController : BaseApiController
|
||||
{
|
||||
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
private readonly ILocalizationService _localizationService;
|
||||
private readonly IKoreaderService _koreaderService;
|
||||
private readonly ILogger<KoreaderController> _logger;
|
||||
|
||||
public KoreaderController(IUnitOfWork unitOfWork, ILocalizationService localizationService,
|
||||
IKoreaderService koreaderService, ILogger<KoreaderController> logger)
|
||||
{
|
||||
_unitOfWork = unitOfWork;
|
||||
_localizationService = localizationService;
|
||||
_koreaderService = koreaderService;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
// We won't allow users to be created from Koreader. Rather, they
|
||||
// must already have an account.
|
||||
/*
|
||||
[HttpPost("/users/create")]
|
||||
public IActionResult CreateUser(CreateUserRequest request)
|
||||
{
|
||||
}
|
||||
*/
|
||||
|
||||
[HttpGet("{apiKey}/users/auth")]
|
||||
public async Task<IActionResult> Authenticate(string apiKey)
|
||||
{
|
||||
var userId = await GetUserId(apiKey);
|
||||
var user = await _unitOfWork.UserRepository.GetUserByIdAsync(userId);
|
||||
if (user == null) return Unauthorized();
|
||||
|
||||
return Ok(new { username = user.UserName });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Syncs book progress with Kavita. Will attempt to save the underlying reader position if possible.
|
||||
/// </summary>
|
||||
/// <param name="apiKey"></param>
|
||||
/// <param name="request"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPut("{apiKey}/syncs/progress")]
|
||||
public async Task<ActionResult<KoreaderProgressUpdateDto>> UpdateProgress(string apiKey, KoreaderBookDto request)
|
||||
{
|
||||
try
|
||||
{
|
||||
var userId = await GetUserId(apiKey);
|
||||
await _koreaderService.SaveProgress(request, userId);
|
||||
|
||||
return Ok(new KoreaderProgressUpdateDto{ Document = request.Document, Timestamp = DateTime.UtcNow });
|
||||
}
|
||||
catch (KavitaException ex)
|
||||
{
|
||||
return BadRequest(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets book progress from Kavita, if not found will return a 400
|
||||
/// </summary>
|
||||
/// <param name="apiKey"></param>
|
||||
/// <param name="ebookHash"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("{apiKey}/syncs/progress/{ebookHash}")]
|
||||
public async Task<ActionResult<KoreaderBookDto>> GetProgress(string apiKey, string ebookHash)
|
||||
{
|
||||
try
|
||||
{
|
||||
var userId = await GetUserId(apiKey);
|
||||
var response = await _koreaderService.GetProgress(ebookHash, userId);
|
||||
_logger.LogDebug("Koreader response progress: {Progress}", response.Progress);
|
||||
|
||||
return Ok(response);
|
||||
}
|
||||
catch (KavitaException ex)
|
||||
{
|
||||
return BadRequest(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<int> GetUserId(string apiKey)
|
||||
{
|
||||
try
|
||||
{
|
||||
return await _unitOfWork.UserRepository.GetUserIdByApiKeyAsync(apiKey);
|
||||
}
|
||||
catch
|
||||
{
|
||||
throw new KavitaException(await _localizationService.Get("en", "user-doesnt-exist"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -254,7 +254,7 @@ public class ScrobblingController : BaseApiController
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a hold against the Series for user's scrobbling
|
||||
/// Remove a hold against the Series for user's scrobbling
|
||||
/// </summary>
|
||||
/// <param name="seriesId"></param>
|
||||
/// <returns></returns>
|
||||
|
|
@ -281,4 +281,18 @@ public class ScrobblingController : BaseApiController
|
|||
var user = await _unitOfWork.UserRepository.GetUserByIdAsync(User.GetUserId());
|
||||
return Ok(user is {HasRunScrobbleEventGeneration: true});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Delete the given scrobble events if they belong to that user
|
||||
/// </summary>
|
||||
/// <param name="eventIds"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost("bulk-remove-events")]
|
||||
public async Task<ActionResult> BulkRemoveScrobbleEvents(IList<long> eventIds)
|
||||
{
|
||||
var events = await _unitOfWork.ScrobbleRepository.GetUserEvents(User.GetUserId(), eventIds);
|
||||
_unitOfWork.ScrobbleRepository.Remove(events);
|
||||
await _unitOfWork.CommitAsync();
|
||||
return Ok();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
33
API/DTOs/Koreader/KoreaderBookDto.cs
Normal file
33
API/DTOs/Koreader/KoreaderBookDto.cs
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
using API.DTOs.Progress;
|
||||
|
||||
namespace API.DTOs.Koreader;
|
||||
|
||||
/// <summary>
|
||||
/// This is the interface for receiving and sending updates to Koreader. The only fields
|
||||
/// that are actually used are the Document and Progress fields.
|
||||
/// </summary>
|
||||
public class KoreaderBookDto
|
||||
{
|
||||
/// <summary>
|
||||
/// This is the Koreader hash of the book. It is used to identify the book.
|
||||
/// </summary>
|
||||
public string Document { get; set; }
|
||||
/// <summary>
|
||||
/// A randomly generated id from the koreader device. Only used to maintain the Koreader interface.
|
||||
/// </summary>
|
||||
public string Device_id { get; set; }
|
||||
/// <summary>
|
||||
/// The Koreader device name. Only used to maintain the Koreader interface.
|
||||
/// </summary>
|
||||
public string Device { get; set; }
|
||||
/// <summary>
|
||||
/// Percent progress of the book. Only used to maintain the Koreader interface.
|
||||
/// </summary>
|
||||
public float Percentage { get; set; }
|
||||
/// <summary>
|
||||
/// An XPath string read by Koreader to determine the location within the epub.
|
||||
/// Essentially, it is Koreader's equivalent to ProgressDto.BookScrollId.
|
||||
/// </summary>
|
||||
/// <seealso cref="ProgressDto.BookScrollId"/>
|
||||
public string Progress { get; set; }
|
||||
}
|
||||
15
API/DTOs/Koreader/KoreaderProgressUpdateDto.cs
Normal file
15
API/DTOs/Koreader/KoreaderProgressUpdateDto.cs
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
using System;
|
||||
|
||||
namespace API.DTOs.Koreader;
|
||||
|
||||
public class KoreaderProgressUpdateDto
|
||||
{
|
||||
/// <summary>
|
||||
/// This is the Koreader hash of the book. It is used to identify the book.
|
||||
/// </summary>
|
||||
public string Document { get; set; }
|
||||
/// <summary>
|
||||
/// UTC Timestamp to return to KOReader
|
||||
/// </summary>
|
||||
public DateTime Timestamp { get; set; }
|
||||
}
|
||||
|
|
@ -5,6 +5,7 @@ namespace API.DTOs.Scrobbling;
|
|||
|
||||
public sealed record ScrobbleEventDto
|
||||
{
|
||||
public long Id { get; init; }
|
||||
public string SeriesName { get; set; }
|
||||
public int SeriesId { get; set; }
|
||||
public int LibraryId { get; set; }
|
||||
|
|
|
|||
|
|
@ -8,5 +8,6 @@ public sealed record ScrobbleResponseDto
|
|||
{
|
||||
public bool Successful { get; set; }
|
||||
public string? ErrorMessage { get; set; }
|
||||
public string? ExtraInformation {get; set;}
|
||||
public int RateLeft { get; set; }
|
||||
}
|
||||
|
|
|
|||
3574
API/Data/Migrations/20250519151126_KoreaderHash.Designer.cs
generated
Normal file
3574
API/Data/Migrations/20250519151126_KoreaderHash.Designer.cs
generated
Normal file
File diff suppressed because it is too large
Load diff
28
API/Data/Migrations/20250519151126_KoreaderHash.cs
Normal file
28
API/Data/Migrations/20250519151126_KoreaderHash.cs
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace API.Data.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class KoreaderHash : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.AddColumn<string>(
|
||||
name: "KoreaderHash",
|
||||
table: "MangaFile",
|
||||
type: "TEXT",
|
||||
nullable: true);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropColumn(
|
||||
name: "KoreaderHash",
|
||||
table: "MangaFile");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1408,6 +1408,9 @@ namespace API.Data.Migrations
|
|||
b.Property<int>("Format")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<string>("KoreaderHash")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<DateTime>("LastFileAnalysis")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
|
|
|
|||
|
|
@ -5,11 +5,13 @@ using API.Entities;
|
|||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace API.Data.Repositories;
|
||||
#nullable enable
|
||||
|
||||
public interface IMangaFileRepository
|
||||
{
|
||||
void Update(MangaFile file);
|
||||
Task<IList<MangaFile>> GetAllWithMissingExtension();
|
||||
Task<MangaFile?> GetByKoreaderHash(string hash);
|
||||
}
|
||||
|
||||
public class MangaFileRepository : IMangaFileRepository
|
||||
|
|
@ -32,4 +34,13 @@ public class MangaFileRepository : IMangaFileRepository
|
|||
.Where(f => string.IsNullOrEmpty(f.Extension))
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<MangaFile?> GetByKoreaderHash(string hash)
|
||||
{
|
||||
if (string.IsNullOrEmpty(hash)) return null;
|
||||
|
||||
return await _context.MangaFile
|
||||
.FirstOrDefaultAsync(f => f.KoreaderHash != null &&
|
||||
f.KoreaderHash.Equals(hash.ToUpper()));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -29,8 +29,23 @@ public interface IScrobbleRepository
|
|||
Task<IList<ScrobbleError>> GetAllScrobbleErrorsForSeries(int seriesId);
|
||||
Task ClearScrobbleErrors();
|
||||
Task<bool> HasErrorForSeries(int seriesId);
|
||||
Task<ScrobbleEvent?> GetEvent(int userId, int seriesId, ScrobbleEventType eventType);
|
||||
/// <summary>
|
||||
/// Get all events for a specific user and type
|
||||
/// </summary>
|
||||
/// <param name="userId"></param>
|
||||
/// <param name="seriesId"></param>
|
||||
/// <param name="eventType"></param>
|
||||
/// <param name="isNotProcessed">If true, only returned not processed events</param>
|
||||
/// <returns></returns>
|
||||
Task<ScrobbleEvent?> GetEvent(int userId, int seriesId, ScrobbleEventType eventType, bool isNotProcessed = false);
|
||||
Task<IEnumerable<ScrobbleEvent>> GetUserEventsForSeries(int userId, int seriesId);
|
||||
/// <summary>
|
||||
/// Return the events with given ids, when belonging to the passed user
|
||||
/// </summary>
|
||||
/// <param name="userId"></param>
|
||||
/// <param name="scrobbleEventIds"></param>
|
||||
/// <returns></returns>
|
||||
Task<IList<ScrobbleEvent>> GetUserEvents(int userId, IList<long> scrobbleEventIds);
|
||||
Task<PagedList<ScrobbleEventDto>> GetUserEvents(int userId, ScrobbleEventFilter filter, UserParams pagination);
|
||||
Task<IList<ScrobbleEvent>> GetAllEventsForSeries(int seriesId);
|
||||
Task<IList<ScrobbleEvent>> GetAllEventsWithSeriesIds(IEnumerable<int> seriesIds);
|
||||
|
|
@ -146,22 +161,32 @@ public class ScrobbleRepository : IScrobbleRepository
|
|||
return await _context.ScrobbleError.AnyAsync(n => n.SeriesId == seriesId);
|
||||
}
|
||||
|
||||
public async Task<ScrobbleEvent?> GetEvent(int userId, int seriesId, ScrobbleEventType eventType)
|
||||
public async Task<ScrobbleEvent?> GetEvent(int userId, int seriesId, ScrobbleEventType eventType, bool isNotProcessed = false)
|
||||
{
|
||||
return await _context.ScrobbleEvent.FirstOrDefaultAsync(e =>
|
||||
e.AppUserId == userId && e.SeriesId == seriesId && e.ScrobbleEventType == eventType);
|
||||
return await _context.ScrobbleEvent
|
||||
.Where(e => e.AppUserId == userId && e.SeriesId == seriesId && e.ScrobbleEventType == eventType)
|
||||
.WhereIf(isNotProcessed, e => !e.IsProcessed)
|
||||
.OrderBy(e => e.LastModifiedUtc)
|
||||
.FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<ScrobbleEvent>> GetUserEventsForSeries(int userId, int seriesId)
|
||||
{
|
||||
return await _context.ScrobbleEvent
|
||||
.Where(e => e.AppUserId == userId && !e.IsProcessed)
|
||||
.Where(e => e.AppUserId == userId && !e.IsProcessed && e.SeriesId == seriesId)
|
||||
.Include(e => e.Series)
|
||||
.OrderBy(e => e.LastModifiedUtc)
|
||||
.AsSplitQuery()
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<IList<ScrobbleEvent>> GetUserEvents(int userId, IList<long> scrobbleEventIds)
|
||||
{
|
||||
return await _context.ScrobbleEvent
|
||||
.Where(e => e.AppUserId == userId && scrobbleEventIds.Contains(e.Id))
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<PagedList<ScrobbleEventDto>> GetUserEvents(int userId, ScrobbleEventFilter filter, UserParams pagination)
|
||||
{
|
||||
var query = _context.ScrobbleEvent
|
||||
|
|
|
|||
|
|
@ -21,6 +21,11 @@ public class MangaFile : IEntityDate
|
|||
/// </summary>
|
||||
public required string FilePath { get; set; }
|
||||
/// <summary>
|
||||
/// A hash of the document using Koreader's unique hashing algorithm
|
||||
/// </summary>
|
||||
/// <remark> KoreaderHash is only available for epub types </remark>
|
||||
public string? KoreaderHash { get; set; }
|
||||
/// <summary>
|
||||
/// Number of pages for the given file
|
||||
/// </summary>
|
||||
public int Pages { get; set; }
|
||||
|
|
|
|||
|
|
@ -68,4 +68,14 @@ public class ScrobbleEvent : IEntityDate
|
|||
public DateTime LastModified { get; set; }
|
||||
public DateTime CreatedUtc { get; set; }
|
||||
public DateTime LastModifiedUtc { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Sets the ErrorDetail and marks the event as <see cref="IsErrored"/>
|
||||
/// </summary>
|
||||
/// <param name="errorMessage"></param>
|
||||
public void SetErrorMessage(string errorMessage)
|
||||
{
|
||||
ErrorDetails = errorMessage;
|
||||
IsErrored = true;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -55,6 +55,7 @@ public static class ApplicationServiceExtensions
|
|||
services.AddScoped<IRatingService, RatingService>();
|
||||
services.AddScoped<IPersonService, PersonService>();
|
||||
services.AddScoped<IReadingProfileService, ReadingProfileService>();
|
||||
services.AddScoped<IKoreaderService, KoreaderService>();
|
||||
|
||||
services.AddScoped<IScannerService, ScannerService>();
|
||||
services.AddScoped<IProcessSeries, ProcessSeries>();
|
||||
|
|
@ -75,6 +76,7 @@ public static class ApplicationServiceExtensions
|
|||
services.AddScoped<ISettingsService, SettingsService>();
|
||||
|
||||
|
||||
services.AddScoped<IKavitaPlusApiService, KavitaPlusApiService>();
|
||||
services.AddScoped<IScrobblingService, ScrobblingService>();
|
||||
services.AddScoped<ILicenseService, LicenseService>();
|
||||
services.AddScoped<IExternalMetadataService, ExternalMetadataService>();
|
||||
|
|
|
|||
|
|
@ -82,6 +82,12 @@ public static class RestrictByAgeExtensions
|
|||
sm.Metadata.AgeRating <= restriction.AgeRating && sm.Metadata.AgeRating > AgeRating.Unknown));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns all Genres where any of the linked Series/Chapters are less than or equal to restriction age rating
|
||||
/// </summary>
|
||||
/// <param name="queryable"></param>
|
||||
/// <param name="restriction"></param>
|
||||
/// <returns></returns>
|
||||
public static IQueryable<Genre> RestrictAgainstAgeRestriction(this IQueryable<Genre> queryable, AgeRestriction restriction)
|
||||
{
|
||||
if (restriction.AgeRating == AgeRating.NotApplicable) return queryable;
|
||||
|
|
|
|||
46
API/Helpers/Builders/KoreaderBookDtoBuilder.cs
Normal file
46
API/Helpers/Builders/KoreaderBookDtoBuilder.cs
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
using System;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using API.DTOs.Koreader;
|
||||
|
||||
namespace API.Helpers.Builders;
|
||||
|
||||
public class KoreaderBookDtoBuilder : IEntityBuilder<KoreaderBookDto>
|
||||
{
|
||||
private readonly KoreaderBookDto _dto;
|
||||
public KoreaderBookDto Build() => _dto;
|
||||
|
||||
public KoreaderBookDtoBuilder(string documentHash)
|
||||
{
|
||||
_dto = new KoreaderBookDto()
|
||||
{
|
||||
Document = documentHash,
|
||||
Device = "Kavita"
|
||||
};
|
||||
}
|
||||
|
||||
public KoreaderBookDtoBuilder WithDocument(string documentHash)
|
||||
{
|
||||
_dto.Document = documentHash;
|
||||
return this;
|
||||
}
|
||||
|
||||
public KoreaderBookDtoBuilder WithProgress(string progress)
|
||||
{
|
||||
_dto.Progress = progress;
|
||||
return this;
|
||||
}
|
||||
|
||||
public KoreaderBookDtoBuilder WithPercentage(int? pageNum, int pages)
|
||||
{
|
||||
_dto.Percentage = (pageNum ?? 0) / (float) pages;
|
||||
return this;
|
||||
}
|
||||
|
||||
public KoreaderBookDtoBuilder WithDeviceId(string installId, int userId)
|
||||
{
|
||||
var hash = SHA256.HashData(Encoding.UTF8.GetBytes(installId + userId));
|
||||
_dto.Device_id = Convert.ToHexString(hash);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
|
@ -60,4 +60,17 @@ public class MangaFileBuilder : IEntityBuilder<MangaFile>
|
|||
_mangaFile.Id = Math.Max(id, 0);
|
||||
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) return this;
|
||||
|
||||
_mangaFile.KoreaderHash = KoreaderHelper.HashContents(_mangaFile.FilePath);
|
||||
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
113
API/Helpers/KoreaderHelper.cs
Normal file
113
API/Helpers/KoreaderHelper.cs
Normal file
|
|
@ -0,0 +1,113 @@
|
|||
using API.DTOs.Progress;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using API.Services.Tasks.Scanner.Parser;
|
||||
|
||||
namespace API.Helpers;
|
||||
|
||||
/// <summary>
|
||||
/// All things related to Koreader
|
||||
/// </summary>
|
||||
/// <remarks>Original developer: https://github.com/MFDeAngelo</remarks>
|
||||
public static class KoreaderHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// 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
|
||||
/// </summary>
|
||||
/// <remarks>The hashing algorithm is relatively quick as it only hashes ~10,000 bytes for the biggest of files.</remarks>
|
||||
/// <see href="https://github.com/koreader/koreader/blob/master/frontend/util.lua#L1040"/>
|
||||
/// <param name="filePath">The path to the file to hash</param>
|
||||
public static string HashContents(string filePath)
|
||||
{
|
||||
if (string.IsNullOrEmpty(filePath) || !File.Exists(filePath) || !Parser.IsEpub(filePath))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
using var file = File.OpenRead(filePath);
|
||||
|
||||
const int step = 1024;
|
||||
const int size = 1024;
|
||||
var md5 = MD5.Create();
|
||||
var buffer = new byte[size];
|
||||
|
||||
for (var i = -1; i < 10; i++)
|
||||
{
|
||||
file.Position = step << 2 * i;
|
||||
var bytesRead = file.Read(buffer, 0, size);
|
||||
if (bytesRead > 0)
|
||||
{
|
||||
md5.TransformBlock(buffer, 0, bytesRead, buffer, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
file.Close();
|
||||
md5.TransformFinalBlock([], 0, 0);
|
||||
|
||||
return md5.Hash == null ? null : Convert.ToHexString(md5.Hash).ToUpper();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Koreader can identify documents based on contents or title.
|
||||
/// For now, we only support by contents.
|
||||
/// </summary>
|
||||
public static string HashTitle(string filePath)
|
||||
{
|
||||
var fileName = Path.GetFileName(filePath);
|
||||
var fileNameBytes = Encoding.ASCII.GetBytes(fileName);
|
||||
var bytes = MD5.HashData(fileNameBytes);
|
||||
|
||||
return Convert.ToHexString(bytes);
|
||||
}
|
||||
|
||||
public static void UpdateProgressDto(ProgressDto progress, string koreaderPosition)
|
||||
{
|
||||
var path = koreaderPosition.Split('/');
|
||||
if (path.Length < 6)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var docNumber = path[2].Replace("DocFragment[", string.Empty).Replace("]", string.Empty);
|
||||
progress.PageNum = int.Parse(docNumber) - 1;
|
||||
var lastTag = path[5].ToUpper();
|
||||
|
||||
if (lastTag == "A")
|
||||
{
|
||||
progress.BookScrollId = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
// 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}";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static string GetKoreaderPosition(ProgressDto progressDto)
|
||||
{
|
||||
string lastTag;
|
||||
var koreaderPageNumber = progressDto.PageNum + 1;
|
||||
|
||||
if (string.IsNullOrEmpty(progressDto.BookScrollId))
|
||||
{
|
||||
lastTag = "a";
|
||||
}
|
||||
else
|
||||
{
|
||||
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}";
|
||||
}
|
||||
}
|
||||
90
API/Services/KoreaderService.cs
Normal file
90
API/Services/KoreaderService.cs
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
using System.Threading.Tasks;
|
||||
using API.Data;
|
||||
using API.DTOs.Koreader;
|
||||
using API.DTOs.Progress;
|
||||
using API.Helpers;
|
||||
using API.Helpers.Builders;
|
||||
using Kavita.Common;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace API.Services;
|
||||
|
||||
#nullable enable
|
||||
|
||||
public interface IKoreaderService
|
||||
{
|
||||
Task SaveProgress(KoreaderBookDto koreaderBookDto, int userId);
|
||||
Task<KoreaderBookDto> GetProgress(string bookHash, int userId);
|
||||
}
|
||||
|
||||
public class KoreaderService : IKoreaderService
|
||||
{
|
||||
private readonly IReaderService _readerService;
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
private readonly ILocalizationService _localizationService;
|
||||
private readonly ILogger<KoreaderService> _logger;
|
||||
|
||||
public KoreaderService(IReaderService readerService, IUnitOfWork unitOfWork, ILocalizationService localizationService, ILogger<KoreaderService> logger)
|
||||
{
|
||||
_readerService = readerService;
|
||||
_unitOfWork = unitOfWork;
|
||||
_localizationService = localizationService;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Given a Koreader hash, locate the underlying file and generate/update a progress event.
|
||||
/// </summary>
|
||||
/// <param name="koreaderBookDto"></param>
|
||||
/// <param name="userId"></param>
|
||||
public async Task SaveProgress(KoreaderBookDto koreaderBookDto, int userId)
|
||||
{
|
||||
_logger.LogDebug("Saving Koreader progress for {UserId}: {KoreaderProgress}", userId, koreaderBookDto.Progress);
|
||||
var file = await _unitOfWork.MangaFileRepository.GetByKoreaderHash(koreaderBookDto.Document);
|
||||
if (file == null) throw new KavitaException(await _localizationService.Translate(userId, "file-missing"));
|
||||
|
||||
var userProgressDto = await _unitOfWork.AppUserProgressRepository.GetUserProgressDtoAsync(file.ChapterId, userId);
|
||||
if (userProgressDto == null)
|
||||
{
|
||||
var chapterDto = await _unitOfWork.ChapterRepository.GetChapterDtoAsync(file.ChapterId);
|
||||
if (chapterDto == null) throw new KavitaException(await _localizationService.Translate(userId, "chapter-doesnt-exist"));
|
||||
|
||||
var volumeDto = await _unitOfWork.VolumeRepository.GetVolumeByIdAsync(chapterDto.VolumeId);
|
||||
if (volumeDto == null) throw new KavitaException(await _localizationService.Translate(userId, "volume-doesnt-exist"));
|
||||
|
||||
userProgressDto = new ProgressDto()
|
||||
{
|
||||
ChapterId = file.ChapterId,
|
||||
VolumeId = chapterDto.VolumeId,
|
||||
SeriesId = volumeDto.SeriesId,
|
||||
};
|
||||
}
|
||||
// Update the bookScrollId if possible
|
||||
KoreaderHelper.UpdateProgressDto(userProgressDto, koreaderBookDto.Progress);
|
||||
|
||||
await _readerService.SaveReadingProgress(userProgressDto, userId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a Koreader Dto representing current book and the progress within
|
||||
/// </summary>
|
||||
/// <param name="bookHash"></param>
|
||||
/// <param name="userId"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<KoreaderBookDto> GetProgress(string bookHash, int userId)
|
||||
{
|
||||
var settingsDto = await _unitOfWork.SettingsRepository.GetSettingsDtoAsync();
|
||||
|
||||
var file = await _unitOfWork.MangaFileRepository.GetByKoreaderHash(bookHash);
|
||||
|
||||
if (file == null) throw new KavitaException(await _localizationService.Translate(userId, "file-missing"));
|
||||
|
||||
var progressDto = await _unitOfWork.AppUserProgressRepository.GetUserProgressDtoAsync(file.ChapterId, userId);
|
||||
var koreaderProgress = KoreaderHelper.GetKoreaderPosition(progressDto);
|
||||
|
||||
return new KoreaderBookDtoBuilder(bookHash).WithProgress(koreaderProgress)
|
||||
.WithPercentage(progressDto?.PageNum, file.Pages)
|
||||
.WithDeviceId(settingsDto.InstallId, userId)
|
||||
.Build();
|
||||
}
|
||||
}
|
||||
75
API/Services/Plus/KavitaPlusApiService.cs
Normal file
75
API/Services/Plus/KavitaPlusApiService.cs
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
#nullable enable
|
||||
using System.Threading.Tasks;
|
||||
using API.DTOs.Scrobbling;
|
||||
using API.Extensions;
|
||||
using Flurl.Http;
|
||||
using Kavita.Common;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace API.Services.Plus;
|
||||
|
||||
/// <summary>
|
||||
/// All Http requests to K+ should be contained in this service, the service will not handle any errors.
|
||||
/// This is expected from the caller.
|
||||
/// </summary>
|
||||
public interface IKavitaPlusApiService
|
||||
{
|
||||
Task<bool> HasTokenExpired(string license, string token, ScrobbleProvider provider);
|
||||
Task<int> GetRateLimit(string license, string token);
|
||||
Task<ScrobbleResponseDto> PostScrobbleUpdate(ScrobbleDto data, string license);
|
||||
}
|
||||
|
||||
public class KavitaPlusApiService(ILogger<KavitaPlusApiService> logger): IKavitaPlusApiService
|
||||
{
|
||||
private const string ScrobblingPath = "/api/scrobbling/";
|
||||
|
||||
public async Task<bool> HasTokenExpired(string license, string token, ScrobbleProvider provider)
|
||||
{
|
||||
var res = await Get(ScrobblingPath + "valid-key?provider=" + provider + "&key=" + token, license, token);
|
||||
var str = await res.GetStringAsync();
|
||||
return bool.Parse(str);
|
||||
}
|
||||
|
||||
public async Task<int> GetRateLimit(string license, string token)
|
||||
{
|
||||
var res = await Get(ScrobblingPath + "rate-limit?accessToken=" + token, license, token);
|
||||
var str = await res.GetStringAsync();
|
||||
return int.Parse(str);
|
||||
}
|
||||
|
||||
public async Task<ScrobbleResponseDto> PostScrobbleUpdate(ScrobbleDto data, string license)
|
||||
{
|
||||
return await PostAndReceive<ScrobbleResponseDto>(ScrobblingPath + "update", data, license);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Send a GET request to K+
|
||||
/// </summary>
|
||||
/// <param name="url">only path of the uri, the host is added</param>
|
||||
/// <param name="license"></param>
|
||||
/// <param name="aniListToken"></param>
|
||||
/// <returns></returns>
|
||||
private static async Task<IFlurlResponse> Get(string url, string license, string? aniListToken = null)
|
||||
{
|
||||
return await (Configuration.KavitaPlusApiUrl + url)
|
||||
.WithKavitaPlusHeaders(license, aniListToken)
|
||||
.GetAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Send a POST request to K+
|
||||
/// </summary>
|
||||
/// <param name="url">only path of the uri, the host is added</param>
|
||||
/// <param name="body"></param>
|
||||
/// <param name="license"></param>
|
||||
/// <param name="aniListToken"></param>
|
||||
/// <typeparam name="T">Return type</typeparam>
|
||||
/// <returns></returns>
|
||||
private static async Task<T> PostAndReceive<T>(string url, object body, string license, string? aniListToken = null)
|
||||
{
|
||||
return await (Configuration.KavitaPlusApiUrl + url)
|
||||
.WithKavitaPlusHeaders(license, aniListToken)
|
||||
.PostJsonAsync(body)
|
||||
.ReceiveJson<T>();
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load diff
|
|
@ -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
|
||||
|
|
@ -888,6 +890,7 @@ public class ProcessSeries : IProcessSeries
|
|||
var file = new MangaFileBuilder(info.FullFilePath, info.Format, _readingItemService.GetNumberOfPages(info.FullFilePath, info.Format))
|
||||
.WithExtension(fileInfo.Extension)
|
||||
.WithBytes(fileInfo.Length)
|
||||
.WithHash()
|
||||
.Build();
|
||||
chapter.Files.Add(file);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue