From 7464d5646452a3126949449076f3d7e435b480fa Mon Sep 17 00:00:00 2001 From: Harri Lehtola Date: Thu, 9 Apr 2015 09:03:54 +0200 Subject: [PATCH] Support image filenames containing parentheses Escape filenames before passing them to shell execution. The fix is tested most easily with the existing tests with an adjusted filename. --- lib/wraith/compare_images.rb | 4 +++- lib/wraith/crop.rb | 4 +++- lib/wraith/save_images.rb | 4 +++- lib/wraith/thumbnails.rb | 5 ++++- spec/wraith_spec.rb | 4 ++-- 5 files changed, 15 insertions(+), 6 deletions(-) diff --git a/lib/wraith/compare_images.rb b/lib/wraith/compare_images.rb index bf098ad3..dd914407 100644 --- a/lib/wraith/compare_images.rb +++ b/lib/wraith/compare_images.rb @@ -2,6 +2,7 @@ require "image_size" require "open3" require "parallel" +require "shellwords" class Wraith::CompareImages attr_reader :wraith @@ -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(:*) diff --git a/lib/wraith/crop.rb b/lib/wraith/crop.rb index 0df63890..5a2bcce9 100644 --- a/lib/wraith/crop.rb +++ b/lib/wraith/crop.rb @@ -1,6 +1,7 @@ require "wraith" require "image_size" require "parallel" +require "shellwords" class Wraith::CropImages attr_reader :wraith @@ -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) diff --git a/lib/wraith/save_images.rb b/lib/wraith/save_images.rb index 4b4bf4eb..f0b852ea 100644 --- a/lib/wraith/save_images.rb +++ b/lib/wraith/save_images.rb @@ -1,5 +1,6 @@ require "wraith" require "parallel" +require "shellwords" class Wraith::SaveImages attr_reader :wraith, :history, :meta @@ -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 diff --git a/lib/wraith/thumbnails.rb b/lib/wraith/thumbnails.rb index 9fddfcb2..c0b4e7cd 100644 --- a/lib/wraith/thumbnails.rb +++ b/lib/wraith/thumbnails.rb @@ -1,6 +1,7 @@ require "wraith" require "parallel" require "fileutils" +require "shellwords" class Wraith::Thumbnails attr_reader :wraith @@ -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 diff --git a/spec/wraith_spec.rb b/spec/wraith_spec.rb index 679d6928..4ecc7e63 100644 --- a/spec/wraith_spec.rb +++ b/spec/wraith_spec.rb @@ -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) { "" } @@ -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