Skip to content

Commit

Permalink
Add ability to send inline image > 3mb
Browse files Browse the repository at this point in the history
  • Loading branch information
Subash Pradhan committed Nov 29, 2024
1 parent 9d6db9e commit 8c65f6a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/nylas/utils/file_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ def self.build_form_request(request_body)
content_type = attachment[:content_type] || attachment["content_type"]
file.define_singleton_method(:content_type) { content_type } if content_type

form_data.merge!({ "file#{index}" => file })
content_id = attachment[:content_id] || attachment["content_id"] || "file#{index}"

form_data.merge!({ content_id => file })
opened_files << file
end

Expand Down Expand Up @@ -100,7 +102,7 @@ def self.handle_message_payload(request_body)
# @param file_path [String] The path to the file to attach.
# @param filename [String] The name of the attached file. Optional, derived from file_path by default.
# @return [Hash] The request that will attach the file to the message/draft
def self.attach_file_request_builder(file_path, filename = nil)
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?
Expand All @@ -110,12 +112,12 @@ def self.attach_file_request_builder(file_path, filename = nil)
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
Expand Down
19 changes: 19 additions & 0 deletions spec/nylas/utils/file_utils_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
content_type: "text/plain",
size: 100,
content: mock_file,
content_id: nil,
file_path: file_path
)
end
Expand All @@ -39,6 +40,7 @@
content_type: "application/octet-stream",
size: file_size,
content: mock_file,
content_id: nil,
file_path: file_path
)
end
Expand All @@ -54,6 +56,23 @@
content_type: "text/plain",
size: 100,
content: mock_file,
content_id: nil,
file_path: file_path
)
end

it "accepts optional content_id parameter" do
file_path = "/path/to/file.txt"
content_id = "content-id-123"

attach_file_req = described_class.attach_file_request_builder(file_path, nil, content_id)

expect(attach_file_req).to eq(
filename: "file.txt",
content_type: "text/plain",
size: 100,
content: mock_file,
content_id: content_id,
file_path: file_path
)
end
Expand Down

0 comments on commit 8c65f6a

Please sign in to comment.