Fix #4958 (Metadata not imported for zipped Mobipocket file)

This commit is contained in:
Kovid Goyal 2010-02-20 13:42:32 -07:00
parent ce5c3e3951
commit 3edb183c25
3 changed files with 14 additions and 9 deletions

View File

@ -70,10 +70,13 @@ def is_recipe(filename):
filename.rpartition('.')[0].endswith('_recipe_out') filename.rpartition('.')[0].endswith('_recipe_out')
def get_metadata(stream, stream_type='lrf', use_libprs_metadata=False): def get_metadata(stream, stream_type='lrf', use_libprs_metadata=False):
pos = 0
if hasattr(stream, 'tell'):
pos = stream.tell() pos = stream.tell()
try: try:
return _get_metadata(stream, stream_type, use_libprs_metadata) return _get_metadata(stream, stream_type, use_libprs_metadata)
finally: finally:
if hasattr(stream, 'seek'):
stream.seek(pos) stream.seek(pos)

View File

@ -3,9 +3,10 @@ __license__ = 'GPL v3'
__copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>' __copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
import os import os
from zipfile import ZipFile
from cStringIO import StringIO
from calibre.utils.zipfile import ZipFile
from calibre.ptempfile import TemporaryDirectory
from calibre import CurrentDir
def get_metadata(stream): def get_metadata(stream):
from calibre.ebooks.metadata.meta import get_metadata from calibre.ebooks.metadata.meta import get_metadata
@ -23,8 +24,10 @@ def get_metadata(stream):
stream_type = stream_type[1:] stream_type = stream_type[1:]
if stream_type in ('lit', 'opf', 'prc', 'mobi', 'fb2', 'epub', if stream_type in ('lit', 'opf', 'prc', 'mobi', 'fb2', 'epub',
'rb', 'imp', 'pdf', 'lrf'): 'rb', 'imp', 'pdf', 'lrf'):
stream = StringIO(zf.read(f)) with TemporaryDirectory() as tdir:
return get_metadata(stream, stream_type) with CurrentDir(tdir):
path = zf.extract(f)
return get_metadata(open(path, 'rb'), stream_type)
raise ValueError('No ebook found in ZIP archive') raise ValueError('No ebook found in ZIP archive')

View File

@ -795,8 +795,7 @@ 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() log = Log(level=Log.DEBUG)
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)
@ -823,5 +822,5 @@ def get_metadata(stream):
im.convert('RGBA').save(obuf, format='JPEG') im.convert('RGBA').save(obuf, format='JPEG')
mi.cover_data = ('jpg', obuf.getvalue()) mi.cover_data = ('jpg', obuf.getvalue())
except: except:
log.exception() log.exception('Failed to read MOBI metadata')
return mi return mi