Skip to content

Commit

Permalink
Merge branch 'better-security'
Browse files Browse the repository at this point in the history
  • Loading branch information
markevans committed May 19, 2021
2 parents 82254ec + 300eb05 commit 2539929
Show file tree
Hide file tree
Showing 23 changed files with 683 additions and 448 deletions.
35 changes: 17 additions & 18 deletions lib/dragonfly/content.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
require 'base64'
require 'forwardable'
require 'dragonfly/has_filename'
require 'dragonfly/temp_object'
require 'dragonfly/utils'
require "base64"
require "forwardable"
require "dragonfly/has_filename"
require "dragonfly/temp_object"
require "dragonfly/utils"

module Dragonfly

Expand All @@ -16,11 +16,10 @@ module Dragonfly
# It is acted upon in generator, processor, analyser and datastore methods and provides a standard interface for updating content,
# no matter how that content first got there (whether in the form of a String/Pathname/File/etc.)
class Content

include HasFilename
extend Forwardable

def initialize(app, obj="", meta=nil)
def initialize(app, obj = "", meta = nil)
@app = app
@meta = {}
@previous_temp_objects = []
Expand Down Expand Up @@ -79,7 +78,7 @@ def name=(name)
# @example "image/jpeg"
# @return [String]
def mime_type
meta['mime_type'] || app.mime_type_for(ext)
meta["mime_type"] || app.mime_type_for(ext)
end

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

# Update the content using a pre-registered processor
# @example
# content.process!(:convert, "-resize 300x300")
# content.process!(:thumb, "300x300")
# @return [Content] self
def process!(name, *args)
app.get_processor(name).call(self, *args)
Expand All @@ -111,10 +110,10 @@ def analyse(name)
# @param obj [String, Pathname, Tempfile, File, Content, TempObject] can be any of these types
# @param meta [Hash] - should be json-like, i.e. contain no types other than String, Number, Boolean
# @return [Content] self
def update(obj, meta=nil)
def update(obj, meta = nil)
meta ||= {}
self.temp_object = TempObject.new(obj, meta['name'])
self.meta['name'] ||= temp_object.name if temp_object.name
self.temp_object = TempObject.new(obj, meta["name"])
self.meta["name"] ||= temp_object.name if temp_object.name
clear_analyser_cache
add_meta(obj.meta) if obj.respond_to?(:meta)
add_meta(meta)
Expand All @@ -135,7 +134,7 @@ def add_meta(meta)
# "file --mime-type #{path}"
# end
# # ===> "beach.jpg: image/jpeg"
def shell_eval(opts={})
def shell_eval(opts = {})
should_escape = opts[:escape] != false
command = yield(should_escape ? shell.escape(path) : path)
run command, :escape => should_escape
Expand All @@ -148,7 +147,7 @@ def shell_eval(opts={})
# "/usr/local/bin/generate_text gumfry -o #{path}"
# end
# @return [Content] self
def shell_generate(opts={})
def shell_generate(opts = {})
ext = opts[:ext] || self.ext
should_escape = opts[:escape] != false
tempfile = Utils.new_tempfile(ext)
Expand All @@ -165,7 +164,7 @@ def shell_generate(opts={})
# "convert -resize 20x10 #{old_path} #{new_path}"
# end
# @return [Content] self
def shell_update(opts={})
def shell_update(opts = {})
ext = opts[:ext] || self.ext
should_escape = opts[:escape] != false
tempfile = Utils.new_tempfile(ext)
Expand All @@ -176,7 +175,7 @@ def shell_update(opts={})
update(tempfile)
end

def store(opts={})
def store(opts = {})
datastore.write(self, opts)
end

Expand All @@ -188,7 +187,7 @@ def b64_data
end

def close
previous_temp_objects.each{|temp_object| temp_object.close }
previous_temp_objects.each { |temp_object| temp_object.close }
temp_object.close
end

Expand All @@ -199,6 +198,7 @@ def inspect
private

attr_reader :previous_temp_objects

def temp_object=(temp_object)
previous_temp_objects.push(@temp_object) if @temp_object
@temp_object = temp_object
Expand All @@ -215,6 +215,5 @@ def clear_analyser_cache
def run(command, opts)
shell.run(command, opts)
end

end
end
35 changes: 35 additions & 0 deletions lib/dragonfly/image_magick/commands.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module Dragonfly
module ImageMagick
module Commands
module_function

def convert(content, args = "", opts = {})
convert_command = content.env[:convert_command] || "convert"
format = opts["format"]

input_args = opts["input_args"] if opts["input_args"]
delegate_string = "#{opts["delegate"]}:" if opts["delegate"]
frame_string = "[#{opts["frame"]}]" if opts["frame"]

content.shell_update :ext => format do |old_path, new_path|
"#{convert_command} #{input_args} #{delegate_string}#{old_path}#{frame_string} #{args} #{new_path}"
end

if format
content.meta["format"] = format.to_s
content.ext = format
content.meta["mime_type"] = nil # don't need it as we have ext now
end
end

def generate(content, args, format)
format = format.to_s
convert_command = content.env[:convert_command] || "convert"
content.shell_generate :ext => format do |path|
"#{convert_command} #{args} #{path}"
end
content.add_meta("format" => format)
end
end
end
end
19 changes: 0 additions & 19 deletions lib/dragonfly/image_magick/generators/convert.rb

This file was deleted.

20 changes: 13 additions & 7 deletions lib/dragonfly/image_magick/generators/plain.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
require "dragonfly/image_magick/commands"
require "dragonfly/param_validators"

module Dragonfly
module ImageMagick
module Generators
class Plain
include ParamValidators

def call(content, width, height, opts={})
def call(content, width, height, opts = {})
validate_all!([width, height], &is_number)
validate_all_keys!(opts, %w(colour color format), &is_word)
format = extract_format(opts)
colour = opts['colour'] || opts['color'] || 'white'
content.generate!(:convert, "-size #{width}x#{height} xc:#{colour}", format)
content.add_meta('format' => format, 'name' => "plain.#{format}")

colour = opts["colour"] || opts["color"] || "white"
Commands.generate(content, "-size #{width}x#{height} xc:#{colour}", format)
content.add_meta("format" => format, "name" => "plain.#{format}")
end

def update_url(url_attributes, width, height, opts={})
def update_url(url_attributes, width, height, opts = {})
url_attributes.name = "plain.#{extract_format(opts)}"
end

private

def extract_format(opts)
opts['format'] || 'png'
opts["format"] || "png"
end

end
end
end
Expand Down
16 changes: 10 additions & 6 deletions lib/dragonfly/image_magick/generators/plasma.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
require "dragonfly/image_magick/commands"

module Dragonfly
module ImageMagick
module Generators
class Plasma
include ParamValidators

def call(content, width, height, opts={})
def call(content, width, height, opts = {})
validate_all!([width, height], &is_number)
validate!(opts["format"], &is_word)
format = extract_format(opts)
content.generate!(:convert, "-size #{width}x#{height} plasma:fractal", format)
content.add_meta('format' => format, 'name' => "plasma.#{format}")
Commands.generate(content, "-size #{width}x#{height} plasma:fractal", format)
content.add_meta("format" => format, "name" => "plasma.#{format}")
end

def update_url(url_attributes, width, height, opts={})
def update_url(url_attributes, width, height, opts = {})
url_attributes.name = "plasma.#{extract_format(opts)}"
end

private

def extract_format(opts)
opts['format'] || 'png'
opts["format"] || "png"
end

end
end
end
Expand Down
Loading

0 comments on commit 2539929

Please sign in to comment.