71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
# File 'lib/active_storage_validations/dimension_validator.rb', line 71
def is_valid?(record, attribute, file_metadata)
flat_options = process_options(record)
if file_metadata[:width].to_i <= 0 || file_metadata[:height].to_i <= 0
add_error(record, attribute, :image_metadata_missing)
return false
end
if flat_options[:min] || flat_options[:max]
if flat_options[:min] && (
(flat_options[:width][:min] && file_metadata[:width] < flat_options[:width][:min]) ||
(flat_options[:height][:min] && file_metadata[:height] < flat_options[:height][:min])
)
add_error(record, attribute, :dimension_min_inclusion, width: flat_options[:width][:min], height: flat_options[:height][:min])
return false
end
if flat_options[:max] && (
(flat_options[:width][:max] && file_metadata[:width] > flat_options[:width][:max]) ||
(flat_options[:height][:max] && file_metadata[:height] > flat_options[:height][:max])
)
add_error(record, attribute, :dimension_max_inclusion, width: flat_options[:width][:max], height: flat_options[:height][:max])
return false
end
else
width_or_height_invalid = false
[:width, :height].each do |length|
next unless flat_options[length]
if flat_options[length].is_a?(Hash)
if flat_options[length][:in] && (file_metadata[length] < flat_options[length][:min] || file_metadata[length] > flat_options[length][:max])
add_error(record, attribute, :"dimension_#{length}_inclusion", min: flat_options[length][:min], max: flat_options[length][:max])
width_or_height_invalid = true
else
if flat_options[length][:min] && file_metadata[length] < flat_options[length][:min]
add_error(record, attribute, :"dimension_#{length}_greater_than_or_equal_to", length: flat_options[length][:min])
width_or_height_invalid = true
end
if flat_options[length][:max] && file_metadata[length] > flat_options[length][:max]
add_error(record, attribute, :"dimension_#{length}_less_than_or_equal_to", length: flat_options[length][:max])
width_or_height_invalid = true
end
end
else
if file_metadata[length] != flat_options[length]
add_error(record, attribute, :"dimension_#{length}_equal_to", length: flat_options[length])
width_or_height_invalid = true
end
end
end
return false if width_or_height_invalid
end
true end
|