dragonfly 1.3.0 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (29) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +1 -0
  3. data/History.md +10 -0
  4. data/lib/dragonfly/content.rb +17 -18
  5. data/lib/dragonfly/image_magick/commands.rb +35 -0
  6. data/lib/dragonfly/image_magick/generators/plain.rb +13 -7
  7. data/lib/dragonfly/image_magick/generators/plasma.rb +10 -6
  8. data/lib/dragonfly/image_magick/generators/text.rb +67 -58
  9. data/lib/dragonfly/image_magick/plugin.rb +26 -25
  10. data/lib/dragonfly/image_magick/processors/encode.rb +16 -5
  11. data/lib/dragonfly/image_magick/processors/thumb.rb +37 -31
  12. data/lib/dragonfly/param_validators.rb +37 -0
  13. data/lib/dragonfly/response.rb +2 -2
  14. data/lib/dragonfly/version.rb +1 -1
  15. data/spec/dragonfly/image_magick/commands_spec.rb +98 -0
  16. data/spec/dragonfly/image_magick/generators/plain_spec.rb +39 -13
  17. data/spec/dragonfly/image_magick/generators/plasma_spec.rb +28 -9
  18. data/spec/dragonfly/image_magick/generators/text_spec.rb +51 -20
  19. data/spec/dragonfly/image_magick/plugin_spec.rb +45 -28
  20. data/spec/dragonfly/image_magick/processors/encode_spec.rb +30 -0
  21. data/spec/dragonfly/image_magick/processors/thumb_spec.rb +46 -45
  22. data/spec/dragonfly/param_validators_spec.rb +89 -0
  23. data/spec/functional/shell_commands_spec.rb +6 -9
  24. data/spec/spec_helper.rb +12 -14
  25. metadata +11 -10
  26. data/lib/dragonfly/image_magick/generators/convert.rb +0 -19
  27. data/lib/dragonfly/image_magick/processors/convert.rb +0 -33
  28. data/spec/dragonfly/image_magick/generators/convert_spec.rb +0 -19
  29. data/spec/dragonfly/image_magick/processors/convert_spec.rb +0 -88
@@ -0,0 +1,89 @@
1
+ require "spec_helper"
2
+ require "dragonfly/param_validators"
3
+
4
+ describe Dragonfly::ParamValidators do
5
+ include Dragonfly::ParamValidators
6
+
7
+ describe "validate!" do
8
+ it "does nothing if the parameter meets the condition" do
9
+ validate!("thing") { |t| t === "thing" }
10
+ end
11
+
12
+ it "raises if the parameter doesn't meet the condition" do
13
+ expect {
14
+ validate!("thing") { |t| t === "ting" }
15
+ }.to raise_error(Dragonfly::ParamValidators::InvalidParameter)
16
+ end
17
+
18
+ it "does nothing if the parameter is nil" do
19
+ validate!(nil) { |t| t === "thing" }
20
+ end
21
+ end
22
+
23
+ describe "validate_all!" do
24
+ it "allows passing an array of parameters to validate" do
25
+ validate_all!(["a", "b"]) { |p| /\w/ === p }
26
+ expect {
27
+ validate_all!(["a", " "]) { |p| /\w/ === p }
28
+ }.to raise_error(Dragonfly::ParamValidators::InvalidParameter)
29
+ end
30
+ end
31
+
32
+ describe "validate_all_keys!" do
33
+ it "allows passing an array of parameters to validate" do
34
+ obj = { "a" => "A", "b" => "B" }
35
+ validate_all_keys!(obj, ["a", "b"]) { |p| /\w/ === p }
36
+ expect {
37
+ validate_all_keys!(obj, ["a", "b"]) { |p| /[a-z]/ === p }
38
+ }.to raise_error(Dragonfly::ParamValidators::InvalidParameter)
39
+ end
40
+ end
41
+
42
+ describe "is_number" do
43
+ [3, 3.14, "3", "3.2"].each do |val|
44
+ it "validates #{val.inspect}" do
45
+ validate!(val, &is_number)
46
+ end
47
+ end
48
+
49
+ ["", "3 2", "hello4", {}, []].each do |val|
50
+ it "validates #{val.inspect}" do
51
+ expect {
52
+ validate!(val, &is_number)
53
+ }.to raise_error(Dragonfly::ParamValidators::InvalidParameter)
54
+ end
55
+ end
56
+ end
57
+
58
+ describe "is_word" do
59
+ ["hello", "helLo", "HELLO"].each do |val|
60
+ it "validates #{val.inspect}" do
61
+ validate!(val, &is_word)
62
+ end
63
+ end
64
+
65
+ ["", "hel%$lo", "hel lo", "hel-lo", {}, []].each do |val|
66
+ it "validates #{val.inspect}" do
67
+ expect {
68
+ validate!(val, &is_word)
69
+ }.to raise_error(Dragonfly::ParamValidators::InvalidParameter)
70
+ end
71
+ end
72
+ end
73
+
74
+ describe "is_words" do
75
+ ["hello there", "Hi", " What is Up "].each do |val|
76
+ it "validates #{val.inspect}" do
77
+ validate!(val, &is_words)
78
+ end
79
+ end
80
+
81
+ ["", "hel%$lo", "What's up", "hel-lo", {}, []].each do |val|
82
+ it "validates #{val.inspect}" do
83
+ expect {
84
+ validate!(val, &is_words)
85
+ }.to raise_error(Dragonfly::ParamValidators::InvalidParameter)
86
+ end
87
+ end
88
+ end
89
+ end
@@ -1,33 +1,30 @@
1
- require 'spec_helper'
1
+ require "spec_helper"
2
2
 
3
3
  describe "using the shell" do
4
-
5
4
  let (:app) { test_app }
6
5
 
7
6
  describe "shell injection" do
8
7
  it "should not allow it!" do
9
8
  app.configure_with(:imagemagick)
10
9
  begin
11
- app.generate(:plain, 10, 10, 'white').convert("-resize 5x5 ; touch tmp/stuff").apply
10
+ app.generate(:plain, 10, 10, "white; touch tmp/stuff").apply
12
11
  rescue Dragonfly::Shell::CommandFailed
13
12
  end
14
- File.exist?('tmp/stuff').should be_falsey
13
+ File.exist?("tmp/stuff").should be_falsey
15
14
  end
16
15
  end
17
16
 
18
17
  describe "env variables with imagemagick" do
19
18
  it "allows configuring the convert path" do
20
- app.configure_with(:imagemagick, :convert_command => '/bin/convert')
19
+ app.configure_with(:imagemagick, :convert_command => "/bin/convert")
21
20
  app.shell.should_receive(:run).with(%r[/bin/convert], hash_including)
22
- app.create("").thumb('30x30').apply
21
+ app.create("").thumb("30x30").apply
23
22
  end
24
23
 
25
24
  it "allows configuring the identify path" do
26
- app.configure_with(:imagemagick, :identify_command => '/bin/identify')
25
+ app.configure_with(:imagemagick, :identify_command => "/bin/identify")
27
26
  app.shell.should_receive(:run).with(%r[/bin/identify], hash_including).and_return("JPG 1 1")
28
27
  app.create("").width
29
28
  end
30
29
  end
31
-
32
30
  end
33
-
data/spec/spec_helper.rb CHANGED
@@ -2,19 +2,19 @@ require "rubygems"
2
2
  require "bundler"
3
3
  Bundler.setup(:default, :test)
4
4
 
5
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
6
6
  $LOAD_PATH.unshift(File.dirname(__FILE__))
7
- require 'rspec'
8
- require 'dragonfly'
9
- require 'fileutils'
10
- require 'tempfile'
11
- require 'webmock/rspec'
12
- require 'pry'
7
+ require "rspec"
8
+ require "dragonfly"
9
+ require "fileutils"
10
+ require "tempfile"
11
+ require "webmock/rspec"
12
+ require "pry"
13
13
 
14
14
  # Requires supporting files with custom matchers and macros, etc,
15
- Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
15
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
16
16
 
17
- SAMPLES_DIR = Pathname.new(File.expand_path('../../samples', __FILE__))
17
+ SAMPLES_DIR = Pathname.new(File.expand_path("../../samples", __FILE__))
18
18
 
19
19
  RSpec.configure do |c|
20
20
  c.include ModelHelpers
@@ -25,8 +25,8 @@ def todo
25
25
  raise "TODO"
26
26
  end
27
27
 
28
- require 'logger'
29
- LOG_FILE = 'tmp/test.log'
28
+ require "logger"
29
+ LOG_FILE = "tmp/test.log"
30
30
  FileUtils.rm_rf(LOG_FILE)
31
31
  Dragonfly.logger = Logger.new(LOG_FILE)
32
32
 
@@ -36,7 +36,7 @@ RSpec.configure do |c|
36
36
  end
37
37
  end
38
38
 
39
- def test_app(name=nil)
39
+ def test_app(name = nil)
40
40
  app = Dragonfly::App.instance(name)
41
41
  app.datastore = Dragonfly::MemoryDataStore.new
42
42
  app.secret = "test secret"
@@ -45,8 +45,6 @@ end
45
45
 
46
46
  def test_imagemagick_app
47
47
  test_app.configure do
48
- generator :convert, Dragonfly::ImageMagick::Generators::Convert.new
49
- processor :convert, Dragonfly::ImageMagick::Processors::Convert.new
50
48
  analyser :image_properties, Dragonfly::ImageMagick::Analysers::ImageProperties.new
51
49
  end
52
50
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dragonfly
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mark Evans
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-01-09 00:00:00.000000000 Z
11
+ date: 2021-05-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -158,12 +158,11 @@ files:
158
158
  - lib/dragonfly/has_filename.rb
159
159
  - lib/dragonfly/hash_with_css_style_keys.rb
160
160
  - lib/dragonfly/image_magick/analysers/image_properties.rb
161
- - lib/dragonfly/image_magick/generators/convert.rb
161
+ - lib/dragonfly/image_magick/commands.rb
162
162
  - lib/dragonfly/image_magick/generators/plain.rb
163
163
  - lib/dragonfly/image_magick/generators/plasma.rb
164
164
  - lib/dragonfly/image_magick/generators/text.rb
165
165
  - lib/dragonfly/image_magick/plugin.rb
166
- - lib/dragonfly/image_magick/processors/convert.rb
167
166
  - lib/dragonfly/image_magick/processors/encode.rb
168
167
  - lib/dragonfly/image_magick/processors/thumb.rb
169
168
  - lib/dragonfly/job.rb
@@ -182,6 +181,7 @@ files:
182
181
  - lib/dragonfly/model/class_methods.rb
183
182
  - lib/dragonfly/model/instance_methods.rb
184
183
  - lib/dragonfly/model/validations.rb
184
+ - lib/dragonfly/param_validators.rb
185
185
  - lib/dragonfly/rails/images.rb
186
186
  - lib/dragonfly/railtie.rb
187
187
  - lib/dragonfly/register.rb
@@ -220,12 +220,12 @@ files:
220
220
  - spec/dragonfly/has_filename_spec.rb
221
221
  - spec/dragonfly/hash_with_css_style_keys_spec.rb
222
222
  - spec/dragonfly/image_magick/analysers/image_properties_spec.rb
223
- - spec/dragonfly/image_magick/generators/convert_spec.rb
223
+ - spec/dragonfly/image_magick/commands_spec.rb
224
224
  - spec/dragonfly/image_magick/generators/plain_spec.rb
225
225
  - spec/dragonfly/image_magick/generators/plasma_spec.rb
226
226
  - spec/dragonfly/image_magick/generators/text_spec.rb
227
227
  - spec/dragonfly/image_magick/plugin_spec.rb
228
- - spec/dragonfly/image_magick/processors/convert_spec.rb
228
+ - spec/dragonfly/image_magick/processors/encode_spec.rb
229
229
  - spec/dragonfly/image_magick/processors/thumb_spec.rb
230
230
  - spec/dragonfly/job/fetch_file_spec.rb
231
231
  - spec/dragonfly/job/fetch_spec.rb
@@ -239,6 +239,7 @@ files:
239
239
  - spec/dragonfly/model/active_record_spec.rb
240
240
  - spec/dragonfly/model/model_spec.rb
241
241
  - spec/dragonfly/model/validations_spec.rb
242
+ - spec/dragonfly/param_validators_spec.rb
242
243
  - spec/dragonfly/register_spec.rb
243
244
  - spec/dragonfly/routed_endpoint_spec.rb
244
245
  - spec/dragonfly/serializer_spec.rb
@@ -285,8 +286,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
285
286
  - !ruby/object:Gem::Version
286
287
  version: '0'
287
288
  requirements: []
288
- rubyforge_project:
289
- rubygems_version: 2.7.6
289
+ rubygems_version: 3.2.15
290
290
  signing_key:
291
291
  specification_version: 4
292
292
  summary: Ideal gem for handling attachments in Rails, Sinatra and Rack applications.
@@ -301,12 +301,12 @@ test_files:
301
301
  - spec/dragonfly/has_filename_spec.rb
302
302
  - spec/dragonfly/hash_with_css_style_keys_spec.rb
303
303
  - spec/dragonfly/image_magick/analysers/image_properties_spec.rb
304
- - spec/dragonfly/image_magick/generators/convert_spec.rb
304
+ - spec/dragonfly/image_magick/commands_spec.rb
305
305
  - spec/dragonfly/image_magick/generators/plain_spec.rb
306
306
  - spec/dragonfly/image_magick/generators/plasma_spec.rb
307
307
  - spec/dragonfly/image_magick/generators/text_spec.rb
308
308
  - spec/dragonfly/image_magick/plugin_spec.rb
309
- - spec/dragonfly/image_magick/processors/convert_spec.rb
309
+ - spec/dragonfly/image_magick/processors/encode_spec.rb
310
310
  - spec/dragonfly/image_magick/processors/thumb_spec.rb
311
311
  - spec/dragonfly/job/fetch_file_spec.rb
312
312
  - spec/dragonfly/job/fetch_spec.rb
@@ -320,6 +320,7 @@ test_files:
320
320
  - spec/dragonfly/model/active_record_spec.rb
321
321
  - spec/dragonfly/model/model_spec.rb
322
322
  - spec/dragonfly/model/validations_spec.rb
323
+ - spec/dragonfly/param_validators_spec.rb
323
324
  - spec/dragonfly/register_spec.rb
324
325
  - spec/dragonfly/routed_endpoint_spec.rb
325
326
  - spec/dragonfly/serializer_spec.rb
@@ -1,19 +0,0 @@
1
- module Dragonfly
2
- module ImageMagick
3
- module Generators
4
- class Convert
5
-
6
- def call(content, args, format)
7
- format = format.to_s
8
- convert_command = content.env[:convert_command] || 'convert'
9
- content.shell_generate :ext => format do |path|
10
- "#{convert_command} #{args} #{path}"
11
- end
12
- content.add_meta('format' => format)
13
- end
14
-
15
- end
16
- end
17
- end
18
- end
19
-
@@ -1,33 +0,0 @@
1
- module Dragonfly
2
- module ImageMagick
3
- module Processors
4
- class Convert
5
-
6
- def call(content, args='', opts={})
7
- convert_command = content.env[:convert_command] || 'convert'
8
- format = opts['format']
9
-
10
- input_args = opts['input_args'] if opts['input_args']
11
- delegate_string = "#{opts['delegate']}:" if opts['delegate']
12
- frame_string = "[#{opts['frame']}]" if opts['frame']
13
-
14
- content.shell_update :ext => format do |old_path, new_path|
15
- "#{convert_command} #{input_args} #{delegate_string}#{old_path}#{frame_string} #{args} #{new_path}"
16
- end
17
-
18
- if format
19
- content.meta['format'] = format.to_s
20
- content.ext = format
21
- content.meta['mime_type'] = nil # don't need it as we have ext now
22
- end
23
- end
24
-
25
- def update_url(attrs, args='', opts={})
26
- format = opts['format']
27
- attrs.ext = format if format
28
- end
29
-
30
- end
31
- end
32
- end
33
- end
@@ -1,19 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Dragonfly::ImageMagick::Generators::Convert do
4
- let (:generator) { Dragonfly::ImageMagick::Generators::Convert.new }
5
- let (:app) { test_app }
6
- let (:image) { Dragonfly::Content.new(app) }
7
-
8
- describe "calling convert" do
9
- before(:each) do
10
- generator.call(image, "-size 1x1 xc:white", 'png')
11
- end
12
- it {image.should have_width(1)}
13
- it {image.should have_height(1)}
14
- it {image.should have_format('png')}
15
- it {image.meta.should == {'format' => 'png'}}
16
- end
17
-
18
- end
19
-
@@ -1,88 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Dragonfly::ImageMagick::Processors::Convert do
4
-
5
- def sample_content(name)
6
- Dragonfly::Content.new(app, SAMPLES_DIR.join(name))
7
- end
8
-
9
- let(:app){ test_app }
10
- let(:image){ sample_content('beach.png') } # 280x355
11
- let(:processor){ Dragonfly::ImageMagick::Processors::Convert.new }
12
-
13
- it "should allow for general convert commands" do
14
- processor.call(image, '-scale 56x71')
15
- image.should have_width(56)
16
- image.should have_height(71)
17
- end
18
-
19
- it "should allow for general convert commands with added format" do
20
- processor.call(image, '-scale 56x71', 'format' => 'gif')
21
- image.should have_width(56)
22
- image.should have_height(71)
23
- image.should have_format('gif')
24
- image.meta['format'].should == 'gif'
25
- end
26
-
27
- it "should work for commands with parenthesis" do
28
- processor.call(image, "\\( +clone -sparse-color Barycentric '0,0 black 0,%[fx:h-1] white' -function polynomial 2,-2,0.5 \\) -compose Blur -set option:compose:args 15 -composite")
29
- image.should have_width(280)
30
- end
31
-
32
- it "should work for files with spaces/apostrophes in the name" do
33
- image = Dragonfly::Content.new(app, SAMPLES_DIR.join("mevs' white pixel.png"))
34
- processor.call(image, "-resize 2x2!")
35
- image.should have_width(2)
36
- end
37
-
38
- it "updates the url with format if given" do
39
- url_attributes = Dragonfly::UrlAttributes.new
40
- processor.update_url(url_attributes, '-scale 56x71', 'format' => 'gif')
41
- url_attributes.ext.should == 'gif'
42
- end
43
-
44
- it "allows converting specific frames" do
45
- gif = sample_content('gif.gif')
46
- processor.call(gif, '-resize 50x50')
47
- all_frames_size = gif.size
48
-
49
- gif = sample_content('gif.gif')
50
- processor.call(gif, '-resize 50x50', 'frame' => 0)
51
- one_frame_size = gif.size
52
-
53
- one_frame_size.should < all_frames_size
54
- end
55
-
56
- it "accepts input arguments for convert commands" do
57
- image2 = image.clone
58
- processor.call(image, '')
59
- processor.call(image2, '', 'input_args' => '-extract 50x50+10+10')
60
-
61
- image.should_not equal_image(image2)
62
- image2.should have_width(50)
63
- end
64
-
65
- it "allows converting using specific delegates" do
66
- expect {
67
- processor.call(image, '', 'format' => 'jpg', 'delegate' => 'png')
68
- }.to call_command(app.shell, %r{convert png:/[^']+?/beach\.png /[^']+?\.jpg})
69
- end
70
-
71
- it "maintains the mime_type meta if it exists already" do
72
- processor.call(image, '-resize 10x')
73
- image.meta['mime_type'].should be_nil
74
-
75
- image.add_meta('mime_type' => 'image/png')
76
- processor.call(image, '-resize 5x')
77
- image.meta['mime_type'].should == 'image/png'
78
- image.mime_type.should == 'image/png' # sanity check
79
- end
80
-
81
- it "doesn't maintain the mime_type meta on format change" do
82
- image.add_meta('mime_type' => 'image/png')
83
- processor.call(image, '', 'format' => 'gif')
84
- image.meta['mime_type'].should be_nil
85
- image.mime_type.should == 'image/gif' # sanity check
86
- end
87
-
88
- end