mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Make reading MOBI metadata a little more robust
This commit is contained in:
parent
3edb183c25
commit
466fb8f4f6
@ -795,10 +795,12 @@ class MobiReader(object):
|
|||||||
|
|
||||||
def get_metadata(stream):
|
def get_metadata(stream):
|
||||||
from calibre.utils.logging import Log
|
from calibre.utils.logging import Log
|
||||||
log = Log(level=Log.DEBUG)
|
log = Log()
|
||||||
mi = MetaInformation(os.path.basename(stream.name), [_('Unknown')])
|
mi = MetaInformation(os.path.basename(stream.name), [_('Unknown')])
|
||||||
try:
|
try:
|
||||||
mh = MetadataHeader(stream, log)
|
mh = MetadataHeader(stream, log)
|
||||||
|
if mh.title and mh.title != _('Unknown'):
|
||||||
|
mi.title = mh.title
|
||||||
|
|
||||||
if mh.exth is not None:
|
if mh.exth is not None:
|
||||||
if mh.exth.mi is not None:
|
if mh.exth.mi is not None:
|
||||||
@ -817,10 +819,15 @@ def get_metadata(stream):
|
|||||||
else:
|
else:
|
||||||
data = mh.section_data(mh.first_image_index)
|
data = mh.section_data(mh.first_image_index)
|
||||||
buf = cStringIO.StringIO(data)
|
buf = cStringIO.StringIO(data)
|
||||||
|
try:
|
||||||
im = PILImage.open(buf)
|
im = PILImage.open(buf)
|
||||||
|
except:
|
||||||
|
log.exception('Failed to read MOBI cover')
|
||||||
|
else:
|
||||||
obuf = cStringIO.StringIO()
|
obuf = cStringIO.StringIO()
|
||||||
im.convert('RGBA').save(obuf, format='JPEG')
|
im.convert('RGB').save(obuf, format='JPEG')
|
||||||
mi.cover_data = ('jpg', obuf.getvalue())
|
mi.cover_data = ('jpg', obuf.getvalue())
|
||||||
except:
|
except:
|
||||||
|
log.filter_level = Log.DEBUG
|
||||||
log.exception('Failed to read MOBI metadata')
|
log.exception('Failed to read MOBI metadata')
|
||||||
return mi
|
return mi
|
||||||
|
@ -224,6 +224,11 @@ def extract_member(path, match=re.compile(r'\.(jpg|jpeg|gif|png)\s*$', re.I), na
|
|||||||
f.write(data)
|
f.write(data)
|
||||||
f.flush()
|
f.flush()
|
||||||
path = f.name
|
path = f.name
|
||||||
|
|
||||||
|
def is_match(fname):
|
||||||
|
return (name is not None and fname == name) or \
|
||||||
|
(match is not None and match.search(fname))
|
||||||
|
|
||||||
with TemporaryDirectory('_libunrar') as dir:
|
with TemporaryDirectory('_libunrar') as dir:
|
||||||
with CurrentDir(dir):
|
with CurrentDir(dir):
|
||||||
open_archive_data = RAROpenArchiveDataEx(ArcName=path, OpenMode=RAR_OM_EXTRACT, CmtBuf=None)
|
open_archive_data = RAROpenArchiveDataEx(ArcName=path, OpenMode=RAR_OM_EXTRACT, CmtBuf=None)
|
||||||
@ -235,14 +240,18 @@ def extract_member(path, match=re.compile(r'\.(jpg|jpeg|gif|png)\s*$', re.I), na
|
|||||||
while True:
|
while True:
|
||||||
if _libunrar.RARReadHeaderEx(arc_data, byref(header_data)) != 0:
|
if _libunrar.RARReadHeaderEx(arc_data, byref(header_data)) != 0:
|
||||||
raise UnRARException('%s has no files'%path)
|
raise UnRARException('%s has no files'%path)
|
||||||
|
file_name = header_data.FileNameW
|
||||||
|
if is_match(file_name):
|
||||||
PFCode = _libunrar.RARProcessFileW(arc_data, RAR_EXTRACT, None, None)
|
PFCode = _libunrar.RARProcessFileW(arc_data, RAR_EXTRACT, None, None)
|
||||||
if PFCode != 0:
|
if PFCode != 0:
|
||||||
raise UnRARException(_interpret_process_file_error(PFCode))
|
raise UnRARException(_interpret_process_file_error(PFCode))
|
||||||
file_name = header_data.FileNameW
|
|
||||||
if (name is not None and file_name == name) or \
|
|
||||||
(match is not None and match.search(file_name)):
|
|
||||||
return header_data.FileNameW.replace('/', os.sep), \
|
return header_data.FileNameW.replace('/', os.sep), \
|
||||||
open(os.path.join(dir, *header_data.FileNameW.split('/')), 'rb').read()
|
open(os.path.join(dir, *header_data.FileNameW.split('/')), 'rb').read()
|
||||||
|
else:
|
||||||
|
PFCode = _libunrar.RARProcessFileW(arc_data, RAR_SKIP, None, None)
|
||||||
|
if PFCode != 0:
|
||||||
|
raise UnRARException(_interpret_process_file_error(PFCode))
|
||||||
|
|
||||||
finally:
|
finally:
|
||||||
_libunrar.RARCloseArchive(arc_data)
|
_libunrar.RARCloseArchive(arc_data)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user