##// END OF EJS Templates
tweak double-encode logic for image data...
MinRK -
Show More
@@ -97,7 +97,9 b' def date_default(obj):'
97
97
98 # constants for identifying png/jpeg data
98 # constants for identifying png/jpeg data
99 PNG = b'\x89PNG\r\n\x1a\n'
99 PNG = b'\x89PNG\r\n\x1a\n'
100 PNG64 = encodebytes(PNG)
100 JPEG = b'\xff\xd8'
101 JPEG = b'\xff\xd8'
102 JPEG64 = encodebytes(JPEG)
101
103
102 def encode_images(format_dict):
104 def encode_images(format_dict):
103 """b64-encodes images in a displaypub format dict
105 """b64-encodes images in a displaypub format dict
@@ -120,12 +122,21 b' def encode_images(format_dict):'
120
122
121 """
123 """
122 encoded = format_dict.copy()
124 encoded = format_dict.copy()
125
123 pngdata = format_dict.get('image/png')
126 pngdata = format_dict.get('image/png')
124 if isinstance(pngdata, bytes) and pngdata[:8] == PNG:
127 if isinstance(pngdata, bytes):
125 encoded['image/png'] = encodebytes(pngdata).decode('ascii')
128 # make sure we don't double-encode
129 if pngdata[:13] != PNG64:
130 pngdata = encodebytes(pngdata)
131 encoded['image/png'] = pngdata.decode('ascii')
132
126 jpegdata = format_dict.get('image/jpeg')
133 jpegdata = format_dict.get('image/jpeg')
127 if isinstance(jpegdata, bytes) and jpegdata[:2] == JPEG:
134 if isinstance(jpegdata, bytes):
128 encoded['image/jpeg'] = encodebytes(jpegdata).decode('ascii')
135 # make sure we don't double-encode
136 if jpegdata[:5] != JPEG64:
137 jpegdata = encodebytes(jpegdata)
138 encoded['image/jpeg'] = jpegdata.decode('ascii')
139
129 return encoded
140 return encoded
130
141
131
142
General Comments 0
You need to be logged in to leave comments. Login now