Skip to content

Commit

Permalink
Merge pull request #55 from curationexperts/cache_control
Browse files Browse the repository at this point in the history
Set Cache-Control headers
  • Loading branch information
mejackreed authored Apr 7, 2017
2 parents 15b07ea + 78da649 commit 2fc6094
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
16 changes: 16 additions & 0 deletions app/controllers/riiif/images_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ def show

data = image.render(image_request_params)
headers['Access-Control-Allow-Origin'] = '*'
# Set a Cache-Control header
expires_in cache_expires, public: false if status == :ok
send_data data,
status: status,
type: Mime::Type.lookup_by_extension(params[:format]),
Expand All @@ -32,6 +34,8 @@ def info
image = model.new(image_id)
if authorization_service.can?(:info, image)
headers['Access-Control-Allow-Origin'] = '*'
# Set a Cache-Control header
expires_in cache_expires, public: false
render json: image.info.to_h.merge(server_info), content_type: 'application/ld+json'
else
render json: { error: 'unauthorized' }, status: :unauthorized
Expand All @@ -48,6 +52,18 @@ def redirect

LEVEL1 = 'http://iiif.io/api/image/2/level1.json'.freeze

# @return seconds before the request expires. Defaults to 1 year.
def cache_expires
1.year
end

# Should the Cache-Control header be public? Override this if you want to have a
# public Cache-Control set.
# @return FalseClass
def public_cache?
false
end

def model
params.fetch(:model, 'riiif/image').camelize.constantize
end
Expand Down
2 changes: 2 additions & 0 deletions spec/controllers/riiif/images_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
expect(response.body).to eq 'IMAGEDATA'
expect(response.headers['Link']).to eq '<http://iiif.io/api/image/2/level1.json>;rel="profile"'
expect(response.headers['Access-Control-Allow-Origin']).to eq '*'
expect(response.headers['Cache-Control']).to eq 'max-age=31557600, private'
end

context 'with an unauthorized image' do
Expand Down Expand Up @@ -115,6 +116,7 @@
expect(response.headers['Link']).to eq '<http://iiif.io/api/image/2/level1.json>;rel="profile"'
expect(response.headers['Content-Type']).to eq 'application/ld+json; charset=utf-8'
expect(response.headers['Access-Control-Allow-Origin']).to eq '*'
expect(response.headers['Cache-Control']).to eq 'max-age=31557600, private'
end

context 'with an unauthorized image' do
Expand Down

0 comments on commit 2fc6094

Please sign in to comment.