Skip to content

Commit

Permalink
Parse out Ghostscript warnings in identfication (#522)
Browse files Browse the repository at this point in the history
Image info sometimes parses incorrectly because Ghostscript may output
warnings about the validity of the object, despite successfully reading
its attributes. This causes parsing problems because the warning
messages are concatenated with the attributes, and trying to access
these attributes via MiniMagick results in type errors.

The fix is to identify if the warning messages appear, and if so, match
the attributes via strict regular expression.
  • Loading branch information
ageacademia committed Nov 6, 2020
1 parent 986466d commit eee6cc0
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/mini_magick/image/info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def clear

def cheap_info(value)
@info.fetch(value) do
format, width, height, size = self["%m %w %h %b"].split(" ")
format, width, height, size = parse_warnings(self["%m %w %h %b"]).split(" ")

path = @path
path = path.match(/\[\d+\]$/).pre_match if path =~ /\[\d+\]$/
Expand All @@ -61,6 +61,16 @@ def cheap_info(value)
rescue ArgumentError, TypeError
raise MiniMagick::Invalid, "image data can't be read"
end

def parse_warnings(raw_info)
return raw_info unless raw_info.split("\n").size > 1

raw_info.split("\n").each do |line|
# must match "%m %w %h %b"
return line if line.match? /^[A-Z]+ \d+ \d+ \d+B$/
end
raise TypeError
end

def colorspace
@info["colorspace"] ||= self["%r"]
Expand Down

0 comments on commit eee6cc0

Please sign in to comment.