Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed: [OPDS] Recreate a pdf chapter's cache if images are missing (This happens when the pdf chapter is accessed first using the web reader) #3477

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

m-amazirh
Copy link

Fixed

  • Fixed: [OPDS] Recreate a pdf chapter's cache if images are missing

More details

When a user acccesses a PDF Chapter for the first time using the web reader, a corresponding cache directory is created containing only the pdf file. The pages are not extracted as png images into the cache directory.

If the user then requests a page from the same PDF Chapter using the OPDS controller/api, Kavita returns the error No such image for page {0}. Try refreshing to allow re-cache. .

This happens because the OPDS controller expects the chapter's pages (png images) to already exist in the cache directory. It does not attempt to extract them from the PDF file if the directory exists.

This code change fixes the issues by recreating the chapter's cache directory if chapter's cache directory exists and the opds contoller doesn't find in it the requested page by the user.

(note : I'm not sure if this is the best way to handle this issue and I didn't look into how the fix behaves if the user tries to requests multiple pages in parallel)

Steps to reproduce

  1. Add a new PDF to the library
  2. Access at least once the pdf/chapter using the web reader.
    • The web readers calls ReaderController#GetPdf which calls CacheService#Ensure with extractPdf=false. A chapter directory is created containing the pdf files without extracting the pages as images.
  3. Next, access the same pdf/chapter using the opds api (ex : http://localhost:5000/api/opds/81235fbc-e77c-4ad5-b789-a0967beb3b6a/image?libraryId=1&seriesId=1&volumeId=1&chapterId=1&pageNumber=50)
    • The OPDS api calls OPDSController#GetPageStreamedImage which calls CacheService#Ensure with extractPdf=true
    • But the call to CacheService#Ensure has no effect since the cache directory already exists
    • Result : the pages are not extracted in the cache directory.
    • Then the code tries to load the page from the cache directory but does not find it and returns an error No such image for page {0}. Try refreshing to allow re-cache.

When a user acccesses a PDF Chapter for the first time using the web reader, a corresponding cache directory is created containing only the pdf file. The pages are not extracted as png images into the cache directory.

If the user then requests a page from the same PDF Chapter using the OPDS controller/api, Kavita returns the error `No such image for page {0}. Try refreshing to allow re-cache.` .

This happens because the OPDS controller expects the chapter's pages (png images) to already exist in the cache directory. It does not attempt to extract them from the PDF file if the directory exists.

This code change fixes the issues by recreating the chapter's cache directory if chapter's cache directory exists and the opds contoller doesn't find in it the requested page by the user.

(I didn't look into how the fix behaves if the user tries to requests multiple pages in parallel)
@majora2007
Copy link
Member

Just got back from Holiday and have some inflight work. Once I wrap that, I'll take a look at this.

You are correct, this is 100% an oversight between the main reader and the way OPDS implementation works. I think there may be a better way to handle this, I'll get back to you once I look over the PR and the code.

@m-amazirh
Copy link
Author

Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

Successfully merging this pull request may close these issues.

2 participants