diff --git a/app/models/resource.rb b/app/models/resource.rb index ab85889..3cad29e 100644 --- a/app/models/resource.rb +++ b/app/models/resource.rb @@ -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? @@ -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) diff --git a/app/services/images/convert.rb b/app/services/images/convert.rb index 6f272fd..89326a2 100644 --- a/app/services/images/convert.rb +++ b/app/services/images/convert.rb @@ -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 diff --git a/lib/tasks/iiif.rake b/lib/tasks/iiif.rake index 7c4cf94..a08e0e2 100644 --- a/lib/tasks/iiif.rake +++ b/lib/tasks/iiif.rake @@ -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 @@ -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|