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

Allow custom filename when adding an attachment #496

Merged
merged 3 commits into from
Nov 7, 2024
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
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.
SubashPradhan marked this conversation as resolved.
Show resolved Hide resolved
# @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)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Alternatively, it could also be named parameter: 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
Loading