readest/apps/readest-calibre-plugin/wire.py
Huang Xin 19a4ffe278
Some checks are pending
CodeQL Advanced / Analyze (actions) (push) Waiting to run
CodeQL Advanced / Analyze (javascript-typescript) (push) Waiting to run
CodeQL Advanced / Analyze (rust) (push) Waiting to run
Publish Docker image / build (linux/amd64, ubuntu-latest) (push) Waiting to run
Publish Docker image / build (linux/arm64, ubuntu-24.04-arm) (push) Waiting to run
Publish Docker image / merge (push) Blocked by required conditions
Build with Nix / tests (push) Waiting to run
PR checks / nix_flake_check (push) Waiting to run
PR checks / rust_lint (push) Waiting to run
PR checks / koplugin_rust_lint (push) Waiting to run
PR checks / build_web_app (push) Waiting to run
PR checks / test_web_app (1) (push) Waiting to run
PR checks / test_web_app (2) (push) Waiting to run
PR checks / test_extensions (push) Waiting to run
PR checks / build_tauri_app (push) Waiting to run
Scorecard supply-chain security / Scorecard analysis (push) Waiting to run
Deploy to vercel on merge / build_and_deploy (push) Waiting to run
fix(calibre): push custom columns in the shape bookshelf filters read (#6538)
* fix(calibre): push custom columns in the shape bookshelf filters read

The calibre plugin pushed custom columns as metadata.customColumns, a bare
{label: value} map that nothing in the app reads. Bookshelf filters, search
and book details read metadata.calibreColumns, the records foliate-js parses
from the OPF's calibre:user_metadata. So a column only showed up on devices
that had parsed the downloaded file, never on web, and no cloud-only book
ever matched a custom column filter.

The plugin now pushes calibreColumns in that same shape (label, display
name, datatype, value, series index), with values read through calibre's
mi.get() so composite columns are evaluated. Re-pushing updates existing
books: their metadata now differs, so the plan is a row-only update.

The bookshelf filter editor also gains:
- value suggestions (a datalist) for tags, subjects, group, series, author,
  publisher, editor, language, format and Calibre text/multi-value columns,
  drawn from the library; free text still works for contains/starts with
- a Download status field: Local only, Downloaded, or Cloud only

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix(bookshelves): count referenced files as local in download status

Books with a local filePath but no downloadedAt stamp are local, as the
availability field already treats them. Streaming formats keep a server URL
in filePath, so they stay excluded.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-01 20:34:42 +02:00

419 lines
15 KiB
Python

__license__ = 'AGPL v3'
__copyright__ = '2026, Bilingify LLC'
"""Calibre metadata → Readest wire records, and per-book push planning.
Standard-library only (no calibre / Qt imports) so it can be unit-tested
outside calibre. The wire shape is the camelCase Book record consumed by
POST /api/sync (apps/readest-app/src/utils/transform.ts::transformBookToDB,
inverted — same as readest.koplugin's row_to_wire).
"""
import json
from datetime import datetime, timezone
try: # inside calibre the plugin loads as a package
from calibre_plugins.readest.api import meta_hash
except ImportError: # plain imports for the unit tests
from api import meta_hash
# Readest-supported formats, in upload preference order.
FORMAT_PRIORITY = ('EPUB', 'PDF', 'AZW3', 'MOBI', 'AZW', 'FB2', 'FBZ', 'CBZ', 'TXT', 'MD')
EXTS = {
'EPUB': 'epub',
'PDF': 'pdf',
'MOBI': 'mobi',
'AZW': 'azw',
'AZW3': 'azw3',
'CBZ': 'cbz',
'FB2': 'fb2',
'FBZ': 'fbz',
'TXT': 'txt',
'MD': 'md',
}
CLOUD_BOOKS_SUBDIR = 'Readest/Books'
COVER_FILE_NAME = 'cover.png'
# calibre marks: in-memory labels shown in the book list and searchable as
# `marked:<label>`. Derived from the cloud on every status check, never stored.
MARK_PREFIX = 'readest_'
PLAN_MARKS = {
'new': MARK_PREFIX + 'missing',
'replace': MARK_PREFIX + 'outdated',
'update': MARK_PREFIX + 'metadata',
'skip': MARK_PREFIX + 'synced',
}
def merge_marks(existing, new_marks):
"""Our marks replaced wholesale, everyone else's left alone.
`existing` is calibre's `View.marked_ids` ({book_id: label}), which also
holds marks the user set by hand.
"""
merged = {i: l for i, l in (existing or {}).items() if not str(l).startswith(MARK_PREFIX)}
merged.update(new_marks or {})
return merged
def pick_format(available):
"""Best Readest-supported format among calibre's, or None."""
upper = {f.upper() for f in available}
for fmt in FORMAT_PRIORITY:
if fmt in upper:
return fmt
return None
def book_file_name(file_hash, fmt):
# Matches getRemoteBookFilename for S3 ({hash}/{hash}.{ext}); R2
# deployments resolve it through the download API's hash+extension
# fallback, same as the koplugin.
return '%s/%s/%s.%s' % (CLOUD_BOOKS_SUBDIR, file_hash, file_hash, EXTS[fmt])
def cover_file_name(file_hash):
return '%s/%s/%s' % (CLOUD_BOOKS_SUBDIR, file_hash, COVER_FILE_NAME)
def cloud_book_hashes(files):
"""Book hashes that still have a book blob (not just a cover) in storage.
Read from the file key (`{user_id}/Readest/Books/{hash}/{name}`) rather
than the row's `book_hash`, which is only set when the uploader passed it.
"""
marker = CLOUD_BOOKS_SUBDIR + '/'
hashes = set()
for record in files or []:
key = record.get('file_key') or ''
index = key.find(marker)
if index < 0:
continue
book_hash, _, name = key[index + len(marker) :].partition('/')
if book_hash and name and name != COVER_FILE_NAME:
hashes.add(book_hash)
return hashes
def _clean(value):
# The server strips NUL from strings (utils/sanitize.ts::sanitizeString);
# strip locally too so pushed and pulled values compare equal.
if isinstance(value, str):
return value.replace('\x00', '')
return value
def _jsonable(value):
if hasattr(value, 'isoformat'):
return value.isoformat()
if isinstance(value, (list, tuple)):
return [_jsonable(v) for v in value]
if isinstance(value, (str, int, float, bool)) or value is None:
return value
return str(value)
def calibre_columns(user_metadata):
"""Custom columns as BookMetadata.calibreColumns (libs/document.ts).
`user_metadata` maps '#label' to calibre's field metadata (name, datatype,
'#value#', '#extra#'), as Metadata.get_all_user_metadata() does. The records
match what foliate-js parses from the OPF's calibre:user_metadata, so a
pushed row and a locally parsed file agree, and empty values are dropped
the same way.
"""
columns = []
for key, fm in (user_metadata or {}).items():
datatype = fm.get('datatype') or 'text'
value = _jsonable(fm.get('#value#'))
if (
value in (None, '', [])
or (datatype == 'datetime' and str(value).startswith('0101-01-01'))
or (datatype == 'rating' and not value)
):
continue
label = key.lstrip('#')
column = {
'label': label,
'name': fm.get('name') or label,
'datatype': datatype,
'value': value,
}
extra = _jsonable(fm.get('#extra#'))
if extra is not None:
column['extra'] = extra
columns.append(column)
return columns
def build_metadata(book):
"""BookMetadata JSON for the books row (libs/document.ts::BookMetadata).
`book` is a plain dict extracted from calibre's Metadata object:
title, authors, languages, publisher, pubdate, comments, tags, series,
series_index, uuid, isbn, custom_columns.
"""
authors = [_clean(a) for a in book.get('authors') or [] if a]
meta = {
'title': _clean(book.get('title')) or '',
'author': authors[0] if len(authors) == 1 else authors,
}
languages = book.get('languages') or []
if isinstance(languages, str):
languages = [languages]
if languages:
meta['language'] = languages[0] if len(languages) == 1 else languages
optional = {
'publisher': _clean(book.get('publisher')),
'published': _clean(book.get('pubdate')),
'description': _clean(book.get('comments')),
'subject': [_clean(t) for t in book.get('tags') or []] or None,
'series': _clean(book.get('series')),
'isbn': _clean(book.get('isbn')),
# Partial MD5 of the RAW calibre library file. The uploaded blob has
# metadata embedded so its hash (book_hash) shifts with every embed;
# this stable fingerprint is how a later push detects "the file
# itself changed" without any machine-local state.
'calibreSourceHash': book.get('source_hash'),
}
if book.get('series'):
optional['seriesIndex'] = book.get('series_index')
if book.get('uuid'):
optional['identifier'] = 'urn:uuid:%s' % book['uuid']
if book.get('custom_columns'):
optional['calibreColumns'] = book['custom_columns']
for key, value in optional.items():
if value not in (None, '', []):
meta[key] = value
return meta
def _author_string(book):
return ', '.join(_clean(a) for a in book.get('authors') or [] if a)
def build_wire_book(book, file_hash, fmt, now_ms):
"""Base wire record for a calibre book (before server-row merging)."""
identifiers = []
if book.get('uuid'):
identifiers.append('urn:uuid:%s' % book['uuid'])
if book.get('isbn'):
identifiers.append('isbn:%s' % book['isbn'])
title = _clean(book.get('title')) or ''
authors = [_clean(a) for a in book.get('authors') or [] if a]
return {
'hash': file_hash,
'bookHash': file_hash,
'metaHash': meta_hash(title, authors, identifiers),
'format': fmt,
'title': title,
'sourceTitle': title,
'author': _author_string(book),
'tags': [_clean(t) for t in book.get('tags') or []],
'metadata': build_metadata(book),
'createdAt': now_ms,
'updatedAt': now_ms,
}
def iso_to_ms(iso):
if not iso:
return None
text = iso.replace('Z', '+00:00')
dt = datetime.fromisoformat(text)
if dt.tzinfo is None:
dt = dt.replace(tzinfo=timezone.utc)
return int(dt.timestamp() * 1000)
def ms_to_iso(ms):
if ms is None:
return None
return datetime.fromtimestamp(ms / 1000, timezone.utc).isoformat()
def _parse_row_metadata(row):
raw = row.get('metadata')
if isinstance(raw, dict):
return raw
if isinstance(raw, str) and raw:
try:
return json.loads(raw)
except ValueError:
return None
return None
def _row_matches_wire(row, wire):
if (row.get('title') or '') != wire['title']:
return False
if (row.get('author') or '') != wire['author']:
return False
if (row.get('tags') or []) != (wire.get('tags') or []):
return False
return _parse_row_metadata(row) == wire['metadata']
def row_source_hash(row):
"""The raw-file fingerprint a server row was built from.
v2 rows carry it as metadata.calibreSourceHash; v1 rows uploaded the raw
file unmodified, so their book_hash IS the raw hash.
"""
meta = _parse_row_metadata(row) or {}
return meta.get('calibreSourceHash') or row.get('book_hash')
def metadata_uuid(meta):
"""Calibre book uuid from a metadata dict's urn:uuid identifier."""
identifier = (meta or {}).get('identifier')
if isinstance(identifier, str) and 'urn:uuid:' in identifier:
return identifier.rsplit(':', 1)[-1].lower()
return None
def _prefer_row(row, over):
row_live, over_live = not row.get('deleted_at'), not over.get('deleted_at')
if row_live != over_live:
return row_live
return (iso_to_ms(row.get('updated_at')) or 0) > (iso_to_ms(over.get('updated_at')) or 0)
def index_rows_by_uuid(rows):
"""Map calibre uuid -> best server row (live over tombstoned, then newest).
This is the identity that survives file-content changes: the uploaded blob
has metadata embedded, so book_hash shifts whenever the file or its
metadata does, but the calibre uuid in metadata.identifier stays put.
"""
best = {}
for row in rows:
uuid = metadata_uuid(_parse_row_metadata(row))
if not uuid:
continue
current = best.get(uuid)
if current is None or _prefer_row(row, current):
best[uuid] = row
return best
def pick_server_row(hash_row, uuid_row):
"""Choose the row a push should target: live hash match, then live uuid
match, then whatever tombstone is left (for resurrection)."""
for row in (hash_row, uuid_row):
if row is not None and not row.get('deleted_at'):
return row
return hash_row if hash_row is not None else uuid_row
def should_bulk_list(total_pages, selected_books):
"""Whether paging the whole storage listing beats per-book lookups.
Paging costs one request per page no matter how many books are selected;
a per-book lookup costs one request per book that needs checking. Measured
against a real account both are around a second, so compare the counts.
Page 1 is already paid for by the time we can ask, hence the <=.
"""
return total_pages <= max(1, selected_books)
def _resolve_blob_present(blob_present, book_hash):
return blob_present(book_hash) if callable(blob_present) else bool(blob_present)
def plan_push(server_row, wire, local_cover_hash, source_hash, blob_present=True):
"""Decide what to do for one book.
Returns {'action': 'new' | 'replace' | 'update' | 'skip',
'upload_cover': bool}.
- new: no server row — embed metadata, upload file, insert row
- replace: the raw file changed (or the row has no file blob) — upload a
fresh embedded blob under its new hash, tombstone the old row
- update: file unchanged, but metadata/cover/tombstone differ — row only
- skip: file + metadata + cover all unchanged
`blob_present` says whether the row's book file is actually in cloud
storage. `uploaded_at` alone doesn't prove it: deleting files under
"Manage Storage" drops the object and its `files` row but never touches
`books.uploaded_at`, and a row-only push against a missing blob leaves a
book that shows up in the library but 404s on download.
It may be a bool, or a callable(book_hash) -> bool for callers that look
storage up one book at a time. It is checked last, so a lookup only costs
a request when it can still change the answer.
"""
if server_row is None:
return {'action': 'new', 'upload_cover': bool(local_cover_hash)}
if (
not server_row.get('uploaded_at')
or row_source_hash(server_row) != source_hash
or not _resolve_blob_present(blob_present, server_row['book_hash'])
):
# A replaced book gets a new hash namespace, so it needs its own cover.
return {'action': 'replace', 'upload_cover': bool(local_cover_hash)}
cover_changed = bool(local_cover_hash) and local_cover_hash != server_row.get('cover_hash')
changed = (
not _row_matches_wire(server_row, wire)
or bool(server_row.get('deleted_at'))
or cover_changed
)
return {'action': 'update' if changed else 'skip', 'upload_cover': cover_changed}
def tombstone_record(row, now_ms):
"""Wire record that soft-deletes a replaced server row."""
return {
'hash': row['book_hash'],
'bookHash': row['book_hash'],
'metaHash': row.get('meta_hash'),
'format': row.get('format'),
'title': row.get('title') or '',
'author': row.get('author') or '',
'createdAt': iso_to_ms(row.get('created_at')) or now_ms,
'updatedAt': now_ms,
'deletedAt': now_ms,
}
def merge_for_push(wire, server_row, now_ms, uploaded_at_ms=None, cover_hash=None):
"""Final record for POST /sync.
The server explicit-nulls any field absent from the wire record
(transformBookToDB), so an update must carry over the server row's
groupId/groupName, progress, readingStatus, uploadedAt and cover fields —
the koplugin learned this the hard way (see syncbooks.lua:291-299).
"""
record = dict(wire)
record['updatedAt'] = now_ms
record['deletedAt'] = None # an explicit push (re)activates the book
row = server_row or {}
record['createdAt'] = iso_to_ms(row.get('created_at')) or wire['createdAt']
if row.get('group_id'):
record['groupId'] = row['group_id']
if row.get('group_name'):
record['groupName'] = row['group_name']
if row.get('progress') is not None:
record['progress'] = row['progress']
if row.get('reading_status'):
record['readingStatus'] = row['reading_status']
record['readingStatusUpdatedAt'] = iso_to_ms(row.get('reading_status_updated_at'))
record['uploadedAt'] = (
uploaded_at_ms if uploaded_at_ms is not None else iso_to_ms(row.get('uploaded_at'))
)
if cover_hash:
record['coverHash'] = cover_hash
record['coverUpdatedAt'] = now_ms
elif row.get('cover_hash'):
record['coverHash'] = row['cover_hash']
record['coverUpdatedAt'] = iso_to_ms(row.get('cover_updated_at'))
# The server resolves title/author/tags/metadata by metadata_updated_at
# (field-level LWW, readest#5438). Stamp it when this push changes the
# group; carry the row's stamp otherwise, so a cover-only push can't win
# the group over a newer Readest edit racing with it.
if server_row is None or not _row_matches_wire(server_row, wire):
record['metadataUpdatedAt'] = now_ms
else:
record['metadataUpdatedAt'] = iso_to_ms(row.get('metadata_updated_at'))
return record