Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert changes to find minimum image size #1070

Merged
merged 2 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 2 additions & 23 deletions app/models/projection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def image_source
end

def real_transformation
return transformation unless (image.is_a? RestrictedImage) || use_original_size?
return transformation unless image.is_a? RestrictedImage

IIIF::Image::Transformation.new(
region: transformation.region,
Expand All @@ -127,7 +127,7 @@ def restricted_size
size = transformation.size
case size
when IIIF::Image::Size::BestFit
max_size = max_image_size
max_size = image.max_size(self)
if size.width <= max_size.width && size.height <= max_size.height
size
else
Expand All @@ -139,25 +139,4 @@ def restricted_size
size
end
end

# For a full image request, if the requested width and height are larger than the original image,
# this method will return true
def use_original_size?
size = transformation.size
max_size = image.max_tile_dimensions.call(self)
(size.is_a? IIIF::Image::Size::BestFit) && max_size.width < size.width && max_size.height < size.height
end

# The original restricted image function used max_size, but the StacksImage class does not have that method
# Since we are using the restricted image block for both RestrictedImage and StacksImage, we check
# which method is available
def max_image_size
image.respond_to?(:max_size) ? image.max_size(self) : dimensions_to_size(image.max_tile_dimensions.call(self))
end

# StacksImage has max_tile_dimensions, which returns dimensions
# We need to create a Size object in order to pass back to create the Iiif image object for image_source
def dimensions_to_size(dimensions)
IIIF::Image::Size::BestFit.new(dimensions.width, dimensions.height)
end
end
12 changes: 0 additions & 12 deletions spec/models/projection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,18 +112,6 @@
expect(http_client).to have_received(:get).with(%r{/full/max/0/default.jpg})
end
end

context "best fit size" do
let(:options) { { size: '!850,700', region: 'full' } }

it 'returns original size when requested dimensions are larger' do
allow(HTTP).to receive(:use)
.and_return(http_client)
allow(http_client).to receive(:get).and_return(double(body: nil))
subject.response
expect(http_client).to have_received(:get).with(%r{/full/!800,600/0/default.jpg})
end
end
end

context 'for a restricted image' do
Expand Down
77 changes: 48 additions & 29 deletions spec/requests/iiif_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -281,54 +281,54 @@
end
end

describe 'image requests' do
let(:image_response) { instance_double(HTTP::Response, body: StringIO.new, status: 200) }
let(:projection) { instance_double(Projection, response: image_response, valid?: true) }
let(:transformation) { double }
let(:image) do
instance_double(StacksImage, etag: nil,
mtime: nil,
cdl_restricted?: false)
end

describe 'image requests for world readable items' do
let(:ability) { instance_double(Ability, 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)
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, image).and_return(true)

allow(IIIF::Image::OptionDecoder).to receive(:decode)
.with(ActionController::Parameters)
.and_return(transformation)
allow(StacksImage).to receive(:new).and_return(image)
allow(image).to receive(:projection_for).with(transformation).and_return(projection)
allow_any_instance_of(IiifController).to receive(:can?).with(:download, StacksImage).and_return(true)
end

it 'loads the image' do
get "/image/iiif/nr349ct7889%2Fnr349ct7889_00_0001/0,640,2552,2552/100,100/0/default.jpg"
context 'when the request is valid' do
before do
stub_request(:get, "http://imageserver-prod.stanford.edu/iiif/2/nr%2F349%2Fct%2F7889%2Fnr349ct7889_00_0001.jp2/0,640,2552,2552/100,100/0/default.jpg")
.to_return(status: 200, body: "")
end

it 'loads the image' do
get "/image/iiif/nr349ct7889%2Fnr349ct7889_00_0001/0,640,2552,2552/100,100/0/default.jpg"

expect(StacksImage).to have_received(:new).with(
id: "nr349ct7889", file_name: 'nr349ct7889_00_0001.jp2',
canonical_url: "http://www.example.com/image/iiif/nr349ct7889/nr349ct7889_00_0001"
)
expect(response.media_type).to eq 'image/jpeg'
expect(response.status).to eq 200
expect(StacksImage).to have_received(:new).with(
id: "nr349ct7889", file_name: 'nr349ct7889_00_0001.jp2',
canonical_url: "http://www.example.com/image/iiif/nr349ct7889/nr349ct7889_00_0001"
)
expect(response.media_type).to eq 'image/jpeg'
expect(response.status).to eq 200
end
end

context 'additional params' do
it 'ignored when instantiating StacksImage' do
context 'when additional params are provided' do
before do
stub_request(:get, "http://imageserver-prod.stanford.edu/iiif/2/nr%2F349%2Fct%2F7889%2Fnr349ct7889_00_0001.jp2/0,640,2552,2552/100,100/0/default.jpg")
.to_return(status: 200, body: "")
end

it 'is ignored when instantiating StacksImage' do
get "/image/iiif/nr349ct7889%2Fnr349ct7889_00_0001/0,640,2552,2552/100,100/0/default.jpg?ignored=ignored&host=host"

expect(response.status).to eq 200
end
end

context 'when image is missing' do
before { allow(projection).to receive(:valid?).and_return(false) }
before do
stub_request(:get, "http://imageserver-prod.stanford.edu/iiif/2/nr%2F349%2Fct%2F7889%2Fnr349ct7889_00_0001.jp2/0,640,2552,2552/100,100/0/default.jpg")
.to_return(status: 404, body: "")
end

it 'returns 404 Not Found' do
get "/image/iiif/nr349ct7889%2Fnr349ct7889_00_0001/0,640,2552,2552/100,100/0/default.jpg?ignored=ignored&host=host"
Expand All @@ -337,12 +337,31 @@
end

context 'with the download flag set' do
before do
stub_request(:get, "http://imageserver-prod.stanford.edu/iiif/2/nr%2F349%2Fct%2F7889%2Fnr349ct7889_00_0001.jp2/0,640,2552,2552/100,100/0/default.jpg")
.to_return(status: 200, body: "")
end

it 'sets the content-disposition header' do
get "/image/iiif/nr349ct7889%2Fnr349ct7889_00_0001/0,640,2552,2552/100,100/0/default.jpg?download=true"

expect(response.headers['Content-Disposition']).to start_with 'attachment'
expect(response.headers['Content-Disposition']).to include 'filename="nr349ct7889_00_0001.jpg"'
end
end

context 'with a pct region' do
before do
stub_request(:get, "http://imageserver-prod.stanford.edu/iiif/2/nr%2F349%2Fct%2F7889%2Fnr349ct7889_00_0001.jp2/pct:3.0,3.0,77.0,77.0/full/0/default.jpg")
.to_return(status: 200, body: "")
end

it 'loads the image' do
get '/image/iiif/nr349ct7889%2Fnr349ct7889_00_0001/pct:3,3,77,77/full/0/default.jpg'

expect(response.media_type).to eq 'image/jpeg'
expect(response.status).to eq 200
end
end
end
end