Skip to content

Commit

Permalink
Merge pull request #65 from performant-software/develop
Browse files Browse the repository at this point in the history
Develop into master
  • Loading branch information
dleadbetter authored Sep 12, 2024
2 parents c432788 + 1bde679 commit 2b6d2f8
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 8 deletions.
21 changes: 21 additions & 0 deletions app/models/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,20 @@ class Resource < ApplicationRecord
alias_method :attachable_content_iiif_url, :content_iiif_url
alias_method :attachable_content_thumbnail_url, :content_thumbnail_url

def self.with_attachment(name)
subquery = attachment_subquery(name)
subquery = yield subquery if block_given?

where(subquery.arel.exists)
end

def self.without_attachment(name)
subquery = attachment_subquery(name)
subquery = yield subquery if block_given?

where.not(subquery.arel.exists)
end

def content_base_url
return attachable_content_base_url unless content_converted.attached?

Expand Down Expand Up @@ -72,6 +86,13 @@ def pdf?

private

def self.attachment_subquery(name)
ActiveStorage::Attachment
.where(ActiveStorage::Attachment.arel_table[:record_id].eq(Resource.arel_table[:id]))
.where(record_type: Resource.to_s)
.where(name: name)
end

def after_create
# Convert the image to a TIFF
ConvertImageJob.perform_later(self.id)
Expand Down
2 changes: 2 additions & 0 deletions app/services/images/convert.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ def self.to_tiff(file)
convert << '8'
convert << '-compress'
convert << 'jpeg'
convert << '-alpha'
convert << 'remove'
convert << "ptif:#{output_path}"
convert.call

Expand Down
55 changes: 47 additions & 8 deletions lib/tasks/iiif.rake
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ namespace :iiif do

desc 'Converts the source images to pyramidal TIFFs for all resources'
task convert_images: :environment do
Resource.all.in_batches do |resources|
query = Resource.with_attachment('content') do |subquery|
subquery
.joins(:blob)
.where('active_storage_blobs.byte_size > ?', 0)
.where('active_storage_blobs.content_type ILIKE \'%image%\'')
end

query.in_batches do |resources|
resources.pluck(:id).each do |resource_id|
ConvertImageJob.perform_later(resource_id)
end
Expand All @@ -12,13 +19,45 @@ namespace :iiif do
desc 'Converts the source images to pyramidal TIFFs for resources with no converted content'
task convert_images_empty: :environment do
query = Resource
.where.not(
ActiveStorage::Attachment
.where(ActiveStorage::Attachment.arel_table[:record_id].eq(Resource.arel_table[:id]))
.where(record_type: Resource.to_s, name: 'content_converted')
.arel
.exists
)
.without_attachment('content_converted')
.with_attachment('content') do |subquery|
subquery
.joins(:blob)
.where('active_storage_blobs.byte_size > ?', 0)
.where('active_storage_blobs.content_type ILIKE \'%image%\'')
end

query.in_batches do |resources|
resources.pluck(:id).each do |resource_id|
ConvertImageJob.perform_later(resource_id)
end
end
end

desc 'Converts the source images to pyramidal TIFFs for resources by the specified MIME type'
task convert_images_by_type: :environment do
# Parse the arguments
options = {}

opt_parser = OptionParser.new do |opts|
opts.banner = 'Usage: rake iiif:convert_images_by_type [options]'
opts.on('-t', '--type ARG', 'Image MIME type') { |type| options[:type] = type }
end

args = opt_parser.order!(ARGV) {}
opt_parser.parse!(args)

if options[:type].blank?
puts 'Please specify a MIME type...'
exit 0
end

query = Resource.with_attachment('content') do |subquery|
subquery
.joins(:blob)
.where('active_storage_blobs.byte_size > ?', 0)
.where('active_storage_blobs.content_type = ?', options[:type])
end

query.in_batches do |resources|
resources.pluck(:id).each do |resource_id|
Expand Down

0 comments on commit 2b6d2f8

Please sign in to comment.