Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep original image format when using MiniMagick unless explicited #2626

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

BuonOmo
Copy link

@BuonOmo BuonOmo commented Aug 31, 2022

Context

We recently switched from RMagick to MiniMagick in our codebase. This lead to an important change of behaviour in the uploader process

Summary

When using minimagick, it seems like the image is always converted to jpeg when running process resize_to_limit: ..., thus failing to have another process afterwards expecting an image that would still be in png format.

Reproduction

# frozen_string_literal: true

require "bundler/inline"
require "bundler/version"

puts "ruby=#{RUBY_VERSION} bundler=#{Bundler::VERSION}"

gemfile do
  source "https://rubygems.org"

  gem "activerecord", "7.0.3.1"
  gem "rails", "7.0.3.1", require: "action_dispatch"
  gem "carrierwave", "2.2.2"
  gem "sqlite3", "1.4.4", require: false
end

require "base64"
require "carrierwave/orm/activerecord"
require "tempfile"


# The base uploader, enforces basic rules, mostly for security reasons.
# It should not do a format conversion, yet, it does.
class BaseUploader < CarrierWave::Uploader::Base
  include CarrierWave::MiniMagick

  storage :file

  process resize_to_limit: [4096, 4096] # This will convert the image to jpeg

  def filename # This won't help (even by remove the ext or changing it)
    return unless original_filename.present?

    "some.png"
  end
end

# A specific uploader that handles some conversion.
class SignatureUploader < BaseUploader
   process convert_and_fill: ["jpg", "#FFFFFF"]


   def convert_and_fill(format, color)
    raise "Cannot work with a non-transparent original image" unless file.content_type == "image/png"

     minimagick! do |image|
       # Actually operate conversion from png to jpg, but it is too late
       image.convert("jpg")
       # NOTE: here would be the filling operation, cut for demo length purpose
     end
  end
end

class AR < ActiveRecord::Base
  self.abstract_class = true
  establish_connection(
    adapter:  "sqlite3",
    database: "tmp/db"
  )
end

class Document < AR
  mount_uploader :signature, SignatureUploader
end
AR.connection.execute <<~SQL
  CREATE TABLE IF NOT EXISTS documents(
    id INTEGER PRIMARY K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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA0In/AZFlR8TVsNgCAAAAAElFTkSuQmCC"


data = Base64.decode64(b64png_img)
tempfile = Tempfile.new("filename")
tempfile.binmode
tempfile.write(data)
tempfile.rewind
content_type = `file --mime -b #{tempfile.path}`.split(";")[0] # image/png

file = ActionDispatch::Http::UploadedFile.new(
  tempfile: tempfile,
  type: content_type,
  filename: "filename"
)

doc.signature = file
ruby=3.1.2 bundler=2.3.21
signature-repro.rb:46:in `convert_and_fill': Cannot work with a non-transparent original image (RuntimeError)
        from /Users/u.buonomo/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/carrierwave-2.2.2/lib/carrierwave/uploader/processing.rb:83:in `block (2 levels) in process!'
        from /Users/u.buonomo/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/carrierwave-2.2.2/lib/carrierwave/uploader/processing.rb:75:in `each'
        from /Users/u.buonomo/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/carrierwave-2.2.2/lib/carrierwave/uploader/processing.rb:75:in `block in process!'
        from /Users/u.buonomo/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/carrierwave-2.2.2/lib/carrierwave/uploader/callbacks.rb:15:in `with_callbacks'
        from /Users/u.buonomo/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/carrierwave-2.2.2/lib/carrierwave/uploader/processing.rb:74:in `process!'
        from /Users/u.buonomo/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/carrierwave-2.2.2/lib/carrierwave/uploader/callbacks.rb:14:in `block in with_callbacks'
        from /Users/u.buonomo/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/carrierwave-2.2.2/lib/carrierwave/uploader/callbacks.rb:14:in `each'
        from /Users/u.buonomo/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/carrierwave-2.2.2/lib/carrierwave/uploader/callbacks.rb:14:in `with_callbacks'
        from /Users/u.buonomo/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/carrierwave-2.2.2/lib/carrierwave/uploader/cache.rb:144:in `cache!'
        from /Users/u.buonomo/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/carrierwave-2.2.2/lib/carrierwave/mounter.rb:63:in `block (2 levels) in cache'
        from /Users/u.buonomo/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/carrierwave-2.2.2/lib/carrierwave/mounter.rb:176:in `handle_error'
        from /Users/u.buonomo/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/carrierwave-2.2.2/lib/carrierwave/mounter.rb:47:in `block in cache'
        from /Users/u.buonomo/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/carrierwave-2.2.2/lib/carrierwave/mounter.rb:46:in `map'
        from /Users/u.buonomo/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/carrierwave-2.2.2/lib/carrierwave/mounter.rb:46:in `cache'
        from /Users/u.buonomo/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/carrierwave-2.2.2/lib/carrierwave/mount.rb:146:in `signature='
        from /Users/u.buonomo/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/carrierwave-2.2.2/lib/carrierwave/mount.rb:373:in `signature='
        from /Users/u.buonomo/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/carrierwave-2.2.2/lib/carrierwave/orm/activerecord.rb:75:in `signature='
        from signature-repro.rb:89:in `<main>'

Current Lead

As far as I understood the problem, it seems like the #minimagick! tends to change the name and format of the file. However, it is likely that this change is just a reflection of the original format change, done within ImageProcessing (checked using file --mime within #minimagick!.

ImageProcessing is actually always converting to jpeg by default unless explicitely stated otherwise:

# frozen_string_literal: true

require "action_dispatch"
require "base64"
require "image_processing"
require "tempfile"

b64png_img = "iVBORw0KGgoAAAANSUhEUgAAAXsAAAI9CAYAAADb19jnAAAAAXNSR0IArs4c6QAAAARzQklUCAgICHwIZIgAAAxASURBVHic7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tempfile.rewind
content_type = `file --mime -b #{tempfile.path}`.split(";")[0] # image/png

file = ActionDispatch::Http::UploadedFile.new(
  tempfile: tempfile,
  type: content_type,
  filename: "filename"
)

file = ImageProcessing::MiniMagick.source(tempfile.path).then { |builder|
  if ENV["ERROR"]
    builder
  else
    builder.convert(content_type)
  end
}.resize_to_limit(4096, 4096).call

new_content_type = `file --mime -b #{file.path}`.split(";")[0]

raise "content type changed from #{content_type} to #{new_content_type}" if content_type != new_content_type

Hence my bet is that we could actually change the way the extension is handled in #minimagick!:

  1. ensure the ImageProcessing pipeline (a.k.a builder) contains a .convert(file.content_type)
  2. check change not by using the extension, but the actual builder option, which is more reliable (ex: if some file did not have an extension)

TODO

I had planned to open this as an issue first. Since I am not 100% sure that the lead here suits you, I'd rather discuss it first. Discuss the fact that this is likely to be a breaking change. And only then implement what is left:

  • document the #minimagick! method according to the changes
  • add tests
  • decide what to do about the backwards compatibility block
  • add changelog entry

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant