ed8956e9ed
* feat(koplugin/library): data layer + busted harness + design doc - LibraryStore: per-user SQLite index merging cloud + local books by partial-md5 hash. listBooks/listBookshelfBooks/listBookshelfGroups, upsertBook with cloud_present/local_present OR-merge + _force/clear sentinels, parseSyncRow, getChangedBooks for tombstone push. - EXTS table mirroring web's document.ts. - busted harness with KOReader stubs (G_reader_settings, DataStorage, lua-ljsqlite3 against :memory:); spec_helper, exts_spec, smoke_spec, librarystore_spec covering schema, sort, group nesting, dedupe. - Library design doc + spec README. - pnpm test:lua wired through root + app package.json; lint-koplugin recurses into library/ + spec/. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * feat(koplugin/library): cloud sync — push, pull, upload, delete, downloads - Spore methods: pullBooks (incremental /sync), getDownloadUrl, getUploadUrl, listFiles, deleteFile. - syncbooks.lua: pushBook + pushChangedBooks (advances watermark to max(updated_at, deleted_at)), syncBooks(opts, mode) for push/pull/both, downloadBook + downloadCover (sync socket.http with file sink; cover download via fork+poll with single-slot queue + visible-page filter + coalesced refresh), uploadBook (presigned-PUT flow + best-effort cover), deleteCloudFiles (list-then-delete-each, mirrors cloudService.deleteBook). - SyncAuth.withFreshToken wrapper resolves the ensureClient race; 401/403 unified across syncconfig + syncannotations. - Cloud + local book covers shared by partial-md5 hash; cover.png cached at <settings>/readest_covers/<hash>.png with sentinel for known 404s. - syncbooks_spec covers row-to-wire conversion + file_key shape. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * feat(koplugin/library): local discovery — sidecar scanner + cover provider - localscanner.lightScan iterates ReadHistory entries, reads partial_md5_checksum from .sdr sidecars, and upserts local rows. Slow filesystem walks deferred to fullSidecarWalk (24h-gated). - coverprovider wraps BookInfoManager:getBookInfo for local books with graceful FakeCover fallback when coverbrowser is absent. - localscanner_spec + coverprovider_spec. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * feat(koplugin/library): UI — widget, item, view menu, FileManager hooks - librarywidget: full-screen Menu mixed in with CoverMenu + Mosaic/List per zen_ui's group_view pattern. Title-bar tap → view menu, search via left icon, drill-in/back for grouping. Async cloud sync deferred via scheduleIn so the menu paints before HTTP fires. - libraryitem: BIM patch for cloud-only (readest-cloud://) and group (readest-group://) URIs; group-cover composer (2x2 mosaic for grid, same in list) with cache key derived from the actual first-N hashes for content-based invalidation; ListMenuItem update + paintTo patches for wider list-mode cover strip and cloud-up/down icon overlay. - libraryviewmenu: ButtonDialog with View/Group by/Sort by/Actions. Default Group by = Groups (parity with web), values authors/groups. - librarypaint: partial-page e-ink repaint shim adapted from zen_ui. - main.lua: Library menu entry, dispatcher actions (Open Library / Push / Pull as general; progress + annotations as reader-only), "Add to Readest" button in FileManager's long-press file dialog (dedupe by partial_md5; bumps updated_at when present, inserts a fresh local-only row otherwise; un-tombstones via _clear_fields). Push books on Library open when auto_sync is on, pull-only otherwise. - Long-press action sheet with Readest BookDetailView parity: Remove from Cloud & Device / Cloud Only / Device Only, Upload to Cloud, Download Book / Cover / All. - Cloud-down + cloud-up SVG icons (LiaCloudDownloadAltSolid / LiaCloudUploadAltSolid) painted in the right-side wpageinfo slot. - i18n catalog updated for new strings. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * refactor(koplugin/library): split libraryitem into focused modules libraryitem.lua had grown to 1018 lines mixing five unrelated concerns. Split along the natural seams: cloud_covers — readest-cloud:// URI scheme, on-disk <hash>.png cache, single-slot async download queue, visible-page filter group_covers — readest-group:// URI scheme, 2x2 mosaic composer with content-derived cache key (first-N hashes), cell layout table cloud_icons — bundled cloud-up/cloud-down SVG loader, IconWidget cache, paint-overlay positioning list_strip — list-mode group row builder (4-cover wider strip replacing ListMenu's square cover slot) bim_patch — BookInfoManager:getBookInfo router (cloud / group / local) + ListMenuItem update + paintTo patches; owns the _library_local_paths set and orig BIM reference libraryitem.lua is now 141 lines: just the entry-table constructors (entry_from_row, entry_from_group, entry_back) plus thin install / set_visible_hashes delegates. Each new module is 88-216 lines. No behavior change — same 113 specs pass. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * chore(koplugin): co-locate dev tooling, ship-zip exclusions, CI job split - apps/readest.koplugin/scripts/build-koplugin.mjs: local sideloading build with the same exclusions the release workflow uses. - Move lint-koplugin + test-koplugin from apps/readest-app/scripts/ to apps/readest.koplugin/scripts/. All koplugin dev tooling now lives with the koplugin and is excluded from the published release zip. - Rename to .mjs so Node treats them as ESM without the reparse warning (the i18n CommonJS scripts stay .js). - Release workflow: zip -r exclusions for scripts/, docs/, spec/, .busted so dev artifacts don't ship to end users. - PR workflow: split build_web_app into build_web_app + test_web_app for parallelism. The test job installs luarocks + busted + lsqlite3complete and runs pnpm test:lua. test-koplugin.mjs now hard-fails (instead of soft-skipping) when CI=true and a tool is missing — a broken CI toolchain previously exited 0 silently. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
204 lines
8.1 KiB
Lua
204 lines
8.1 KiB
Lua
-- group_covers.lua
|
|
-- macOS-style folder previews: a 2x2 mosaic of the first N child book
|
|
-- covers, served as a synthetic readest-group:// URI through the
|
|
-- patched BookInfoManager. Composites are cached on disk under
|
|
-- <settings>/readest_group_covers/<key>.png
|
|
-- with the cache key derived from the actual first-N hashes, so any
|
|
-- change to the group's content (sort flip, add/remove, reordering
|
|
-- bump) auto-invalidates the cached PNG.
|
|
|
|
local logger = require("logger")
|
|
local cloud_covers = require("library.cloud_covers")
|
|
|
|
local M = {}
|
|
|
|
M.URI_PREFIX = "readest-group://"
|
|
|
|
-- Bump when the composite layout/dimensions change so existing on-disk
|
|
-- composites get regenerated on next paint instead of serving the old
|
|
-- aspect ratio forever.
|
|
local CACHE_VERSION = 3
|
|
|
|
-- Layouts:
|
|
-- "grid" — 2x2, 360x480 (3:4 — typical book-cover aspect).
|
|
-- "list" — 2x2, 480x480 (square — matches ListMenu's rigid square
|
|
-- cover slot, so the composite fills it vertically and each
|
|
-- mini-cover stays book-shaped instead of getting squished).
|
|
M.LAYOUTS = {
|
|
grid = { target_w = 360, target_h = 480, cols = 2, rows = 2 },
|
|
list = { target_w = 480, target_h = 480, cols = 2, rows = 2 },
|
|
}
|
|
|
|
local function group_covers_dir()
|
|
local DataStorage = require("datastorage")
|
|
return DataStorage:getSettingsDir() .. "/readest_group_covers"
|
|
end
|
|
|
|
local function group_cover_path(cache_key)
|
|
return group_covers_dir() .. "/" .. cache_key .. "_v" .. CACHE_VERSION .. ".png"
|
|
end
|
|
|
|
-- "Asimov" → "417369..." — filesystem-safe regardless of slashes,
|
|
-- colons, etc. in the original group value.
|
|
local function hex_encode(s)
|
|
return (s:gsub(".", function(c) return string.format("%02x", string.byte(c)) end))
|
|
end
|
|
|
|
local function hex_decode(hex)
|
|
return (hex:gsub("..", function(h) return string.char(tonumber(h, 16)) end))
|
|
end
|
|
|
|
-- shape ∈ {"grid", "list"} — controls the composite layout. Defaults
|
|
-- to "grid" for backward compat with older callers.
|
|
function M.build_uri(group_by, value, shape)
|
|
return M.URI_PREFIX .. group_by .. ":" .. hex_encode(value)
|
|
.. ":" .. (shape or "grid") .. ".png"
|
|
end
|
|
|
|
-- Returns group_by, value, cache_key, shape; nil if not a group URI.
|
|
-- cache_key here is the static "identity" portion; the BIM patch
|
|
-- appends the actual first-N hashes for content-based invalidation.
|
|
function M.parse_uri(uri)
|
|
if uri:sub(1, #M.URI_PREFIX) ~= M.URI_PREFIX then return nil end
|
|
local body = uri:sub(#M.URI_PREFIX + 1)
|
|
if body:sub(-4) == ".png" then body = body:sub(1, -5) end
|
|
local parts = {}
|
|
for p in body:gmatch("[^:]+") do parts[#parts + 1] = p end
|
|
if #parts < 2 then return nil end
|
|
local group_by = parts[1]
|
|
local hex = parts[2]
|
|
local shape = parts[3] or "grid"
|
|
local value = hex_decode(hex)
|
|
return group_by, value, group_by .. "_" .. hex .. "_" .. shape, shape
|
|
end
|
|
|
|
-- Pull a usable cover bb for a single child book during composition.
|
|
-- Tries (in order):
|
|
-- 1. local file via the original BIM cache (already-cached only;
|
|
-- no extraction triggered)
|
|
-- 2. cloud cover .png we previously downloaded
|
|
-- Returns nil if neither path produces one. Caller owns the bb.
|
|
function M.child_cover_bb(book, orig_getBookInfo, BIM)
|
|
if book.local_present == 1 and book.file_path and orig_getBookInfo then
|
|
local ok, info = pcall(orig_getBookInfo, BIM, book.file_path, true)
|
|
if ok and info and info.has_cover and info.cover_bb then
|
|
-- BIM hands us a cached bb whose ownership it keeps; we copy
|
|
-- before scaling so the BIM cache stays intact when our
|
|
-- composition pipeline frees what it received.
|
|
local Blitbuffer = require("ffi/blitbuffer")
|
|
local copy = Blitbuffer.new(info.cover_bb:getWidth(),
|
|
info.cover_bb:getHeight(),
|
|
info.cover_bb:getType())
|
|
copy:blitFrom(info.cover_bb, 0, 0, 0, 0,
|
|
info.cover_bb:getWidth(), info.cover_bb:getHeight())
|
|
return copy
|
|
end
|
|
end
|
|
return cloud_covers.load_cover_bb(book.hash)
|
|
end
|
|
|
|
-- Compose up to N child covers into a mosaic and write the PNG to
|
|
-- disk. Returns the loaded composite as a fresh bb, or nil on total
|
|
-- failure (no usable child covers, or PNG write failure).
|
|
local function compose(books, dest_path, shape, orig_getBookInfo, BIM)
|
|
if #books == 0 then return nil end
|
|
local layout = M.LAYOUTS[shape] or M.LAYOUTS.grid
|
|
local target_w, target_h = layout.target_w, layout.target_h
|
|
local cols, rows = layout.cols, layout.rows
|
|
local max_cells = cols * rows
|
|
|
|
local Blitbuffer = require("ffi/blitbuffer")
|
|
local target = Blitbuffer.new(target_w, target_h, Blitbuffer.TYPE_BBRGB32)
|
|
target:fill(Blitbuffer.COLOR_WHITE)
|
|
|
|
local gap = 8
|
|
local cell_w = math.floor((target_w - (cols - 1) * gap) / cols)
|
|
local cell_h = math.floor((target_h - (rows - 1) * gap) / rows)
|
|
local placed = 0
|
|
|
|
for i = 1, math.min(max_cells, #books) do
|
|
local book = books[i]
|
|
local cover = M.child_cover_bb(book, orig_getBookInfo, BIM)
|
|
if cover then
|
|
local row = math.floor((i - 1) / cols)
|
|
local col = (i - 1) % cols
|
|
local dx = col * (cell_w + gap)
|
|
local dy = row * (cell_h + gap)
|
|
local ok_scale, scaled = pcall(cover.scale, cover, cell_w, cell_h)
|
|
if ok_scale and scaled then
|
|
target:blitFrom(scaled, dx, dy, 0, 0, cell_w, cell_h)
|
|
scaled:free()
|
|
placed = placed + 1
|
|
end
|
|
cover:free()
|
|
end
|
|
end
|
|
|
|
if placed == 0 then
|
|
target:free()
|
|
return nil
|
|
end
|
|
|
|
local lfs = require("libs/libkoreader-lfs")
|
|
local dir = group_covers_dir()
|
|
if lfs.attributes(dir, "mode") ~= "directory" then lfs.mkdir(dir) end
|
|
|
|
local ok_write = pcall(target.writeToFile, target, dest_path, "PNG")
|
|
target:free()
|
|
if not ok_write then
|
|
logger.warn("ReadestLibrary group cover write failed: " .. tostring(dest_path))
|
|
return nil
|
|
end
|
|
-- Re-load the file so the bb we hand back is one ImageWidget can
|
|
-- safely take ownership of.
|
|
local ok_render, RenderImage = pcall(require, "ui/renderimage")
|
|
if not ok_render then return nil end
|
|
local ok_load, bb = pcall(RenderImage.renderImageFile, RenderImage, dest_path, false)
|
|
if not ok_load then return nil end
|
|
return bb
|
|
end
|
|
|
|
-- Cells-per-mosaic for a given shape. Used by callers to know how many
|
|
-- books to fetch from the store.
|
|
function M.cells_for(shape)
|
|
local layout = M.LAYOUTS[shape] or M.LAYOUTS.grid
|
|
return layout.cols * layout.rows
|
|
end
|
|
|
|
-- High-level: query the store, derive a content-based cache key from
|
|
-- the actual first-N hashes, load from disk if present, else compose
|
|
-- fresh. Returns (bb, books) — books is the resolved list (so callers
|
|
-- can reuse it without a second query).
|
|
function M.serve_or_compose(group_by, value, cache_key, shape,
|
|
store, settings, orig_getBookInfo, BIM)
|
|
if not store then return nil, {} end
|
|
local n = M.cells_for(shape)
|
|
local books = store:listBooksInGroup(group_by, value, n, {
|
|
sort_by = settings and settings.library_sort_by,
|
|
sort_asc = settings and settings.library_sort_ascending == true,
|
|
})
|
|
-- Fingerprint: short prefix of each hash, joined. Stable for the
|
|
-- same set in the same order; changes the moment any of those
|
|
-- shifts.
|
|
local parts = {}
|
|
for i = 1, #books do
|
|
parts[i] = (books[i].hash or ""):sub(1, 12)
|
|
end
|
|
local fingerprint = (#parts > 0) and table.concat(parts, "-") or "empty"
|
|
local content_key = cache_key .. "_" .. fingerprint
|
|
local cache_path = group_cover_path(content_key)
|
|
|
|
local lfs = require("libs/libkoreader-lfs")
|
|
if lfs.attributes(cache_path, "mode") == "file" then
|
|
local ok, RenderImage = pcall(require, "ui/renderimage")
|
|
if ok then
|
|
local ok2, loaded = pcall(RenderImage.renderImageFile,
|
|
RenderImage, cache_path, false)
|
|
if ok2 then return loaded, books end
|
|
end
|
|
end
|
|
return compose(books, cache_path, shape, orig_getBookInfo, BIM), books
|
|
end
|
|
|
|
return M
|