From 85429c25de0102dbedbbbf14d24350430adbe495 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 31 Mar 2026 04:20:43 +0000 Subject: [PATCH] Fix cover not being set when adding files to existing book records with no cover Agent-Logs-Url: https://github.com/kovidgoyal/calibre/sessions/aa4abbc0-5dd8-4380-a760-ace343a70589 Co-authored-by: kovidgoyal <1308621+kovidgoyal@users.noreply.github.com> --- src/calibre/db/copy_to_library.py | 2 ++ src/calibre/gui2/actions/add.py | 12 ++++++++++++ src/calibre/gui2/add.py | 6 ++++++ 3 files changed, 20 insertions(+) diff --git a/src/calibre/db/copy_to_library.py b/src/calibre/db/copy_to_library.py index 40eb0fcd81..646826b1fb 100644 --- a/src/calibre/db/copy_to_library.py +++ b/src/calibre/db/copy_to_library.py @@ -20,6 +20,8 @@ def automerge_book(automerge_action, book_id, mi, identical_book_list, newdb, fo at_least_one_format_added = True if at_least_one_format_added and extra_file_map: newdb.add_extra_files(identical_book, extra_file_map, replace=False, auto_rename=True) + if mi.cover_data and mi.cover_data[1] and not newdb.field_for('cover', identical_book): + newdb.set_cover({identical_book: mi.cover_data[1]}) if automerge_action == 'new record': incoming_fmts = {fmt.upper() for fmt in format_map} diff --git a/src/calibre/gui2/actions/add.py b/src/calibre/gui2/actions/add.py index ca883c3889..51c724e01f 100644 --- a/src/calibre/gui2/actions/add.py +++ b/src/calibre/gui2/actions/add.py @@ -194,6 +194,18 @@ class AddAction(InterfaceAction): if fmt: db.add_format_with_hooks(id_, fmt, fpath, index_is_id=True, notify=True) + if not db.has_cover(id_): + from calibre.ebooks.metadata.meta import get_metadata + for fmt, fpath in fmt_map.items(): + if fmt: + try: + with open(fpath, 'rb') as f: + mi = get_metadata(f, stream_type=fmt.lower()) + if mi.cover_data and mi.cover_data[1]: + db.new_api.set_cover({id_: mi.cover_data[1]}) + break + except Exception: + pass current_idx = self.gui.library_view.currentIndex() if current_idx.isValid(): self.gui.library_view.model().current_changed(current_idx, current_idx) diff --git a/src/calibre/gui2/add.py b/src/calibre/gui2/add.py index 3de98a55c1..2b223dbc6c 100644 --- a/src/calibre/gui2/add.py +++ b/src/calibre/gui2/add.py @@ -420,11 +420,17 @@ class Adder(QObject): seen_fmts = set() replace = gprefs['automerge'] == 'overwrite' cover_removed = False + cdata = None for identical_book_id in identical_book_ids: ib_fmts = {fmt.upper() for fmt in self.db.formats(identical_book_id)} seen_fmts |= ib_fmts self.add_formats(identical_book_id, paths, mi, replace=replace) self.merged_formats_added_to.add(identical_book_id) + if cover_path and not self.db.field_for('cover', identical_book_id): + if cdata is None: + with open(cover_path, 'rb') as f: + cdata = f.read() + self.db.set_cover({identical_book_id: cdata}) if gprefs['automerge'] == 'new record': incoming_fmts = {path.rpartition(os.extsep)[-1].upper() for path in paths} if incoming_fmts.intersection(seen_fmts):