Skip to content

Commit

Permalink
Merge pull request #1189 from sul-dlss/injection
Browse files Browse the repository at this point in the history
[refactor] Pass StacksFile into the StacksImage
  • Loading branch information
cbeer authored Jun 28, 2024
2 parents f981b9d + 1417c89 commit df4dcd0
Show file tree
Hide file tree
Showing 18 changed files with 86 additions and 131 deletions.
14 changes: 7 additions & 7 deletions app/controllers/iiif_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def set_attachment_content_disposition_header

def current_image
@image ||= begin
img = StacksImage.new(stacks_image_params)
img = StacksImage.new(stacks_file:, canonical_url:)
can?(:download, img) ? img : img.restricted
end
end
Expand All @@ -147,12 +147,12 @@ def download_filename
[File.basename(identifier_params[:file_name], '.*'), iiif_params[:format]].join('.')
end

def stacks_image_params
{
id: identifier_params[:id],
file_name: identifier_params[:file_name] + '.jp2',
canonical_url: iiif_base_url(id: identifier_params[:id], file_name: identifier_params[:file_name], host: request.host_with_port)
}
def canonical_url
iiif_base_url(id: identifier_params[:id], file_name: identifier_params[:file_name], host: request.host_with_port)
end

def stacks_file
StacksFile.new(id: identifier_params[:id], file_name: "#{identifier_params[:file_name]}.jp2")
end

# @return [IIIF::Image::Transformation] returns the transformation for the parameters
Expand Down
10 changes: 1 addition & 9 deletions app/controllers/legacy_image_service_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@ def allowed_params
end

def load_image
@image ||= StacksImage.new(stacks_image_params)
end

def stacks_image_params
{ transformation: iiif_params }.merge(identifier_params)
@image ||= StacksImage.new(transformation: iiif_params, stacks_file: StacksFile.new(id:, file_name:))
end

def iiif_params
Expand Down Expand Up @@ -86,10 +82,6 @@ def iiif_region
end
end

def identifier_params
{ id:, file_name: }
end

def id
allowed_params[:id]
end
Expand Down
6 changes: 1 addition & 5 deletions app/controllers/media_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,6 @@ def hash_for_auth_check
end
end

def stacks_media_stream_params
allowed_params.slice(:id, :file_name)
end

def id
allowed_params[:id]
end
Expand All @@ -70,7 +66,7 @@ def file_name
end

def load_media
@media ||= StacksMediaStream.new(stacks_media_stream_params)
@media ||= StacksMediaStream.new(stacks_file: StacksFile.new(id:, file_name:))
end

def current_media
Expand Down
12 changes: 5 additions & 7 deletions app/models/iiif_image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
# Represents a remote Iiif endpoint
class IiifImage
include ActiveSupport::Benchmarkable
# @params id [String]
# @params file_name [String]
# @params stacks_file [StacksFile]
# @params transformation [IIIF::Image::Transformation]
# @params base_uri [String]
def initialize(id:, file_name:, transformation:, base_uri: Settings.imageserver.base_uri)
@id = id
@file_name = file_name
def initialize(stacks_file:, transformation:, base_uri: Settings.imageserver.base_uri)
@stacks_file = stacks_file
@transformation = transformation
@base_uri = base_uri
end
Expand Down Expand Up @@ -37,10 +35,10 @@ def image_url
end

def remote_id
CGI.escape(StacksFile.new(id:, file_name:).treeified_path)
CGI.escape(stacks_file.treeified_path)
end

attr_reader :transformation, :id, :file_name
attr_reader :transformation, :stacks_file

delegate :logger, to: Rails
end
6 changes: 4 additions & 2 deletions app/models/projection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ def self.thumbnail(image)
new(image, IIIF::Image::Transformation.new(size: THUMBNAIL_BOUNDS, region: IIIF::Image::Region::Full.new))
end

# @param [StacksImage] image
# @transformation [IIIF::Image::Transformation] transformation
def initialize(image, transformation)
@image = image
@transformation = transformation
Expand Down Expand Up @@ -62,7 +64,7 @@ def valid?
image.exist? && image_source.valid?
end

delegate :object_thumbnail?, :id, :file_name, to: :image
delegate :object_thumbnail?, :stacks_file, to: :image

delegate :response, to: :image_source

Expand Down Expand Up @@ -108,7 +110,7 @@ def square_region_dimensions

# @return [IiifImage]
def image_source
@image_source ||= IiifImage.new(id:, file_name:, transformation: real_transformation)
@image_source ||= IiifImage.new(stacks_file:, transformation: real_transformation)
end

def real_transformation
Expand Down
29 changes: 11 additions & 18 deletions app/models/stacks_image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@
##
# An image that can be delivered over the IIIF endpoint
class StacksImage
include ActiveModel::Model
def initialize(stacks_file:, canonical_url: nil, transformation: nil)
@stacks_file = stacks_file
@canonical_url = canonical_url
@transformation = transformation
end

attr_accessor :id, :file_name
attr_accessor :canonical_url, :transformation
attr_accessor :canonical_url, :transformation, :stacks_file

# @return [RestrictedImage] the restricted version of this image
def restricted
RestrictedImage.new(transformation:,
id:,
file_name:,
RestrictedImage.new(stacks_file:,
transformation:,
canonical_url:)
end

Expand All @@ -32,11 +34,11 @@ def profile
end

def exist?
file_source.readable? && image_width.positive?
readable? && image_width.positive?
end

delegate :image_width, :image_height, to: :info_service
delegate :etag, :mtime, to: :file_source
delegate :etag, :mtime, :stacks_rights, :readable?, to: :stacks_file

# This is overriden in RestrictedImage
def max_tile_dimensions
Expand All @@ -49,18 +51,9 @@ def projection_for(transformation)

private

# @return [StacksFile]
def file_source
@file_source ||= StacksFile.new(id:, file_name:)
end

# @return [InfoService]
def info_service
@info_service ||= IiifMetadataService.new(id:, file_name:, canonical_url:)
end

def stacks_rights
@stacks_rights ||= StacksRights.new(cocina: Cocina.find(id), file_name:)
@info_service ||= IiifMetadataService.new(id: stacks_file.id, file_name: stacks_file.file_name, canonical_url:)
end

delegate :rights, :maybe_downloadable?, :object_thumbnail?,
Expand Down
17 changes: 5 additions & 12 deletions app/models/stacks_media_stream.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,13 @@
##
# media stream via stacks
class StacksMediaStream
include ActiveModel::Model

# @return [StacksFile] the file on disk that back this projection
def file
@file ||= StacksFile.new(id:, file_name:)
# @param [StacksFile] stacks_file the file on disk that back this stream
def initialize(stacks_file:)
@stacks_file = stacks_file
end
attr_accessor :stacks_file

attr_accessor :id, :file_name

delegate :etag, :mtime, to: :file

def stacks_rights
@stacks_rights ||= StacksRights.new(cocina: Cocina.find(id), file_name:)
end
delegate :etag, :mtime, :stacks_rights, to: :stacks_file

delegate :rights, :restricted_by_location?, :stanford_restricted?, :embargoed?,
:embargo_release_date, :location, to: :stacks_rights
Expand Down
4 changes: 2 additions & 2 deletions spec/abilities/cocina_ability_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@
StacksFile.new(id: 'xxxxxxx', file_name: 'file.csv')
end
let(:image) do
StacksImage.new(id: 'yx350pf4616', file_name: 'image.jpg')
StacksImage.new(stacks_file: StacksFile.new(id: 'yx350pf4616', file_name: 'image.jpg'))
end
let(:media) do
StacksMediaStream.new(id: 'xxxxxxx', file_name: 'movie.mp4')
StacksMediaStream.new(stacks_file: StacksFile.new(id: 'xxxxxxx', file_name: 'movie.mp4'))
end

let(:thumbnail_transformation) { IIIF::Image::OptionDecoder.decode(region: 'full', size: '!400,400') }
Expand Down
5 changes: 2 additions & 3 deletions spec/models/iiif_image_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
let(:druid) { 'nr349ct7889' }
let(:file_name) { 'image.jp2' }
let(:transformation) { IIIF::Image::Transformation.new(size: 'full', region: 'full') }
let(:instance) do
described_class.new(base_uri:, id: druid, file_name:, transformation:)
end
let(:stacks_file) { StacksFile.new(id: druid, file_name:) }
let(:instance) { described_class.new(stacks_file:, base_uri:, transformation:) }

describe "#remote_id" do
subject { instance.send(:remote_id) }
Expand Down
13 changes: 7 additions & 6 deletions spec/models/projection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require 'rails_helper'

RSpec.describe Projection do
let(:image) { StacksImage.new }
let(:image) { StacksImage.new(stacks_file: instance_double(StacksFile)) }
let(:instance) { described_class.new(image, transformation) }
let(:transformation) { IIIF::Image::OptionDecoder.decode(options) }
let(:http_client) { instance_double(HTTP::Client) }
Expand Down Expand Up @@ -68,7 +68,7 @@
end

context "for a restricted image" do
let(:image) { RestrictedImage.new }
let(:image) { RestrictedImage.new(stacks_file: instance_double(StacksFile)) }

context "full region" do
let(:options) { { size: 'max', region: 'full' } }
Expand Down Expand Up @@ -101,7 +101,7 @@
let(:file_name) { 'image.jp2' }

context 'for an image' do
let(:image) { StacksImage.new(id: druid, file_name:) }
let(:image) { StacksImage.new(stacks_file: StacksFile.new(id: druid, file_name:)) }

subject(:projection) { described_class.new(image, transformation) }

Expand Down Expand Up @@ -129,7 +129,7 @@
end

context 'for a restricted image' do
let(:image) { RestrictedImage.new(id: druid, file_name:) }
let(:image) { RestrictedImage.new(stacks_file: StacksFile.new(id: druid, file_name:)) }

subject(:projection) { described_class.new(image, transformation) }

Expand Down Expand Up @@ -306,11 +306,11 @@

describe '#valid?' do
let(:options) { { size: 'max', region: 'full' } }
let(:image) { StacksImage.new(stacks_file: file) }
subject { instance.valid? }

before do
allow(IiifImage).to receive(:new).and_return(source_image)
allow(StacksFile).to receive(:new).and_return(file)
end

context 'when file exists and transformation is valid' do
Expand All @@ -335,7 +335,8 @@
end

describe '#use_original_size?' do
let(:image) { StacksImage.new id: 'bc123cd4567', file_name: 'b' }
let(:image) { StacksImage.new(stacks_file: instance_double(StacksFile)) }

subject(:use_original) { described_class.new(image, transformation).send(:use_original_size?) }

context 'when percentage region is requested' do
Expand Down
2 changes: 1 addition & 1 deletion spec/models/restricted_image_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require 'rails_helper'

RSpec.describe RestrictedImage do
let(:instance) { described_class.new }
let(:instance) { described_class.new(stacks_file: instance_double(StacksImage)) }

describe '#info' do
subject { instance.info }
Expand Down
33 changes: 13 additions & 20 deletions spec/models/stacks_image_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,17 @@
require 'rails_helper'

RSpec.describe StacksImage do
subject { instance }
let(:instance) { described_class.new }
subject(:instance) { described_class.new(stacks_file:) }
let(:stacks_file) { instance_double(StacksFile) }

before do
allow(instance).to receive_messages(image_width: 800, image_height: 600)
allow(instance).to receive_messages(image_width: 800, image_height: 600) # rubocop:disable RSpec/SubjectStub
end

describe "#info_service" do
subject { instance.send(:info_service) }

let(:druid) { 'nr349ct7889' }
let(:file_name) { 'image.jp2' }
let(:instance) { described_class.new(id: druid, file_name:) }
let(:stacks_file) { StacksFile.new(id: 'nr349ct7889', file_name: 'image.jp2') }
let(:instance) { described_class.new(stacks_file:) }

it { is_expected.to be_a IiifMetadataService }
end
Expand All @@ -26,10 +24,10 @@
let(:info_service) { instance_double(IiifMetadataService, fetch: {}) }

before do
allow(instance).to receive(:info_service).and_return(info_service)
allow(instance).to receive(:info_service).and_return(info_service) # rubocop:disable RSpec/SubjectStub
end

it "gets the info from the djatoka response" do
it "gets the info from the image server response" do
subject
expect(info_service).to have_received(:fetch).with(nil)
end
Expand All @@ -45,7 +43,9 @@
subject { image.restricted }

let(:image) do
described_class.new(attributes)
described_class.new(stacks_file:,
canonical_url: 'http://example.com/',
transformation:)
end

let(:transformation) do
Expand All @@ -57,17 +57,10 @@
)
end

let(:attributes) do
{ id: 'bc012cd3456', file_name: 'def',
canonical_url: 'http://example.com/',
transformation: }
end

it 'passes all the parameters' do
expect(subject.transformation).to eq attributes[:transformation]
expect(subject.id).to eq attributes[:id]
expect(subject.file_name).to eq attributes[:file_name]
expect(subject.canonical_url).to eq attributes[:canonical_url]
expect(subject.transformation).to eq transformation
expect(subject.stacks_file).to eq stacks_file
expect(subject.canonical_url).to eq 'http://example.com/'
end
end
end
Loading

0 comments on commit df4dcd0

Please sign in to comment.