Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

:magick columns can ignore non-image files

git-svn-id: http://opensvn.csie.org/rails_file_column/plugins/file_column/trunk@64 6a5db2c9-b104-0410-84e5-917aad89f9d9
  • Loading branch information...
commit bd5b1c6c3e4cf180c4ad6b86ac9532c0183fe795 1 parent 3993bf3
skanthak authored
View
2  CHANGELOG
@@ -23,6 +23,8 @@
* added support for file_column enabled unit tests [Manuel Holtgrewe]
* support for custom transformation of images [Frederik Fix]
* allow setting of image attributes (e.g., quality) [Frederik Fix]
+ * :magick columns can optionally ignore non-images (i.e., do not try to
+ resize them)
0.3.1
* make object with file_columns serializable
View
9 lib/file_column_helper.rb
@@ -130,7 +130,8 @@ def url_for_file_column(object, method, options=nil)
# although it will start with a slash.
# If you pass this URL to rails' +image_tag+ helper, it will be converted to an
# absolute URL automatically.
- # If there is currently no image uploaded, this method will return +nil+.
+ # If there is currently no image uploaded, or there is a problem while loading
+ # the image this method will return +nil+.
def url_for_image_column(object, method, options=nil)
case object
when String, Symbol
@@ -140,6 +141,10 @@ def url_for_image_column(object, method, options=nil)
if options
subdir = object.send("#{method}_state").create_magick_version_if_needed(options)
end
- url_for_file_column(object, method, subdir)
+ if subdir.nil?
+ nil
+ else
+ url_for_file_column(object, method, subdir)
+ end
end
end
View
24 lib/magick_file_column.rb
@@ -6,8 +6,10 @@ def transform_with_magick
begin
img = ::Magick::Image::read(absolute_path).first
rescue ::Magick::ImageMagickError
- @magick_errors ||= []
- @magick_errors << "invalid image"
+ if options[:magick][:image_required]
+ @magick_errors ||= []
+ @magick_errors << "invalid image"
+ end
return
end
@@ -45,7 +47,13 @@ def create_magick_version_if_needed(version)
end
unless File.exists?(absolute_path(version_options[:name]))
- img = ::Magick::Image::read(absolute_path).first
+ begin
+ img = ::Magick::Image::read(absolute_path).first
+ rescue ::Magick::ImageMagickError
+ # we might be called directly from the view here
+ # so we just return nil if we cannot load the image
+ return nil
+ end
dirname = version_options[:name]
FileUtils.mkdir File.join(@dir, dirname)
transform_image(img, version_options, absolute_path(dirname))
@@ -119,6 +127,15 @@ def transform_image(img, img_options, dest_path)
#
# file_column :image, :magick => {:size => "800x600>"}
#
+ # If the uploaded file cannot be loaded by RMagick, file_column will
+ # signal a validation error for the corresponding attribute. If you
+ # want to allow non-image files to be uploaded in a column that uses
+ # the <tt>:magick</tt> option, you can set the <tt>:image_required</tt>
+ # attribute to +false+:
+ #
+ # file_column :image, :magick => {:size => "800x600>",
+ # :image_required => false }
+ #
# == Multiple versions
#
# You can also create additional versions of your image, for example
@@ -224,6 +241,7 @@ def self.process_options(options,create_name=true)
if options[:geometry]
options[:size] = options.delete(:geometry)
end
+ options[:image_required] = true unless options.key?(:image_required)
if options[:name].nil? and create_name
if create_name == true
hash = 0
View
35 test/magick_test.rb
@@ -91,6 +91,41 @@ def test_imagemagick_still_usable
end
end
+class RMagickRequiresImageTest < AbstractRMagickTest
+ def setup
+ Entry.file_column :image, :magick => {
+ :size => "100x100>",
+ :image_required => false,
+ :versions => {
+ :thumb => "80x80>",
+ :large => {:size => "200x200>", :lazy => true}
+ }
+ }
+ end
+
+ def test_image_required_with_image
+ e = Entry.new(:image => upload(f("skanthak.png")))
+ assert_max_image_size read_image(e.image), 100
+ assert e.valid?
+ end
+
+ def test_image_required_with_invalid_image
+ e = Entry.new(:image => upload(f("invalid-image.jpg")))
+ assert e.valid?, "did not ignore invalid image"
+ assert FileUtils.identical?(e.image, f("invalid-image.jpg")), "uploaded file has not been left alone"
+ end
+
+ def test_versions_with_invalid_image
+ e = Entry.new(:image => upload(f("invalid-image.jpg")))
+ assert e.valid?
+
+ image_state = e.send(:image_state)
+ assert_nil image_state.create_magick_version_if_needed(:thumb)
+ assert_nil image_state.create_magick_version_if_needed(:large)
+ assert_nil image_state.create_magick_version_if_needed("300x300>")
+ end
+end
+
class RMagickCustomAttributesTest < AbstractRMagickTest
def assert_image_property(img, property, value, text = nil)
assert File.exists?(img), "the image does not exist"
Please sign in to comment.
Something went wrong with that request. Please try again.