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

Support custom attachment filename for large files #502

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
7 changes: 7 additions & 0 deletions lib/nylas/utils/file_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ def self.build_form_request(request_body)
file = File.open(attachment[:file_path], "rb")
end

# Setting original filename and content type if available. See rest-client#lib/restclient/payload.rb
filename = attachment[:filename] || attachment["filename"]
file.define_singleton_method(:original_filename) { filename } if filename

content_type = attachment[:content_type] || attachment["content_type"]
file.define_singleton_method(:content_type) { content_type } if content_type
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might not be required - added as a bonus.


form_data.merge!({ "file#{index}" => file })
opened_files << file
end
Expand Down
9 changes: 8 additions & 1 deletion spec/nylas/utils/file_utils_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,12 @@
let(:mock_file) { instance_double("file") }

it "returns form data when attachment size is greater than 3MB" do
large_attachment = { size: 4 * 1024 * 1024, content: mock_file }
large_attachment = {
size: 4 * 1024 * 1024,
content: mock_file,
filename: "file.txt",
content_type: "text/plain"
}
request_body = { attachments: [large_attachment] }

allow(mock_file).to receive(:read).and_return("file content")
Expand All @@ -202,6 +207,8 @@

expect(payload).to include("multipart" => true)
expect(opened_files).to include(mock_file)
expect(mock_file.original_filename).to eq("file.txt")
expect(mock_file.content_type).to eq("text/plain")
end

it "returns json data when attachment size is less than 3MB" do
Expand Down
Loading