Module: Nylas::FileUtils

Defined in:
lib/nylas/utils/file_utils.rb

Overview

A collection of file-related utilities.

Constant Summary collapse

FORM_DATA_ATTACHMENT_SIZE =

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

3 * 1024 * 1024

Class Method Summary collapse

Class Method Details

.attach_file_request_builder(file_path, filename = nil, content_id = nil) ⇒ Hash

Build the request to attach a file to a message/draft object.

Parameters:

  • file_path (String)

    The path to the file to attach.

  • filename (String) (defaults to: nil)

    The name of the attached file. Optional, derived from file_path by default.

Returns:

  • (Hash)

    The request that will attach the file to the message/draft



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/nylas/utils/file_utils.rb', line 105

def self.attach_file_request_builder(file_path, filename = nil, content_id = nil)
  filename ||= File.basename(file_path)
  content_type = MIME::Types.type_for(file_path)
  content_type = if !content_type.nil? && !content_type.empty?
                   content_type.first.to_s
                 else
                   "application/octet-stream"
                 end
  size = File.size(file_path)
  content = File.new(file_path, "rb")
  {
    filename: filename,
    content_type: content_type,
    size: size,
    content: content,
    content_id: content_id,
    file_path: file_path
  }
end