mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Check for DRM when preparing KEPUB for viewing
This commit is contained in:
parent
2f98ce3a8e
commit
f1bda707e9
@ -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
|
||||
|
@ -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):
|
||||
|
@ -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'
|
||||
|
Loading…
x
Reference in New Issue
Block a user