Skip to content

Commit

Permalink
Merge pull request #13 from performant-software/feature/iiif54_redirects
Browse files Browse the repository at this point in the history
IIIF #54 - Redirects
  • Loading branch information
dleadbetter authored May 8, 2024
2 parents 2794131 + b7d556a commit 9034fb1
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 40 deletions.
1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.2.2
5 changes: 4 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ GEM
mime-types-data (~> 3.2015)
mime-types-data (3.2022.0105)
mini_mime (1.1.2)
mini_portile2 (2.8.6)
minitest (5.16.2)
multi_xml (0.6.0)
net-imap (0.2.3)
Expand All @@ -113,7 +114,8 @@ GEM
net-protocol
timeout
nio4r (2.5.8)
nokogiri (1.13.8-x86_64-darwin)
nokogiri (1.13.8)
mini_portile2 (~> 2.8.0)
racc (~> 1.4)
racc (1.6.0)
rack (2.2.4)
Expand Down Expand Up @@ -157,6 +159,7 @@ GEM
zeitwerk (2.6.0)

PLATFORMS
arm64-darwin-22
x86_64-darwin-19

DEPENDENCIES
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module TripleEyeEffable
module ResourceableController
extend ActiveSupport::Concern

included do
preloads :resource_description
end
end
end
11 changes: 1 addition & 10 deletions app/models/concerns/triple_eye_effable/resourceable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Resourceable

included do
# Relationships
has_one :resource_description, as: :resourceable, dependent: :destroy, class_name: 'TripleEyeEffable::ResourceDescription'
has_one :resource_description, as: :resourceable, dependent: :destroy, class_name: ResourceDescription.to_s

# Transient attributes
attr_accessor :content
Expand All @@ -17,11 +17,9 @@ module Resourceable
delegate :content_preview_url, to: :resource_description, allow_nil: true
delegate :content_thumbnail_url, to: :resource_description, allow_nil: true
delegate :content_type, to: :resource_description, allow_nil: true
delegate :manifest, to: :resource_description, allow_nil: true
delegate :manifest_url, to: :resource_description, allow_nil: true

# Callbacks
after_find :load_resource
before_create :create_resource
before_destroy :delete_resource
before_update :update_resource
Expand All @@ -42,13 +40,6 @@ def delete_resource
throw(:abort) unless self.errors.empty?
end

def load_resource
service = TripleEyeEffable::Cloud.new
service.load_resource(self)

throw(:abort) unless self.errors.empty?
end

def update_resource
service = TripleEyeEffable::Cloud.new
service.update_resource(self)
Expand Down
17 changes: 15 additions & 2 deletions app/models/triple_eye_effable/resource_description.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
module TripleEyeEffable
class ResourceDescription < ApplicationRecord
# Relationships
belongs_to :resourceable, polymorphic: true

# Transient attributes
attr_accessor :content_url
attr_accessor :content_download_url
attr_accessor :content_iiif_url
attr_accessor :content_inline_url
attr_accessor :content_preview_url
attr_accessor :content_thumbnail_url
attr_accessor :content_type
attr_accessor :manifest
attr_accessor :manifest_url

# Callbacks
after_initialize :load_description

# Validations
validates :resource_id, presence: true

private

def load_description
service = TripleEyeEffable::Cloud.new
service.load_description(self)

throw(:abort) unless self.errors.empty?
end
end
end
4 changes: 2 additions & 2 deletions app/serializers/triple_eye_effable/resourceable_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ module ResourceableSerializer

included do
index_attributes :content_type, :content_url, :content_download_url, :content_iiif_url, :content_inline_url,
:content_preview_url, :content_thumbnail_url, :manifest, :manifest_url
:content_preview_url, :content_thumbnail_url, :manifest_url
show_attributes :content_type, :content_url, :content_download_url, :content_iiif_url, :content_inline_url,
:content_preview_url, :content_thumbnail_url, :manifest, :manifest_url
:content_preview_url, :content_thumbnail_url, :manifest_url
end

end
Expand Down
36 changes: 20 additions & 16 deletions app/services/triple_eye_effable/cloud.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ def create_resource(resourceable)

resource_id, data = parse_response(response)

resourceable.resource_description = ResourceDescription.new(resource_id: resource_id)
populate_description resourceable.resource_description, data
resourceable.resource_description = ResourceDescription.new(resource_id: resource_id, content_type: data[:content_type])
load_description resourceable.resource_description
end

def delete_resource(resourceable)
Expand All @@ -52,15 +52,27 @@ def delete_resource(resourceable)
add_error(resourceable, response) unless response.success?
end

def load_resource(resourceable)
return if resourceable.resource_description.nil?
def download_resource(resourceable)
return if resourceable.nil? || resourceable.resource_description.nil?

resource_description = resourceable.resource_description
response = self.class.get("#{base_url}/#{resource_description.resource_id}", headers: headers)
add_error(resourceable, response) and return unless response.success?

resource_id, data = parse_response(response)
populate_description resource_description, data unless data.nil?
parse_response(response)
end

def load_description(resource_description)
id = resource_description.resource_id

resource_description.assign_attributes(
content_url: "#{base_url}/#{id}/content",
content_download_url: "#{base_url}/#{id}/download",
content_iiif_url: "#{base_url}/#{id}/iiif",
content_inline_url: "#{base_url}/#{id}/inline",
content_preview_url: "#{base_url}/#{id}/preview",
content_thumbnail_url: "#{base_url}/#{id}/thumbnail",
manifest_url: "#{base_url}/#{id}/manifest"
)
end

def update_resource(resourceable)
Expand All @@ -72,8 +84,7 @@ def update_resource(resourceable)
response = self.class.put("#{base_url}/#{id}", body: request_body(resourceable), headers: headers)
add_error(resourceable, response) and return unless response.success?

resource_id, data = parse_response(response)
populate_description(resource_description, data)
load_description resource_description
end

def upload_resource(resourceable)
Expand Down Expand Up @@ -104,13 +115,6 @@ def parse_response(response)
[data[:uuid], data.except(:uuid)]
end

def populate_description(resource_description, data)
RESPONSE_KEYS.each do |key|
next unless resource_description.respond_to?("#{key.to_s}=")
resource_description.send("#{key.to_s}=", data[key])
end
end

def request_body(resourceable)
name = self.class.filename(resourceable.name) if resourceable.respond_to?(:name)
content = resourceable.content if resourceable.respond_to?(:content)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddContentTypeToResourceDescriptions < ActiveRecord::Migration[7.0]
def change
add_column :triple_eye_effable_resource_descriptions, :content_type, :string
end
end
45 changes: 36 additions & 9 deletions lib/tasks/triple_eye_effable_tasks.rake
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,37 @@ require 'httparty'
require 'optparse'

namespace :triple_eye_effable do
desc 'Sets the content_type attribute on each resource record'
task :set_content_type => :environment do
service = TripleEyeEffable::Cloud.new(read_only: true)

# Build the list of classes that include the Resourceable concern
classes = []

Rails.application.eager_load! if Rails.env.development?

ActiveRecord::Base.descendants.each do |model|
next unless model.include?(TripleEyeEffable::Resourceable)
classes << model
end

classes.each do |klass|
query = klass
.joins(:resource_description)
.preload(:resource_description)
.where(resource_description: { content_type: nil })

query.find_each do |resourceable|
resource_id, data = service.download_resource(resourceable)
content_type = data[:content_type]

next if content_type.nil?

resource_description = resourceable.resource_description
resource_description.update_attribute(:content_type, content_type)
end
end
end

desc 'Transfer resources from one IIIF Cloud instance to another.'
task :transfer_resources => :environment do
Expand Down Expand Up @@ -42,18 +73,14 @@ namespace :triple_eye_effable do
exit 0
end

# Build the list of classes that include the Resourcable concern
# Build the list of classes that include the Resourceable concern
classes = []

ActiveRecord::Base.connection.tables.each do |table_name|
begin
klass = table_name.classify.constantize
next unless klass.ancestors.include?(TripleEyeEffable::Resourceable)
Rails.application.eager_load! if Rails.env.development?

classes << klass
rescue
# Skip the record, there's a chance a table exists with no model
end
ActiveRecord::Base.descendants.each do |model|
next unless model.include?(TripleEyeEffable::Resourceable)
classes << model
end

source_service = TripleEyeEffable::Cloud.new(
Expand Down

0 comments on commit 9034fb1

Please sign in to comment.