Skip to content

Commit

Permalink
Add content-type and filename params for file upload
Browse files Browse the repository at this point in the history
  • Loading branch information
MrRTi committed Sep 20, 2023
1 parent 7441bde commit 9761f19
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/rocket_chat/messages/room.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
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 9761f19

Please sign in to comment.