Validate image dimensions only for valid attachment content types
This commit is contained in:
@@ -59,6 +59,7 @@ class Image < ActiveRecord::Base
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def redimension_using_origin_styles
|
def redimension_using_origin_styles
|
||||||
@@ -66,10 +67,12 @@ class Image < ActiveRecord::Base
|
|||||||
end
|
end
|
||||||
|
|
||||||
def validate_image_dimensions
|
def validate_image_dimensions
|
||||||
|
if attachment_of_valid_content_type?
|
||||||
dimensions = Paperclip::Geometry.from_file(attachment.queued_for_write[:original].path)
|
dimensions = Paperclip::Geometry.from_file(attachment.queued_for_write[:original].path)
|
||||||
errors.add(:attachment, :min_image_width, required_min_width: MIN_SIZE) if dimensions.width < MIN_SIZE
|
errors.add(:attachment, :min_image_width, required_min_width: MIN_SIZE) if dimensions.width < MIN_SIZE
|
||||||
errors.add(:attachment, :min_image_height, required_min_height: MIN_SIZE) if dimensions.height < MIN_SIZE
|
errors.add(:attachment, :min_image_height, required_min_height: MIN_SIZE) if dimensions.height < MIN_SIZE
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def validate_attachment_size
|
def validate_attachment_size
|
||||||
if imageable.present? &&
|
if imageable.present? &&
|
||||||
@@ -81,8 +84,7 @@ class Image < ActiveRecord::Base
|
|||||||
end
|
end
|
||||||
|
|
||||||
def validate_attachment_content_type
|
def validate_attachment_content_type
|
||||||
if imageable.present? &&
|
if imageable.present? && !attachment_of_valid_content_type?
|
||||||
!imageable_accepted_content_types.include?(attachment_content_type)
|
|
||||||
errors[:attachment] = I18n.t("images.errors.messages.wrong_content_type",
|
errors[:attachment] = I18n.t("images.errors.messages.wrong_content_type",
|
||||||
content_type: attachment_content_type,
|
content_type: attachment_content_type,
|
||||||
accepted_content_types: imageable_humanized_accepted_content_types)
|
accepted_content_types: imageable_humanized_accepted_content_types)
|
||||||
@@ -98,4 +100,9 @@ class Image < ActiveRecord::Base
|
|||||||
def remove_cached_image
|
def remove_cached_image
|
||||||
File.delete(cached_attachment) if File.exists?(cached_attachment)
|
File.delete(cached_attachment) if File.exists?(cached_attachment)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def attachment_of_valid_content_type?
|
||||||
|
attachment.present? && imageable_accepted_content_types.include?(attachment_content_type)
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user