MOBI metadata: Do not try to extarct embedded metadata from MOBI files larger than 4MB

This commit is contained in:
Kovid Goyal 2010-02-21 13:33:49 -07:00
parent 6b04e57275
commit eae6bf3608

View File

@ -10,6 +10,7 @@ import re
import struct import struct
import textwrap import textwrap
import cStringIO import cStringIO
import sys
try: try:
from PIL import Image as PILImage from PIL import Image as PILImage
@ -806,13 +807,20 @@ def get_metadata(stream):
if mh.exth.mi is not None: if mh.exth.mi is not None:
mi = mh.exth.mi mi = mh.exth.mi
else: else:
with TemporaryDirectory('_mobi_meta_reader') as tdir: size = sys.maxint
with CurrentDir(tdir): if hasattr(stream, 'seek') and hasattr(stream, 'tell'):
mr = MobiReader(stream, log) pos = stream.tell()
parse_cache = {} stream.seek(0, 2)
mr.extract_content(tdir, parse_cache) size = stream.tell()
if mr.embedded_mi is not None: stream.seek(pos)
mi = mr.embedded_mi if size < 4*1024*1024:
with TemporaryDirectory('_mobi_meta_reader') as tdir:
with CurrentDir(tdir):
mr = MobiReader(stream, log)
parse_cache = {}
mr.extract_content(tdir, parse_cache)
if mr.embedded_mi is not None:
mi = mr.embedded_mi
if hasattr(mh.exth, 'cover_offset'): if hasattr(mh.exth, 'cover_offset'):
cover_index = mh.first_image_index + mh.exth.cover_offset cover_index = mh.first_image_index + mh.exth.cover_offset
data = mh.section_data(int(cover_index)) data = mh.section_data(int(cover_index))