Skip to content

Commit

Permalink
ebook-meta: Add an option to disallow rendered cover of first page fo…
Browse files Browse the repository at this point in the history
…r EPUB
  • Loading branch information
kovidgoyal committed Oct 27, 2024
1 parent 57017c4 commit cec78ca
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/calibre/ebooks/metadata/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ def config():
c.add_opt('get_cover', ['--get-cover'],
help=_('Get the cover from the e-book and save it at as the '
'specified file.'))
c.add_opt('disallow_rendered_cover', ['--disallow-rendered-cover'], action='store_true', help=_(
'For formats like EPUB that use a "default cover" of the first page rendered, disallow such default covers'))

c.add_opt('to_opf', ['--to-opf'],
help=_('Specify the name of an OPF file. The metadata will '
'be written to the OPF file.'))
Expand Down Expand Up @@ -182,7 +185,8 @@ def main(args=sys.argv):
if getattr(opts, pref.name) is not None:
trying_to_set = True
break
with open(path, 'rb') as stream:
from calibre.ebooks.metadata.epub import epub_metadata_settings
with open(path, 'rb') as stream, epub_metadata_settings(allow_rendered_cover=not opts.disallow_rendered_cover):
mi = get_metadata(stream, stream_type, force_read_metadata=True)
if trying_to_set:
prints(_('Original metadata')+'::')
Expand Down
19 changes: 17 additions & 2 deletions src/calibre/ebooks/metadata/epub.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import io
import os
import posixpath
from contextlib import closing, suppress
from contextlib import closing, contextmanager, suppress

from calibre import CurrentDir
from calibre.ebooks.metadata.opf import get_metadata as get_metadata_from_opf
Expand Down Expand Up @@ -230,6 +230,20 @@ def render_cover(cpage, zf, reader=None):
return render_html_svg_workaround(cpage, default_log, root=tdir)


epub_allow_rendered_cover = True


@contextmanager
def epub_metadata_settings(allow_rendered_cover=epub_allow_rendered_cover):
global epub_allow_rendered_cover
oarc = epub_allow_rendered_cover
epub_allow_rendered_cover = allow_rendered_cover
try:
yield
finally:
epub_allow_rendered_cover = oarc


def get_cover(raster_cover, first_spine_item, reader):
zf = reader.archive

Expand All @@ -241,7 +255,8 @@ def get_cover(raster_cover, first_spine_item, reader):
except Exception:
pass

return render_cover(first_spine_item, zf, reader=reader)
if epub_allow_rendered_cover:
return render_cover(first_spine_item, zf, reader=reader)


def get_metadata(stream, extract_cover=True):
Expand Down

0 comments on commit cec78ca

Please sign in to comment.