diff --git a/docs/channels.md b/docs/channels.md index bafd8e6..9c9eddd 100644 --- a/docs/channels.md +++ b/docs/channels.md @@ -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") ``` diff --git a/docs/groups.md b/docs/groups.md index 85684c0..7da14c9 100644 --- a/docs/groups.md +++ b/docs/groups.md @@ -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") ``` diff --git a/lib/rocket_chat/messages/room.rb b/lib/rocket_chat/messages/room.rb index 7904d3c..7b6fa77 100644 --- a/lib/rocket_chat/messages/room.rb +++ b/lib/rocket_chat/messages/room.rb @@ -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'] @@ -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 diff --git a/lib/rocket_chat/request_helper.rb b/lib/rocket_chat/request_helper.rb index 5d75838..f280aa8 100644 --- a/lib/rocket_chat/request_helper.rb +++ b/lib/rocket_chat/request_helper.rb @@ -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 @@ -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 diff --git a/spec/shared/room_behaviors.rb b/spec/shared/room_behaviors.rb index 6bf6ef4..579e04c 100644 --- a/spec/shared/room_behaviors.rb +++ b/spec/shared/room_behaviors.rb @@ -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)