Skip to content

Commit

Permalink
When an Id can't be resolved, render a 404, per the IIIF spec
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne committed Sep 8, 2017
1 parent dbf481f commit 76c207c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/resolvers/riiif/abstract_file_system_resolver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def find(id)
# @return the path of the file
def path(id)
search = pattern(id)
Dir.glob(search).first || raise(ImageNotFoundError, search)
search && Dir.glob(search).first || raise(ImageNotFoundError, search)
end

def pattern(_id)
Expand Down
6 changes: 5 additions & 1 deletion app/resolvers/riiif/file_system_file_resolver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ module Riiif
class FileSystemFileResolver < AbstractFileSystemResolver
attr_writer :input_types

# @return nil when the id is not valid
def pattern(id)
raise ArgumentError, "Invalid characters in id `#{id}`" unless id =~ /^[\w\-:]+$/
unless id =~ /^[\w\-:]+$/
Rails.logger.warn "Invalid characters in id `#{id}`"
return
end
::File.join(base_path, "#{id}.{#{input_types.join(',')}}")
end

Expand Down
15 changes: 15 additions & 0 deletions spec/models/riiif/file_system_file_resolver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@
expect(subject.path).to eq base_path + '/world.jp2'
end
end

context 'when pattern raises an ArgumentError' do
let(:id) { 'foo/bar' } # slashes are not permitted by default

it 'casts the error to a not found (required by the IIIF spec)' do
expect { subject.path }.to raise_error Riiif::ImageNotFoundError
end
end
end

describe '#input_types' do
Expand Down Expand Up @@ -63,5 +71,12 @@
expect { subject }.not_to raise_error
end
end

context 'with slashes (unallowed by default)' do
let(:id) { 'fo/baz' }
it 'returns nil' do
expect(subject).to be_nil
end
end
end
end

0 comments on commit 76c207c

Please sign in to comment.