Skip to content

Commit dbeae1b

Browse files
committed
Add content-type and filename params for file upload
1 parent 7441bde commit dbeae1b

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

lib/rocket_chat/messages/room.rb

+11-2
Original file line numberDiff line numberDiff line change
@@ -316,10 +316,11 @@ def members(room_id: nil, name: nil, offset: nil, count: nil, sort: nil)
316316
#
317317
# https://developer.rocket.chat/reference/api/rest-api/endpoints/rooms-endpoints/upload-file-to-a-room
318318
def upload_file(room_id:, file:, **rest_params)
319+
form_data = file_upload_hash(file: file, **rest_params)
319320
response = session.request_json(
320321
"#{API_PREFIX}/rooms.upload/#{room_id}",
321322
method: :post,
322-
form_data: file_upload_hash(file: file, **rest_params)
323+
form_data: form_data
323324
)
324325

325326
RocketChat::Message.new response['message'] if response['success']
@@ -347,7 +348,15 @@ def validate_attribute(attribute)
347348

348349
def file_upload_hash(**params)
349350
permited_keys_for_file_upload = %i[file msg description tmid]
350-
Util.slice_hash(params, *permited_keys_for_file_upload)
351+
hash = Util.slice_hash(params, *permited_keys_for_file_upload)
352+
353+
# NOTE: https://www.rubydoc.info/github/ruby/ruby/Net/HTTPHeader:set_form
354+
file_options = params.slice(:filename, :content_type).reject { |_key, value| value.nil? }
355+
hash.map do |key, value|
356+
next [key, value] unless key == :file && file_options.keys.any?
357+
358+
[key, value, file_options]
359+
end
351360
end
352361
end
353362
end

spec/shared/room_behaviors.rb

+16
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,22 @@
724724
it { expect(upload).to be_a(RocketChat::Message) }
725725
end
726726

727+
context 'with content_type params' do
728+
subject(:upload) { scope.upload_file(room_id: room_id, file: file, content_type: content_type, **rest_params) }
729+
730+
let(:content_type) { 'image/png' }
731+
let(:file) { File.open('spec/fixtures/files/image.png') }
732+
let(:response) { png_upload_response(room_id: room_id) }
733+
734+
before do
735+
stub_authed_request(:post, path).to_return(body: response, status: 200)
736+
end
737+
738+
it { expect { upload }.not_to raise_error }
739+
it { expect(upload).to be_a(RocketChat::Message) }
740+
end
741+
742+
727743
context 'when not accepted error is raised' do
728744
before do
729745
stub_authed_request(:post, path).to_return(body: response, status: 400)

0 commit comments

Comments
 (0)