mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2024-12-26 09:42:46 +08:00
Implement JPEG quality extraction for ActiveStorage [SCI-3959]
This commit is contained in:
parent
39a2669eef
commit
3d9c2282f3
4 changed files with 26 additions and 15 deletions
|
@ -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,
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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'
|
||||
|
|
22
lib/active_storage/analyzer/custom_image_analyzer.rb
Normal file
22
lib/active_storage/analyzer/custom_image_analyzer.rb
Normal file
|
@ -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
|
Loading…
Reference in a new issue