def html(self):
"""
Returns :attr:`self.file_obj` as an <img> tag with the src set to a
data::URI.
"""
if not self.file_obj:
return u""
self.file_obj.seek(0)
# Need to encode base64 to create a data URI
encoded = base64.b64encode(self.file_obj.read())
data_uri = "data:{mimetype};base64,{encoded}".format(
mimetype=self.mimetype, encoded=encoded.decode('utf-8'))
link = "%s/%s" % (self.linkpath, os.path.split(self.path)[1])
if self.original_file:
link = "%s/%s" % (
self.linkpath, os.path.split(self.original_file.name)[1])
if self.thumbnail:
return self.html_icon_template.format(
link=link,
src=data_uri,
icon=self.thumbnail,
mimetype=self.mimetype)
return self.html_template.format(
link=link, src=data_uri, mimetype=self.mimetype)
评论列表
文章目录