From 113da092bb07950d21b38c406d7672995669eea9 Mon Sep 17 00:00:00 2001 From: Jarek Lipski Date: Wed, 16 Oct 2024 18:47:52 +0200 Subject: [PATCH 1/2] Allow custom filename when adding an attachment --- lib/nylas/utils/file_utils.rb | 4 ++-- spec/nylas/utils/file_utils_spec.rb | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/nylas/utils/file_utils.rb b/lib/nylas/utils/file_utils.rb index 1e730f33..9e85e786 100644 --- a/lib/nylas/utils/file_utils.rb +++ b/lib/nylas/utils/file_utils.rb @@ -92,8 +92,8 @@ def self.handle_message_payload(request_body) # Build the request to attach a file to a message/draft object. # @param file_path [String] The path to the file to attach. # @return [Hash] The request that will attach the file to the message/draft - def self.attach_file_request_builder(file_path) - filename = File.basename(file_path) + def self.attach_file_request_builder(file_path, filename = 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 diff --git a/spec/nylas/utils/file_utils_spec.rb b/spec/nylas/utils/file_utils_spec.rb index e2357b8a..f64d694c 100644 --- a/spec/nylas/utils/file_utils_spec.rb +++ b/spec/nylas/utils/file_utils_spec.rb @@ -42,6 +42,21 @@ file_path: file_path ) end + + it "accepts optional filename parameter" do + file_path = "/path/to/file.txt" + filename = "customm-file.txt" + + attach_file_req = described_class.attach_file_request_builder(file_path, filename) + + expect(attach_file_req).to eq( + filename: "customm-file.txt", + content_type: "text/plain", + size: 100, + content: mock_file, + file_path: file_path + ) + end end describe "#build_form_request" do From 24c194befac82032747d2fe5419052c367ca4745 Mon Sep 17 00:00:00 2001 From: Jarek Lipski Date: Thu, 7 Nov 2024 12:16:38 +0100 Subject: [PATCH 2/2] comment --- lib/nylas/utils/file_utils.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/nylas/utils/file_utils.rb b/lib/nylas/utils/file_utils.rb index 9e85e786..08bdf952 100644 --- a/lib/nylas/utils/file_utils.rb +++ b/lib/nylas/utils/file_utils.rb @@ -91,6 +91,7 @@ def self.handle_message_payload(request_body) # Build the request to attach a file to a message/draft object. # @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) filename ||= File.basename(file_path)