Skip to content

Commit

Permalink
Merge pull request #1949 from alphagov/dependabot/bundler/mini_magick…
Browse files Browse the repository at this point in the history
…-5.0.0

Bump mini_magick from 4.13.1 to 5.0.0
  • Loading branch information
ChrisBAshton authored Jul 22, 2024
2 parents dd3c466 + 7ffce06 commit a4b0bf4
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 9 deletions.
1 change: 0 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ gem "govspeak"
gem "govuk_app_config"
gem "govuk_publishing_components"
gem "govuk_sidekiq"
gem "mini_magick"
gem "mongo"
gem "mongoid"
gem "pdf-reader"
Expand Down
2 changes: 0 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,6 @@ GEM
mime-types (3.5.2)
mime-types-data (~> 3.2015)
mime-types-data (3.2024.0604)
mini_magick (4.13.1)
mini_mime (1.1.5)
mini_portile2 (2.8.7)
minitest (5.24.1)
Expand Down Expand Up @@ -767,7 +766,6 @@ DEPENDENCIES
govuk_sidekiq
govuk_test
listen
mini_magick
mongo
mongoid
pdf-reader
Expand Down
12 changes: 6 additions & 6 deletions app/validators/image_validator.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require "marcel"

class ImageValidator < ActiveModel::EachValidator
MIME_TYPES = {
"image/jpeg" => /(\.jpeg|\.jpg)$/,
Expand All @@ -8,15 +10,13 @@ class ImageValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
return unless value.present? && File.exist?(value.path)

image = MiniMagick::Image.open(value.path)
mime_type = Marcel::MimeType.for(Pathname.new(value.path))

valid_extension = MIME_TYPES[image.mime_type]
valid_extension = MIME_TYPES[mime_type]
if valid_extension.nil?
record.errors[attribute] << "is not an allowed image format"
record.errors.add(attribute, message: "is not an allowed image format")
elsif !value.path.downcase.match?(valid_extension)
record.errors.add(attribute, message: "is of type '#{image.mime_type}', but has the extension '#{File.extname(value.path)}'")
record.errors.add(attribute, message: "is of type '#{mime_type}', but has the extension '#{File.extname(value.path)}'")
end
rescue MiniMagick::Error, MiniMagick::Invalid
record.errors.add(attribute, message: "is not an image")
end
end

0 comments on commit a4b0bf4

Please sign in to comment.