diff --git a/app/controllers/assets_controller.rb b/app/controllers/assets_controller.rb index f248da78b..563011824 100644 --- a/app/controllers/assets_controller.rb +++ b/app/controllers/assets_controller.rb @@ -36,7 +36,7 @@ class AssetsController < ApplicationController end if response_json['type'] == 'previewable' if ['image/jpeg', 'image/pjpeg'].include? @asset.file.content_type - response_json['quality'] = @asset.file_image_quality || 90 + response_json['quality'] = @asset.file_image_quality || 80 end response_json.merge!( 'editable' => @asset.editable_image? && can_edit, diff --git a/app/models/asset.rb b/app/models/asset.rb index 785e8da99..2307d2b7a 100644 --- a/app/models/asset.rb +++ b/app/models/asset.rb @@ -15,8 +15,6 @@ class Asset < ApplicationRecord # ActiveStorage configuration has_one_attached :file - # before_post_process :extract_image_quality - # Asset validation # This could cause some problems if you create empty asset and want to # assign it to result @@ -204,18 +202,6 @@ class Asset < ApplicationRecord to_asset.post_process_file(to_asset.team) end - def extract_image_quality - return unless ['image/jpeg', 'image/pjpeg'].include? content_type - - tempfile = file.queued_for_write[:original] - unless tempfile.nil? - quality = Paperclip::Processor.new(tempfile).identify(" -format '%Q' #{tempfile.path}") - self.file_image_quality = quality.to_i - end - rescue StandardError => e - Rails.logger.info "There was an error extracting image quality - #{e}" - end - def image? content_type =~ %r{^image/#{Regexp.union(Constants::WHITELISTED_IMAGE_TYPES)}} end diff --git a/config/initializers/active_storage.rb b/config/initializers/active_storage.rb index 2efa60f57..609cfbc12 100644 --- a/config/initializers/active_storage.rb +++ b/config/initializers/active_storage.rb @@ -1,11 +1,14 @@ # frozen_string_literal: true require 'active_storage/previewer/libreoffice_previewer' +require 'active_storage/analyzer/custom_image_analyzer' require 'active_storage/downloader' Rails.application.config.active_storage.previewers = [ActiveStorage::Previewer::PopplerPDFPreviewer, ActiveStorage::Previewer::LibreofficePreviewer] +Rails.application.config.active_storage.analyzers.prepend(ActiveStorage::Analyzer::CustomImageAnalyzer) + Rails.application.config.active_storage.variable_content_types << 'image/svg+xml' Rails.application.config.active_storage.variant_processor = :vips if ENV['ACTIVESTORAGE_ENABLE_VIPS'] == 'true' diff --git a/lib/active_storage/analyzer/custom_image_analyzer.rb b/lib/active_storage/analyzer/custom_image_analyzer.rb new file mode 100644 index 000000000..8dd069e3f --- /dev/null +++ b/lib/active_storage/analyzer/custom_image_analyzer.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +module ActiveStorage + class Analyzer::CustomImageAnalyzer < Analyzer::ImageAnalyzer + def self.accept?(blob) + ['image/jpeg', 'image/pjpeg'].include?(blob.content_type) && 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