From fa6b3846e90317d2cf54d82d0040ac1b3fb50d61 Mon Sep 17 00:00:00 2001 From: Jarek Lipski Date: Wed, 13 Nov 2024 19:42:12 +0100 Subject: [PATCH 1/2] Support custom attachment filename for large files --- lib/nylas/utils/file_utils.rb | 7 +++++++ spec/nylas/utils/file_utils_spec.rb | 4 +++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/nylas/utils/file_utils.rb b/lib/nylas/utils/file_utils.rb index 08bdf952..d7f8cdf0 100644 --- a/lib/nylas/utils/file_utils.rb +++ b/lib/nylas/utils/file_utils.rb @@ -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 code 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 + form_data.merge!({ "file#{index}" => file }) opened_files << file end diff --git a/spec/nylas/utils/file_utils_spec.rb b/spec/nylas/utils/file_utils_spec.rb index f64d694c..e88e7037 100644 --- a/spec/nylas/utils/file_utils_spec.rb +++ b/spec/nylas/utils/file_utils_spec.rb @@ -192,7 +192,7 @@ 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") @@ -202,6 +202,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 From 462f68bfdc86bea9da5e344c6993da2aa7deb5c3 Mon Sep 17 00:00:00 2001 From: Jarek Lipski Date: Wed, 13 Nov 2024 19:50:49 +0100 Subject: [PATCH 2/2] rubocop --- lib/nylas/utils/file_utils.rb | 2 +- spec/nylas/utils/file_utils_spec.rb | 11 ++++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/nylas/utils/file_utils.rb b/lib/nylas/utils/file_utils.rb index d7f8cdf0..264745f8 100644 --- a/lib/nylas/utils/file_utils.rb +++ b/lib/nylas/utils/file_utils.rb @@ -36,7 +36,7 @@ def self.build_form_request(request_body) file = File.open(attachment[:file_path], "rb") end - # Setting original filename and content type if available. See code rest-client#lib/restclient/payload.rb + # 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 diff --git a/spec/nylas/utils/file_utils_spec.rb b/spec/nylas/utils/file_utils_spec.rb index e88e7037..f8bfc947 100644 --- a/spec/nylas/utils/file_utils_spec.rb +++ b/spec/nylas/utils/file_utils_spec.rb @@ -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, filename: 'file.txt', content_type: 'text/plain' } + 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") @@ -202,8 +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') + 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