Skip to content

Commit a4f3bad

Browse files
authored
Fixed issue where attachments < 3mb were not being encoded correctly (#362)
Fixed issue where attachments < 3mb were not being encoded correctly
1 parent 39710f8 commit a4f3bad

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Unreleased
88
* Added clean messages support
99
* Added additional webhook triggers
1010
* Made event visibility optional to support iCloud events
11+
* Fixed issue where attachments < 3mb were not being encoded correctly
1112

1213
v6.1.1
1314
----------------

nylas/resources/drafts.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import io
12
from typing import Optional
23

34
from nylas.handler.api_resources import (
@@ -15,7 +16,11 @@
1516
)
1617
from nylas.models.messages import Message
1718
from nylas.models.response import ListResponse, Response, DeleteResponse
18-
from nylas.utils.file_utils import _build_form_request, MAXIMUM_JSON_ATTACHMENT_SIZE
19+
from nylas.utils.file_utils import (
20+
_build_form_request,
21+
MAXIMUM_JSON_ATTACHMENT_SIZE,
22+
encode_stream_to_base64,
23+
)
1924

2025

2126
class Drafts(
@@ -99,6 +104,11 @@ def create(
99104

100105
return Response.from_dict(json_response, Draft)
101106

107+
# Encode the content of the attachments to base64
108+
for attachment in request_body.get("attachments", []):
109+
if issubclass(type(attachment["content"]), io.IOBase):
110+
attachment["content"] = encode_stream_to_base64(attachment["content"])
111+
102112
return super().create(
103113
path=path,
104114
response_type=Draft,
@@ -138,6 +148,11 @@ def update(
138148

139149
return Response.from_dict(json_response, Draft)
140150

151+
# Encode the content of the attachments to base64
152+
for attachment in request_body.get("attachments", []):
153+
if issubclass(type(attachment["content"]), io.IOBase):
154+
attachment["content"] = encode_stream_to_base64(attachment["content"])
155+
141156
return super().update(
142157
path=path,
143158
response_type=Draft,

nylas/resources/messages.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import io
12
from typing import Optional, List
23

34
from nylas.handler.api_resources import (
@@ -19,7 +20,11 @@
1920
)
2021
from nylas.models.response import Response, ListResponse, DeleteResponse
2122
from nylas.resources.smart_compose import SmartCompose
22-
from nylas.utils.file_utils import _build_form_request, MAXIMUM_JSON_ATTACHMENT_SIZE
23+
from nylas.utils.file_utils import (
24+
_build_form_request,
25+
MAXIMUM_JSON_ATTACHMENT_SIZE,
26+
encode_stream_to_base64,
27+
)
2328

2429

2530
class Messages(
@@ -151,6 +156,13 @@ def send(
151156
if attachment_size >= MAXIMUM_JSON_ATTACHMENT_SIZE:
152157
form_data = _build_form_request(request_body)
153158
else:
159+
# Encode the content of the attachments to base64
160+
for attachment in request_body.get("attachments", []):
161+
if issubclass(type(attachment["content"]), io.IOBase):
162+
attachment["content"] = encode_stream_to_base64(
163+
attachment["content"]
164+
)
165+
154166
json_body = request_body
155167

156168
json_response = self._http_client._execute(

nylas/utils/file_utils.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import base64
12
import json
23
import mimetypes
34
import os
45
from pathlib import Path
6+
from typing import BinaryIO
57

68
from requests_toolbelt import MultipartEncoder
79

@@ -36,6 +38,21 @@ def attach_file_request_builder(file_path) -> CreateAttachmentRequest:
3638
}
3739

3840

41+
def encode_stream_to_base64(binary_stream: BinaryIO) -> str:
42+
"""
43+
Encode the content of a binary stream to a base64 string.
44+
45+
Attributes:
46+
binary_stream: The binary stream to encode.
47+
48+
Returns:
49+
The base64 encoded content of the binary stream.
50+
"""
51+
binary_stream.seek(0)
52+
binary_content = binary_stream.read()
53+
return base64.b64encode(binary_content).decode("utf-8")
54+
55+
3956
def _build_form_request(request_body: dict) -> MultipartEncoder:
4057
"""
4158
Build a form-data request.

0 commit comments

Comments
 (0)