Hooked in character counts per page for estimation, needs some cleanup.

This commit is contained in:
Joseph Milazzo 2025-07-08 06:06:35 -05:00
parent 9b7eb11359
commit ab6669703d
8 changed files with 105 additions and 27 deletions

View file

@ -2,6 +2,7 @@
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using API.Constants;
using API.Data;
using API.DTOs.Reader;
using API.Entities.Enums;
@ -40,11 +41,14 @@ public class BookController : BaseApiController
/// <param name="chapterId"></param>
/// <returns></returns>
[HttpGet("{chapterId}/book-info")]
public async Task<ActionResult<BookInfoDto>> GetBookInfo(int chapterId)
[ResponseCache(CacheProfileName = ResponseCacheProfiles.Hour, VaryByQueryKeys = ["chapterId", "includeWordCounts"])]
public async Task<ActionResult<BookInfoDto>> GetBookInfo(int chapterId, bool includeWordCounts = false)
{
var dto = await _unitOfWork.ChapterRepository.GetChapterInfoDtoAsync(chapterId);
if (dto == null) return BadRequest(await _localizationService.Translate(User.GetUserId(), "chapter-doesnt-exist"));
var bookTitle = string.Empty;
IDictionary<int, int>? pageWordCounts = null;
switch (dto.SeriesFormat)
{
case MangaFormat.Epub:
@ -52,6 +56,12 @@ public class BookController : BaseApiController
var mangaFile = (await _unitOfWork.ChapterRepository.GetFilesForChapterAsync(chapterId))[0];
using var book = await EpubReader.OpenBookAsync(mangaFile.FilePath, BookService.LenientBookReaderOptions);
bookTitle = book.Title;
if (includeWordCounts)
{
// TODO: Cache this in temp/chapterId folder to avoid having to process file each time
pageWordCounts = await _bookService.GetWordCountsPerPage(mangaFile.FilePath);
}
break;
}
case MangaFormat.Pdf:
@ -72,9 +82,9 @@ public class BookController : BaseApiController
break;
}
return Ok(new BookInfoDto()
var info = new BookInfoDto()
{
ChapterNumber = dto.ChapterNumber,
ChapterNumber = dto.ChapterNumber,
VolumeNumber = dto.VolumeNumber,
VolumeId = dto.VolumeId,
BookTitle = bookTitle,
@ -84,7 +94,14 @@ public class BookController : BaseApiController
LibraryId = dto.LibraryId,
IsSpecial = dto.IsSpecial,
Pages = dto.Pages,
});
PageWordCounts = pageWordCounts
};
return Ok(info);
}
/// <summary>

View file

@ -41,6 +41,7 @@ public class ReaderController : BaseApiController
private readonly IEventHub _eventHub;
private readonly IScrobblingService _scrobblingService;
private readonly ILocalizationService _localizationService;
private readonly IBookService _bookService;
/// <inheritdoc />
public ReaderController(ICacheService cacheService,
@ -48,7 +49,8 @@ public class ReaderController : BaseApiController
IReaderService readerService, IBookmarkService bookmarkService,
IAccountService accountService, IEventHub eventHub,
IScrobblingService scrobblingService,
ILocalizationService localizationService)
ILocalizationService localizationService,
IBookService bookService)
{
_cacheService = cacheService;
_unitOfWork = unitOfWork;
@ -59,6 +61,7 @@ public class ReaderController : BaseApiController
_eventHub = eventHub;
_scrobblingService = scrobblingService;
_localizationService = localizationService;
_bookService = bookService;
}
/// <summary>
@ -218,11 +221,11 @@ public class ReaderController : BaseApiController
/// <remarks>This is generally the first call when attempting to read to allow pre-generation of assets needed for reading</remarks>
/// <param name="chapterId"></param>
/// <param name="extractPdf">Should Kavita extract pdf into images. Defaults to false.</param>
/// <param name="includeDimensions">Include file dimensions. Only useful for image based reading</param>
/// <param name="includeDimensions">Include file dimensions. Only useful for image-based reading</param>
/// <param name="includeWordCounts">Include epub word counts per page. Only useful for epub-based reading</param>
/// <returns></returns>
[HttpGet("chapter-info")]
[ResponseCache(CacheProfileName = ResponseCacheProfiles.Hour, VaryByQueryKeys = ["chapterId", "extractPdf", "includeDimensions"
])]
[ResponseCache(CacheProfileName = ResponseCacheProfiles.Hour, VaryByQueryKeys = ["chapterId", "extractPdf", "includeDimensions"])]
public async Task<ActionResult<ChapterInfoDto>> GetChapterInfo(int chapterId, bool extractPdf = false, bool includeDimensions = false)
{
if (chapterId <= 0) return Ok(null); // This can happen occasionally from UI, we should just ignore
@ -846,6 +849,7 @@ public class ReaderController : BaseApiController
// Patch in the reading progress
await _unitOfWork.ChapterRepository.AddChapterModifiers(User.GetUserId(), chapter);
// TODO: We need to actually use word count from the pages
if (series.Format == MangaFormat.Epub)
{
var progressCount = chapter.WordCount;