def _encode_attachments(cls, email: dict) -> dict: if not email.get('attachments'): return email for attachment in email['attachments']: content_bytes = attachment['content'] attachment['content'] = to_base64(content_bytes) return email
def _create_attachment(cls, attachment: dict) -> Attachment: filename = attachment.get('filename', '') content = attachment.get('content', b'') return Attachment( disposition='attachment', file_name=filename, content_id=filename, file_type=guess_type(filename)[0], file_content=to_base64(content), )
def _fetch_image_to_base64(image_url: str) -> Optional[str]: response = http_get(image_url) if not response.ok: return None image_type = _get_image_type(response, image_url) if not image_type: return None if not response.content: return None small_image_bytes = _change_image_size(response.content) small_image_base64 = to_base64(small_image_bytes) return f'data:{image_type};base64,{small_image_base64}'
def test_roundtrip(self): original = b'some bytes' serialized = serialization.to_base64(original) deserialized = serialization.from_base64(serialized) self.assertEqual(original, deserialized)