Skip to content

Commit

Permalink
Set the Content-Disposition header to trigger download with the file …
Browse files Browse the repository at this point in the history
…name
  • Loading branch information
cbeer committed Nov 11, 2015
1 parent 23da10e commit ef450e2
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ AllCops:

Metrics/LineLength:
Max: 120

Metrics/AbcSize:
Max: 15.1
10 changes: 10 additions & 0 deletions app/controllers/iiif_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ def show
authorize! :read, @image
expires_in 10.minutes, public: anonymous_ability.can?(:read, @image)

set_image_response_headers

self.content_type = Mime::Type.lookup_by_extension(params[:format]).to_s
self.response_body = @image.response
end
Expand Down Expand Up @@ -64,6 +66,14 @@ def cache_headers
}
end

def set_image_response_headers
set_attachment_content_disposition_header if params[:download]
end

def set_attachment_content_disposition_header
response.headers['Content-Disposition'] = "attachment;filename=#{params[:identifier]}.#{params[:format]}"
end

def load_image
@image ||= StacksImage.new(image_params)
end
Expand Down
30 changes: 24 additions & 6 deletions spec/controllers/iiif_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@
end

describe '#show' do
let(:iiif_params) do
{
identifier: 'nr349ct7889%2Fnr349ct7889_00_0001',
region: '0,640,2552,2552',
size: '100,100',
rotation: '0',
quality: 'default',
format: 'jpg'
}
end

subject do
get :show, identifier: 'nr349ct7889%2Fnr349ct7889_00_0001',
region: '0,640,2552,2552',
size: '100,100',
rotation: '0',
quality: 'default',
format: 'jpg'
get :show, iiif_params
end

it 'loads the image' do
Expand Down Expand Up @@ -63,6 +69,18 @@
end
end
end

context 'with the download flag set' do
subject { get :show, iiif_params.merge(download: true) }

it 'sets the content-disposition header to attachment' do
expect(subject.headers['Content-Disposition']).to start_with 'attachment'
end

it 'sets the preferred filename' do
expect(subject.headers['Content-Disposition']).to include 'filename=nr349ct7889%2Fnr349ct7889_00_0001.jpg'
end
end
end

describe '#metadata' do
Expand Down

0 comments on commit ef450e2

Please sign in to comment.