Skip to content

Fixed issue where attachments < 3mb were not being encoded correctly #362

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Unreleased
* Added clean messages support
* Added additional webhook triggers
* Made event visibility optional to support iCloud events
* Fixed issue where attachments < 3mb were not being encoded correctly

v6.1.1
----------------
Expand Down
17 changes: 16 additions & 1 deletion nylas/resources/drafts.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import io
from typing import Optional

from nylas.handler.api_resources import (
Expand All @@ -15,7 +16,11 @@
)
from nylas.models.messages import Message
from nylas.models.response import ListResponse, Response, DeleteResponse
from nylas.utils.file_utils import _build_form_request, MAXIMUM_JSON_ATTACHMENT_SIZE
from nylas.utils.file_utils import (
_build_form_request,
MAXIMUM_JSON_ATTACHMENT_SIZE,
encode_stream_to_base64,
)


class Drafts(
Expand Down Expand Up @@ -99,6 +104,11 @@ def create(

return Response.from_dict(json_response, Draft)

# Encode the content of the attachments to base64
for attachment in request_body.get("attachments", []):
if issubclass(type(attachment["content"]), io.IOBase):
attachment["content"] = encode_stream_to_base64(attachment["content"])

return super().create(
path=path,
response_type=Draft,
Expand Down Expand Up @@ -138,6 +148,11 @@ def update(

return Response.from_dict(json_response, Draft)

# Encode the content of the attachments to base64
for attachment in request_body.get("attachments", []):
if issubclass(type(attachment["content"]), io.IOBase):
attachment["content"] = encode_stream_to_base64(attachment["content"])

return super().update(
path=path,
response_type=Draft,
Expand Down
14 changes: 13 additions & 1 deletion nylas/resources/messages.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import io
from typing import Optional, List

from nylas.handler.api_resources import (
Expand All @@ -19,7 +20,11 @@
)
from nylas.models.response import Response, ListResponse, DeleteResponse
from nylas.resources.smart_compose import SmartCompose
from nylas.utils.file_utils import _build_form_request, MAXIMUM_JSON_ATTACHMENT_SIZE
from nylas.utils.file_utils import (
_build_form_request,
MAXIMUM_JSON_ATTACHMENT_SIZE,
encode_stream_to_base64,
)


class Messages(
Expand Down Expand Up @@ -151,6 +156,13 @@ def send(
if attachment_size >= MAXIMUM_JSON_ATTACHMENT_SIZE:
form_data = _build_form_request(request_body)
else:
# Encode the content of the attachments to base64
for attachment in request_body.get("attachments", []):
if issubclass(type(attachment["content"]), io.IOBase):
attachment["content"] = encode_stream_to_base64(
attachment["content"]
)

json_body = request_body

json_response = self._http_client._execute(
Expand Down
17 changes: 17 additions & 0 deletions nylas/utils/file_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import base64
import json
import mimetypes
import os
from pathlib import Path
from typing import BinaryIO

from requests_toolbelt import MultipartEncoder

Expand Down Expand Up @@ -36,6 +38,21 @@ def attach_file_request_builder(file_path) -> CreateAttachmentRequest:
}


def encode_stream_to_base64(binary_stream: BinaryIO) -> str:
"""
Encode the content of a binary stream to a base64 string.

Attributes:
binary_stream: The binary stream to encode.

Returns:
The base64 encoded content of the binary stream.
"""
binary_stream.seek(0)
binary_content = binary_stream.read()
return base64.b64encode(binary_content).decode("utf-8")


def _build_form_request(request_body: dict) -> MultipartEncoder:
"""
Build a form-data request.
Expand Down
Loading