fix: check the content type for the file when uploading from cloud storage (#5378)

When sending the message with audio, only the signed id of the file is sent.
In the back end check only the UploadedFile type.
The attachment has the default file type image, now it gets the content type from the signed id

Fixes: #5375

Co-authored-by: Sojan Jose <sojan@pepalo.com>
This commit is contained in:
jacsonsantospht
2022-10-21 22:05:36 -03:00
committed by GitHub
parent 6823b04e5b
commit af020f446e
5 changed files with 51 additions and 1 deletions

View File

@@ -35,7 +35,13 @@ class Messages::MessageBuilder
file: uploaded_attachment file: uploaded_attachment
) )
attachment.file_type = file_type(uploaded_attachment&.content_type) if uploaded_attachment.is_a?(ActionDispatch::Http::UploadedFile) attachment.file_type = if uploaded_attachment.is_a?(String)
file_type_by_signed_id(
uploaded_attachment
)
else
file_type(uploaded_attachment&.content_type)
end
end end
end end

View File

@@ -8,6 +8,12 @@ module FileTypeHelper
:file :file
end end
# Used in case of DIRECT_UPLOADS_ENABLED=true
def file_type_by_signed_id(signed_id)
blob = ActiveStorage::Blob.find_signed(signed_id)
file_type(blob&.content_type)
end
def image_file?(content_type) def image_file?(content_type)
[ [
'image/jpeg', 'image/jpeg',

View File

@@ -50,5 +50,33 @@ describe ::Messages::MessageBuilder do
expect(message.message_type).to eq params[:message_type] expect(message.message_type).to eq params[:message_type]
end end
end end
context 'when attachment messages' do
let(:params) do
ActionController::Parameters.new({
content: 'test',
attachments: [Rack::Test::UploadedFile.new('spec/assets/avatar.png', 'image/png')]
})
end
it 'creates message with attachments' do
message = message_builder
expect(message.attachments.first.file_type).to eq 'image'
end
context 'when DIRECT_UPLOAD_ENABLED' do
let(:params) do
ActionController::Parameters.new({
content: 'test',
attachments: [get_blob_for('spec/assets/avatar.png', 'image/png').signed_id]
})
end
it 'creates message with attachments' do
message = message_builder
expect(message.attachments.first.file_type).to eq 'image'
end
end
end
end end
end end

View File

@@ -64,6 +64,7 @@ RSpec.configure do |config|
# arbitrary gems may also be filtered via: # arbitrary gems may also be filtered via:
# config.filter_gems_from_backtrace("gem name") # config.filter_gems_from_backtrace("gem name")
config.include SlackStubs config.include SlackStubs
config.include FileUploadHelpers
config.include Devise::Test::IntegrationHelpers, type: :request config.include Devise::Test::IntegrationHelpers, type: :request
config.include ActiveSupport::Testing::TimeHelpers config.include ActiveSupport::Testing::TimeHelpers
config.include ActionCable::TestHelper config.include ActionCable::TestHelper

View File

@@ -0,0 +1,9 @@
module FileUploadHelpers
def get_blob_for(file_path, content_type)
ActiveStorage::Blob.create_and_upload!(
io: File.open(file_path, 'rb'),
filename: File.basename(file_path),
content_type: content_type
)
end
end