paperclip 5.0.0.beta2 → 5.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (89) hide show
  1. checksums.yaml +4 -4
  2. data/.codeclimate.yml +17 -0
  3. data/.hound.yml +5 -16
  4. data/.travis.yml +14 -15
  5. data/Appraisals +3 -23
  6. data/CONTRIBUTING.md +10 -4
  7. data/Gemfile +1 -0
  8. data/NEWS +78 -2
  9. data/README.md +175 -81
  10. data/Rakefile +1 -1
  11. data/UPGRADING +1 -1
  12. data/features/basic_integration.feature +2 -2
  13. data/features/step_definitions/attachment_steps.rb +6 -6
  14. data/features/step_definitions/rails_steps.rb +29 -22
  15. data/features/step_definitions/s3_steps.rb +1 -1
  16. data/features/support/env.rb +1 -0
  17. data/features/support/paths.rb +1 -1
  18. data/features/support/rails.rb +0 -24
  19. data/gemfiles/{4.2.awsv2.0.gemfile → 4.2.gemfile} +1 -1
  20. data/gemfiles/{5.0.awsv2.1.gemfile → 5.0.gemfile} +2 -2
  21. data/lib/generators/paperclip/paperclip_generator.rb +9 -1
  22. data/lib/generators/paperclip/templates/paperclip_migration.rb.erb +1 -1
  23. data/lib/paperclip.rb +13 -10
  24. data/lib/paperclip/attachment.rb +16 -6
  25. data/lib/paperclip/content_type_detector.rb +3 -2
  26. data/lib/paperclip/errors.rb +3 -1
  27. data/lib/paperclip/file_command_content_type_detector.rb +1 -1
  28. data/lib/paperclip/geometry_detector_factory.rb +2 -2
  29. data/lib/paperclip/helpers.rb +15 -12
  30. data/lib/paperclip/interpolations.rb +1 -1
  31. data/lib/paperclip/io_adapters/abstract_adapter.rb +29 -3
  32. data/lib/paperclip/io_adapters/attachment_adapter.rb +10 -5
  33. data/lib/paperclip/io_adapters/data_uri_adapter.rb +8 -8
  34. data/lib/paperclip/io_adapters/empty_string_adapter.rb +5 -4
  35. data/lib/paperclip/io_adapters/file_adapter.rb +12 -6
  36. data/lib/paperclip/io_adapters/http_url_proxy_adapter.rb +7 -7
  37. data/lib/paperclip/io_adapters/identity_adapter.rb +12 -6
  38. data/lib/paperclip/io_adapters/nil_adapter.rb +8 -5
  39. data/lib/paperclip/io_adapters/registry.rb +6 -2
  40. data/lib/paperclip/io_adapters/stringio_adapter.rb +9 -6
  41. data/lib/paperclip/io_adapters/uploaded_file_adapter.rb +10 -6
  42. data/lib/paperclip/io_adapters/uri_adapter.rb +41 -19
  43. data/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb +4 -4
  44. data/lib/paperclip/media_type_spoof_detector.rb +3 -2
  45. data/lib/paperclip/processor.rb +5 -4
  46. data/lib/paperclip/storage/filesystem.rb +13 -2
  47. data/lib/paperclip/storage/fog.rb +12 -7
  48. data/lib/paperclip/storage/s3.rb +46 -19
  49. data/lib/paperclip/thumbnail.rb +18 -8
  50. data/lib/paperclip/url_generator.rb +17 -13
  51. data/lib/paperclip/validators.rb +1 -1
  52. data/lib/paperclip/version.rb +3 -1
  53. data/lib/tasks/paperclip.rake +18 -4
  54. data/paperclip.gemspec +4 -5
  55. data/spec/paperclip/attachment_processing_spec.rb +2 -4
  56. data/spec/paperclip/attachment_spec.rb +40 -9
  57. data/spec/paperclip/content_type_detector_spec.rb +1 -1
  58. data/spec/paperclip/file_command_content_type_detector_spec.rb +15 -1
  59. data/spec/paperclip/io_adapters/abstract_adapter_spec.rb +76 -22
  60. data/spec/paperclip/io_adapters/attachment_adapter_spec.rb +6 -3
  61. data/spec/paperclip/io_adapters/data_uri_adapter_spec.rb +7 -1
  62. data/spec/paperclip/io_adapters/file_adapter_spec.rb +2 -2
  63. data/spec/paperclip/io_adapters/http_url_proxy_adapter_spec.rb +26 -6
  64. data/spec/paperclip/io_adapters/identity_adapter_spec.rb +1 -1
  65. data/spec/paperclip/io_adapters/registry_spec.rb +2 -2
  66. data/spec/paperclip/io_adapters/stringio_adapter_spec.rb +1 -1
  67. data/spec/paperclip/io_adapters/uploaded_file_adapter_spec.rb +5 -5
  68. data/spec/paperclip/io_adapters/uri_adapter_spec.rb +77 -7
  69. data/spec/paperclip/matchers/validate_attachment_content_type_matcher_spec.rb +10 -0
  70. data/spec/paperclip/media_type_spoof_detector_spec.rb +27 -3
  71. data/spec/paperclip/paperclip_spec.rb +13 -13
  72. data/spec/paperclip/processor_spec.rb +4 -4
  73. data/spec/paperclip/storage/fog_spec.rb +28 -0
  74. data/spec/paperclip/storage/s3_live_spec.rb +12 -10
  75. data/spec/paperclip/storage/s3_spec.rb +150 -39
  76. data/spec/paperclip/tempfile_spec.rb +35 -0
  77. data/spec/paperclip/thumbnail_spec.rb +38 -35
  78. data/spec/paperclip/url_generator_spec.rb +54 -43
  79. data/spec/paperclip/validators_spec.rb +3 -2
  80. data/spec/spec_helper.rb +3 -1
  81. data/spec/support/assertions.rb +5 -1
  82. data/spec/support/conditional_filter_helper.rb +5 -0
  83. data/spec/support/mock_attachment.rb +2 -0
  84. data/spec/support/mock_url_generator_builder.rb +2 -2
  85. metadata +37 -36
  86. data/gemfiles/4.2.awsv2.1.gemfile +0 -17
  87. data/gemfiles/4.2.awsv2.gemfile +0 -20
  88. data/gemfiles/5.0.awsv2.0.gemfile +0 -17
  89. data/gemfiles/5.0.awsv2.gemfile +0 -25
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f6fccc7d5627358582473c510bef269b25156565
4
- data.tar.gz: 07eb1f02126bb0022635083684eaf43ba049a466
3
+ metadata.gz: 823fa4ed80655c6ae50b1d241142e5afb3ed9e22
4
+ data.tar.gz: bb9ceacd62b0e1f6167f6d0c6392a696b788ca73
5
5
  SHA512:
6
- metadata.gz: 7bf6930cb7bd20a9dd477d1596f9a4ea6ec78108f079424ed4163125f86358275e164667597a6bedec27244435e749dbaf97fbff73a2df724eeb4174ec60ba6c
7
- data.tar.gz: ee22ab27c69ca3a529ba6b41fcbe6a4f580a16f46b31eae9e1427d13d0bf29bbbe01b65471ebc148c0c21f53213ad39ff2b74f60287b70671d7b676abd2b1d76
6
+ metadata.gz: 4e5b7a64e9db33be68a6a5b8467a8df6c074d16990e51c93bea5a070896b2ce3905ccdc0cad6323148229f3a03209974791a90234d948bb65f7f85868cab35f2
7
+ data.tar.gz: 376768f292bdc225375aee8dce8c22b58e22904dabbf5044a7a2383b760b362e7a39830e9c63091e5ff1774b49ecd6f1dfbfdb293f198c66f80d3d8bb1fe1987
data/.codeclimate.yml ADDED
@@ -0,0 +1,17 @@
1
+ ---
2
+ engines:
3
+ duplication:
4
+ enabled: true
5
+ config:
6
+ languages:
7
+ - ruby
8
+ fixme:
9
+ enabled: true
10
+ rubocop:
11
+ enabled: true
12
+ ratings:
13
+ paths:
14
+ - "**.rb"
15
+ exclude_paths:
16
+ - features/
17
+ - spec/
data/.hound.yml CHANGED
@@ -20,7 +20,8 @@ AllCops:
20
20
  - "db/schema.rb"
21
21
  - 'vendor/**/*'
22
22
  - 'gemfiles/vendor/**/*'
23
- RunRailsCops: false
23
+ Rails:
24
+ Enabled: false
24
25
  DisplayCopNames: false
25
26
  StyleGuideCopsOnly: false
26
27
  Style/AccessModifierIndentation:
@@ -434,7 +435,7 @@ Style/TrailingBlankLines:
434
435
  SupportedStyles:
435
436
  - final_newline
436
437
  - final_blank_line
437
- Style/TrailingComma:
438
+ Style/TrailingCommaInLiteral:
438
439
  Description: Checks for trailing comma in parameter lists and literals.
439
440
  StyleGuide: https://github.com/bbatsov/ruby-style-guide#no-trailing-array-commas
440
441
  Enabled: false
@@ -560,11 +561,6 @@ Rails/ActionFilter:
560
561
  - filter
561
562
  Include:
562
563
  - app/controllers/**/*.rb
563
- Rails/DefaultScope:
564
- Description: Checks if the argument passed to default_scope is a block.
565
- Enabled: true
566
- Include:
567
- - app/models/**/*.rb
568
564
  Rails/HasAndBelongsToMany:
569
565
  Description: Prefer has_many :through to has_and_belongs_to_many.
570
566
  Enabled: true
@@ -685,10 +681,6 @@ Style/DefWithParentheses:
685
681
  Description: Use def with parentheses when there are arguments.
686
682
  StyleGuide: https://github.com/bbatsov/ruby-style-guide#method-parens
687
683
  Enabled: true
688
- Style/DeprecatedHashMethods:
689
- Description: Checks for use of deprecated Hash methods.
690
- StyleGuide: https://github.com/bbatsov/ruby-style-guide#hash-key
691
- Enabled: false
692
684
  Style/Documentation:
693
685
  Description: Document classes and non-namespace modules.
694
686
  Enabled: false
@@ -840,7 +832,7 @@ Style/SelfAssignment:
840
832
  used.
841
833
  StyleGuide: https://github.com/bbatsov/ruby-style-guide#self-assignment
842
834
  Enabled: false
843
- Style/SingleSpaceBeforeFirstArg:
835
+ Style/SpaceBeforeFirstArg:
844
836
  Description: Checks that exactly one space is used between a method name and the
845
837
  first argument for method calls without parentheses.
846
838
  Enabled: true
@@ -852,7 +844,7 @@ Style/SpaceAfterComma:
852
844
  Description: Use spaces after commas.
853
845
  StyleGuide: https://github.com/bbatsov/ruby-style-guide#spaces-operators
854
846
  Enabled: true
855
- Style/SpaceAfterControlKeyword:
847
+ Style/SpaceAroundKeyword:
856
848
  Description: Use spaces after if/elsif/unless/while/until/case/when.
857
849
  Enabled: true
858
850
  Style/SpaceAfterMethodName:
@@ -881,9 +873,6 @@ Style/SpaceAroundOperators:
881
873
  Description: Use spaces around operators.
882
874
  StyleGuide: https://github.com/bbatsov/ruby-style-guide#spaces-operators
883
875
  Enabled: true
884
- Style/SpaceBeforeModifierKeyword:
885
- Description: Put a space before the modifier keyword.
886
- Enabled: true
887
876
  Style/SpaceInsideBrackets:
888
877
  Description: No spaces after [ or before ].
889
878
  StyleGuide: https://github.com/bbatsov/ruby-style-guide#no-spaces-braces
data/.travis.yml CHANGED
@@ -1,26 +1,25 @@
1
+ language: ruby
2
+ sudo: false
3
+
1
4
  rvm:
2
5
  - 2.1
3
- - 2.2.2
6
+ - 2.2
7
+ - 2.3
8
+ - 2.4
4
9
 
5
10
  script: "bundle exec rake clean spec cucumber"
6
11
 
12
+ addons:
13
+ apt:
14
+ packages:
15
+ - ghostscript
16
+
7
17
  gemfile:
8
- - gemfiles/4.2.awsv2.0.gemfile
9
- - gemfiles/4.2.awsv2.1.gemfile
10
- - gemfiles/5.0.awsv2.0.gemfile
11
- - gemfiles/5.0.awsv2.1.gemfile
18
+ - gemfiles/4.2.gemfile
19
+ - gemfiles/5.0.gemfile
12
20
 
13
21
  matrix:
14
22
  fast_finish: true
15
23
  exclude:
16
- - gemfile: gemfiles/5.0.awsv2.0.gemfile
17
- rvm: 2.0
18
- - gemfile: gemfiles/5.0.awsv2.1.gemfile
19
- rvm: 2.0
20
- - gemfile: gemfiles/5.0.awsv2.0.gemfile
24
+ - gemfile: gemfiles/5.0.gemfile
21
25
  rvm: 2.1
22
- - gemfile: gemfiles/5.0.awsv2.1.gemfile
23
- rvm: 2.1
24
-
25
- sudo: false
26
- cache: bundler
data/Appraisals CHANGED
@@ -1,27 +1,7 @@
1
- appraise "4.2.awsv2.0" do
1
+ appraise "4.2" do
2
2
  gem "rails", "~> 4.2.0"
3
- gem "aws-sdk", "~> 2.0.0"
4
3
  end
5
4
 
6
- appraise "4.2.awsv2.1" do
7
- gem "rails", "~> 4.2.0"
8
- gem "aws-sdk", "~> 2.1.0"
9
-
10
- group :development, :test do
11
- gem 'mime-types', '>= 1.16', '< 4'
12
- end
13
- end
14
-
15
- appraise "5.0.awsv2.0" do
16
- gem "rails", "5.0.0.beta3"
17
- gem "aws-sdk", "~> 2.0.0"
18
-
19
- group :development, :test do
20
- gem 'mime-types', '>= 1.16', '< 4'
21
- end
22
- end
23
-
24
- appraise "5.0.awsv2.1" do
25
- gem "rails", "5.0.0.beta3"
26
- gem "aws-sdk", "~> 2.1.0"
5
+ appraise "5.0" do
6
+ gem "rails", "~> 5.0.0"
27
7
  end
data/CONTRIBUTING.md CHANGED
@@ -10,16 +10,22 @@ Here's a quick guide for contributing:
10
10
 
11
11
  1. Fork the repo.
12
12
 
13
- 2. Run the tests. We only take pull requests with passing tests, and it's great
13
+ 1. Make sure you have ImageMagick and Ghostscript installed. See [this section]
14
+ (./README.md#image-processor) of the README.
15
+
16
+ 1. Run the tests. We only take pull requests with passing tests, and it's great
14
17
  to know that you have a clean slate: `bundle && bundle exec rake`
15
18
 
16
- 3. Add a test for your change. Only refactoring and documentation changes
19
+ 1. Add a test for your change. Only refactoring and documentation changes
17
20
  require no new tests. If you are adding functionality or fixing a bug, we need
18
21
  a test!
19
22
 
20
- 4. Make the test pass.
23
+ 1. Make the test pass.
24
+
25
+ 1. Mention how your changes affect the project to other developers and users in
26
+ the `NEWS.md` file.
21
27
 
22
- 5. Push to your fork and submit a pull request.
28
+ 1. Push to your fork and submit a pull request.
23
29
 
24
30
  At this point you're waiting on us. We like to at least comment on, if not
25
31
  accept, pull requests within seven business days (most of the work on Paperclip
data/Gemfile CHANGED
@@ -12,4 +12,5 @@ group :development, :test do
12
12
  gem 'mime-types'
13
13
  gem 'builder'
14
14
  gem 'rubocop', require: false
15
+ gem 'rspec'
15
16
  end
data/NEWS CHANGED
@@ -1,12 +1,88 @@
1
- 5.0.0.beta2 (2015-04-01):
1
+ 5.3.0 (2018-03-09):
2
+
3
+ * Improvement: Use `FactoryBot` instead of `FactoryGirl` (https://github.com/thoughtbot/paperclip/pull/2501)
4
+ * Improvement: README updates (https://github.com/thoughtbot/paperclip/pull/2411, https://github.com/thoughtbot/paperclip/pull/2433, https://github.com/thoughtbot/paperclip/pull/2374, https://github.com/thoughtbot/paperclip/pull/2417, https://github.com/thoughtbot/paperclip/pull/2536)
5
+ * Improvement: Remove Ruby 2.4 deprecation warning (https://github.com/thoughtbot/paperclip/pull/2401)
6
+ * Improvement: Rails 5 migration compatibility (https://github.com/thoughtbot/paperclip/pull/2470)
7
+ * Improvement: Documentation around post processing (https://github.com/thoughtbot/paperclip/pull/2381)
8
+ * Improvement: S3 hostname example documentation (https://github.com/thoughtbot/paperclip/pull/2379)
9
+ * Bugfix: Allow paperclip to load in IRB (https://github.com/thoughtbot/paperclip/pull/2369)
10
+ * Bugfix: MIME type detection (https://github.com/thoughtbot/paperclip/issues/2527)
11
+ * Bugfix: Bad tempfile state after symlink failure (https://github.com/thoughtbot/paperclip/pull/2540)
12
+ * Bugfix: Rewind file after Fog bucket creation (https://github.com/thoughtbot/paperclip/pull/2572)
13
+ * Improvement: Use `Terrapin` instead of `Cocaine` (https://github.com/thoughtbot/paperclip/pull/2553)
14
+
15
+ 5.2.1 (2018-01-25):
16
+
17
+ * Bugfix: Fix copying files on Windows. (#2532)
18
+
19
+ 5.2.0 (2018-01-23):
20
+
21
+ * Security: Remove the automatic loading of URI adapters. Some of these
22
+ adapters can be specially crafted to expose your network topology. (#2435)
23
+ * Bugfix: The rake task no longer rescues `Exception`. (#2476)
24
+ * Bugfix: Handle malformed `Content-Disposition` headers (#2283)
25
+ * Bugfix: The `:only_process` option works when passed a lambda again. (#2289)
26
+ * Improvement: Added `:use_accelerate_endpoint` option when using S3 to enable
27
+ [Amazon S3 Transfer Acceleration](http://docs.aws.amazon.com/AmazonS3/latest/dev/transfer-acceleration.html)
28
+ (#2291)
29
+ * Improvement: Make the fingerprint digest configurable per attachment. The
30
+ default remains MD5. Making this configurable means it can change in a future
31
+ version because it is not considered secure anymore against intentional file
32
+ corruption. For more info, see https://en.wikipedia.org/wiki/MD5#Security
33
+
34
+ You can change the digest used for an attachment by adding the
35
+ `:adapter_options` parameter to the `has_attached_file` options like this:
36
+ `has_attached_file :avatar, adapter_options: { hash_digest: Digest::SHA256 }`
37
+
38
+ Use the rake task to regenerate fingerprints with the new digest for a given
39
+ class. Note that this does **not** check the file integrity using the old
40
+ fingerprint. Run the following command to regenerate fingerprints for all
41
+ User attachments:
42
+ `CLASS=User rake paperclip:refresh:fingerprints`
43
+ You can optionally limit the attachment that will be processed, e.g:
44
+ `CLASS=User ATTACHMENT=avatar rake paperclip:refresh:fingerprints` (#2229)
45
+ * Improvement: The new `frame_index` option on the thumbnail processor allows
46
+ you to select a specific frame from an animated upload to use as a thumbnail.
47
+ Initial support is for mkv, avi, MP4, mov, MPEG, and GIF. (#2155)
48
+ * Improvement: Instead of copying files, use hard links. This is an
49
+ optimization. (#2120)
50
+ * Improvement: S3 storage option `:s3_prefixes_in_alias`. (#2287)
51
+ * Improvement: Fog option `:fog_public` can be a lambda. (#2302)
52
+ * Improvement: One fewer warning on JRuby. (#2352)
53
+ * Ruby 2.4.0 compatibility (doesn't use Fixnum anymore)
54
+
55
+ 5.1.0 (2016-08-19):
56
+
57
+ * Add default `content_type_detector` to `UploadedFileAdapter` (#2270)
58
+ * Default S3 protocol to empty string (#2038)
59
+ * Don't write original file if it wasn't reprocessed (#1993)
60
+ * Disallow trailing newlines in regular expressions (#2266)
61
+ * Support for readbyte in Paperclip attachments (#2034)
62
+ * (port from 4.3) Uri io adapter uses the content-disposition filename (#2250)
63
+ * General refactors and documentation improvements
64
+
65
+ 5.0.0 (2016-07-01):
66
+
67
+ * Improvement: Add `read_timeout` configuration for URI Adapter download_content method.
68
+ * README adjustments for Ruby beginners (add links, elucidate model in Quick Start)
69
+ * Bugfix: Now it's possible to save images from URLs with special characters [#1932]
70
+ * Bugfix: Return false when file to copy is not present in cloud storage [#2173]
71
+ * Automatically close file while checking mime type [#2016]
72
+ * Add `read_timeout` option to `UriAdapter#download_content` method [#2232]
73
+ * Fix a nil error in content type validation matcher [#1910]
74
+ * Documentation improvements
75
+
76
+ 5.0.0.beta2 (2016-04-01):
2
77
 
3
78
  * Bugfix: Dynamic fog directory option is now respected
4
79
  * Bugfix: Fixes cocaine duplicated paths [#2169]
5
80
  * Removal of dead code (older versions of Rails and AWS SDK)
6
81
  * README adjustments
7
82
 
8
- 5.0.0.beta1 (2015-03-13):
83
+ 5.0.0.beta1 (2016-03-13):
9
84
 
85
+ * Bug Fix: megabytes of mime-types info in logs when a spoofed media type is detected.
10
86
  * Drop support to end-of-life'd ruby 2.0.
11
87
  * Drop support for end-of-life'd Rails 3.2 and 4.1
12
88
  * Drop support for AWS v1
data/README.md CHANGED
@@ -14,6 +14,9 @@ https://github.com/thoughtbot/paperclip/releases
14
14
  [![Inline docs](http://inch-ci.org/github/thoughtbot/paperclip.svg)](http://inch-ci.org/github/thoughtbot/paperclip)
15
15
  [![Security](https://hakiri.io/github/thoughtbot/paperclip/master.svg)](https://hakiri.io/github/thoughtbot/paperclip/master)
16
16
 
17
+ <!-- START doctoc generated TOC please keep comment here to allow auto update -->
18
+ <!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
19
+
17
20
  - [Requirements](#requirements)
18
21
  - [Ruby and Rails](#ruby-and-rails)
19
22
  - [Image Processor](#image-processor)
@@ -25,7 +28,8 @@ https://github.com/thoughtbot/paperclip/releases
25
28
  - [Edit and New Views](#edit-and-new-views)
26
29
  - [Edit and New Views with Simple Form](#edit-and-new-views-with-simple-form)
27
30
  - [Controller](#controller)
28
- - [Show View](#show-view)
31
+ - [View Helpers](#view-helpers)
32
+ - [Checking a File Exists](#checking-a-file-exists)
29
33
  - [Deleting an Attachment](#deleting-an-attachment)
30
34
  - [Usage](#usage)
31
35
  - [Validations](#validations)
@@ -35,25 +39,29 @@ https://github.com/thoughtbot/paperclip/releases
35
39
  - [Migrations](#migrations-1)
36
40
  - [Add Attachment Column To A Table](#add-attachment-column-to-a-table)
37
41
  - [Schema Definition](#schema-definition)
38
- - [Vintage syntax](#vintage-syntax)
42
+ - [Vintage Syntax](#vintage-syntax)
39
43
  - [Storage](#storage)
40
44
  - [Understanding Storage](#understanding-storage)
45
+ - [IO Adapters](#io-adapters)
41
46
  - [Post Processing](#post-processing)
47
+ - [Custom Attachment Processors](#custom-attachment-processors)
42
48
  - [Events](#events)
43
49
  - [URI Obfuscation](#uri-obfuscation)
44
- - [MD5 Checksum / Fingerprint](#md5-checksum--fingerprint)
50
+ - [Checksum / Fingerprint](#checksum--fingerprint)
45
51
  - [File Preservation for Soft-Delete](#file-preservation-for-soft-delete)
46
- - [Custom Attachment Processors](#custom-attachment-processors)
47
52
  - [Dynamic Configuration](#dynamic-configuration)
48
- - [Dynamic Styles](#dynamic-styles)
49
- - [Dynamic Processors](#dynamic-processors)
53
+ - [Dynamic Styles:](#dynamic-styles)
54
+ - [Dynamic Processors:](#dynamic-processors)
50
55
  - [Logging](#logging)
51
56
  - [Deployment](#deployment)
57
+ - [Attachment Styles](#attachment-styles)
52
58
  - [Testing](#testing)
53
59
  - [Contributing](#contributing)
54
60
  - [License](#license)
55
61
  - [About thoughtbot](#about-thoughtbot)
56
62
 
63
+ <!-- END doctoc generated TOC please keep comment here to allow auto update -->
64
+
57
65
  Paperclip is intended as an easy file attachment library for ActiveRecord. The
58
66
  intent behind it was to keep setup as easy as possible and to treat files as
59
67
  much like other attributes as possible. This means they aren't saved to their
@@ -79,7 +87,7 @@ Requirements
79
87
  ### Ruby and Rails
80
88
 
81
89
  Paperclip now requires Ruby version **>= 2.1** and Rails version **>= 4.2**
82
- (only if you're going to use Paperclip with Ruby on Rails.)
90
+ (only if you're going to use Paperclip with Ruby on Rails).
83
91
 
84
92
  ### Image Processor
85
93
 
@@ -97,7 +105,7 @@ In development mode, you might add this line to `config/environments/development
97
105
  Paperclip.options[:command_path] = "/usr/local/bin/"
98
106
  ```
99
107
 
100
- If you're on Mac OS X, you'll want to run the following with Homebrew:
108
+ If you're on Mac OS X, you'll want to run the following with [Homebrew](http://www.brew.sh):
101
109
 
102
110
  brew install imagemagick
103
111
 
@@ -159,7 +167,7 @@ Paperclip is distributed as a gem, which is how it should be used in your app.
159
167
  Include the gem in your Gemfile:
160
168
 
161
169
  ```ruby
162
- gem "paperclip", "~> 5.0.0.beta1"
170
+ gem "paperclip", "~> 5.2.1"
163
171
  ```
164
172
 
165
173
  Or, if you want to get the latest, you can get master from the main paperclip repository:
@@ -192,12 +200,14 @@ Quick Start
192
200
  ```ruby
193
201
  class User < ActiveRecord::Base
194
202
  has_attached_file :avatar, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: "/images/:style/missing.png"
195
- validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\Z/
203
+ validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\z/
196
204
  end
197
205
  ```
198
206
 
199
207
  ### Migrations
200
208
 
209
+
210
+ Assuming you have a `users` table, add an `avatar` column to the `users` table:
201
211
  ```ruby
202
212
  class AddAvatarColumnsToUsers < ActiveRecord::Migration
203
213
  def up
@@ -213,17 +223,20 @@ end
213
223
  (Or you can use the Rails migration generator: `rails generate paperclip user avatar`)
214
224
 
215
225
  ### Edit and New Views
216
-
226
+ Make sure you have corresponding methods in your controller:
217
227
  ```erb
218
228
  <%= form_for @user, url: users_path, html: { multipart: true } do |form| %>
219
229
  <%= form.file_field :avatar %>
230
+ <%= form.submit %>
220
231
  <% end %>
221
232
  ```
222
233
 
223
- ### Edit and New Views with Simple Form
234
+ ### Edit and New Views with [Simple Form](https://github.com/plataformatec/simple_form)
235
+
224
236
  ```erb
225
237
  <%= simple_form_for @user, url: users_path do |form| %>
226
238
  <%= form.input :avatar, as: :file %>
239
+ <%= form.submit %>
227
240
  <% end %>
228
241
  ```
229
242
 
@@ -231,7 +244,7 @@ end
231
244
 
232
245
  ```ruby
233
246
  def create
234
- @user = User.create( user_params )
247
+ @user = User.create(user_params)
235
248
  end
236
249
 
237
250
  private
@@ -244,14 +257,24 @@ def user_params
244
257
  end
245
258
  ```
246
259
 
247
- ### Show View
248
-
260
+ ### View Helpers
261
+ Add these to the view where you want your images displayed:
249
262
  ```erb
250
263
  <%= image_tag @user.avatar.url %>
251
264
  <%= image_tag @user.avatar.url(:medium) %>
252
265
  <%= image_tag @user.avatar.url(:thumb) %>
253
266
  ```
254
267
 
268
+ ### Checking a File Exists
269
+
270
+ There are two methods for checking if a file exists:
271
+
272
+ - `file?` and `present?` checks if the `_file_name` field is populated
273
+ - `exists?` checks if the file exists (will perform a TCP connection if stored in the cloud)
274
+
275
+ Keep this in mind if you are checking if files are present in a loop. The first
276
+ version is significantly more performant, but has different semantics.
277
+
255
278
  ### Deleting an Attachment
256
279
 
257
280
  Set the attribute to `nil` and save.
@@ -328,7 +351,7 @@ called with valid attachments._
328
351
 
329
352
  ```ruby
330
353
  class Message < ActiveRecord::Base
331
- has_attached_file :asset, styles: {thumb: "100x100#"}
354
+ has_attached_file :asset, styles: { thumb: "100x100#" }
332
355
 
333
356
  before_post_process :skip_for_audio
334
357
 
@@ -344,7 +367,7 @@ afterwards, then assign manually:
344
367
 
345
368
  ```ruby
346
369
  class Book < ActiveRecord::Base
347
- has_attached_file :document, styles: {thumbnail: "60x60#"}
370
+ has_attached_file :document, styles: { thumbnail: "60x60#" }
348
371
  validates_attachment :document, content_type: { content_type: "application/pdf" }
349
372
  validates_something_else # Other validations that conflict with Paperclip's
350
373
  end
@@ -409,7 +432,7 @@ class ActiveRecord::Base
409
432
  # Validate content type
410
433
  validates_attachment_content_type :avatar, content_type: /\Aimage/
411
434
  # Validate filename
412
- validates_attachment_file_name :avatar, matches: [/png\Z/, /jpe?g\Z/]
435
+ validates_attachment_file_name :avatar, matches: [/png\z/, /jpe?g\z/]
413
436
  # Explicitly do not validate
414
437
  do_not_validate_attachment_file_type :avatar
415
438
  end
@@ -531,7 +554,7 @@ class AddAttachmentColumnsToUsers < ActiveRecord::Migration
531
554
  end
532
555
  ```
533
556
 
534
- ### Vintage syntax
557
+ ### Vintage Syntax
535
558
 
536
559
  Vintage syntax (such as `t.has_attached_file` and `drop_attached_file`) is still supported in
537
560
  Paperclip 3.x, but you're advised to update those migration files to use this new syntax.
@@ -550,6 +573,7 @@ Paperclip ships with 3 storage adapters:
550
573
  If you would like to use Paperclip with another storage, you can install these
551
574
  gems along side with Paperclip:
552
575
 
576
+ * [paperclip-azure](https://github.com/supportify/paperclip-azure)
553
577
  * [paperclip-azure-storage](https://github.com/gmontard/paperclip-azure-storage)
554
578
  * [paperclip-dropbox](https://github.com/janko-m/paperclip-dropbox)
555
579
 
@@ -559,8 +583,8 @@ The files that are assigned as attachments are, by default, placed in the
559
583
  directory specified by the `:path` option to `has_attached_file`. By default, this
560
584
  location is `:rails_root/public/system/:class/:attachment/:id_partition/:style/:filename`.
561
585
  This location was chosen because, on standard Capistrano deployments, the
562
- `public/system` directory is symlinked to the app's shared directory, meaning it
563
- will survive between deployments. For example, using that `:path`, you may have a
586
+ `public/system` directory can be symlinked to the app's shared directory, meaning it
587
+ survives between deployments. For example, using that `:path`, you may have a
564
588
  file at
565
589
 
566
590
  /data/myapp/releases/20081229172410/public/system/users/avatar/000/000/013/small/my_pic.png
@@ -572,7 +596,7 @@ You may also choose to store your files using Amazon's S3 service. To do so, inc
572
596
  the `aws-sdk` gem in your Gemfile:
573
597
 
574
598
  ```ruby
575
- gem 'aws-sdk', '>= 2.0.34'
599
+ gem 'aws-sdk', '~> 2.3.0'
576
600
  ```
577
601
 
578
602
  And then you can specify using S3 from `has_attached_file`.
@@ -588,64 +612,121 @@ variables.
588
612
 
589
613
  ---
590
614
 
615
+ IO Adapters
616
+ -----------
617
+
618
+ When a file is uploaded or attached, it can be in one of a few different input
619
+ forms, from Rails' UploadedFile object to a StringIO to a Tempfile or even a
620
+ simple String that is a URL that points to an image.
621
+
622
+ Paperclip will accept, by default, many of these sources. It also is capable of
623
+ handling even more with a little configuration. The IO Adapters that handle
624
+ images from non-local sources are not enabled by default. They can be enabled by
625
+ adding a line similar to the following into `config/initializers/paperclip.rb`:
626
+
627
+ ```ruby
628
+ Paperclip::DataUriAdapter.register
629
+ ```
630
+
631
+ It's best to only enable a remote-loading adapter if you need it. Otherwise
632
+ there's a chance that someone can gain insight into your internal network
633
+ structure using it as a vector.
634
+
635
+ The following adapters are *not* loaded by default:
636
+
637
+ * `Paperclip::UriAdapter` - which accepts a `URI` instance.
638
+ * `Paperclip::HttpUrlProxyAdapter` - which accepts a `http` string.
639
+ * `Paperclip::DataUriAdapter` - which accepts a Base64-encoded `data:` string.
640
+
641
+ ---
642
+
591
643
  Post Processing
592
644
  ---------------
593
645
 
594
646
  Paperclip supports an extensible selection of post-processors. When you define
595
647
  a set of styles for an attachment, by default it is expected that those
596
- "styles" are actually "thumbnails." However, you can do much more than just
597
- thumbnail images. By defining a subclass of Paperclip::Processor, you can
598
- perform any processing you want on the files that are attached. Any file in
599
- your Rails app's `lib/paperclip` and `lib/paperclip_processors` directories is
600
- automatically loaded by Paperclip, allowing you to easily define custom
601
- processors. You can specify a processor with the `:processors` option to
602
- `has_attached_file`:
648
+ "styles" are actually "thumbnails." These are processed by
649
+ `Paperclip::Thumbnail`. For backward compatibility reasons you can pass either
650
+ a single geometry string, or an array containing a geometry and a format that
651
+ the file will be converted to, like so:
603
652
 
604
653
  ```ruby
605
- has_attached_file :scan, styles: { text: { quality: :better } },
606
- processors: [:ocr]
654
+ has_attached_file :avatar, styles: { thumb: ["32x32#", :png] }
607
655
  ```
608
656
 
609
- This would load the hypothetical class Paperclip::Ocr, which would have the
610
- hash "{ quality: :better }" passed to it along with the uploaded file. For
611
- more information about defining processors, see Paperclip::Processor.
657
+ This will convert the "thumb" style to a 32x32 square in PNG format, regardless
658
+ of what was uploaded. If the format is not specified, it is kept the same (e.g.
659
+ JPGs will remain JPGs). `Paperclip::Thumbnail` uses ImageMagick to process
660
+ images; [ImageMagick's geometry documentation](http://www.imagemagick.org/script/command-line-processing.php#geometry)
661
+ has more information on the accepted style formats.
612
662
 
613
- The default processor is Paperclip::Thumbnail. For backward compatibility
614
- reasons, you can pass a single geometry string or an array containing a
615
- geometry and a format that the file will be converted to, like so:
663
+ For more fine-grained control of the conversion process, `source_file_options` and `convert_options` can be used to pass flags and settings directly to ImageMagick's powerful Convert tool, [documented here](https://www.imagemagick.org/script/convert.php). For example:
616
664
 
617
665
  ```ruby
618
- has_attached_file :avatar, styles: { thumb: ["32x32#", :png] }
666
+ has_attached_file :image, styles: { regular: ['800x800>', :png]},
667
+ source_file_options: { regular: "-density 96 -depth 8 -quality 85" },
668
+ convert_options: { regular: "-posterize 3"}
619
669
  ```
620
670
 
621
- This will convert the "thumb" style to a 32x32 square in PNG format, regardless
622
- of what was uploaded. If the format is not specified, it is kept the same (i.e.
623
- JPGs will remain JPGs). For more information on the accepted style formats, see
624
- [here](http://www.imagemagick.org/script/command-line-processing.php#geometry).
671
+ ImageMagick supports a number of environment variables for controlling its resource limits. For example, you can enforce memory or execution time limits by setting the following variables in your application's process environment:
672
+
673
+ * `MAGICK_MEMORY_LIMIT=128MiB`
674
+ * `MAGICK_MAP_LIMIT=64MiB`
675
+ * `MAGICK_TIME_LIMIT=30`
676
+
677
+ For a full list of variables and description, see [ImageMagick's resources documentation](http://www.imagemagick.org/script/resources.php).
678
+
679
+ ---
680
+
681
+ Custom Attachment Processors
682
+ -------
683
+
684
+ You can write your own custom attachment processors to carry out tasks like
685
+ adding watermarks, compressing images, or encrypting files. Custom processors
686
+ must be defined within the `Paperclip` module, inherit from
687
+ `Paperclip::Processor` (see [`lib/paperclip/processor.rb`](https://github.com/thoughtbot/paperclip/blob/master/lib/paperclip/processor.rb)),
688
+ and implement a `make` method that returns a `File`. All files in your Rails
689
+ app's `lib/paperclip` and `lib/paperclip_processors` directories will be
690
+ automatically loaded by Paperclip. Processors are specified using the
691
+ `:processors` option to `has_attached_file`:
692
+
693
+ ```ruby
694
+ has_attached_file :scan, styles: { text: { quality: :better } },
695
+ processors: [:ocr]
696
+ ```
697
+
698
+ This would load the hypothetical class `Paperclip::Ocr`, and pass it the
699
+ options hash `{ quality: :better }`, along with the uploaded file.
625
700
 
626
701
  Multiple processors can be specified, and they will be invoked in the order
627
- they are defined in the `:processors` array. Each successive processor will
628
- be given the result of the previous processor's execution. All processors will
629
- receive the same parameters, which are defined in the `:styles` hash.
630
- For example, assuming we had this definition:
702
+ they are defined in the `:processors` array. Each successive processor is given
703
+ the result from the previous processor. All processors receive the same
704
+ parameters, which are defined in the `:styles` hash. For example, assuming we
705
+ had this definition:
631
706
 
632
707
  ```ruby
633
708
  has_attached_file :scan, styles: { text: { quality: :better } },
634
709
  processors: [:rotator, :ocr]
635
710
  ```
636
711
 
637
- then both the :rotator processor and the :ocr processor would receive the
638
- options `{ quality: :better }`. This parameter may not mean anything to one
639
- or more or the processors, and they are expected to ignore it.
712
+ Both the `:rotator` processor and the `:ocr` processor would receive the
713
+ options `{ quality: :better }`. If a processor receives an option it doesn't
714
+ recognise, it's expected to ignore it.
640
715
 
641
716
  _NOTE: Because processors operate by turning the original attachment into the
642
717
  styles, no processors will be run if there are no styles defined._
643
718
 
644
719
  If you're interested in caching your thumbnail's width, height and size in the
645
- database, take a look at the [paperclip-meta](https://github.com/teeparham/paperclip-meta) gem.
720
+ database, take a look at the [paperclip-meta](https://github.com/teeparham/paperclip-meta)
721
+ gem.
646
722
 
647
723
  Also, if you're interested in generating the thumbnail on-the-fly, you might want
648
- to look into the [attachment_on_the_fly](https://github.com/drpentode/Attachment-on-the-Fly) gem.
724
+ to look into the [attachment_on_the_fly](https://github.com/drpentode/Attachment-on-the-Fly)
725
+ gem.
726
+
727
+ Paperclip's thumbnail generator (see [`lib/paperclip/thumbnail.rb`](lib/paperclip/thumbnail.rb))
728
+ is implemented as a processor, and may be a good reference for writing your own
729
+ processors.
649
730
 
650
731
  ---
651
732
 
@@ -669,7 +750,7 @@ called with valid attachments._
669
750
 
670
751
  ```ruby
671
752
  class Message < ActiveRecord::Base
672
- has_attached_file :asset, styles: {thumb: "100x100#"}
753
+ has_attached_file :asset, styles: { thumb: "100x100#" }
673
754
 
674
755
  before_post_process :skip_for_audio
675
756
 
@@ -704,10 +785,10 @@ is specified in `:hash_data`. The default value for `:hash_data` is `":class/:at
704
785
 
705
786
  For more on this feature, read [the author's own explanation](https://github.com/thoughtbot/paperclip/pull/416)
706
787
 
707
- MD5 Checksum / Fingerprint
788
+ Checksum / Fingerprint
708
789
  -------
709
790
 
710
- An MD5 checksum of the original file assigned will be placed in the model if it
791
+ A checksum of the original file assigned will be placed in the model if it
711
792
  has an attribute named fingerprint. Following the user model migration example
712
793
  above, the migration would look like the following:
713
794
 
@@ -723,6 +804,17 @@ class AddAvatarFingerprintColumnToUser < ActiveRecord::Migration
723
804
  end
724
805
  ```
725
806
 
807
+ The algorithm can be specified using a configuration option; it defaults to MD5
808
+ for backwards compatibility with Paperclip 5 and earlier.
809
+
810
+ ```ruby
811
+ has_attached_file :some_attachment, adapter_options: { hash_digest: Digest::SHA256 }
812
+ ```
813
+
814
+ Run `CLASS=User ATTACHMENT=avatar rake paperclip:refresh:fingerprints` after
815
+ changing the digest on existing attachments to update the fingerprints in the
816
+ database.
817
+
726
818
  File Preservation for Soft-Delete
727
819
  -------
728
820
 
@@ -730,7 +822,7 @@ An option is available to preserve attachments in order to play nicely with soft
730
822
 
731
823
  ```ruby
732
824
  has_attached_file :some_attachment, {
733
- preserve_files: "true",
825
+ preserve_files: true,
734
826
  }
735
827
  ```
736
828
 
@@ -738,25 +830,6 @@ This will prevent ```some_attachment``` from being wiped out when the model gets
738
830
 
739
831
  ---
740
832
 
741
- Custom Attachment Processors
742
- -------
743
-
744
- Custom attachment processors can be implemented and their only requirement is
745
- to inherit from `Paperclip::Processor` (see `lib/paperclip/processor.rb`).
746
- For example, when `:styles` are specified for an image attachment, the
747
- thumbnail processor (see `lib/paperclip/thumbnail.rb`) is loaded without having
748
- to specify it as a `:processor` parameter to `has_attached_file`. When any
749
- other processor is defined, it must be called out in the `:processors`
750
- parameter if it is to be applied to the attachment. The thumbnail processor
751
- uses the ImageMagick `convert` command to do the work of resizing image
752
- thumbnails. It would be easy to create a custom processor that watermarks
753
- an image using ImageMagick's `composite` command. Following the
754
- implementation pattern of the thumbnail processor would be a way to implement a
755
- watermark processor. All kinds of attachment processors can be created;
756
- a few utility examples would be compression and encryption processors.
757
-
758
- ---
759
-
760
833
  Dynamic Configuration
761
834
  ---------------------
762
835
 
@@ -821,6 +894,16 @@ More information in the [rdocs](http://www.rubydoc.info/github/thoughtbot/paperc
821
894
  Deployment
822
895
  ----------
823
896
 
897
+ To make Capistrano symlink the `public/system` directory so that attachments
898
+ survive new deployments, set the `linked_dirs` option in your `config/deploy.rb`
899
+ file:
900
+
901
+ ```ruby
902
+ set :linked_dirs, fetch(:linked_dirs, []).push('public/system')
903
+ ```
904
+
905
+ ### Attachment Styles
906
+
824
907
  Paperclip is aware of new attachment styles you have added in previous deploys. The only thing you should do after each deployment is to call
825
908
  `rake paperclip:refresh:missing_styles`. It will store current attachment styles in `RAILS_ROOT/public/system/paperclip_attachments.yml`
826
909
  by default. You can change it by:
@@ -832,9 +915,9 @@ Paperclip.registered_attachments_styles_path = '/tmp/config/paperclip_attachment
832
915
  Here is an example for Capistrano:
833
916
 
834
917
  ```ruby
835
- namespace :deploy do
918
+ namespace :paperclip do
836
919
  desc "build missing paperclip styles"
837
- task :build_missing_paperclip_styles do
920
+ task :build_missing_styles do
838
921
  on roles(:app) do
839
922
  within release_path do
840
923
  with rails_env: fetch(:rails_env) do
@@ -845,7 +928,7 @@ namespace :deploy do
845
928
  end
846
929
  end
847
930
 
848
- after("deploy:compile_assets", "deploy:build_missing_paperclip_styles")
931
+ after("deploy:compile_assets", "paperclip:build_missing_styles")
849
932
  ```
850
933
 
851
934
  Now you don't have to remember to refresh thumbnails in production every time you add a new style.
@@ -912,7 +995,7 @@ similar mechanism for whichever parallel testing library you use.
912
995
 
913
996
  **Integration Tests**
914
997
 
915
- Using integration tests with FactoryGirl may save multiple copies of
998
+ Using integration tests with FactoryBot may save multiple copies of
916
999
  your test files within the app. To avoid this, specify a custom path in
917
1000
  the `config/environments/test.rb` like so:
918
1001
 
@@ -928,6 +1011,17 @@ config.after(:suite) do
928
1011
  FileUtils.rm_rf(Dir["#{Rails.root}/spec/test_files/"])
929
1012
  end
930
1013
  ```
1014
+
1015
+ **Example of test configuration with Factory Bot**
1016
+
1017
+
1018
+ ```ruby
1019
+ FactoryBot.define do
1020
+ factory :user do
1021
+ avatar { File.new("#{Rails.root}/spec/support/fixtures/image.jpg") }
1022
+ end
1023
+ end
1024
+ ```
931
1025
  ---
932
1026
 
933
1027
  Contributing
@@ -943,20 +1037,20 @@ guidelines:
943
1037
  about writing tests for paperclip, please open a
944
1038
  [GitHub issue](https://github.com/thoughtbot/paperclip/issues/new).
945
1039
 
946
- Please see `CONTRIBUTING.md` for more details on contributing and running test.
1040
+ Please see [`CONTRIBUTING.md`](./CONTRIBUTING.md) for more details on contributing and running test.
947
1041
 
948
1042
  Thank you to all [the contributors](https://github.com/thoughtbot/paperclip/graphs/contributors)!
949
1043
 
950
1044
  License
951
1045
  -------
952
1046
 
953
- Paperclip is Copyright © 2008-2016 thoughtbot, inc. It is free software, and may be
1047
+ Paperclip is Copyright © 2008-2017 thoughtbot, inc. It is free software, and may be
954
1048
  redistributed under the terms specified in the MIT-LICENSE file.
955
1049
 
956
1050
  About thoughtbot
957
1051
  ----------------
958
1052
 
959
- ![thoughtbot](https://thoughtbot.com/logo.png)
1053
+ ![thoughtbot](http://presskit.thoughtbot.com/images/thoughtbot-logo-for-readmes.svg)
960
1054
 
961
1055
  Paperclip is maintained and funded by thoughtbot.
962
1056
  The names and logos for thoughtbot are trademarks of thoughtbot, inc.