paperclip 2.8.0 → 3.0.2

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.

Files changed (94) hide show
  1. data/.gitignore +1 -0
  2. data/.travis.yml +9 -7
  3. data/Appraisals +6 -12
  4. data/Gemfile +2 -0
  5. data/NEWS +24 -0
  6. data/README.md +53 -21
  7. data/Rakefile +7 -2
  8. data/UPGRADING +14 -0
  9. data/features/basic_integration.feature +8 -8
  10. data/features/rake_tasks.feature +1 -1
  11. data/features/step_definitions/attachment_steps.rb +11 -2
  12. data/features/step_definitions/rails_steps.rb +17 -79
  13. data/features/support/env.rb +3 -0
  14. data/features/support/file_helpers.rb +24 -0
  15. data/features/support/rails.rb +3 -3
  16. data/gemfiles/{rails3_1.gemfile → 3.0.gemfile} +3 -1
  17. data/gemfiles/{rails2.gemfile → 3.1.gemfile} +3 -1
  18. data/gemfiles/{rails3.gemfile → 3.2.gemfile} +3 -1
  19. data/images.rake +21 -0
  20. data/lib/generators/paperclip/paperclip_generator.rb +1 -2
  21. data/lib/paperclip.rb +48 -319
  22. data/lib/paperclip/attachment.rb +33 -81
  23. data/lib/paperclip/attachment_options.rb +0 -1
  24. data/lib/paperclip/callbacks.rb +30 -0
  25. data/lib/paperclip/errors.rb +27 -0
  26. data/lib/paperclip/geometry.rb +6 -4
  27. data/lib/paperclip/glue.rb +15 -0
  28. data/lib/paperclip/helpers.rb +71 -0
  29. data/lib/paperclip/instance_methods.rb +35 -0
  30. data/lib/paperclip/interpolations.rb +2 -2
  31. data/lib/paperclip/io_adapters/attachment_adapter.rb +62 -0
  32. data/lib/paperclip/io_adapters/file_adapter.rb +81 -0
  33. data/lib/paperclip/io_adapters/identity_adapter.rb +12 -0
  34. data/lib/paperclip/io_adapters/nil_adapter.rb +34 -0
  35. data/lib/paperclip/io_adapters/registry.rb +32 -0
  36. data/lib/paperclip/io_adapters/stringio_adapter.rb +64 -0
  37. data/lib/paperclip/io_adapters/uploaded_file_adapter.rb +63 -0
  38. data/lib/paperclip/locales/en.yml +17 -0
  39. data/lib/paperclip/logger.rb +21 -0
  40. data/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb +1 -1
  41. data/lib/paperclip/matchers/validate_attachment_presence_matcher.rb +2 -2
  42. data/lib/paperclip/matchers/validate_attachment_size_matcher.rb +7 -7
  43. data/lib/paperclip/processor.rb +32 -17
  44. data/lib/paperclip/railtie.rb +10 -15
  45. data/lib/paperclip/storage/filesystem.rb +5 -14
  46. data/lib/paperclip/storage/fog.rb +2 -21
  47. data/lib/paperclip/storage/s3.rb +12 -29
  48. data/lib/paperclip/tempfile.rb +41 -0
  49. data/lib/paperclip/thumbnail.rb +2 -3
  50. data/lib/paperclip/validators.rb +45 -0
  51. data/lib/paperclip/validators/attachment_content_type_validator.rb +47 -0
  52. data/lib/paperclip/validators/attachment_presence_validator.rb +26 -0
  53. data/lib/paperclip/validators/attachment_size_validator.rb +102 -0
  54. data/lib/paperclip/version.rb +1 -1
  55. data/lib/tasks/paperclip.rake +3 -11
  56. data/paperclip.gemspec +15 -5
  57. data/test/adapter_registry_test.rb +32 -0
  58. data/test/attachment_adapter_test.rb +48 -0
  59. data/test/attachment_options_test.rb +0 -13
  60. data/test/attachment_test.rb +27 -55
  61. data/test/file_adapter_test.rb +43 -0
  62. data/test/generator_test.rb +78 -0
  63. data/test/geometry_test.rb +5 -5
  64. data/test/helper.rb +9 -11
  65. data/test/identity_adapter_test.rb +8 -0
  66. data/test/integration_test.rb +39 -94
  67. data/test/interpolations_test.rb +8 -1
  68. data/test/matchers/validate_attachment_size_matcher_test.rb +16 -2
  69. data/test/nil_adapter_test.rb +25 -0
  70. data/test/paperclip_test.rb +30 -189
  71. data/test/storage/filesystem_test.rb +0 -14
  72. data/test/storage/fog_test.rb +0 -14
  73. data/test/storage/s3_live_test.rb +22 -9
  74. data/test/storage/s3_test.rb +70 -34
  75. data/test/stringio_adapter_test.rb +42 -0
  76. data/test/style_test.rb +10 -16
  77. data/test/thumbnail_test.rb +16 -10
  78. data/test/uploaded_file_adapter_test.rb +98 -0
  79. data/test/validators/attachment_content_type_validator_test.rb +140 -0
  80. data/test/validators/attachment_presence_validator_test.rb +85 -0
  81. data/test/validators/attachment_size_validator_test.rb +207 -0
  82. data/test/validators_test.rb +25 -0
  83. metadata +152 -30
  84. data/gemfiles/rails3_2.gemfile +0 -9
  85. data/generators/paperclip/USAGE +0 -5
  86. data/generators/paperclip/paperclip_generator.rb +0 -27
  87. data/generators/paperclip/templates/paperclip_migration.rb.erb +0 -19
  88. data/init.rb +0 -4
  89. data/lib/paperclip/callback_compatibility.rb +0 -61
  90. data/lib/paperclip/iostream.rb +0 -45
  91. data/lib/paperclip/upfile.rb +0 -64
  92. data/rails/init.rb +0 -2
  93. data/test/iostream_test.rb +0 -71
  94. data/test/upfile_test.rb +0 -53
data/init.rb DELETED
@@ -1,4 +0,0 @@
1
- require File.join(File.dirname(__FILE__), "lib", "paperclip")
2
- require 'paperclip/railtie'
3
-
4
- Paperclip::Railtie.insert
@@ -1,61 +0,0 @@
1
- module Paperclip
2
- module CallbackCompatability
3
- module Rails21
4
- def self.included(base)
5
- base.extend(Defining)
6
- base.send(:include, Running)
7
- end
8
-
9
- module Defining
10
- def define_paperclip_callbacks(*args)
11
- args.each do |callback|
12
- define_callbacks("before_#{callback}")
13
- define_callbacks("after_#{callback}")
14
- end
15
- end
16
- end
17
-
18
- module Running
19
- def run_paperclip_callbacks(callback, opts = nil, &blk)
20
- # The overall structure of this isn't ideal since after callbacks run even if
21
- # befores return false. But this is how rails 3's callbacks work, unfortunately.
22
- if run_callbacks(:"before_#{callback}"){ |result, object| result == false } != false
23
- blk.call
24
- end
25
- run_callbacks(:"after_#{callback}"){ |result, object| result == false }
26
- end
27
- end
28
- end
29
-
30
- module Rails3
31
- def self.included(base)
32
- base.extend(Defining)
33
- base.send(:include, Running)
34
- end
35
-
36
- module Defining
37
- def define_paperclip_callbacks(*callbacks)
38
- define_callbacks *[callbacks, {:terminator => "result == false"}].flatten
39
- callbacks.each do |callback|
40
- eval <<-end_callbacks
41
- def before_#{callback}(*args, &blk)
42
- set_callback(:#{callback}, :before, *args, &blk)
43
- end
44
- def after_#{callback}(*args, &blk)
45
- set_callback(:#{callback}, :after, *args, &blk)
46
- end
47
- end_callbacks
48
- end
49
- end
50
- end
51
-
52
- module Running
53
- def run_paperclip_callbacks(callback, opts = nil, &block)
54
- run_callbacks(callback, opts, &block)
55
- end
56
- end
57
-
58
- end
59
-
60
- end
61
- end
@@ -1,45 +0,0 @@
1
- # Provides method that can be included on File-type objects (IO, StringIO, Tempfile, etc) to allow stream copying
2
- # and Tempfile conversion.
3
- module IOStream
4
- # Returns a Tempfile containing the contents of the readable object.
5
- def to_tempfile(object)
6
- return object.to_tempfile if object.respond_to?(:to_tempfile)
7
- name = object.respond_to?(:original_filename) ? object.original_filename : (object.respond_to?(:path) ? object.path : "stream")
8
- tempfile = Paperclip::Tempfile.new(["stream", File.extname(name)])
9
- tempfile.binmode
10
- stream_to(object, tempfile)
11
- end
12
-
13
- # Copies one read-able object from one place to another in blocks, obviating the need to load
14
- # the whole thing into memory. Defaults to 8k blocks. Returns a File if a String is passed
15
- # in as the destination and returns the IO or Tempfile as passed in if one is sent as the destination.
16
- def stream_to object, path_or_file, in_blocks_of = 8192
17
- dstio = case path_or_file
18
- when String then File.new(path_or_file, "wb+")
19
- when IO then path_or_file
20
- when Tempfile then path_or_file
21
- end
22
- buffer = ""
23
- object.rewind
24
- while object.read(in_blocks_of, buffer) do
25
- dstio.write(buffer)
26
- end
27
- dstio.rewind
28
- dstio
29
- end
30
- end
31
-
32
- # Corrects a bug in Windows when asking for Tempfile size.
33
- if defined?(Tempfile) && RUBY_PLATFORM !~ /java/
34
- class Tempfile
35
- def size
36
- if @tmpfile
37
- @tmpfile.fsync
38
- @tmpfile.flush
39
- @tmpfile.stat.size
40
- else
41
- 0
42
- end
43
- end
44
- end
45
- end
@@ -1,64 +0,0 @@
1
- require 'mime/types'
2
-
3
- module Paperclip
4
- # The Upfile module is a convenience module for adding uploaded-file-type methods
5
- # to the +File+ class. Useful for testing.
6
- # user.avatar = File.new("test/test_avatar.jpg")
7
- module Upfile
8
- # Infer the MIME-type of the file from the extension.
9
- def content_type
10
- types = MIME::Types.type_for(self.original_filename)
11
- if types.length == 0
12
- type_from_file_command
13
- elsif types.length == 1
14
- types.first.content_type
15
- else
16
- iterate_over_array_to_find_best_option(types)
17
- end
18
- end
19
-
20
- def iterate_over_array_to_find_best_option(types)
21
- types.reject {|type| type.content_type.match(/\/x-/) }.first
22
- end
23
-
24
- def type_from_file_command
25
- # On BSDs, `file` doesn't give a result code of 1 if the file doesn't exist.
26
- type = (self.original_filename.match(/\.(\w+)$/)[1] rescue "octet-stream").downcase
27
- mime_type = (Paperclip.run("file", "-b --mime :file", :file => self.path).split(/[:;]\s+/)[0] rescue "application/x-#{type}")
28
- mime_type = "application/x-#{type}" if mime_type.match(/\(.*?\)/)
29
- mime_type
30
- end
31
-
32
- # Returns the file's normal name.
33
- def original_filename
34
- File.basename(self.path)
35
- end
36
-
37
- # Returns the size of the file.
38
- def size
39
- File.size(self)
40
- end
41
- end
42
- end
43
-
44
- if defined? StringIO
45
- class StringIO
46
- attr_accessor :original_filename, :content_type, :fingerprint
47
-
48
- def original_filename
49
- @original_filename ||= "stringio.txt"
50
- end
51
-
52
- def content_type
53
- @content_type ||= "text/plain"
54
- end
55
-
56
- def fingerprint
57
- @fingerprint ||= Digest::MD5.hexdigest(self.string)
58
- end
59
- end
60
- end
61
-
62
- class File #:nodoc:
63
- include Paperclip::Upfile
64
- end
@@ -1,2 +0,0 @@
1
- require 'paperclip/railtie'
2
- Paperclip::Railtie.insert
@@ -1,71 +0,0 @@
1
- require './test/helper'
2
-
3
- class IOStreamTest < Test::Unit::TestCase
4
- include IOStream
5
- context "A file" do
6
- setup do
7
- @file = File.new(File.join(File.dirname(__FILE__), "fixtures", "5k.png"), 'rb')
8
- end
9
-
10
- teardown { @file.close }
11
-
12
- context "that is sent #stream_to" do
13
-
14
- context "and given a String" do
15
- setup do
16
- FileUtils.mkdir_p(File.join(ROOT, 'tmp'))
17
- assert @result = stream_to(@file, File.join(ROOT, 'tmp', 'iostream.string.test'))
18
- end
19
-
20
- should "return a File" do
21
- assert @result.is_a?(File)
22
- end
23
-
24
- should "contain the same data as the original file" do
25
- @file.rewind; @result.rewind
26
- assert_equal @file.read, @result.read
27
- end
28
- end
29
-
30
- context "and given a Tempfile" do
31
- setup do
32
- tempfile = Tempfile.new('iostream.test')
33
- tempfile.binmode
34
- assert @result = stream_to(@file, tempfile)
35
- end
36
-
37
- should "return a Tempfile" do
38
- assert @result.is_a?(Tempfile)
39
- end
40
-
41
- should "contain the same data as the original file" do
42
- @file.rewind; @result.rewind
43
- assert_equal @file.read, @result.read
44
- end
45
- end
46
-
47
- end
48
-
49
- context "that is converted #to_tempfile" do
50
- setup do
51
- assert @tempfile = to_tempfile(@file)
52
- end
53
-
54
- should "convert it to a Paperclip Tempfile" do
55
- assert @tempfile.is_a?(Paperclip::Tempfile)
56
- end
57
-
58
- should "have the name be based on the original_filename" do
59
- name = File.basename(@file.path)
60
- extension = File.extname(name)
61
- basename = File.basename(name, extension)
62
- assert_match %r[^stream.*?#{Regexp.quote(extension)}], File.basename(@tempfile.path)
63
- end
64
-
65
- should "have the Tempfile contain the same data as the file" do
66
- @file.rewind; @tempfile.rewind
67
- assert_equal @file.read, @tempfile.read
68
- end
69
- end
70
- end
71
- end
@@ -1,53 +0,0 @@
1
- require './test/helper'
2
-
3
- class UpfileTest < Test::Unit::TestCase
4
- { %w(jpg jpe jpeg) => 'image/jpeg',
5
- %w(tif tiff) => 'image/tiff',
6
- %w(png) => 'image/png',
7
- %w(gif) => 'image/gif',
8
- %w(bmp) => 'image/bmp',
9
- %w(svg) => 'image/svg+xml',
10
- %w(txt) => 'text/plain',
11
- %w(htm html) => 'text/html',
12
- %w(csv) => 'text/csv',
13
- %w(xml) => 'application/xml',
14
- %w(css) => 'text/css',
15
- %w(js) => 'application/javascript',
16
- %w(foo) => 'application/x-foo'
17
- }.each do |extensions, content_type|
18
- extensions.each do |extension|
19
- should "return a content_type of #{content_type} for a file with extension .#{extension}" do
20
- file = stub('file', :path => "basename.#{extension}")
21
- class << file
22
- include Paperclip::Upfile
23
- end
24
-
25
- assert_equal content_type, file.content_type
26
- end
27
- end
28
- end
29
-
30
- should "return a content_type of text/plain on a real file whose content_type is determined with the file command" do
31
- file = File.new(File.join(File.dirname(__FILE__), "..", "LICENSE"))
32
- class << file
33
- include Paperclip::Upfile
34
- end
35
- assert_equal 'text/plain', file.content_type
36
- end
37
-
38
- { '5k.png' => 'image/png',
39
- 'animated.gif' => 'image/gif',
40
- 'text.txt' => 'text/plain',
41
- 'twopage.pdf' => 'application/pdf'
42
- }.each do |filename, content_type|
43
- should "return a content type of #{content_type} from a file command for file #{filename}" do
44
- file = File.new(File.join(File.dirname(__FILE__), "fixtures", filename))
45
- class << file
46
- include Paperclip::Upfile
47
- end
48
-
49
- assert_equal content_type, file.type_from_file_command
50
- end
51
- end
52
-
53
- end