Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

20Q1C3 #4

Open
wants to merge 3 commits into
base: fix/SEC/remove-http-proxy
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/paperclip.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@
require 'mime/types'
require 'logger'
require 'cocaine'
require 'mimemagic'
require 'mimemagic/overlay'

require 'paperclip/railtie' if defined?(Rails)

Expand Down
3 changes: 2 additions & 1 deletion lib/paperclip/attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ def url(style_name = default_style, options = {})
def default_options
{
:timestamp => @options[:use_timestamp],
:escape => @options[:escape_url]
:escape => @options[:escape_url],
:url_filter => Paperclip.options[:url_filter]
}
end

Expand Down
21 changes: 18 additions & 3 deletions lib/paperclip/content_type_detector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def detect
elsif calculated_type_matches.any?
calculated_type_matches.first
else
type_from_file_command || SENSIBLE_DEFAULT
type_from_file_contents || SENSIBLE_DEFAULT
end.to_s
end

Expand All @@ -54,11 +54,26 @@ def possible_types
end

def calculated_type_matches
possible_types.select{|content_type| content_type == type_from_file_command }
possible_types.select do |content_type|
content_type == type_from_file_contents
end
end

def type_from_file_contents
type_from_mime_magic || type_from_file_command
rescue Errno::ENOENT => e
Paperclip.log("Error while determining content type: #{e}")
SENSIBLE_DEFAULT
end

def type_from_file_command
@type_from_file_command ||= FileCommandContentTypeDetector.new(@filename).detect
@type_from_file_command ||=
FileCommandContentTypeDetector.new(@filename).detect
end

def type_from_mime_magic
@type_from_mime_magic ||=
MimeMagic.by_magic(File.open(@filename)).try(:type)
end
end
end
14 changes: 6 additions & 8 deletions lib/paperclip/file_command_content_type_detector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,18 @@ def detect
private

def type_from_file_command
# On BSDs, `file` doesn't give a result code of 1 if the file doesn't exist.
type = begin
# On BSDs, `file` doesn't give a result code of 1 if the file doesn't exist.
Paperclip.run("file", "-b --mime :file", :file => @filename)
rescue Cocaine::CommandLineError => e
Paperclip.log("Error while determining content type: #{e}")
SENSIBLE_DEFAULT
end
Paperclip.run("file", "-b --mime :file", file: @filename)
rescue Cocaine::CommandLineError => e
Paperclip.log("Error while determining content type: #{e}")
SENSIBLE_DEFAULT
end

if type.nil? || type.match(/\(.*?\)/)
type = SENSIBLE_DEFAULT
end
type.split(/[:;\s]+/)[0]
end

end
end

2 changes: 1 addition & 1 deletion lib/paperclip/interpolations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def filename attachment, style_name
RIGHT_HERE = "#{__FILE__.gsub(%r{\A\./}, "")}:#{__LINE__ + 3}"
def url attachment, style_name
raise Errors::InfiniteInterpolationError if caller.any?{|b| b.index(RIGHT_HERE) }
attachment.url(style_name, :timestamp => false, :escape => false)
attachment.url(style_name, :timestamp => false, :escape => false, :signing_validity => :internal)
end

# Returns the timestamp as defined by the <attachment>_updated_at field
Expand Down
17 changes: 12 additions & 5 deletions lib/paperclip/url_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,22 @@ def initialize(attachment, attachment_options)
end

def for(style_name, options)
escape_url_as_needed(
timestamp_as_needed(
@attachment_options[:interpolator].interpolate(most_appropriate_url, @attachment, style_name),
options
), options)
run_filter(
escape_url_as_needed(
timestamp_as_needed(
@attachment_options[:interpolator].interpolate(most_appropriate_url, @attachment, style_name),
options
), options
), options
)
end

private

def run_filter(url, options)
return url if @attachment.original_filename.nil?
options[:url_filter].call(url, options)
end
# This method is all over the place.
def default_url
if @attachment_options[:default_url].respond_to?(:call)
Expand Down
1 change: 1 addition & 0 deletions paperclip.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Gem::Specification.new do |s|
s.add_dependency('activesupport', '>= 3.0.0')
s.add_dependency('cocaine', '~> 0.5.3')
s.add_dependency('mime-types')
s.add_dependency('mimemagic', '0.3.3')

s.add_development_dependency('activerecord', '>= 3.0.0')
s.add_development_dependency('shoulda')
Expand Down