diff --git a/lib/api/helpers/attachment_renderer.rb b/lib/api/helpers/attachment_renderer.rb index 6a3241bfb5..525341be27 100644 --- a/lib/api/helpers/attachment_renderer.rb +++ b/lib/api/helpers/attachment_renderer.rb @@ -44,7 +44,7 @@ module API redirect attachment.external_url.to_s else content_type attachment.content_type - header['Content-Disposition'] = "attachment; filename=#{attachment.filename}" + header['Content-Disposition'] = attachment.content_disposition env['api.format'] = :binary attachment.diskfile.read end diff --git a/spec/requests/api/v3/attachments/attachment_resource_shared_examples.rb b/spec/requests/api/v3/attachments/attachment_resource_shared_examples.rb index bd0019feff..b65411c7b4 100644 --- a/spec/requests/api/v3/attachments/attachment_resource_shared_examples.rb +++ b/spec/requests/api/v3/attachments/attachment_resource_shared_examples.rb @@ -252,8 +252,10 @@ shared_examples 'an APIv3 attachment resource', type: :request, content_type: :j subject(:response) { last_response } context 'with required permissions' do - context 'for a local file' do - let(:mock_file) { FileHelpers.mock_uploaded_file name: 'foobar.txt' } + shared_examples 'for a local file' do + let(:mock_file) { raise "define mock_file" } + let(:content_disposition) { raise "define content_disposition" } + let(:attachment) do att = FactoryBot.create(:attachment, container: container, file: mock_file) @@ -270,7 +272,7 @@ shared_examples 'an APIv3 attachment resource', type: :request, content_type: :j it 'has the necessary headers' do expect(subject.headers['Content-Disposition']) - .to eql "attachment; filename=#{mock_file.original_filename}" + .to eql content_disposition expect(subject.headers['Content-Type']) .to eql mock_file.content_type @@ -282,6 +284,20 @@ shared_examples 'an APIv3 attachment resource', type: :request, content_type: :j end end + context 'for a local text file' do + it_behaves_like 'for a local file' do + let(:mock_file) { FileHelpers.mock_uploaded_file name: 'foobar.txt' } + let(:content_disposition) { "inline" } + end + end + + context 'for a local binary file' do + it_behaves_like 'for a local file' do + let(:mock_file) { FileHelpers.mock_uploaded_file name: 'foobar.dat', content_type: "application/octet-stream" } + let(:content_disposition) { "attachment; filename=#{mock_file.original_filename}" } + end + end + context 'for a remote file' do let(:external_url) { 'http://some_service.org/blubs.gif' } let(:mock_file) { FileHelpers.mock_uploaded_file name: 'foobar.txt' }