Skip to content

Commit

Permalink
Return 404 when the iiif endpoint is called for a file on a collection
Browse files Browse the repository at this point in the history
Filxes #1152
  • Loading branch information
jcoyne committed May 22, 2024
1 parent e826328 commit a78c8e7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
8 changes: 5 additions & 3 deletions app/models/stacks_rights.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,11 @@ def cocina_file
end

def find_file
public_json.dig('structural', 'contains')
.lazy.flat_map { |file_set| file_set.dig('structural', 'contains') }
.find { |file| file['filename'] == file_name } || raise(ActionController::MissingFile, "File not found '#{file_name}'")
file_sets = public_json.dig('structural', 'contains')
raise(ActionController::MissingFile, "File not found '#{file_name}'") unless file_sets # Trap for Collections

file_sets.lazy.flat_map { |file_set| file_set.dig('structural', 'contains') }
.find { |file| file['filename'] == file_name } || raise(ActionController::MissingFile, "File not found '#{file_name}'")
end

def public_json
Expand Down
22 changes: 13 additions & 9 deletions spec/requests/iiif_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -267,15 +267,6 @@
describe 'image requests for world readable items' do
let(:ability) { instance_double(CocinaAbility, can?: false) }

before do
# for the cache headers
allow_any_instance_of(IiifController).to receive(:anonymous_ability).and_return ability
# for authorize! in #show
allow_any_instance_of(IiifController).to receive(:authorize!).with(:read, Projection).and_return(true)
# for current_image
allow_any_instance_of(IiifController).to receive(:can?).with(:download, StacksImage).and_return(true)
end

context 'when the request is valid' do
before do
stub_request(:get, "http://imageserver-prod.stanford.edu/iiif/2/#{image_server_path('nr349ct7889', 'image.jp2')}/0,640,2552,2552/100,100/0/default.jpg")
Expand Down Expand Up @@ -319,6 +310,19 @@
end
end

context 'when the object has no filesets (e.g. a collection)' do
let(:public_json) do
{ 'structural' => {} }
end

it 'returns 404 Not Found' do
stub_request(:get, "http://imageserver-prod.stanford.edu/iiif/2/nr%2F349%2Fct%2F7889%2Fimage.jp2/0,640,2552,2552/100,100/0/default.jpg").
to_return(status: 200, body: "", headers: {})
get "/image/iiif/nr349ct7889%2Fimage.jp2/0,640,2552,2552/100,100/0/default.jpg"
expect(response).to have_http_status :not_found
end
end

context 'with the download flag set' do
before do
stub_request(:get, "http://imageserver-prod.stanford.edu/iiif/2/#{image_server_path('nr349ct7889', 'image.jp2')}/0,640,2552,2552/100,100/0/default.jpg")
Expand Down

0 comments on commit a78c8e7

Please sign in to comment.