Fix thumbnail handling in metadata objects

This commit is contained in:
Kovid Goyal 2010-09-12 16:03:32 -06:00
commit 895942c332
4 changed files with 16 additions and 3 deletions

View File

@ -110,7 +110,7 @@ COPYABLE_METADATA_FIELDS = SOCIAL_METADATA_FIELDS.union(
frozenset(['title', 'title_sort', 'authors', frozenset(['title', 'title_sort', 'authors',
'author_sort', 'author_sort_map' 'comments', 'author_sort', 'author_sort_map' 'comments',
'cover_data', 'tags', 'language', 'lpath', 'cover_data', 'tags', 'language', 'lpath',
'size']) 'size', 'thumbnail'])
SERIALIZABLE_FIELDS = SOCIAL_METADATA_FIELDS.union( SERIALIZABLE_FIELDS = SOCIAL_METADATA_FIELDS.union(
USER_METADATA_FIELDS).union( USER_METADATA_FIELDS).union(

View File

@ -223,7 +223,7 @@ class Metadata(object):
self.author_sort = other.author_sort self.author_sort = other.author_sort
if replace_metadata: if replace_metadata:
SPECIAL_FIELDS = frozenset(['lpath', 'size', 'comments']) SPECIAL_FIELDS = frozenset(['lpath', 'size', 'comments', 'thumbnail'])
for attr in COPYABLE_METADATA_FIELDS: for attr in COPYABLE_METADATA_FIELDS:
setattr(self, attr, getattr(other, attr, 1.0 if \ setattr(self, attr, getattr(other, attr, 1.0 if \
attr == 'series_index' else None)) attr == 'series_index' else None))
@ -238,6 +238,7 @@ class Metadata(object):
for attr in COPYABLE_METADATA_FIELDS: for attr in COPYABLE_METADATA_FIELDS:
if hasattr(other, attr): if hasattr(other, attr):
copy_not_none(self, other, attr) copy_not_none(self, other, attr)
copy_not_none(self, other, 'thumbnail')
if other.tags: if other.tags:
# Case-insensitive but case preserving merging # Case-insensitive but case preserving merging
lotags = [t.lower() for t in other.tags] lotags = [t.lower() for t in other.tags]

View File

@ -12,6 +12,7 @@ from calibre.ebooks.metadata.book import SERIALIZABLE_FIELDS
from calibre.constants import filesystem_encoding, preferred_encoding from calibre.constants import filesystem_encoding, preferred_encoding
from calibre.library.field_metadata import FieldMetadata from calibre.library.field_metadata import FieldMetadata
from calibre.utils.date import parse_date, isoformat, UNDEFINED_DATE from calibre.utils.date import parse_date, isoformat, UNDEFINED_DATE
from calibre.utils.magick import Image
from calibre import isbytestring from calibre import isbytestring
# Translate datetimes to and from strings. The string form is the datetime in # Translate datetimes to and from strings. The string form is the datetime in
@ -32,6 +33,14 @@ def encode_thumbnail(thumbnail):
''' '''
if thumbnail is None: if thumbnail is None:
return None return None
if not isinstance(thumbnail, tuple):
try:
img = Image()
img.load(thumbnail)
width, height = img.size
thumbnail = (width, height, thumbnail)
except:
return None
return (thumbnail[0], thumbnail[1], b64encode(str(thumbnail[2]))) return (thumbnail[0], thumbnail[1], b64encode(str(thumbnail[2])))
def decode_thumbnail(tup): def decode_thumbnail(tup):

View File

@ -1056,7 +1056,10 @@ class DeviceBooksModel(BooksModel): # {{{
if hasattr(cdata, 'image_path'): if hasattr(cdata, 'image_path'):
img.load(cdata.image_path) img.load(cdata.image_path)
elif cdata: elif cdata:
img.loadFromData(cdata) if isinstance(cdata, tuple):
img.loadFromData(cdata[2])
else:
img.loadFromData(cdata)
if img.isNull(): if img.isNull():
img = self.default_image img = self.default_image
data['cover'] = img data['cover'] = img