Check for DRM when preparing KEPUB for viewing

This commit is contained in:
Kovid Goyal 2025-02-24 10:08:13 +05:30
parent 2f98ce3a8e
commit f1bda707e9
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 29 additions and 7 deletions

View File

@ -284,11 +284,18 @@ class EPUBInput(InputFormatPlugin):
if opf is None:
raise ValueError(f'{path} is not a valid EPUB file (could not find opf)')
if is_kepub and not self.for_viewer:
log('Removing Kobo markup...')
if is_kepub:
from calibre.ebooks.oeb.polish.container import Container
from calibre.ebooks.oeb.polish.kepubify import unkepubify_container
from calibre.ebooks.oeb.polish.errors import drm_message
from calibre.ebooks.oeb.polish.kepubify import check_for_kobo_drm, unkepubify_container
container = Container(os.getcwd(), opf, log)
if self.for_viewer:
log('Checking for Kobo DRM...')
with drm_message(_('The file {} is locked with DRM. It cannot be viewed').format(path)):
check_for_kobo_drm(container)
else:
log('Removing Kobo markup...')
with drm_message(_('The file {} is locked with DRM. It cannot be converted').format(path)):
unkepubify_container(container)
container.commit()
del container

View File

@ -5,6 +5,8 @@ __license__ = 'GPL v3'
__copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>'
__docformat__ = 'restructuredtext en'
from contextlib import contextmanager
from calibre.ebooks import DRMError as _DRMError
@ -12,10 +14,23 @@ class InvalidBook(ValueError):
pass
_drm_message = ''
@contextmanager
def drm_message(msg: str) -> None:
global _drm_message
orig, _drm_message = _drm_message, msg
try:
yield
finally:
_drm_message = orig
class DRMError(_DRMError):
def __init__(self):
super().__init__(_('This file is locked with DRM. It cannot be edited.'))
super().__init__(_drm_message or _('This file is locked with DRM. It cannot be edited.'))
class MalformedMarkup(ValueError):

View File

@ -473,6 +473,7 @@ def remove_kobo_files(container):
def unkepubify_container(container: Container, max_workers: int = 0) -> None:
check_for_kobo_drm(container)
remove_dummy_cover_image(container)
remove_dummy_title_page(container)
remove_kobo_files(container)
@ -537,7 +538,6 @@ def check_for_kobo_drm(container: Container) -> None:
def unkepubify_path(path, outpath='', max_workers=0, allow_overwrite=False):
container = get_container(path, tweak_mode=True, ebook_cls=EpubContainer)
check_for_kobo_drm(container)
unkepubify_container(container, max_workers)
base, ext = os.path.splitext(path)
outpath = outpath or base + '.epub'