From 9761f19702568fe796a42c20ac1ede742b9854d3 Mon Sep 17 00:00:00 2001 From: Artem Musalitin Date: Wed, 20 Sep 2023 16:54:35 +0400 Subject: [PATCH 1/6] Add content-type and filename params for file upload --- lib/rocket_chat/messages/room.rb | 10 +++++++++- spec/shared/room_behaviors.rb | 15 +++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/rocket_chat/messages/room.rb b/lib/rocket_chat/messages/room.rb index 7904d3c..f2585e5 100644 --- a/lib/rocket_chat/messages/room.rb +++ b/lib/rocket_chat/messages/room.rb @@ -347,7 +347,15 @@ def validate_attribute(attribute) def file_upload_hash(**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) + + # NOTE: https://www.rubydoc.info/github/ruby/ruby/Net/HTTPHeader:set_form + file_options = params.slice(:filename, :content_type).compact + hash.map do |key, value| + next [key, value] unless key == :file && file_options.keys.any? + + [key, value, file_options] + end end end 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) From fa9b84767c4ffd49cf6fcf3a86dad3d29b4f9db6 Mon Sep 17 00:00:00 2001 From: Artem Musalitin Date: Wed, 20 Sep 2023 17:01:10 +0400 Subject: [PATCH 2/6] Update guard clause to prevent unless with complex condition in room file upload hash --- lib/rocket_chat/messages/room.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/rocket_chat/messages/room.rb b/lib/rocket_chat/messages/room.rb index f2585e5..36c2735 100644 --- a/lib/rocket_chat/messages/room.rb +++ b/lib/rocket_chat/messages/room.rb @@ -352,9 +352,9 @@ def file_upload_hash(**params) # NOTE: https://www.rubydoc.info/github/ruby/ruby/Net/HTTPHeader:set_form file_options = params.slice(:filename, :content_type).compact hash.map do |key, value| - next [key, value] unless key == :file && file_options.keys.any? + next [key, value, file_options] if key == :file && file_options.keys.any? - [key, value, file_options] + [key, value] end end end From 66aed53e4906a324a00d5b1dd7b6eaca90f2d4bb Mon Sep 17 00:00:00 2001 From: Artem Musalitin Date: Wed, 20 Sep 2023 17:03:53 +0400 Subject: [PATCH 3/6] Add additional params into docs --- docs/channels.md | 2 +- docs/groups.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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") ``` From ee2527fc0fb4e5b234d1a4ae684c4874c1b43ba5 Mon Sep 17 00:00:00 2001 From: Artem Musalitin Date: Wed, 20 Sep 2023 17:05:02 +0400 Subject: [PATCH 4/6] Remove trailing spaces --- lib/rocket_chat/messages/room.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rocket_chat/messages/room.rb b/lib/rocket_chat/messages/room.rb index 36c2735..a28bd3b 100644 --- a/lib/rocket_chat/messages/room.rb +++ b/lib/rocket_chat/messages/room.rb @@ -354,7 +354,7 @@ def file_upload_hash(**params) hash.map do |key, value| next [key, value, file_options] if key == :file && file_options.keys.any? - [key, value] + [key, value] end end end From 8a91e5672db65ff657e3d0c686b024d3d5362eec Mon Sep 17 00:00:00 2001 From: Artem Musalitin Date: Wed, 20 Sep 2023 18:21:20 +0400 Subject: [PATCH 5/6] Remove logic when form data is hash --- lib/rocket_chat/messages/room.rb | 10 +++++----- lib/rocket_chat/request_helper.rb | 3 +-- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/lib/rocket_chat/messages/room.rb b/lib/rocket_chat/messages/room.rb index a28bd3b..2148319 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,16 +345,16 @@ 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] - hash = Util.slice_hash(params, *permited_keys_for_file_upload) + hash = Util.slice_hash(params, *permited_keys_for_file_upload).compact # NOTE: https://www.rubydoc.info/github/ruby/ruby/Net/HTTPHeader:set_form file_options = params.slice(:filename, :content_type).compact hash.map do |key, value| - next [key, value, file_options] if key == :file && file_options.keys.any? + next [key.to_s, value, file_options] if key == :file && file_options.keys.any? - [key, value] + [key.to_s, value] 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 From 569d139be116e46519ea4c426193725713300b0b Mon Sep 17 00:00:00 2001 From: Artem Musalitin <38328305+MrRTi@users.noreply.github.com> Date: Mon, 9 Oct 2023 05:52:11 +0000 Subject: [PATCH 6/6] Change url to set_form doc at lib/rocket_chat/messages/room.rb Co-authored-by: Andrew Bromwich --- lib/rocket_chat/messages/room.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rocket_chat/messages/room.rb b/lib/rocket_chat/messages/room.rb index 2148319..7b6fa77 100644 --- a/lib/rocket_chat/messages/room.rb +++ b/lib/rocket_chat/messages/room.rb @@ -349,7 +349,7 @@ def file_upload_array(**params) permited_keys_for_file_upload = %i[file msg description tmid] hash = Util.slice_hash(params, *permited_keys_for_file_upload).compact - # NOTE: https://www.rubydoc.info/github/ruby/ruby/Net/HTTPHeader:set_form + # 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?