stages/email: fix binary files not being encoded correctly

This commit is contained in:
Jens Langhammer 2020-09-13 17:40:13 +02:00
parent b317852e8a
commit 8b99afa34d
1 changed files with 14 additions and 6 deletions

View File

@ -1,4 +1,5 @@
"""passbook core inlining template tags""" """passbook core inlining template tags"""
from base64 import b64encode
from pathlib import Path from pathlib import Path
from django import template from django import template
@ -9,16 +10,23 @@ register = template.Library()
@register.simple_tag() @register.simple_tag()
def inline_static_ascii(path: str) -> str: def inline_static_ascii(path: str) -> str:
"""Inline static asset. Doesn't check file contents, plain text is assumed""" """Inline static asset. Doesn't check file contents, plain text is assumed.
If no file could be found, original path is returned"""
result = finders.find(path) result = finders.find(path)
with open(result) as _file: if result:
return _file.read() with open(result) as _file:
return _file.read()
return path
@register.simple_tag() @register.simple_tag()
def inline_static_binary(path: str) -> str: def inline_static_binary(path: str) -> str:
"""Inline static asset. Uses file extension for base64 block""" """Inline static asset. Uses file extension for base64 block. If no file could be found,
path is returned."""
result = finders.find(path) result = finders.find(path)
suffix = Path(path).suffix suffix = Path(path).suffix
with open(result) as _file: if result:
return f"data:image/{suffix};base64," + _file.read() with open(result) as _file:
b64content = b64encode(_file.read())
return f"data:image/{suffix};base64,{b64content.decode('utf-8')}"
return path