Skip to content
This repository has been archived by the owner on Dec 29, 2021. It is now read-only.

Commit

Permalink
Merge branch 'master' into keep_old_files
Browse files Browse the repository at this point in the history
  • Loading branch information
Alain Ravet committed Aug 9, 2009
2 parents 0943fa3 + 98eff60 commit 2faf360
Show file tree
Hide file tree
Showing 9 changed files with 192 additions and 112 deletions.
4 changes: 2 additions & 2 deletions lib/paperclip.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
# documentation for Paperclip::ClassMethods for more useful information.
module Paperclip

VERSION = "2.2.9.2"
VERSION = "2.3.0"

class << self
# Provides configurability to Paperclip. There are a number of options available, such as:
Expand Down Expand Up @@ -93,7 +93,7 @@ def interpolates key, &block
# Paperclip.options[:log_command] is set to true (defaults to false). This
# will only log if logging in general is set to true as well.
def run cmd, params = "", expected_outcodes = 0
command = %Q<#{%Q[#{path_for_command(cmd)} #{params}].gsub(/\s+/, " ")}>
command = %Q[#{path_for_command(cmd)} #{params}].gsub(/\s+/, " ")
command = "#{command} 2>#{bit_bucket}" if Paperclip.options[:swallow_stderr]
Paperclip.log(command) if Paperclip.options[:log_command]
output = `#{command}`
Expand Down
51 changes: 26 additions & 25 deletions lib/paperclip/storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ def exists?(style = default_style)
def to_file style = default_style
@queued_for_write[style] || (File.new(path(style), 'rb') if exists?(style))
end
alias_method :to_io, :to_file

def flush_writes #:nodoc:
@queued_for_write.each do |style, file|
Expand Down Expand Up @@ -128,17 +127,21 @@ def flush_deletes #:nodoc:
# separate parts of your file name.
module S3
def self.extended base
require 'right_aws'
require 'aws/s3'
base.instance_eval do
@s3_credentials = parse_credentials(@options[:s3_credentials])
@bucket = @options[:bucket] || @s3_credentials[:bucket]
@bucket = @bucket.call(self) if @bucket.is_a?(Proc)
@s3_options = @options[:s3_options] || {}
@s3_permissions = @options[:s3_permissions] || 'public-read'
@s3_protocol = @options[:s3_protocol] || (@s3_permissions == 'public-read' ? 'http' : 'https')
@s3_permissions = @options[:s3_permissions] || :public_read
@s3_protocol = @options[:s3_protocol] || (@s3_permissions == :public_read ? 'http' : 'https')
@s3_headers = @options[:s3_headers] || {}
@s3_host_alias = @options[:s3_host_alias]
@url = ":s3_path_url" unless @url.to_s.match(/^:s3.*url$/)
AWS::S3::Base.establish_connection!( @s3_options.merge(
:access_key_id => @s3_credentials[:access_key_id],
:secret_access_key => @s3_credentials[:secret_access_key]
))
end
Paperclip.interpolates(:s3_alias_url) do |attachment, style|
"#{attachment.s3_protocol}://#{attachment.s3_host_alias}/#{attachment.path(style).gsub(%r{^/}, "")}"
Expand All @@ -151,16 +154,6 @@ def self.extended base
end
end

def s3
@s3 ||= RightAws::S3.new(@s3_credentials[:access_key_id],
@s3_credentials[:secret_access_key],
@s3_options)
end

def s3_bucket
@s3_bucket ||= s3.bucket(@bucket, true, @s3_permissions)
end

def bucket_name
@bucket
end
Expand All @@ -175,7 +168,11 @@ def parse_credentials creds
end

def exists?(style = default_style)
s3_bucket.key(path(style)) ? true : false
if original_filename
AWS::S3::S3Object.exists?(path(style), bucket_name)
else
false
end
end

def s3_protocol
Expand All @@ -185,18 +182,24 @@ def s3_protocol
# Returns representation of the data of the file assigned to the given
# style, in the format most representative of the current storage.
def to_file style = default_style
@queued_for_write[style] || s3_bucket.key(path(style))
return @queued_for_write[style] if @queued_for_write[style]
file = Tempfile.new(path(style))
file.write(AWS::S3::S3Object.value(path(style), bucket_name))
file.rewind
return file
end
alias_method :to_io, :to_file

def flush_writes #:nodoc:
@queued_for_write.each do |style, file|
begin
log("saving #{path(style)}")
key = s3_bucket.key(path(style))
key.data = file
key.put(nil, @s3_permissions, {'Content-type' => instance_read(:content_type)}.merge(@s3_headers))
rescue RightAws::AwsError => e
AWS::S3::S3Object.store(path(style),
file,
bucket_name,
{:content_type => instance_read(:content_type),
:access => @s3_permissions,
}.merge(@s3_headers))
rescue AWS::S3::ResponseError => e
raise
end
end
Expand All @@ -207,10 +210,8 @@ def flush_deletes #:nodoc:
@queued_for_delete.each do |path|
begin
log("deleting #{path}")
if file = s3_bucket.key(path)
file.delete
end
rescue RightAws::AwsError
AWS::S3::S3Object.delete(path, bucket_name)
rescue AWS::S3::ResponseError
# Ignore this.
end
end
Expand Down
27 changes: 15 additions & 12 deletions lib/paperclip/thumbnail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Paperclip
# Handles thumbnailing images that are uploaded.
class Thumbnail < Processor

attr_accessor :current_geometry, :target_geometry, :format, :whiny, :convert_options
attr_accessor :current_geometry, :target_geometry, :format, :whiny, :convert_options, :source_file_options

# Creates a Thumbnail object set to work on the +file+ given. It
# will attempt to transform the image into one defined by +target_geometry+
Expand All @@ -12,17 +12,18 @@ class Thumbnail < Processor
# set, the options will be appended to the convert command upon image conversion
def initialize file, options = {}, attachment = nil
super
geometry = options[:geometry]
@file = file
@crop = geometry[-1,1] == '#'
@target_geometry = Geometry.parse geometry
@current_geometry = Geometry.from_file @file
@convert_options = options[:convert_options]
@whiny = options[:whiny].nil? ? true : options[:whiny]
@format = options[:format]
geometry = options[:geometry]
@file = file
@crop = geometry[-1,1] == '#'
@target_geometry = Geometry.parse geometry
@current_geometry = Geometry.from_file @file
@source_file_options = options[:source_file_options]
@convert_options = options[:convert_options]
@whiny = options[:whiny].nil? ? true : options[:whiny]
@format = options[:format]

@current_format = File.extname(@file.path)
@basename = File.basename(@file.path, @current_format)
@current_format = File.extname(@file.path)
@basename = File.basename(@file.path, @current_format)
end

# Returns true if the +target_geometry+ is meant to crop.
Expand All @@ -43,6 +44,7 @@ def make
dst.binmode

command = <<-end_command
#{ source_file_options }
"#{ File.expand_path(src.path) }[0]"
#{ transformation_command }
"#{ File.expand_path(dst.path) }"
Expand All @@ -61,7 +63,8 @@ def make
# into the thumbnail.
def transformation_command
scale, crop = @current_geometry.transformation_to(@target_geometry, crop?)
trans = "-resize \"#{scale}\""
trans = ""
trans << " -resize \"#{scale}\"" unless scale.blank?
trans << " -crop \"#{crop}\" +repage" if crop
trans << " #{convert_options}" if convert_options?
trans
Expand Down
2 changes: 1 addition & 1 deletion paperclip.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Gem::Specification.new do |s|
s.name = %q{paperclip}
s.version = "2.2.9.2"
s.version = "2.3.0"

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Jon Yurek"]
Expand Down
14 changes: 13 additions & 1 deletion test/attachment_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ class AttachmentTest < Test::Unit::TestCase
assert_equal "#{RAILS_ROOT}/public/fake_models/1234/fake", @attachment.path
end

should "call a proc sent to check_guard" do
@dummy = Dummy.new
@dummy.expects(:one).returns(:one)
assert_equal :one, @dummy.avatar.send(:check_guard, lambda{|x| x.one })
end

should "call a method name sent to check_guard" do
@dummy = Dummy.new
@dummy.expects(:one).returns(:one)
assert_equal :one, @dummy.avatar.send(:check_guard, :one)
end

context "Attachment default_options" do
setup do
rebuild_model
Expand Down Expand Up @@ -593,7 +605,7 @@ def do_after_all; end

should "commit the files to disk" do
[:large, :medium, :small].each do |style|
io = @attachment.to_io(style)
io = @attachment.to_file(style)
assert File.exists?(io)
assert ! io.is_a?(::Tempfile)
io.close
Expand Down
24 changes: 13 additions & 11 deletions test/integration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,11 @@ def s3_headers_for attachment, style
@files_on_s3 = s3_files_for @dummy.avatar
end

should "have the same contents as the original" do
@file.rewind
assert_equal @file.read, @files_on_s3[:original].read
end

should "write and delete its files" do
[["434x66", :original],
["300x46", :large],
Expand All @@ -403,19 +408,17 @@ def s3_headers_for attachment, style
assert @dummy.valid?
assert @dummy.save

saved_keys = [:thumb, :medium, :large, :original].collect{|s| @dummy.avatar.to_file(s) }

saved_keys.each do |key|
assert key.exists?
[:thumb, :medium, :large, :original].each do |style|
assert @dummy.avatar.exists?(style)
end

@dummy.avatar.clear
assert_nil @dummy.avatar_file_name
assert @dummy.valid?
assert @dummy.save

saved_keys.each do |key|
assert ! key.exists?
[:thumb, :medium, :large, :original].each do |style|
assert ! @dummy.avatar.exists?(style)
end

@d2 = Dummy.find(@dummy.id)
Expand All @@ -427,24 +430,24 @@ def s3_headers_for attachment, style

assert_equal @dummy.avatar_file_name, @d2.avatar_file_name
[:thumb, :medium, :large, :original].each do |style|
assert_equal @dummy.avatar.to_file(style).to_s, @d2.avatar.to_file(style).to_s
assert_equal @dummy.avatar.to_file(style).read, @d2.avatar.to_file(style).read
end

saved_keys = [:thumb, :medium, :large, :original].collect{|s| @dummy.avatar.to_file(s) }

@d2.avatar.clear
assert @d2.save

saved_keys.each do |key|
assert ! key.exists?
[:thumb, :medium, :large, :original].each do |style|
assert ! @dummy.avatar.exists?(style)
end
end

should "know the difference between good files, bad files, not files, and nil" do
expected = @dummy.avatar.to_file
@dummy.avatar = "not a file"
assert @dummy.valid?
assert_equal expected.full_name, @dummy.avatar.to_file.full_name
assert_equal expected.read, @dummy.avatar.to_file.read

@dummy.avatar = @bad_file
assert ! @dummy.valid?
Expand Down Expand Up @@ -472,7 +475,6 @@ def s3_headers_for attachment, style

should "have the right content type" do
headers = s3_headers_for(@dummy.avatar, :original)
p headers
assert_equal 'image/png', headers['content-type']
end
end
Expand Down
Loading

0 comments on commit 2faf360

Please sign in to comment.