Skip to content

Kavita is a fast, feature rich, cross platform reading server. Built with the goal of being a full solution for all your reading needs. Setup your own server and share your reading collection with your friends and family.

Fixed an off by 1 issue with reading manga.

Joseph Milazzo9a502417346d
diff --git a/API/Controllers/ReaderController.cs b/API/Controllers/ReaderController.cs
index 478a5dd58..4ead36701 100644
--- a/API/Controllers/ReaderController.cs
+++ b/API/Controllers/ReaderController.cs
@@ -34,12 +34,22 @@ namespace API.Controllers
var chapter = await _cacheService.Ensure(chapterId);
if (chapter == null) return BadRequest("There was an issue finding image file for reading");
+
+ // TODO: This code works, but might need bounds checking. UI can send bad data
+ // if (page >= chapter.Pages)
+ // {
+ // page = chapter.Pages - 1;
+ // } else if (page < 0)
+ // {
+ // page = 0;
+ // }
var (path, mangaFile) = await _cacheService.GetCachedPagePath(chapter, page);
if (string.IsNullOrEmpty(path)) return BadRequest($"No such image for page {page}");
var file = await _directoryService.ReadImageAsync(path);
file.Page = page;
file.MangaFileName = mangaFile.FilePath;
+ file.NeedsSplitting = file.Width > file.Height;
return Ok(file);
}
diff --git a/API/Services/CacheService.cs b/API/Services/CacheService.cs
index 1092c57c1..a37812f53 100644
--- a/API/Services/CacheService.cs
+++ b/API/Services/CacheService.cs
@@ -106,12 +106,18 @@ namespace API.Services
var chapterFiles = chapter.Files ?? await _unitOfWork.VolumeRepository.GetFilesForChapter(chapter.Id);
foreach (var mangaFile in chapterFiles)
{
- if (page < (mangaFile.NumberOfPages + pagesSoFar))
+ if (page <= (mangaFile.NumberOfPages + pagesSoFar))
{
var path = GetCachePath(chapter.Id);
var files = _directoryService.GetFiles(path, Parser.Parser.ImageFileExtensions);
Array.Sort(files, _numericComparer);
+ // Since array is 0 based, we need to keep that in account (only affects last image)
+ if (page - 1 == files.Length)
+ {
+ return (files.ElementAt(page - 1 - pagesSoFar), mangaFile);
+ }
+
return (files.ElementAt(page - pagesSoFar), mangaFile);
}