Skip to content
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

Add ability to send inline image > 3mb #506

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
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
Loading