paperclip 2.3.3 → 2.3.4
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of paperclip might be problematic. Click here for more details.
- data/README.rdoc +4 -1
- data/lib/paperclip.rb +34 -61
- data/lib/paperclip/attachment.rb +23 -9
- data/lib/paperclip/command_line.rb +80 -0
- data/lib/paperclip/geometry.rb +1 -1
- data/lib/paperclip/interpolations.rb +6 -1
- data/lib/paperclip/iostream.rb +1 -1
- data/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb +2 -1
- data/lib/paperclip/processor.rb +13 -4
- data/lib/paperclip/railtie.rb +1 -1
- data/lib/paperclip/storage.rb +2 -247
- data/lib/paperclip/storage/filesystem.rb +73 -0
- data/lib/paperclip/storage/s3.rb +191 -0
- data/lib/paperclip/thumbnail.rb +12 -11
- data/lib/paperclip/upfile.rb +10 -2
- data/lib/paperclip/version.rb +1 -1
- data/shoulda_macros/paperclip.rb +0 -1
- data/test/attachment_test.rb +48 -2
- data/test/command_line_test.rb +133 -0
- data/test/helper.rb +19 -9
- data/test/integration_test.rb +1 -2
- data/test/interpolations_test.rb +6 -3
- data/test/matchers/validate_attachment_content_type_matcher_test.rb +10 -0
- data/test/paperclip_test.rb +26 -89
- data/test/storage_test.rb +16 -1
- data/test/thumbnail_test.rb +8 -7
- metadata +25 -4
@@ -0,0 +1,73 @@
|
|
1
|
+
module Paperclip
|
2
|
+
module Storage
|
3
|
+
# The default place to store attachments is in the filesystem. Files on the local
|
4
|
+
# filesystem can be very easily served by Apache without requiring a hit to your app.
|
5
|
+
# They also can be processed more easily after they've been saved, as they're just
|
6
|
+
# normal files. There is one Filesystem-specific option for has_attached_file.
|
7
|
+
# * +path+: The location of the repository of attachments on disk. This can (and, in
|
8
|
+
# almost all cases, should) be coordinated with the value of the +url+ option to
|
9
|
+
# allow files to be saved into a place where Apache can serve them without
|
10
|
+
# hitting your app. Defaults to
|
11
|
+
# ":rails_root/public/:attachment/:id/:style/:basename.:extension"
|
12
|
+
# By default this places the files in the app's public directory which can be served
|
13
|
+
# directly. If you are using capistrano for deployment, a good idea would be to
|
14
|
+
# make a symlink to the capistrano-created system directory from inside your app's
|
15
|
+
# public directory.
|
16
|
+
# See Paperclip::Attachment#interpolate for more information on variable interpolaton.
|
17
|
+
# :path => "/var/app/attachments/:class/:id/:style/:basename.:extension"
|
18
|
+
module Filesystem
|
19
|
+
def self.extended base
|
20
|
+
end
|
21
|
+
|
22
|
+
def exists?(style_name = default_style)
|
23
|
+
if original_filename
|
24
|
+
File.exist?(path(style_name))
|
25
|
+
else
|
26
|
+
false
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
# Returns representation of the data of the file assigned to the given
|
31
|
+
# style, in the format most representative of the current storage.
|
32
|
+
def to_file style_name = default_style
|
33
|
+
@queued_for_write[style_name] || (File.new(path(style_name), 'rb') if exists?(style_name))
|
34
|
+
end
|
35
|
+
|
36
|
+
def flush_writes #:nodoc:
|
37
|
+
@queued_for_write.each do |style_name, file|
|
38
|
+
file.close
|
39
|
+
FileUtils.mkdir_p(File.dirname(path(style_name)))
|
40
|
+
log("saving #{path(style_name)}")
|
41
|
+
FileUtils.mv(file.path, path(style_name))
|
42
|
+
FileUtils.chmod(0644, path(style_name))
|
43
|
+
end
|
44
|
+
@queued_for_write = {}
|
45
|
+
end
|
46
|
+
|
47
|
+
def flush_deletes #:nodoc:
|
48
|
+
@queued_for_delete.each do |path|
|
49
|
+
begin
|
50
|
+
log("deleting #{path}")
|
51
|
+
FileUtils.rm(path) if File.exist?(path)
|
52
|
+
rescue Errno::ENOENT => e
|
53
|
+
# ignore file-not-found, let everything else pass
|
54
|
+
end
|
55
|
+
begin
|
56
|
+
while(true)
|
57
|
+
path = File.dirname(path)
|
58
|
+
FileUtils.rmdir(path)
|
59
|
+
break if File.exists?(path) # Ruby 1.9.2 does not raise if the removal failed.
|
60
|
+
end
|
61
|
+
rescue Errno::EEXIST, Errno::ENOTEMPTY, Errno::ENOENT, Errno::EINVAL, Errno::ENOTDIR
|
62
|
+
# Stop trying to remove parent directories
|
63
|
+
rescue SystemCallError => e
|
64
|
+
log("There was an unexpected error while deleting directories: #{e.class}")
|
65
|
+
# Ignore it
|
66
|
+
end
|
67
|
+
end
|
68
|
+
@queued_for_delete = []
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,191 @@
|
|
1
|
+
module Paperclip
|
2
|
+
module Storage
|
3
|
+
# Amazon's S3 file hosting service is a scalable, easy place to store files for
|
4
|
+
# distribution. You can find out more about it at http://aws.amazon.com/s3
|
5
|
+
# There are a few S3-specific options for has_attached_file:
|
6
|
+
# * +s3_credentials+: Takes a path, a File, or a Hash. The path (or File) must point
|
7
|
+
# to a YAML file containing the +access_key_id+ and +secret_access_key+ that Amazon
|
8
|
+
# gives you. You can 'environment-space' this just like you do to your
|
9
|
+
# database.yml file, so different environments can use different accounts:
|
10
|
+
# development:
|
11
|
+
# access_key_id: 123...
|
12
|
+
# secret_access_key: 123...
|
13
|
+
# test:
|
14
|
+
# access_key_id: abc...
|
15
|
+
# secret_access_key: abc...
|
16
|
+
# production:
|
17
|
+
# access_key_id: 456...
|
18
|
+
# secret_access_key: 456...
|
19
|
+
# This is not required, however, and the file may simply look like this:
|
20
|
+
# access_key_id: 456...
|
21
|
+
# secret_access_key: 456...
|
22
|
+
# In which case, those access keys will be used in all environments. You can also
|
23
|
+
# put your bucket name in this file, instead of adding it to the code directly.
|
24
|
+
# This is useful when you want the same account but a different bucket for
|
25
|
+
# development versus production.
|
26
|
+
# * +s3_permissions+: This is a String that should be one of the "canned" access
|
27
|
+
# policies that S3 provides (more information can be found here:
|
28
|
+
# http://docs.amazonwebservices.com/AmazonS3/2006-03-01/RESTAccessPolicy.html#RESTCannedAccessPolicies)
|
29
|
+
# The default for Paperclip is :public_read.
|
30
|
+
# * +s3_protocol+: The protocol for the URLs generated to your S3 assets. Can be either
|
31
|
+
# 'http' or 'https'. Defaults to 'http' when your :s3_permissions are :public_read (the
|
32
|
+
# default), and 'https' when your :s3_permissions are anything else.
|
33
|
+
# * +s3_headers+: A hash of headers such as {'Expires' => 1.year.from_now.httpdate}
|
34
|
+
# * +bucket+: This is the name of the S3 bucket that will store your files. Remember
|
35
|
+
# that the bucket must be unique across all of Amazon S3. If the bucket does not exist
|
36
|
+
# Paperclip will attempt to create it. The bucket name will not be interpolated.
|
37
|
+
# You can define the bucket as a Proc if you want to determine it's name at runtime.
|
38
|
+
# Paperclip will call that Proc with attachment as the only argument.
|
39
|
+
# * +s3_host_alias+: The fully-qualified domain name (FQDN) that is the alias to the
|
40
|
+
# S3 domain of your bucket. Used with the :s3_alias_url url interpolation. See the
|
41
|
+
# link in the +url+ entry for more information about S3 domains and buckets.
|
42
|
+
# * +url+: There are three options for the S3 url. You can choose to have the bucket's name
|
43
|
+
# placed domain-style (bucket.s3.amazonaws.com) or path-style (s3.amazonaws.com/bucket).
|
44
|
+
# Lastly, you can specify a CNAME (which requires the CNAME to be specified as
|
45
|
+
# :s3_alias_url. You can read more about CNAMEs and S3 at
|
46
|
+
# http://docs.amazonwebservices.com/AmazonS3/latest/index.html?VirtualHosting.html
|
47
|
+
# Normally, this won't matter in the slightest and you can leave the default (which is
|
48
|
+
# path-style, or :s3_path_url). But in some cases paths don't work and you need to use
|
49
|
+
# the domain-style (:s3_domain_url). Anything else here will be treated like path-style.
|
50
|
+
# NOTE: If you use a CNAME for use with CloudFront, you can NOT specify https as your
|
51
|
+
# :s3_protocol; This is *not supported* by S3/CloudFront. Finally, when using the host
|
52
|
+
# alias, the :bucket parameter is ignored, as the hostname is used as the bucket name
|
53
|
+
# by S3.
|
54
|
+
# * +path+: This is the key under the bucket in which the file will be stored. The
|
55
|
+
# URL will be constructed from the bucket and the path. This is what you will want
|
56
|
+
# to interpolate. Keys should be unique, like filenames, and despite the fact that
|
57
|
+
# S3 (strictly speaking) does not support directories, you can still use a / to
|
58
|
+
# separate parts of your file name.
|
59
|
+
module S3
|
60
|
+
def self.extended base
|
61
|
+
begin
|
62
|
+
require 'aws/s3'
|
63
|
+
rescue LoadError => e
|
64
|
+
e.message << " (You may need to install the aws-s3 gem)"
|
65
|
+
raise e
|
66
|
+
end
|
67
|
+
|
68
|
+
base.instance_eval do
|
69
|
+
@s3_credentials = parse_credentials(@options[:s3_credentials])
|
70
|
+
@bucket = @options[:bucket] || @s3_credentials[:bucket]
|
71
|
+
@bucket = @bucket.call(self) if @bucket.is_a?(Proc)
|
72
|
+
@s3_options = @options[:s3_options] || {}
|
73
|
+
@s3_permissions = @options[:s3_permissions] || :public_read
|
74
|
+
@s3_protocol = @options[:s3_protocol] || (@s3_permissions == :public_read ? 'http' : 'https')
|
75
|
+
@s3_headers = @options[:s3_headers] || {}
|
76
|
+
@s3_host_alias = @options[:s3_host_alias]
|
77
|
+
unless @url.to_s.match(/^:s3.*url$/)
|
78
|
+
@path = @path.gsub(/:url/, @url)
|
79
|
+
@url = ":s3_path_url"
|
80
|
+
end
|
81
|
+
AWS::S3::Base.establish_connection!( @s3_options.merge(
|
82
|
+
:access_key_id => @s3_credentials[:access_key_id],
|
83
|
+
:secret_access_key => @s3_credentials[:secret_access_key]
|
84
|
+
))
|
85
|
+
end
|
86
|
+
Paperclip.interpolates(:s3_alias_url) do |attachment, style|
|
87
|
+
"#{attachment.s3_protocol}://#{attachment.s3_host_alias}/#{attachment.path(style).gsub(%r{^/}, "")}"
|
88
|
+
end
|
89
|
+
Paperclip.interpolates(:s3_path_url) do |attachment, style|
|
90
|
+
"#{attachment.s3_protocol}://s3.amazonaws.com/#{attachment.bucket_name}/#{attachment.path(style).gsub(%r{^/}, "")}"
|
91
|
+
end
|
92
|
+
Paperclip.interpolates(:s3_domain_url) do |attachment, style|
|
93
|
+
"#{attachment.s3_protocol}://#{attachment.bucket_name}.s3.amazonaws.com/#{attachment.path(style).gsub(%r{^/}, "")}"
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
def expiring_url(time = 3600)
|
98
|
+
AWS::S3::S3Object.url_for(path, bucket_name, :expires_in => time )
|
99
|
+
end
|
100
|
+
|
101
|
+
def bucket_name
|
102
|
+
@bucket
|
103
|
+
end
|
104
|
+
|
105
|
+
def s3_host_alias
|
106
|
+
@s3_host_alias
|
107
|
+
end
|
108
|
+
|
109
|
+
def parse_credentials creds
|
110
|
+
creds = find_credentials(creds).stringify_keys
|
111
|
+
(creds[Rails.env] || creds).symbolize_keys
|
112
|
+
end
|
113
|
+
|
114
|
+
def exists?(style = default_style)
|
115
|
+
if original_filename
|
116
|
+
AWS::S3::S3Object.exists?(path(style), bucket_name)
|
117
|
+
else
|
118
|
+
false
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
def s3_protocol
|
123
|
+
@s3_protocol
|
124
|
+
end
|
125
|
+
|
126
|
+
# Returns representation of the data of the file assigned to the given
|
127
|
+
# style, in the format most representative of the current storage.
|
128
|
+
def to_file style = default_style
|
129
|
+
return @queued_for_write[style] if @queued_for_write[style]
|
130
|
+
filename = path(style).split(".")
|
131
|
+
extname = File.extname(filename)
|
132
|
+
basename = File.basename(filename, extname)
|
133
|
+
file = Tempfile.new(basename, extname)
|
134
|
+
file.write(AWS::S3::S3Object.value(path(style), bucket_name))
|
135
|
+
file.rewind
|
136
|
+
return file
|
137
|
+
end
|
138
|
+
|
139
|
+
def create_bucket
|
140
|
+
AWS::S3::Bucket.create(bucket_name)
|
141
|
+
end
|
142
|
+
|
143
|
+
def flush_writes #:nodoc:
|
144
|
+
@queued_for_write.each do |style, file|
|
145
|
+
begin
|
146
|
+
log("saving #{path(style)}")
|
147
|
+
AWS::S3::S3Object.store(path(style),
|
148
|
+
file,
|
149
|
+
bucket_name,
|
150
|
+
{:content_type => instance_read(:content_type),
|
151
|
+
:access => @s3_permissions,
|
152
|
+
}.merge(@s3_headers))
|
153
|
+
rescue AWS::S3::NoSuchBucket => e
|
154
|
+
create_bucket
|
155
|
+
retry
|
156
|
+
rescue AWS::S3::ResponseError => e
|
157
|
+
raise
|
158
|
+
end
|
159
|
+
end
|
160
|
+
@queued_for_write = {}
|
161
|
+
end
|
162
|
+
|
163
|
+
def flush_deletes #:nodoc:
|
164
|
+
@queued_for_delete.each do |path|
|
165
|
+
begin
|
166
|
+
log("deleting #{path}")
|
167
|
+
AWS::S3::S3Object.delete(path, bucket_name)
|
168
|
+
rescue AWS::S3::ResponseError
|
169
|
+
# Ignore this.
|
170
|
+
end
|
171
|
+
end
|
172
|
+
@queued_for_delete = []
|
173
|
+
end
|
174
|
+
|
175
|
+
def find_credentials creds
|
176
|
+
case creds
|
177
|
+
when File
|
178
|
+
YAML::load(ERB.new(File.read(creds.path)).result)
|
179
|
+
when String, Pathname
|
180
|
+
YAML::load(ERB.new(File.read(creds)).result)
|
181
|
+
when Hash
|
182
|
+
creds
|
183
|
+
else
|
184
|
+
raise ArgumentError, "Credentials are not a path, file, or hash."
|
185
|
+
end
|
186
|
+
end
|
187
|
+
private :find_credentials
|
188
|
+
|
189
|
+
end
|
190
|
+
end
|
191
|
+
end
|
data/lib/paperclip/thumbnail.rb
CHANGED
@@ -45,19 +45,20 @@ module Paperclip
|
|
45
45
|
# that contains the new image.
|
46
46
|
def make
|
47
47
|
src = @file
|
48
|
-
dst = Tempfile.new([@basename, @format
|
48
|
+
dst = Tempfile.new([@basename, @format ? ".#{@format}" : ''])
|
49
49
|
dst.binmode
|
50
50
|
|
51
51
|
begin
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
].flatten.compact
|
52
|
+
parameters = []
|
53
|
+
parameters << source_file_options
|
54
|
+
parameters << ":source"
|
55
|
+
parameters << transformation_command
|
56
|
+
parameters << convert_options
|
57
|
+
parameters << ":dest"
|
59
58
|
|
60
|
-
|
59
|
+
parameters = parameters.flatten.compact.join(" ").strip.squeeze(" ")
|
60
|
+
|
61
|
+
success = Paperclip.run("convert", parameters, :source => "#{File.expand_path(src.path)}[0]", :dest => File.expand_path(dst.path))
|
61
62
|
rescue PaperclipCommandLineError => e
|
62
63
|
raise PaperclipError, "There was an error processing the thumbnail for #{@basename}" if @whiny
|
63
64
|
end
|
@@ -70,8 +71,8 @@ module Paperclip
|
|
70
71
|
def transformation_command
|
71
72
|
scale, crop = @current_geometry.transformation_to(@target_geometry, crop?)
|
72
73
|
trans = []
|
73
|
-
trans << "-resize" << scale unless scale.nil? || scale.empty?
|
74
|
-
trans << "-crop" << crop << "+repage" if crop
|
74
|
+
trans << "-resize" << %["#{scale}"] unless scale.nil? || scale.empty?
|
75
|
+
trans << "-crop" << %["#{crop}"] << "+repage" if crop
|
75
76
|
trans
|
76
77
|
end
|
77
78
|
end
|
data/lib/paperclip/upfile.rb
CHANGED
@@ -17,7 +17,7 @@ module Paperclip
|
|
17
17
|
when "csv", "xml", "css" then "text/#{type}"
|
18
18
|
else
|
19
19
|
# On BSDs, `file` doesn't give a result code of 1 if the file doesn't exist.
|
20
|
-
content_type = (Paperclip.run("file", "--mime-type", self.path).split(':').last.strip rescue "application/x-#{type}")
|
20
|
+
content_type = (Paperclip.run("file", "-b --mime-type :file", :file => self.path).split(':').last.strip rescue "application/x-#{type}")
|
21
21
|
content_type = "application/x-#{type}" if content_type.match(/\(.*?\)/)
|
22
22
|
content_type
|
23
23
|
end
|
@@ -32,18 +32,26 @@ module Paperclip
|
|
32
32
|
def size
|
33
33
|
File.size(self)
|
34
34
|
end
|
35
|
+
|
36
|
+
# Returns the hash of the file.
|
37
|
+
def fingerprint
|
38
|
+
Digest::MD5.hexdigest(self.read)
|
39
|
+
end
|
35
40
|
end
|
36
41
|
end
|
37
42
|
|
38
43
|
if defined? StringIO
|
39
44
|
class StringIO
|
40
|
-
attr_accessor :original_filename, :content_type
|
45
|
+
attr_accessor :original_filename, :content_type, :fingerprint
|
41
46
|
def original_filename
|
42
47
|
@original_filename ||= "stringio.txt"
|
43
48
|
end
|
44
49
|
def content_type
|
45
50
|
@content_type ||= "text/plain"
|
46
51
|
end
|
52
|
+
def fingerprint
|
53
|
+
@fingerprint ||= Digest::MD5.hexdigest(self.string)
|
54
|
+
end
|
47
55
|
end
|
48
56
|
end
|
49
57
|
|
data/lib/paperclip/version.rb
CHANGED
data/shoulda_macros/paperclip.rb
CHANGED
data/test/attachment_test.rb
CHANGED
@@ -338,9 +338,29 @@ class AttachmentTest < Test::Unit::TestCase
|
|
338
338
|
end
|
339
339
|
end
|
340
340
|
|
341
|
+
should "include the filesystem module when loading the filesystem storage" do
|
342
|
+
rebuild_model :storage => :filesystem
|
343
|
+
@dummy = Dummy.new
|
344
|
+
assert @dummy.avatar.is_a?(Paperclip::Storage::Filesystem)
|
345
|
+
end
|
346
|
+
|
347
|
+
should "include the filesystem module even if capitalization is wrong" do
|
348
|
+
rebuild_model :storage => :FileSystem
|
349
|
+
@dummy = Dummy.new
|
350
|
+
assert @dummy.avatar.is_a?(Paperclip::Storage::Filesystem)
|
351
|
+
end
|
352
|
+
|
353
|
+
should "raise an error if you try to include a storage module that doesn't exist" do
|
354
|
+
rebuild_model :storage => :not_here
|
355
|
+
@dummy = Dummy.new
|
356
|
+
assert_raises(Paperclip::StorageMethodNotFound) do
|
357
|
+
@dummy.avatar
|
358
|
+
end
|
359
|
+
end
|
360
|
+
|
341
361
|
context "An attachment with styles but no processors defined" do
|
342
362
|
setup do
|
343
|
-
rebuild_model :processors => [], :styles => {:something => 1}
|
363
|
+
rebuild_model :processors => [], :styles => {:something => '1'}
|
344
364
|
@dummy = Dummy.new
|
345
365
|
@file = StringIO.new("...")
|
346
366
|
end
|
@@ -446,6 +466,8 @@ class AttachmentTest < Test::Unit::TestCase
|
|
446
466
|
@not_file = mock
|
447
467
|
@tempfile = mock
|
448
468
|
@not_file.stubs(:nil?).returns(false)
|
469
|
+
@not_file.stubs(:fingerprint).returns('bd94545193321376b70136f8b223abf8')
|
470
|
+
@tempfile.stubs(:fingerprint).returns('bd94545193321376b70136f8b223abf8')
|
449
471
|
@not_file.expects(:size).returns(10)
|
450
472
|
@tempfile.expects(:size).returns(10)
|
451
473
|
@not_file.expects(:to_tempfile).returns(@tempfile)
|
@@ -656,7 +678,7 @@ class AttachmentTest < Test::Unit::TestCase
|
|
656
678
|
end
|
657
679
|
|
658
680
|
should "not be able to find the module" do
|
659
|
-
assert_raise(
|
681
|
+
assert_raise(Paperclip::StorageMethodNotFound){ Dummy.new.avatar }
|
660
682
|
end
|
661
683
|
end
|
662
684
|
end
|
@@ -754,5 +776,29 @@ class AttachmentTest < Test::Unit::TestCase
|
|
754
776
|
assert_equal @file.size, @dummy.avatar.size
|
755
777
|
end
|
756
778
|
end
|
779
|
+
|
780
|
+
context "and avatar_fingerprint column" do
|
781
|
+
setup do
|
782
|
+
ActiveRecord::Base.connection.add_column :dummies, :avatar_fingerprint, :string
|
783
|
+
rebuild_class
|
784
|
+
@dummy = Dummy.new
|
785
|
+
end
|
786
|
+
|
787
|
+
should "not error when assigned an attachment" do
|
788
|
+
assert_nothing_raised { @dummy.avatar = @file }
|
789
|
+
end
|
790
|
+
|
791
|
+
should "return the right value when sent #avatar_fingerprint" do
|
792
|
+
@dummy.avatar = @file
|
793
|
+
assert_equal 'aec488126c3b33c08a10c3fa303acf27', @dummy.avatar_fingerprint
|
794
|
+
end
|
795
|
+
|
796
|
+
should "return the right value when saved, reloaded, and sent #avatar_fingerprint" do
|
797
|
+
@dummy.avatar = @file
|
798
|
+
@dummy.save
|
799
|
+
@dummy = Dummy.find(@dummy.id)
|
800
|
+
assert_equal 'aec488126c3b33c08a10c3fa303acf27', @dummy.avatar_fingerprint
|
801
|
+
end
|
802
|
+
end
|
757
803
|
end
|
758
804
|
end
|
@@ -0,0 +1,133 @@
|
|
1
|
+
require 'test/helper'
|
2
|
+
|
3
|
+
class CommandLineTest < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
Paperclip::CommandLine.path = nil
|
6
|
+
File.stubs(:exist?).with("/dev/null").returns(true)
|
7
|
+
end
|
8
|
+
|
9
|
+
should "take a command and parameters and produce a shell command for bash" do
|
10
|
+
cmd = Paperclip::CommandLine.new("convert", "a.jpg b.png", :swallow_stderr => false)
|
11
|
+
assert_equal "convert a.jpg b.png", cmd.command
|
12
|
+
end
|
13
|
+
|
14
|
+
should "be able to set a path and produce commands with that path" do
|
15
|
+
Paperclip::CommandLine.path = "/opt/bin"
|
16
|
+
cmd = Paperclip::CommandLine.new("convert", "a.jpg b.png", :swallow_stderr => false)
|
17
|
+
assert_equal "/opt/bin/convert a.jpg b.png", cmd.command
|
18
|
+
end
|
19
|
+
|
20
|
+
should "be able to interpolate quoted variables into the parameters" do
|
21
|
+
cmd = Paperclip::CommandLine.new("convert",
|
22
|
+
":one :{two}",
|
23
|
+
:one => "a.jpg",
|
24
|
+
:two => "b.png",
|
25
|
+
:swallow_stderr => false)
|
26
|
+
assert_equal "convert 'a.jpg' 'b.png'", cmd.command
|
27
|
+
end
|
28
|
+
|
29
|
+
should "quote command line options differently if we're on windows" do
|
30
|
+
File.stubs(:exist?).with("/dev/null").returns(false)
|
31
|
+
cmd = Paperclip::CommandLine.new("convert",
|
32
|
+
":one :{two}",
|
33
|
+
:one => "a.jpg",
|
34
|
+
:two => "b.png",
|
35
|
+
:swallow_stderr => false)
|
36
|
+
assert_equal 'convert "a.jpg" "b.png"', cmd.command
|
37
|
+
end
|
38
|
+
|
39
|
+
should "be able to quote and interpolate dangerous variables" do
|
40
|
+
cmd = Paperclip::CommandLine.new("convert",
|
41
|
+
":one :two",
|
42
|
+
:one => "`rm -rf`.jpg",
|
43
|
+
:two => "ha'ha.png",
|
44
|
+
:swallow_stderr => false)
|
45
|
+
assert_equal "convert '`rm -rf`.jpg' 'ha'\\''ha.png'", cmd.command
|
46
|
+
end
|
47
|
+
|
48
|
+
should "be able to quote and interpolate dangerous variables even on windows" do
|
49
|
+
File.stubs(:exist?).with("/dev/null").returns(false)
|
50
|
+
cmd = Paperclip::CommandLine.new("convert",
|
51
|
+
":one :two",
|
52
|
+
:one => "`rm -rf`.jpg",
|
53
|
+
:two => "ha'ha.png",
|
54
|
+
:swallow_stderr => false)
|
55
|
+
assert_equal %{convert "`rm -rf`.jpg" "ha'ha.png"}, cmd.command
|
56
|
+
end
|
57
|
+
|
58
|
+
should "add redirection to get rid of stderr in bash" do
|
59
|
+
File.stubs(:exist?).with("/dev/null").returns(true)
|
60
|
+
cmd = Paperclip::CommandLine.new("convert",
|
61
|
+
"a.jpg b.png",
|
62
|
+
:swallow_stderr => true)
|
63
|
+
|
64
|
+
assert_equal "convert a.jpg b.png 2>/dev/null", cmd.command
|
65
|
+
end
|
66
|
+
|
67
|
+
should "add redirection to get rid of stderr in cmd.exe" do
|
68
|
+
File.stubs(:exist?).with("/dev/null").returns(false)
|
69
|
+
cmd = Paperclip::CommandLine.new("convert",
|
70
|
+
"a.jpg b.png",
|
71
|
+
:swallow_stderr => true)
|
72
|
+
|
73
|
+
assert_equal "convert a.jpg b.png 2>NUL", cmd.command
|
74
|
+
end
|
75
|
+
|
76
|
+
should "raise if trying to interpolate :swallow_stderr or :expected_outcodes" do
|
77
|
+
cmd = Paperclip::CommandLine.new("convert",
|
78
|
+
":swallow_stderr :expected_outcodes",
|
79
|
+
:swallow_stderr => false,
|
80
|
+
:expected_outcodes => [0, 1])
|
81
|
+
assert_raise(Paperclip::PaperclipCommandLineError) do
|
82
|
+
cmd.command
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
should "run the #command it's given and return the output" do
|
87
|
+
cmd = Paperclip::CommandLine.new("convert", "a.jpg b.png", :swallow_stderr => false)
|
88
|
+
cmd.class.stubs(:"`").with("convert a.jpg b.png").returns(:correct_value)
|
89
|
+
with_exitstatus_returning(0) do
|
90
|
+
assert_equal :correct_value, cmd.run
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
should "raise a PaperclipCommandLineError if the result code isn't expected" do
|
95
|
+
cmd = Paperclip::CommandLine.new("convert", "a.jpg b.png", :swallow_stderr => false)
|
96
|
+
cmd.class.stubs(:"`").with("convert a.jpg b.png").returns(:correct_value)
|
97
|
+
with_exitstatus_returning(1) do
|
98
|
+
assert_raises(Paperclip::PaperclipCommandLineError) do
|
99
|
+
cmd.run
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
should "not raise a PaperclipCommandLineError if the result code is expected" do
|
105
|
+
cmd = Paperclip::CommandLine.new("convert",
|
106
|
+
"a.jpg b.png",
|
107
|
+
:expected_outcodes => [0, 1],
|
108
|
+
:swallow_stderr => false)
|
109
|
+
cmd.class.stubs(:"`").with("convert a.jpg b.png").returns(:correct_value)
|
110
|
+
with_exitstatus_returning(1) do
|
111
|
+
assert_nothing_raised do
|
112
|
+
cmd.run
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
should "log the command" do
|
118
|
+
cmd = Paperclip::CommandLine.new("convert", "a.jpg b.png", :swallow_stderr => false)
|
119
|
+
cmd.class.stubs(:'`')
|
120
|
+
Paperclip.expects(:log).with("convert a.jpg b.png")
|
121
|
+
cmd.run
|
122
|
+
end
|
123
|
+
|
124
|
+
should "detect that the system is unix or windows based on presence of /dev/null" do
|
125
|
+
File.stubs(:exist?).returns(true)
|
126
|
+
assert Paperclip::CommandLine.unix?
|
127
|
+
end
|
128
|
+
|
129
|
+
should "detect that the system is not unix or windows based on absence of /dev/null" do
|
130
|
+
File.stubs(:exist?).returns(false)
|
131
|
+
assert ! Paperclip::CommandLine.unix?
|
132
|
+
end
|
133
|
+
end
|