Created
July 10, 2015 07:26
-
-
Save Bajena/2e82df877609e758a6f8 to your computer and use it in GitHub Desktop.
Fix for carrierwave with imagevoodoo throwing JavaxImageIO IIOexception
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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