Skip to content

Commit 2539929

Browse files
committed
Merge branch 'better-security'
2 parents 82254ec + 300eb05 commit 2539929

23 files changed

+683
-448
lines changed

Diff for: lib/dragonfly/content.rb

+17-18
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
require 'base64'
2-
require 'forwardable'
3-
require 'dragonfly/has_filename'
4-
require 'dragonfly/temp_object'
5-
require 'dragonfly/utils'
1+
require "base64"
2+
require "forwardable"
3+
require "dragonfly/has_filename"
4+
require "dragonfly/temp_object"
5+
require "dragonfly/utils"
66

77
module Dragonfly
88

@@ -16,11 +16,10 @@ module Dragonfly
1616
# It is acted upon in generator, processor, analyser and datastore methods and provides a standard interface for updating content,
1717
# no matter how that content first got there (whether in the form of a String/Pathname/File/etc.)
1818
class Content
19-
2019
include HasFilename
2120
extend Forwardable
2221

23-
def initialize(app, obj="", meta=nil)
22+
def initialize(app, obj = "", meta = nil)
2423
@app = app
2524
@meta = {}
2625
@previous_temp_objects = []
@@ -79,7 +78,7 @@ def name=(name)
7978
# @example "image/jpeg"
8079
# @return [String]
8180
def mime_type
82-
meta['mime_type'] || app.mime_type_for(ext)
81+
meta["mime_type"] || app.mime_type_for(ext)
8382
end
8483

8584
# Set the content using a pre-registered generator
@@ -93,7 +92,7 @@ def generate!(name, *args)
9392

9493
# Update the content using a pre-registered processor
9594
# @example
96-
# content.process!(:convert, "-resize 300x300")
95+
# content.process!(:thumb, "300x300")
9796
# @return [Content] self
9897
def process!(name, *args)
9998
app.get_processor(name).call(self, *args)
@@ -111,10 +110,10 @@ def analyse(name)
111110
# @param obj [String, Pathname, Tempfile, File, Content, TempObject] can be any of these types
112111
# @param meta [Hash] - should be json-like, i.e. contain no types other than String, Number, Boolean
113112
# @return [Content] self
114-
def update(obj, meta=nil)
113+
def update(obj, meta = nil)
115114
meta ||= {}
116-
self.temp_object = TempObject.new(obj, meta['name'])
117-
self.meta['name'] ||= temp_object.name if temp_object.name
115+
self.temp_object = TempObject.new(obj, meta["name"])
116+
self.meta["name"] ||= temp_object.name if temp_object.name
118117
clear_analyser_cache
119118
add_meta(obj.meta) if obj.respond_to?(:meta)
120119
add_meta(meta)
@@ -135,7 +134,7 @@ def add_meta(meta)
135134
# "file --mime-type #{path}"
136135
# end
137136
# # ===> "beach.jpg: image/jpeg"
138-
def shell_eval(opts={})
137+
def shell_eval(opts = {})
139138
should_escape = opts[:escape] != false
140139
command = yield(should_escape ? shell.escape(path) : path)
141140
run command, :escape => should_escape
@@ -148,7 +147,7 @@ def shell_eval(opts={})
148147
# "/usr/local/bin/generate_text gumfry -o #{path}"
149148
# end
150149
# @return [Content] self
151-
def shell_generate(opts={})
150+
def shell_generate(opts = {})
152151
ext = opts[:ext] || self.ext
153152
should_escape = opts[:escape] != false
154153
tempfile = Utils.new_tempfile(ext)
@@ -165,7 +164,7 @@ def shell_generate(opts={})
165164
# "convert -resize 20x10 #{old_path} #{new_path}"
166165
# end
167166
# @return [Content] self
168-
def shell_update(opts={})
167+
def shell_update(opts = {})
169168
ext = opts[:ext] || self.ext
170169
should_escape = opts[:escape] != false
171170
tempfile = Utils.new_tempfile(ext)
@@ -176,7 +175,7 @@ def shell_update(opts={})
176175
update(tempfile)
177176
end
178177

179-
def store(opts={})
178+
def store(opts = {})
180179
datastore.write(self, opts)
181180
end
182181

@@ -188,7 +187,7 @@ def b64_data
188187
end
189188

190189
def close
191-
previous_temp_objects.each{|temp_object| temp_object.close }
190+
previous_temp_objects.each { |temp_object| temp_object.close }
192191
temp_object.close
193192
end
194193

@@ -199,6 +198,7 @@ def inspect
199198
private
200199

201200
attr_reader :previous_temp_objects
201+
202202
def temp_object=(temp_object)
203203
previous_temp_objects.push(@temp_object) if @temp_object
204204
@temp_object = temp_object
@@ -215,6 +215,5 @@ def clear_analyser_cache
215215
def run(command, opts)
216216
shell.run(command, opts)
217217
end
218-
219218
end
220219
end

Diff for: lib/dragonfly/image_magick/commands.rb

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
module Dragonfly
2+
module ImageMagick
3+
module Commands
4+
module_function
5+
6+
def convert(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 generate(content, args, format)
26+
format = format.to_s
27+
convert_command = content.env[:convert_command] || "convert"
28+
content.shell_generate :ext => format do |path|
29+
"#{convert_command} #{args} #{path}"
30+
end
31+
content.add_meta("format" => format)
32+
end
33+
end
34+
end
35+
end

Diff for: lib/dragonfly/image_magick/generators/convert.rb

-19
This file was deleted.

Diff for: lib/dragonfly/image_magick/generators/plain.rb

+13-7
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,31 @@
1+
require "dragonfly/image_magick/commands"
2+
require "dragonfly/param_validators"
3+
14
module Dragonfly
25
module ImageMagick
36
module Generators
47
class Plain
8+
include ParamValidators
59

6-
def call(content, width, height, opts={})
10+
def call(content, width, height, opts = {})
11+
validate_all!([width, height], &is_number)
12+
validate_all_keys!(opts, %w(colour color format), &is_word)
713
format = extract_format(opts)
8-
colour = opts['colour'] || opts['color'] || 'white'
9-
content.generate!(:convert, "-size #{width}x#{height} xc:#{colour}", format)
10-
content.add_meta('format' => format, 'name' => "plain.#{format}")
14+
15+
colour = opts["colour"] || opts["color"] || "white"
16+
Commands.generate(content, "-size #{width}x#{height} xc:#{colour}", format)
17+
content.add_meta("format" => format, "name" => "plain.#{format}")
1118
end
1219

13-
def update_url(url_attributes, width, height, opts={})
20+
def update_url(url_attributes, width, height, opts = {})
1421
url_attributes.name = "plain.#{extract_format(opts)}"
1522
end
1623

1724
private
1825

1926
def extract_format(opts)
20-
opts['format'] || 'png'
27+
opts["format"] || "png"
2128
end
22-
2329
end
2430
end
2531
end

Diff for: lib/dragonfly/image_magick/generators/plasma.rb

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
1+
require "dragonfly/image_magick/commands"
2+
13
module Dragonfly
24
module ImageMagick
35
module Generators
46
class Plasma
7+
include ParamValidators
58

6-
def call(content, width, height, opts={})
9+
def call(content, width, height, opts = {})
10+
validate_all!([width, height], &is_number)
11+
validate!(opts["format"], &is_word)
712
format = extract_format(opts)
8-
content.generate!(:convert, "-size #{width}x#{height} plasma:fractal", format)
9-
content.add_meta('format' => format, 'name' => "plasma.#{format}")
13+
Commands.generate(content, "-size #{width}x#{height} plasma:fractal", format)
14+
content.add_meta("format" => format, "name" => "plasma.#{format}")
1015
end
1116

12-
def update_url(url_attributes, width, height, opts={})
17+
def update_url(url_attributes, width, height, opts = {})
1318
url_attributes.name = "plasma.#{extract_format(opts)}"
1419
end
1520

1621
private
1722

1823
def extract_format(opts)
19-
opts['format'] || 'png'
24+
opts["format"] || "png"
2025
end
21-
2226
end
2327
end
2428
end

0 commit comments

Comments
 (0)