Skip to content

Commit

Permalink
Support image filenames containing parentheses
Browse files Browse the repository at this point in the history
Escape filenames before passing them to shell execution. The fix is tested
most easily with the existing tests with an adjusted filename.
  • Loading branch information
peruukki committed May 19, 2015
1 parent 878660e commit 7464d56
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 6 deletions.
4 changes: 3 additions & 1 deletion lib/wraith/compare_images.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require "image_size"
require "open3"
require "parallel"
require "shellwords"

class Wraith::CompareImages
attr_reader :wraith
Expand All @@ -27,7 +28,8 @@ def percentage(img_size, px_value, info)
end

def compare_task(base, compare, output, info)
cmdline = "compare -dissimilarity-threshold 1 -fuzz #{wraith.fuzz} -metric AE -highlight-color #{wraith.highlight_color} #{base} #{compare} #{output}"
compare_escaped = Shellwords.escape(compare)
cmdline = "compare -dissimilarity-threshold 1 -fuzz #{wraith.fuzz} -metric AE -highlight-color #{wraith.highlight_color} #{base} #{compare_escaped} #{output}"
px_value = Open3.popen3(cmdline) { |_stdin, _stdout, stderr, _wait_thr| stderr.read }.to_f
begin
img_size = ImageSize.path(output).size.inject(:*)
Expand Down
4 changes: 3 additions & 1 deletion lib/wraith/crop.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require "wraith"
require "image_size"
require "parallel"
require "shellwords"

class Wraith::CropImages
attr_reader :wraith
Expand Down Expand Up @@ -32,7 +33,8 @@ def crop_images
end

def crop_task(crop, height, width)
`convert #{crop} -background none -extent #{width}x#{height} #{crop}`
crop_escaped = Shellwords.escape(crop)
`convert #{crop_escaped} -background none -extent #{width}x#{height} #{crop_escaped}`
end

def image_dimensions(image)
Expand Down
4 changes: 3 additions & 1 deletion lib/wraith/save_images.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require "wraith"
require "parallel"
require "shellwords"

class Wraith::SaveImages
attr_reader :wraith, :history, :meta
Expand Down Expand Up @@ -73,7 +74,8 @@ def create_invalid_image(filename, width)
end

def set_image_width(image, width)
`convert #{image} -background none -extent #{width}x0 #{image}`
image_escaped = Shellwords.escape(image)
`convert #{image_escaped} -background none -extent #{width}x0 #{image_escaped}`
end
end

Expand Down
5 changes: 4 additions & 1 deletion lib/wraith/thumbnails.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require "wraith"
require "parallel"
require "fileutils"
require "shellwords"

class Wraith::Thumbnails
attr_reader :wraith
Expand All @@ -25,6 +26,8 @@ def thumbnail_image(png_path, output_path)
FileUtils.mkdir_p(File.dirname(output_path))
end

`convert #{png_path} -thumbnail 200 -crop 200x200+0+0 #{output_path}`
png_path_escaped = Shellwords.escape(png_path)
output_path_escaped = Shellwords.escape(output_path)
`convert #{png_path_escaped} -thumbnail 200 -crop 200x200+0+0 #{output_path_escaped}`
end
end
4 changes: 2 additions & 2 deletions spec/wraith_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
let(:test_url1) { "http://www.bbc.co.uk/russian" }
let(:test_url2) { "http://www.bbc.co.uk/russian" }
let(:test_image1) { "shots/test/test1.png" }
let(:test_image2) { "shots/test/test2.png" }
let(:test_image2) { "shots/test/test(2).png" }
let(:diff_image) { "shots/test/test_diff.png" }
let(:data_txt) { "shots/test/test.txt" }
let(:selector) { "" }
Expand Down Expand Up @@ -94,7 +94,7 @@
Wraith::Thumbnails.new(config_name).generate_thumbnails

expect(File).to exist("shots/thumbnails/test/test1.png")
expect(File).to exist("shots/thumbnails/test/test2.png")
expect(File).to exist("shots/thumbnails/test/test(2).png")
expect(File).to exist("shots/thumbnails/test/test_diff.png")
end
end
Expand Down

0 comments on commit 7464d56

Please sign in to comment.