Skip to content

Commit

Permalink
Merge pull request #496 from loomchild/attach_file_custom_filename
Browse files Browse the repository at this point in the history
Allow custom filename when adding an attachment
  • Loading branch information
SubashPradhan authored Nov 7, 2024
2 parents 25b9aad + b260359 commit f016363
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/nylas/utils/file_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,10 @@ 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 = 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
Expand Down
15 changes: 15 additions & 0 deletions spec/nylas/utils/file_utils_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f016363

Please sign in to comment.