Skip to content

Commit

Permalink
update to the latest upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
judyhe committed Nov 16, 2010
2 parents d06e0d5 + 35cdaa5 commit 7d2e949
Show file tree
Hide file tree
Showing 30 changed files with 134 additions and 83 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -5,3 +5,6 @@ test/s3.yml
public
paperclip*.gem
capybara*.html
*.rbc
.bundle
*SPIKE*
1 change: 1 addition & 0 deletions lib/paperclip.rb
Expand Up @@ -37,6 +37,7 @@
require 'paperclip/interpolations'
require 'paperclip/style'
require 'paperclip/attachment'
require 'paperclip/storage'
require 'paperclip/callback_compatability'
require 'paperclip/command_line'
require 'paperclip/railtie'
Expand Down
11 changes: 5 additions & 6 deletions lib/paperclip/attachment.rb
Expand Up @@ -4,6 +4,7 @@ module Paperclip
# when the model saves, deletes when the model is destroyed, and processes
# the file upon assignment.
class Attachment
include IOStream

def self.default_options
@default_options ||= {
Expand Down Expand Up @@ -89,7 +90,7 @@ def assign uploaded_file

return nil if uploaded_file.nil?

@queued_for_write[:original] = uploaded_file.to_tempfile
@queued_for_write[:original] = to_tempfile(uploaded_file)
instance_write(:file_name, uploaded_file.original_filename.strip)
instance_write(:content_type, uploaded_file.content_type.to_s.strip)
instance_write(:file_size, uploaded_file.size.to_i)
Expand Down Expand Up @@ -282,13 +283,11 @@ def valid_assignment? file #:nodoc:

def initialize_storage #:nodoc:
storage_class_name = @storage.to_s.capitalize
storage_file_name = @storage.to_s.downcase
begin
require "paperclip/storage/#{storage_file_name}"
rescue MissingSourceFile
raise StorageMethodNotFound, "Cannot load 'paperclip/storage/#{storage_file_name}'"
@storage_module = Paperclip::Storage.const_get(storage_class_name)
rescue NameError
raise StorageMethodNotFound, "Cannot load storage module '#{storage_class_name}'"
end
@storage_module = Paperclip::Storage.const_get(storage_class_name)
self.extend(@storage_module)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/paperclip/command_line.rb
Expand Up @@ -8,7 +8,7 @@ def initialize(binary, params = "", options = {})
@binary = binary.dup
@params = params.dup
@options = options.dup
@swallow_stderr = @options.delete(:swallow_stderr)
@swallow_stderr = @options.has_key?(:swallow_stderr) ? @options.delete(:swallow_stderr) : Paperclip.options[:swallow_stderr]
@expected_outcodes = @options.delete(:expected_outcodes)
@expected_outcodes ||= [0]
end
Expand Down
3 changes: 2 additions & 1 deletion lib/paperclip/interpolations.rb
Expand Up @@ -41,8 +41,9 @@ def filename attachment, style_name
# Returns the interpolated URL. Will raise an error if the url itself
# contains ":url" to prevent infinite recursion. This interpolation
# is used in the default :path to ease default specifications.
RIGHT_HERE = "#{__FILE__.gsub(%r{^\./}, "")}:#{__LINE__ + 3}"
def url attachment, style_name
raise InfiniteInterpolationError if caller.any?{|b| b.index("#{__FILE__}:#{__LINE__ + 1}") }
raise InfiniteInterpolationError if caller.any?{|b| b.index(RIGHT_HERE) }
attachment.url(style_name, false)
end

Expand Down
34 changes: 10 additions & 24 deletions lib/paperclip/iostream.rb
@@ -1,48 +1,34 @@
# Provides method that can be included on File-type objects (IO, StringIO, Tempfile, etc) to allow stream copying
# and Tempfile conversion.
module IOStream

# Returns a Tempfile containing the contents of the readable object.
def to_tempfile
name = respond_to?(:original_filename) ? original_filename : (respond_to?(:path) ? path : "stream")
tempfile = Paperclip::Tempfile.new("stream" + File.extname(name))
def to_tempfile(object)
return object.to_tempfile if object.respond_to?(:to_tempfile)
name = object.respond_to?(:original_filename) ? object.original_filename : (object.respond_to?(:path) ? object.path : "stream")
tempfile = Paperclip::Tempfile.new(["stream", File.extname(name)])
tempfile.binmode
self.stream_to(tempfile)
stream_to(object, tempfile)
end

# Copies one read-able object from one place to another in blocks, obviating the need to load
# the whole thing into memory. Defaults to 8k blocks. If this module is included in both
# StringIO and Tempfile, then either can have its data copied anywhere else without typing
# worries or memory overhead worries. Returns a File if a String is passed in as the destination
# and returns the IO or Tempfile as passed in if one is sent as the destination.
def stream_to path_or_file, in_blocks_of = 8192
# the whole thing into memory. Defaults to 8k blocks. Returns a File if a String is passed
# in as the destination and returns the IO or Tempfile as passed in if one is sent as the destination.
def stream_to object, path_or_file, in_blocks_of = 8192
dstio = case path_or_file
when String then File.new(path_or_file, "wb+")
when IO then path_or_file
when Tempfile then path_or_file
end
buffer = ""
self.rewind
while self.read(in_blocks_of, buffer) do
object.rewind
while object.read(in_blocks_of, buffer) do
dstio.write(buffer)
end
dstio.rewind
dstio
end
end

class IO #:nodoc:
include IOStream
end

%w( Tempfile StringIO ).each do |klass|
if Object.const_defined? klass
Object.const_get(klass).class_eval do
include IOStream
end
end
end

# Corrects a bug in Windows when asking for Tempfile size.
if defined? Tempfile
class Tempfile
Expand Down
17 changes: 13 additions & 4 deletions lib/paperclip/processor.rb
Expand Up @@ -40,10 +40,19 @@ def self.make file, options = {}, attachment = nil
# on this blog post:
# http://marsorange.com/archives/of-mogrify-ruby-tempfile-dynamic-class-definitions
class Tempfile < ::Tempfile
# Replaces Tempfile's +make_tmpname+ with one that honors file extensions.
def make_tmpname(basename, n)
extension = File.extname(basename)
sprintf("%s,%d,%d%s", File.basename(basename, extension), $$, n.to_i, extension)
# This is Ruby 1.8.7's implementation.
if RUBY_VERSION <= "1.8.6"
def make_tmpname(basename, n)
case basename
when Array
prefix, suffix = *basename
else
prefix, suffix = basename, ''
end

t = Time.now.strftime("%y%m%d")
path = "#{prefix}#{t}-#{$$}-#{rand(0x100000000).to_s(36)}-#{n}#{suffix}"
end
end
end
end
2 changes: 2 additions & 0 deletions lib/paperclip/storage.rb
@@ -0,0 +1,2 @@
require "paperclip/storage/filesystem"
require "paperclip/storage/s3"
1 change: 1 addition & 0 deletions lib/paperclip/storage/filesystem.rb
Expand Up @@ -56,6 +56,7 @@ def flush_deletes #:nodoc:
while(true)
path = File.dirname(path)
FileUtils.rmdir(path)
break if File.exists?(path) # Ruby 1.9.2 does not raise if the removal failed.
end
rescue Errno::EEXIST, Errno::ENOTEMPTY, Errno::ENOENT, Errno::EINVAL, Errno::ENOTDIR
# Stop trying to remove parent directories
Expand Down
16 changes: 14 additions & 2 deletions lib/paperclip/storage/s3.rb
Expand Up @@ -127,13 +127,20 @@ def s3_protocol
# style, in the format most representative of the current storage.
def to_file style = default_style
return @queued_for_write[style] if @queued_for_write[style]
file = Tempfile.new(path(style))
filename = path(style)
extname = File.extname(filename)
basename = File.basename(filename, extname)
file = Tempfile.new([basename, extname])
file.binmode
file.write(AWS::S3::S3Object.value(path(style), bucket_name))
file.rewind
return file
end

def create_bucket
AWS::S3::Bucket.create(bucket_name)
end

def flush_writes #:nodoc:
@queued_for_write.each do |style, file|
begin
Expand All @@ -142,7 +149,12 @@ def flush_writes #:nodoc:
AWS::S3::S3Object.store(style_path,
file,
bucket_name,
{:access => @s3_permissions}.merge(@s3_headers))
{:content_type => instance_read(:content_type),
:access => @s3_permissions,
}.merge(@s3_headers))
rescue AWS::S3::NoSuchBucket => e
create_bucket
retry
rescue AWS::S3::ResponseError => e
raise
end
Expand Down
2 changes: 1 addition & 1 deletion lib/paperclip/thumbnail.rb
Expand Up @@ -45,7 +45,7 @@ def convert_options?
# that contains the new image.
def make
src = @file
dst = Tempfile.new([@basename, @format].compact.join("."))
dst = Tempfile.new([@basename, @format ? ".#{@format}" : ''])
dst.binmode

begin
Expand Down
4 changes: 3 additions & 1 deletion lib/paperclip/upfile.rb
Expand Up @@ -35,7 +35,9 @@ def size

# Returns the hash of the file.
def fingerprint
Digest::MD5.hexdigest(self.read)
data = self.read
self.rewind
Digest::MD5.hexdigest(data)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/paperclip/version.rb
@@ -1,3 +1,3 @@
module Paperclip
VERSION = "2.3.3" unless defined? Paperclip::VERSION
VERSION = "2.3.5" unless defined? Paperclip::VERSION
end
6 changes: 3 additions & 3 deletions test/attachment_test.rb
@@ -1,5 +1,5 @@
# encoding: utf-8
require 'test/helper'
require './test/helper'

class Dummy
# This is a dummy class
Expand Down Expand Up @@ -606,7 +606,7 @@ def do_after_all; end
[:large, :medium, :small].each do |style|
io = @attachment.to_file(style)
# p "in commit to disk test, io is #{io.inspect} and @instance.id is #{@instance.id}"
assert File.exists?(io)
assert File.exists?(io.path)
assert ! io.is_a?(::Tempfile)
io.close
end
Expand Down Expand Up @@ -703,7 +703,7 @@ def do_after_all; end
now = Time.now
Time.stubs(:now).returns(now)
@dummy.avatar = @file
assert now, @dummy.avatar.updated_at
assert_equal now.to_i, @dummy.avatar.updated_at.to_i
end

should "return nil when reloaded and sent #avatar_updated_at" do
Expand Down
27 changes: 16 additions & 11 deletions test/command_line_test.rb
@@ -1,4 +1,4 @@
require 'test/helper'
require './test/helper'

class CommandLineTest < Test::Unit::TestCase
def setup
Expand All @@ -7,21 +7,22 @@ def setup
end

should "take a command and parameters and produce a shell command for bash" do
cmd = Paperclip::CommandLine.new("convert", "a.jpg b.png")
cmd = Paperclip::CommandLine.new("convert", "a.jpg b.png", :swallow_stderr => false)
assert_equal "convert a.jpg b.png", cmd.command
end

should "be able to set a path and produce commands with that path" do
Paperclip::CommandLine.path = "/opt/bin"
cmd = Paperclip::CommandLine.new("convert", "a.jpg b.png")
cmd = Paperclip::CommandLine.new("convert", "a.jpg b.png", :swallow_stderr => false)
assert_equal "/opt/bin/convert a.jpg b.png", cmd.command
end

should "be able to interpolate quoted variables into the parameters" do
cmd = Paperclip::CommandLine.new("convert",
":one :{two}",
:one => "a.jpg",
:two => "b.png")
:two => "b.png",
:swallow_stderr => false)
assert_equal "convert 'a.jpg' 'b.png'", cmd.command
end

Expand All @@ -30,15 +31,17 @@ def setup
cmd = Paperclip::CommandLine.new("convert",
":one :{two}",
:one => "a.jpg",
:two => "b.png")
:two => "b.png",
:swallow_stderr => false)
assert_equal 'convert "a.jpg" "b.png"', cmd.command
end

should "be able to quote and interpolate dangerous variables" do
cmd = Paperclip::CommandLine.new("convert",
":one :two",
:one => "`rm -rf`.jpg",
:two => "ha'ha.png")
:two => "ha'ha.png",
:swallow_stderr => false)
assert_equal "convert '`rm -rf`.jpg' 'ha'\\''ha.png'", cmd.command
end

Expand All @@ -47,7 +50,8 @@ def setup
cmd = Paperclip::CommandLine.new("convert",
":one :two",
:one => "`rm -rf`.jpg",
:two => "ha'ha.png")
:two => "ha'ha.png",
:swallow_stderr => false)
assert_equal %{convert "`rm -rf`.jpg" "ha'ha.png"}, cmd.command
end

Expand Down Expand Up @@ -80,15 +84,15 @@ def setup
end

should "run the #command it's given and return the output" do
cmd = Paperclip::CommandLine.new("convert", "a.jpg b.png")
cmd = Paperclip::CommandLine.new("convert", "a.jpg b.png", :swallow_stderr => false)
cmd.class.stubs(:"`").with("convert a.jpg b.png").returns(:correct_value)
with_exitstatus_returning(0) do
assert_equal :correct_value, cmd.run
end
end

should "raise a PaperclipCommandLineError if the result code isn't expected" do
cmd = Paperclip::CommandLine.new("convert", "a.jpg b.png")
cmd = Paperclip::CommandLine.new("convert", "a.jpg b.png", :swallow_stderr => false)
cmd.class.stubs(:"`").with("convert a.jpg b.png").returns(:correct_value)
with_exitstatus_returning(1) do
assert_raises(Paperclip::PaperclipCommandLineError) do
Expand All @@ -100,7 +104,8 @@ def setup
should "not raise a PaperclipCommandLineError if the result code is expected" do
cmd = Paperclip::CommandLine.new("convert",
"a.jpg b.png",
:expected_outcodes => [0, 1])
:expected_outcodes => [0, 1],
:swallow_stderr => false)
cmd.class.stubs(:"`").with("convert a.jpg b.png").returns(:correct_value)
with_exitstatus_returning(1) do
assert_nothing_raised do
Expand All @@ -110,7 +115,7 @@ def setup
end

should "log the command" do
cmd = Paperclip::CommandLine.new("convert", "a.jpg b.png")
cmd = Paperclip::CommandLine.new("convert", "a.jpg b.png", :swallow_stderr => false)
cmd.class.stubs(:'`')
Paperclip.expects(:log).with("convert a.jpg b.png")
cmd.run
Expand Down
2 changes: 1 addition & 1 deletion test/geometry_test.rb
@@ -1,4 +1,4 @@
require 'test/helper'
require './test/helper'

class GeometryTest < Test::Unit::TestCase
context "Paperclip::Geometry" do
Expand Down
2 changes: 1 addition & 1 deletion test/helper.rb
Expand Up @@ -53,7 +53,7 @@ def setup

require File.join(ROOT, 'lib', 'paperclip.rb')

require 'shoulda_macros/paperclip'
require './shoulda_macros/paperclip'

FIXTURES_DIR = File.join(File.dirname(__FILE__), "fixtures")
config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
Expand Down
2 changes: 1 addition & 1 deletion test/integration_test.rb
@@ -1,4 +1,4 @@
require 'test/helper'
require './test/helper'

class IntegrationTest < Test::Unit::TestCase
context "Many models at once" do
Expand Down
2 changes: 1 addition & 1 deletion test/interpolations_test.rb
@@ -1,4 +1,4 @@
require 'test/helper'
require './test/helper'

class InterpolationsTest < Test::Unit::TestCase
should "return all methods but the infrastructure when sent #all" do
Expand Down

0 comments on commit 7d2e949

Please sign in to comment.