Fixed an off by 1 issue with reading manga.

This commit is contained in:
Joseph Milazzo 2021-02-12 17:43:19 -06:00
parent a6b49052b9
commit 9a50241734
2 changed files with 17 additions and 1 deletions

View File

@ -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);
}

View File

@ -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);
}