Skip to content

file_utils

MAXIMUM_JSON_ATTACHMENT_SIZE = 3 * 1024 * 1024 module-attribute

The maximum size of an attachment that can be sent using json.

attach_file_request_builder(file_path)

Build a request to attach a file.

Attributes:

Name Type Description
file_path

The path to the file to attach.

Returns:

Type Description
CreateAttachmentRequest

A properly-formatted request to attach the file.

Source code in nylas/utils/file_utils.py
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
def attach_file_request_builder(file_path) -> CreateAttachmentRequest:
    """
    Build a request to attach a file.

    Attributes:
        file_path: The path to the file to attach.

    Returns:
        A properly-formatted request to attach the file.
    """
    path = Path(file_path)
    filename = path.name
    size = os.path.getsize(file_path)
    content_type = mimetypes.guess_type(file_path)[0]
    file_stream = open(file_path, "rb")  # pylint: disable=consider-using-with

    return {
        "filename": filename,
        "content_type": content_type if content_type else "application/octet-stream",
        "content": file_stream,
        "size": size,
    }

encode_stream_to_base64(binary_stream)

Encode the content of a binary stream to a base64 string.

Attributes:

Name Type Description
binary_stream

The binary stream to encode.

Returns:

Type Description
str

The base64 encoded content of the binary stream.

Source code in nylas/utils/file_utils.py
41
42
43
44
45
46
47
48
49
50
51
52
53
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")