diff --git a/app/builders/messages/message_builder.rb b/app/builders/messages/message_builder.rb index e9bf0802b..69ed786ce 100644 --- a/app/builders/messages/message_builder.rb +++ b/app/builders/messages/message_builder.rb @@ -35,7 +35,13 @@ class Messages::MessageBuilder 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 diff --git a/app/helpers/file_type_helper.rb b/app/helpers/file_type_helper.rb index db3f6249d..03b807aad 100644 --- a/app/helpers/file_type_helper.rb +++ b/app/helpers/file_type_helper.rb @@ -8,6 +8,12 @@ module FileTypeHelper :file 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) [ 'image/jpeg', diff --git a/spec/builders/messages/message_builder_spec.rb b/spec/builders/messages/message_builder_spec.rb index 6227f2a48..b17f752db 100644 --- a/spec/builders/messages/message_builder_spec.rb +++ b/spec/builders/messages/message_builder_spec.rb @@ -50,5 +50,33 @@ describe ::Messages::MessageBuilder do expect(message.message_type).to eq params[:message_type] 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 diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 517f336a0..3b42c77ff 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -64,6 +64,7 @@ RSpec.configure do |config| # arbitrary gems may also be filtered via: # config.filter_gems_from_backtrace("gem name") config.include SlackStubs + config.include FileUploadHelpers config.include Devise::Test::IntegrationHelpers, type: :request config.include ActiveSupport::Testing::TimeHelpers config.include ActionCable::TestHelper diff --git a/spec/support/file_upload_helpers.rb b/spec/support/file_upload_helpers.rb new file mode 100644 index 000000000..fa4e577cf --- /dev/null +++ b/spec/support/file_upload_helpers.rb @@ -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