Skip to content

Commit

Permalink
Test updates and rubocop updates
Browse files Browse the repository at this point in the history
  • Loading branch information
elohanlon committed Dec 22, 2024
1 parent 9e89c37 commit 425b3dd
Show file tree
Hide file tree
Showing 15 changed files with 302 additions and 206 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

# URI encoding and decoding
gem 'addressable', '~> 2.8.0'
# Use best_type for media type detection
gem 'best_type', '~> 0.0.10'
# For schema validation
Expand Down
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ GEM
minitest (>= 5.1)
mutex_m
tzinfo (~> 2.0)
addressable (2.8.7)
public_suffix (>= 2.0.2, < 7.0)
airbrussh (1.5.0)
sshkit (>= 1.6.1, != 1.7.0)
ast (2.4.2)
Expand Down Expand Up @@ -247,6 +249,7 @@ GEM
racc
psych (5.1.2)
stringio
public_suffix (6.0.1)
puma (5.6.8)
nio4r (~> 2.0)
racc (1.7.3)
Expand Down Expand Up @@ -446,6 +449,7 @@ PLATFORMS
ruby

DEPENDENCIES
addressable (~> 2.8.0)
base64 (= 0.1.1)
best_type (~> 0.0.10)
bootsnap (>= 1.4.2)
Expand Down
13 changes: 4 additions & 9 deletions app/models/concerns/triclops/resource/as_json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,10 @@ def as_json(_options = {})
:has_view_limitation,
:featured_region,
:source_uri,
:standard_width,
:standard_height,
:limited_width,
:limited_height,
:featured_width,
:featured_height,
:created_at,
:updated_at,
:accessed_at
:standard_width, :standard_height,
:limited_width, :limited_height,
:featured_width, :featured_height,
:created_at, :updated_at, :accessed_at
].map { |field_name| [field_name, self.send(field_name)] }.to_h
end
end
Expand Down
39 changes: 17 additions & 22 deletions app/models/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,31 @@ class Resource < ApplicationRecord
after_destroy :delete_filesystem_cache!

def wait_for_source_uri_if_local_disk_file
return if self.source_uri.nil?
protocol, path = self.source_uri.split('://')
return unless protocol == 'file'
return if self.source_uri.nil? || !self.source_uri.start_with?('file:/')
file_path = Triclops::Utils::UriUtils.location_uri_to_file_path(self.source_uri)

# Under certain circumstances, a source_uri file that was recently writter by an external
# process may take a few seconds to become available for reading (for example, if the file
# was written to a network disk and the change has not been propagated yet to other servers).
# So we'll wait and try again a few times, if it's not found right away.
5.times do
break if File.exist?(path)
break if File.exist?(file_path)
sleep 1
end
end

# Generates a placeholder resource dynamically (without any database interaction)
def self.placeholder_resource_for(identifier)
# Generates a placeholder resource dynamically (without any database interaction),
# based on the given placeholder_resource_identifier. Note that this method
# will raise an exception if placeholder_resource_identifier is not a
# recognized placeholder identifier.
def self.placeholder_resource_for(placeholder_resource_identifier)
raise ArgumentError unless KNOWN_PLACEHOLDER_IDENTIFIERS.include?(placeholder_resource_identifier)
Resource.new(
identifier: identifier,
identifier: placeholder_resource_identifier,
has_view_limitation: false,
status: 'ready',
updated_at: Time.current,
source_uri: identifier.sub(':', '://'),
source_uri: placeholder_resource_identifier.sub(':', ':///'),
standard_width: PLACEHOLDER_SIZE,
standard_height: PLACEHOLDER_SIZE,
limited_width: Triclops::Iiif::Constants::LIMITED_BASE_SIZE,
Expand Down Expand Up @@ -331,21 +334,13 @@ def switch_to_pending_state_if_core_properties_changed!
# @yield source_image_file [File] A file holding the source image content.
def with_source_image_file
raise Errno::ENOENT, 'Missing source_uri' if self.source_uri.blank?
protocol, path = self.source_uri.split('://')
uri_scheme = Addressable::URI.parse(self.source_uri).scheme

case protocol
when 'railsroot'
yield File.new(Rails.root.join(path).to_s)
if ['file', 'railsroot', 'placeholder'].include?(uri_scheme)
yield File.new(Triclops::Utils::UriUtils.location_uri_to_file_path(self.source_uri))
return
when 'placeholder'
yield File.new(File.join(PLACEHOLDER_ROOT, path + '.png').to_s)
return
when 'file'
if File.exist?(path)
yield File.new(path)
return
end
end

raise Errno::ENOENT, "Could not resolve file location: #{self.source_uri}"
end

Expand Down Expand Up @@ -450,10 +445,10 @@ def yield_uncached_raster(raster_opts)
# Returns true if this Resource's source_uri points to a placeholder image value.
#
# @api private
# @return [Boolean] true if source_uri starts with 'placeholder://'
# @return [Boolean] true if source_uri starts with 'placeholder:///'
def source_uri_is_placeholder?
return false if self.source_uri.nil?
self.source_uri.start_with?('placeholder://')
self.source_uri.start_with?('placeholder:///')
end

def self.generate_raster_tempfile_path(extension = '.blob')
Expand Down
6 changes: 3 additions & 3 deletions lib/tasks/triclops/setup.rake
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,21 @@ namespace :triclops do
[
{
identifier: 'sample',
source_uri: 'railsroot://' + File.join('spec', 'fixtures', 'files', 'sample.jpg'),
source_uri: 'railsroot:///' + File.join('spec', 'fixtures', 'files', 'sample.jpg'),
width: 1920,
height: 3125,
featured_region: '320,616,1280,1280'
},
{
identifier: 'sample-with-transparency',
source_uri: 'railsroot://' + File.join('spec', 'fixtures', 'files', 'sample-with-transparency.png'),
source_uri: 'railsroot:///' + File.join('spec', 'fixtures', 'files', 'sample-with-transparency.png'),
width: 1920,
height: 1920,
featured_region: '320,320,1280,1280'
},
{
identifier: 'sound-resource',
source_uri: 'placeholder://sound',
source_uri: 'placeholder:///sound',
width: 2292,
height: 2292,
featured_region: '0,0,1280,1280'
Expand Down
29 changes: 29 additions & 0 deletions lib/triclops/utils/uri_utils.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

# TODO: A very similar UriUtils file also exists in the Hyacinth 2 code base.
# Probably want to move this to a shared gem at some point.
class Triclops::Utils::UriUtils
# Converts a file path to a location URI value
def self.file_path_to_location_uri(path)
raise ArgumentError, "Given path must be absolute. Must start with a slash: #{path}" unless path.start_with?('/')

"file://#{Addressable::URI.encode(path).gsub('&', '%26').gsub('#', '%23')}"
end

def self.location_uri_to_file_path(location_uri)
parsed_uri = Addressable::URI.parse(location_uri)
uri_scheme = parsed_uri.scheme
unencoded_uri_path = Addressable::URI.unencode(parsed_uri.path)

case uri_scheme
when 'file'
return unencoded_uri_path
when 'railsroot'
return Rails.root.join(unencoded_uri_path[1..]).to_s
when 'placeholder'
return File.join(PLACEHOLDER_ROOT, "#{unencoded_uri_path[1..]}.png")
end

raise ArgumentError, "Unhandled URI: #{location_uri}"
end
end
12 changes: 6 additions & 6 deletions spec/factories/resources.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@

identifier { "test-resource-#{identifier_counter}" }
has_view_limitation { false }
source_uri { "railsroot://#{File.join('spec', 'fixtures', 'files', 'sample.jpg')}" }
source_uri { "railsroot:///#{File.join('spec', 'fixtures', 'files', 'sample.jpg')}" }
standard_width { 1920 }
standard_height { 3125 }
limited_width { standard_width > standard_height ? 768 : ((768.to_f / standard_height) * standard_width).round }
limited_height { standard_height > standard_width ? 768 : ((768.to_f / standard_width) * standard_height).round }
featured_width { 768 }
featured_height { 768 }
featured_region { '320,616,1280,1280' }
pcdm_type { BestType::PcdmTypeLookup::IMAGE }
status { Resource.statuses[:pending] }
accessed_at { DateTime.parse('2024-01-01T01:23:45-05:00') }

trait :ready do
status { Resource.statuses[:ready] }
limited_width { standard_width > standard_height ? 768 : ((768.to_f / standard_height) * standard_width).round }
limited_height { standard_height > standard_width ? 768 : ((768.to_f / standard_width) * standard_height).round }
featured_width { 768 }
featured_height { 768 }
accessed_at { DateTime.parse('2024-01-01T01:23:45-05:00') }
end

trait :with_view_limitation do
Expand Down
3 changes: 0 additions & 3 deletions spec/models/concerns/triclops/resource/as_json_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@

context "#as_json" do
it 'returns the expected hash' do
created_at = resource.created_at
updated_at = resource.updated_at
accessed_at = resource.accessed_at
expect(
{
identifier: resource.identifier,
Expand Down
2 changes: 1 addition & 1 deletion spec/models/concerns/triclops/resource/iiif_info_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
let(:identifier) { 'test' }
let(:rails_root_relative_path) { File.join('spec', 'fixtures', 'files', 'sample.jpg') }
let(:source_file_path) { Rails.root.join(rails_root_relative_path).to_s }
let(:source_uri) { 'railsroot://' + rails_root_relative_path }
let(:source_uri) { 'railsroot:///' + rails_root_relative_path }
let(:standard_width) { 1920 }
let(:standard_height) { 3125 }
let(:featured_region) { '320,616,1280,1280' }
Expand Down
Loading

0 comments on commit 425b3dd

Please sign in to comment.