openlibrary icon indicating copy to clipboard operation
openlibrary copied to clipboard

Give Patrons Ability to Save Open Access Books Offline (JS File System Access API)

Open mekarpeles opened this issue 1 year ago • 9 comments

Feature Request

Problem / Opportunity

For Open Library to be useful as a library to patrons, it makes sense that patrons should be able to identify certain books from within their library that they want to be available when the app is offline.

Success may look like a prototype to see if/how this works and whether it may be useful to us as a way to make files available offline via PWA mode.

Proposal

The UI could show an option (e.g. in the Unified Read Button) with a checkbox, whether to save this book in their library.

This requires the patron to grant access via JS File System Access API. The patron can specify e.g. a device and directory and this information can be stored on their account preferences object as a list.

Once access is granted, the API can be used to fetch a list of books that have been saved offline. The filename would somehow have to contain data (e.g. openlibrary edition ID) that would allow us to resolve the file to a book on Open Library. This filename would also have to work with some API to follow redirects (as editions/works can be merged and cause drift of the filename v. the record on Open Library).

A new tab could be added to the My Books page (e.g. "Offline Library").

https://gist.github.com/mekarpeles/db2cc61a55c62ef3a4204ef72530fb42

Breakdown

Related files

Refer to this map of common Endpoints:

  • macros/LoanStatus would likely be the place where we can add this feature (the option/checkbox to add a book offline). When clicked, the button would likely use JS and require grant access. Once grant access is provided, a default path of e.g. ~/.openlibrary would be used or allow the patron to specify.
  • https://github.com/internetarchive/openlibrary/wiki/Coding-Recipes#privacy-settings -- this API allows one to save values to the patron's account preferences.

Requirements Checklist

Checklist of requirements that need to be satisfied in order for this issue to be closed:

  • [ ]

Stakeholders


Instructions for Contributors

mekarpeles avatar Mar 02 '25 20:03 mekarpeles

@mekarpeles I would love to try this one.

harsha-0907 avatar Mar 05 '25 02:03 harsha-0907

@harsha-0907 I think for starters just trying to create a prototype that will add an option to the read button dropper to save a PDF file for an open access book to their FS would be a great demo.

mekarpeles avatar Mar 05 '25 22:03 mekarpeles

Hello @mekarpeles, How can i become a patron user in the local to test this feature?

harsha-0907 avatar Mar 07 '25 07:03 harsha-0907

Success: A great experiment for the File System API approach would be to select an open access book from Open Library and click save, use the PWA, open the book in the bookreader offline. Another way to prototype this is by saving a trusted book provider epub, saving it offline, and opening in Open Library via the Readium/Thorium Web reader (issue needed)

mekarpeles avatar Mar 07 '25 16:03 mekarpeles

@harsha-0907 locally, you can login with these instructions: https://github.com/internetarchive/openlibrary/blob/master/CONTRIBUTING.md#logging-in-as-admin

mekarpeles avatar Mar 10 '25 18:03 mekarpeles

@harsha-0907 Since there haven't been any updates in the last two weeks, I'm going to free up the issue and give someone else a try.

mekarpeles avatar Mar 21 '25 15:03 mekarpeles

Can I work on this issue @mekarpeles

alokdangre avatar Mar 22 '25 14:03 alokdangre

@alokdangre that would be great; can you first propose:

  1. Plan: How will you start? What first steps / minimal plan will you start with to prototype the basics of this idea (not the entire feature)?
  2. Identify: What files will you use?
  3. Help: What questions / blockers do you have that may make this PR difficult? (how can we help you?)

mekarpeles avatar Mar 23 '25 17:03 mekarpeles

Hey @mekarpeles if this is available, i would like to give this a try while the . i am not sure if it will work 100% but this could be an approach

Plan

I’d start with a minimal prototype that proves the basic authenticated offline reading lifecycle for one book

The first prototype would:

  1. Require the patron to be logged into Open Library before allowing a book to be saved offline.

  2. Verify that the patron currently has legitimate access to the book through the existing lending/read flow.

  3. Generate or obtain a browser-local encryption key while the patron is authenticated. I would not use the raw session cookie, or a substring of it, directly as the encryption key.

  4. Download the required book content and encrypt it before storing it locally.

  5. Store only the encrypted content plus the minimum metadata required to identify the edition and loan, such as:

    • Edition OLID
    • Archive.org identifier
    • loan/access identifier
    • expiration timestamp
    • encryption metadata/version
  6. Allow the book to be opened from an Offline Library while offline only if the locally stored loan authorization is still valid.

  7. Refuse access once the loan has expired, even though the encrypted file may still exist locally.

  8. Keep the first prototype limited to one book and one supported browser flow. The complete My Books integration, synchronization, merge handling, and broader PWA experience can follow in later phases.

The main goal of this phase is to prove:

Can an authenticated Open Library patron save a book locally in an encrypted form, reopen it while offline through Open Library, and have access expire according to the loan?

Identify

The initial files I would investigate/use are:

  • openlibrary/macros/LoanStatus.html

    • Existing lending/access-state UI and the natural place to expose “Save for offline”.
  • openlibrary/macros/ReadButton.html

    • Existing Read/Borrow entry point.
  • openlibrary/core/lending.py

    • Existing loan/access state and loan resolution.
    • Useful for determining whether the patron currently has legitimate access and what expiration information is available.
  • openlibrary/plugins/upstream/borrow.py

    • Existing Archive.org borrowing/authentication flow.
    • Important for understanding what credentials/access information is available for the current patron and how book-reader access is authorized.
  • openlibrary/core/models.py

    • Edition and Archive.org identifier/download helpers.
  • openlibrary/book_providers.py

    • Existing acquisition/access information and Archive.org file handling.
  • openlibrary/plugins/openlibrary/js/

    • New client-side implementation for:

      • File System Access API, if filesystem storage remains the preferred approach.
      • Web Crypto API encryption/decryption.
      • Local metadata/key handling.
      • Offline reader access checks.
  • openlibrary/plugins/openlibrary/js/index.js

    • Existing frontend JS entry point.

For the first prototype, I would avoid changing the full My Books architecture unless it is necessary to demonstrate the lifecycle.

Help / Questions & Potential Blockers

The biggest questions I would like guidance on are around authentication, key storage, and loan enforcement.

  1. How should offline access be bound to the patron? The book should only be available to a patron who has legitimately borrowed/accessed it. Is there an existing Open Library/Archive.org loan credential or identifier that the client can safely persist for this purpose?

  2. Where should the encryption key live? I don't think we should simply store a plaintext encryption key alongside the encrypted book in IndexedDB/local storage, because copying both would defeat the purpose. One possibility is to use the browser's Web Crypto APIs and keep a non-exportable key in browser-managed storage. Another possibility is to derive/unwrap a key from an authenticated Open Library credential. I would like guidance on what security model Open Library would consider appropriate.

  3. Can the Open Library login/session be used to establish offline authorization? While online, authentication can be checked against the server. However, once the patron is completely offline, the server cannot authenticate the request. Therefore, we need some locally verifiable authorization that says something equivalent to: this patron may access edition X until time Y. Is there an existing loan token/authorization mechanism we should reuse, or would this require a new server-side mechanism?

  4. What should happen when the patron logs out? If the encryption key is tied to the authenticated browser/profile, should logging out make all offline books inaccessible until that same patron logs back in? I don't think that it's preferable to allowing offline books to remain accessible to whoever uses the browser afterward, but I would like confirmation of the expected behavior.

  5. What happens if the encrypted file is copied? The intended behavior is that copying the encrypted file to another machine/browser should not make it readable because the required key/authorization is not present there and it will become one likely limitation that one person cannot use different devices to use same offline book until he's downloaded that book on another device. Is this level of protection the intended goal, understanding that a browser-based implementation cannot provide absolute DRM because the patron controls the client?

  6. How should expiration be enforced while completely offline?

    If the loan expires while the patron is offline, should the locally stored expiration timestamp be sufficient to block access? When the patron reconnects, should Open Library revalidate the loan with the server and update/remove the offline authorization?

  7. Archive.org lending/content access

    Borrowed books currently use Archive.org's authenticated/temporary access mechanisms rather than ordinary public download URLs. Can the existing lending infrastructure provide the content required for offline storage, or would this require cooperation/additional support from Internet Archive?

  8. Filesystem vs browser storage

    The original issue proposes saving to a user-selected directory. For encrypted content, I would like to confirm whether Open Library prefers: File System Access API → encrypted file or: IndexedDB → encrypted content

    Browser-managed storage may provide a cleaner way to keep the content associated with the browser/profile, while the filesystem approach gives the patron explicit control over storage.

The most important help I need before implementation is clarification on how Open Library wants offline authorization to work: specifically, what existing authenticated loan information can safely be persisted and how that authorization should survive a completely offline period without turning the downloaded content into a permanently usable copy.

CipherZ3r0 avatar Sep 04 '26 06:09 CipherZ3r0