scinote-web/lib/active_storage/analyzer/custom_image_analyzer.rb

25 lines
681 B
Ruby
Raw Normal View History

# frozen_string_literal: true
module ActiveStorage
class Analyzer::CustomImageAnalyzer < Analyzer::ImageAnalyzer
2019-10-08 17:43:45 +08:00
JPEG_MIME_TYPES = ['image/jpeg', 'image/pjpeg'].freeze
def self.accept?(blob)
2019-10-08 17:43:45 +08:00
blob.content_type.in?(JPEG_MIME_TYPES) && blob.attachments.take.record_type == 'Asset'
end
def metadata
read_image do |image|
quality = image.identify { |b| b.format('%Q') }.to_i
blob.attachments.take.record.update(file_image_quality: quality)
if rotated_image?(image)
{ width: image.height, height: image.width }
else
{ width: image.width, height: image.height }
end
end
end
end
end