def _thumb_filename(self, attachment): root = os.path.join(self.thumb_path, os.path.basename(attachment)) mime_type = self.mime_type(attachment) if mime_type == 'image/svg+xml': return '%s.svg' % root elif can_resize(mime_type): return '%s.jpg' % root
def thumb_open(self, attachment): """Returns the attachment's thumbnail image as a file-like object""" s = self._filename(attachment) t = self._thumb_filename(attachment) # Regenerate the thumbnail if it's stale if os.path.exists(s) and t is not None: if (not os.path.exists(t) or self.thumb_updated(attachment) < self.updated(attachment)): path = os.path.dirname(t) if not os.path.exists(path): os.makedirs(path) mime_type = self.mime_type(attachment) if mime_type == 'image/svg+xml': # Just copy the SVG over - we'll resize it when we display it shutil.copyfile(s, t) elif can_resize(mime_type): # Otherwise, resize to a JPEG make_thumbnail(s, t) if os.path.exists(t): return io.open(t, 'rb') else: # XXX Return some generic thumbnail? return None
def test_can_resize(): assert can_resize('image/png') assert can_resize('image/jpeg') assert not can_resize('application/octet-stream') assert not can_resize('image/svg+xml') assert not can_resize('text/html')