Skip to content

Commit

Permalink
Add content-type and filename params for file upload (#47)
Browse files Browse the repository at this point in the history
* Add content-type and filename params for file upload
* Add additional params into docs
  • Loading branch information
MrRTi authored Dec 22, 2023
1 parent 7441bde commit 2628a91
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 7 deletions.
2 changes: 1 addition & 1 deletion docs/channels.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,6 @@ require 'rocketchat'

rocket_server = RocketChat::Server.new('http://your.server.address/')
session = rocket_server.login('username', 'password')
session.channels.upload_file(room_id: 'GENERAL', file: File, msg: "Optional Message", description: "Optional Description", tmid: "Optional thread message id")
session.channels.upload_file(room_id: 'GENERAL', file: File, filename: "Optional. The name of the file to use.", content_type: "Optional. The content type of the uploaded file", msg: "Optional Message", description: "Optional Description", tmid: "Optional thread message id")

```
2 changes: 1 addition & 1 deletion docs/groups.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,6 @@ require 'rocketchat'

rocket_server = RocketChat::Server.new('http://your.server.address/')
session = rocket_server.login('username', 'password')
session.groups.upload_file(room_id: 'GENERAL', file: File, msg: "Optional Message", description: "Optional Description", tmid: "Optional thread message id")
session.groups.upload_file(room_id: 'GENERAL', file: File, filename: "Optional. The name of the file to use.", content_type: "Optional. The content type of the uploaded file", msg: "Optional Message", description: "Optional Description", tmid: "Optional thread message id")

```
14 changes: 11 additions & 3 deletions lib/rocket_chat/messages/room.rb
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ def upload_file(room_id:, file:, **rest_params)
response = session.request_json(
"#{API_PREFIX}/rooms.upload/#{room_id}",
method: :post,
form_data: file_upload_hash(file: file, **rest_params)
form_data: file_upload_array(file: file, **rest_params)
)

RocketChat::Message.new response['message'] if response['success']
Expand All @@ -345,9 +345,17 @@ def validate_attribute(attribute)
self.class.settable_attributes.include?(attribute)
end

def file_upload_hash(**params)
def file_upload_array(**params)
permited_keys_for_file_upload = %i[file msg description tmid]
Util.slice_hash(params, *permited_keys_for_file_upload)
hash = Util.slice_hash(params, *permited_keys_for_file_upload).compact

# NOTE: https://docs.ruby-lang.org/en/master/Net/HTTPHeader.html#method-i-set_form
file_options = params.slice(:filename, :content_type).compact
hash.map do |key, value|
next [key.to_s, value, file_options] if key == :file && file_options.keys.any?

[key.to_s, value]
end
end
end
end
Expand Down
3 changes: 1 addition & 2 deletions lib/rocket_chat/request_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def create_request(path, options)
req = Net::HTTP::Post.new(path, headers)
add_body(req, body) if body

form_data = reject_nils(options[:form_data])
form_data = options[:form_data]
add_form_data(req, form_data) if form_data
else
uri = path
Expand All @@ -131,7 +131,6 @@ def add_body(request, body)
end

def add_form_data(request, form_data)
form_data = form_data.transform_keys(&:to_s) if form_data.is_a? Hash
request.set_form(form_data, 'multipart/form-data')
end

Expand Down
15 changes: 15 additions & 0 deletions spec/shared/room_behaviors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,21 @@
it { expect(upload).to be_a(RocketChat::Message) }
end

context 'with content_type params' do
subject(:upload) { scope.upload_file(room_id: room_id, file: file, content_type: content_type, **rest_params) }

let(:content_type) { 'image/png' }
let(:file) { File.open('spec/fixtures/files/image.png') }
let(:response) { png_upload_response(room_id: room_id) }

before do
stub_authed_request(:post, path).to_return(body: response, status: 200)
end

it { expect { upload }.not_to raise_error }
it { expect(upload).to be_a(RocketChat::Message) }
end

context 'when not accepted error is raised' do
before do
stub_authed_request(:post, path).to_return(body: response, status: 400)
Expand Down

0 comments on commit 2628a91

Please sign in to comment.