Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Bajena/2e82df877609e758a6f8 to your computer and use it in GitHub Desktop.
Save Bajena/2e82df877609e758a6f8 to your computer and use it in GitHub Desktop.
Fix for carrierwave with imagevoodoo throwing JavaxImageIO IIOexception
# This module overrides mount_uploader method and assign methods
# in order to handle Java ImageIO exception which occurs for
# some png files and is unhandled by CarrierWave gem.
# If the file is invalid, it removes newly created
# files, assigns old file back and returns validation error.
module CarrierWavePngFix
extend ActiveSupport::Concern
module ClassMethods
def mount_uploader(column, uploader=nil, options={}, &block)
super
class_eval <<-RUBY
def #{column}=(new_file)
old_val = #{column}.clone
begin
super
@#{column}_upload_failed = false # reset invalid file flag
rescue Java::JavaxImageio::IIOException
@#{column}_upload_failed = true # set invalid file flag for validation
super(old_val) # assign old value back
#{column}_was.remove! # remove leftovers after invalid assignment
end
end
validate :validate_#{column}_upload_failed
def validate_#{column}_upload_failed
errors.add(:#{column}, :corrupted_image) if @#{column}_upload_failed
end
RUBY
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment