paperclip 4.3.7 → 5.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.codeclimate.yml +17 -0
- data/.hound.yml +5 -16
- data/.travis.yml +15 -12
- data/Appraisals +4 -8
- data/CONTRIBUTING.md +16 -5
- data/Gemfile +3 -8
- data/LICENSE +1 -1
- data/NEWS +86 -31
- data/README.md +220 -146
- data/Rakefile +1 -1
- data/UPGRADING +12 -9
- data/features/basic_integration.feature +3 -2
- data/features/migration.feature +0 -24
- data/features/step_definitions/attachment_steps.rb +6 -6
- data/features/step_definitions/rails_steps.rb +29 -28
- data/features/step_definitions/s3_steps.rb +2 -2
- data/features/support/env.rb +1 -0
- data/features/support/paths.rb +1 -1
- data/features/support/rails.rb +0 -24
- data/gemfiles/4.2.gemfile +3 -5
- data/gemfiles/{3.2.gemfile → 5.0.gemfile} +4 -6
- data/lib/paperclip.rb +12 -11
- data/lib/paperclip/attachment.rb +25 -14
- data/lib/paperclip/attachment_registry.rb +2 -1
- data/lib/paperclip/callbacks.rb +8 -6
- data/lib/paperclip/content_type_detector.rb +3 -2
- data/lib/paperclip/errors.rb +3 -1
- data/lib/paperclip/glue.rb +1 -1
- data/lib/paperclip/has_attached_file.rb +7 -1
- data/lib/paperclip/helpers.rb +14 -10
- data/lib/paperclip/interpolations.rb +1 -1
- data/lib/paperclip/io_adapters/abstract_adapter.rb +25 -3
- data/lib/paperclip/io_adapters/attachment_adapter.rb +10 -5
- data/lib/paperclip/io_adapters/data_uri_adapter.rb +8 -8
- data/lib/paperclip/io_adapters/empty_string_adapter.rb +5 -4
- data/lib/paperclip/io_adapters/file_adapter.rb +12 -6
- data/lib/paperclip/io_adapters/http_url_proxy_adapter.rb +7 -7
- data/lib/paperclip/io_adapters/identity_adapter.rb +12 -6
- data/lib/paperclip/io_adapters/nil_adapter.rb +8 -5
- data/lib/paperclip/io_adapters/registry.rb +6 -2
- data/lib/paperclip/io_adapters/stringio_adapter.rb +9 -6
- data/lib/paperclip/io_adapters/uploaded_file_adapter.rb +10 -6
- data/lib/paperclip/io_adapters/uri_adapter.rb +17 -14
- data/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb +4 -4
- data/lib/paperclip/processor.rb +5 -4
- data/lib/paperclip/schema.rb +2 -8
- data/lib/paperclip/storage/filesystem.rb +13 -2
- data/lib/paperclip/storage/fog.rb +18 -12
- data/lib/paperclip/storage/s3.rb +85 -57
- data/lib/paperclip/thumbnail.rb +14 -4
- data/lib/paperclip/url_generator.rb +16 -13
- data/lib/paperclip/validators.rb +1 -1
- data/lib/paperclip/validators/attachment_size_validator.rb +1 -7
- data/lib/paperclip/version.rb +3 -1
- data/lib/tasks/paperclip.rake +18 -4
- data/paperclip.gemspec +12 -9
- data/spec/paperclip/attachment_processing_spec.rb +2 -4
- data/spec/paperclip/attachment_registry_spec.rb +28 -0
- data/spec/paperclip/attachment_spec.rb +72 -18
- data/spec/paperclip/has_attached_file_spec.rb +24 -8
- data/spec/paperclip/integration_spec.rb +4 -3
- data/spec/paperclip/io_adapters/abstract_adapter_spec.rb +45 -22
- data/spec/paperclip/io_adapters/attachment_adapter_spec.rb +6 -3
- data/spec/paperclip/io_adapters/data_uri_adapter_spec.rb +7 -1
- data/spec/paperclip/io_adapters/file_adapter_spec.rb +2 -2
- data/spec/paperclip/io_adapters/http_url_proxy_adapter_spec.rb +18 -1
- data/spec/paperclip/io_adapters/identity_adapter_spec.rb +1 -1
- data/spec/paperclip/io_adapters/registry_spec.rb +2 -2
- data/spec/paperclip/io_adapters/stringio_adapter_spec.rb +1 -1
- data/spec/paperclip/io_adapters/uploaded_file_adapter_spec.rb +5 -5
- data/spec/paperclip/io_adapters/uri_adapter_spec.rb +48 -3
- data/spec/paperclip/matchers/validate_attachment_content_type_matcher_spec.rb +10 -0
- data/spec/paperclip/paperclip_spec.rb +3 -33
- data/spec/paperclip/storage/fog_spec.rb +26 -0
- data/spec/paperclip/storage/s3_live_spec.rb +20 -14
- data/spec/paperclip/storage/s3_spec.rb +357 -190
- data/spec/paperclip/tempfile_spec.rb +35 -0
- data/spec/paperclip/thumbnail_spec.rb +35 -32
- data/spec/paperclip/url_generator_spec.rb +54 -43
- data/spec/paperclip/validators/attachment_size_validator_spec.rb +26 -20
- data/spec/paperclip/validators_spec.rb +5 -5
- data/spec/spec_helper.rb +6 -2
- data/spec/support/assertions.rb +12 -1
- data/spec/support/conditional_filter_helper.rb +5 -0
- data/spec/support/mock_attachment.rb +2 -0
- data/spec/support/mock_url_generator_builder.rb +2 -2
- data/spec/support/model_reconstruction.rb +9 -1
- data/spec/support/reporting.rb +11 -0
- metadata +61 -158
- data/cucumber/paperclip_steps.rb +0 -6
- data/gemfiles/4.1.gemfile +0 -19
- data/lib/paperclip/deprecations.rb +0 -42
- data/lib/paperclip/locales/de.yml +0 -18
- data/lib/paperclip/locales/es.yml +0 -18
- data/lib/paperclip/locales/ja.yml +0 -18
- data/lib/paperclip/locales/pt-BR.yml +0 -18
- data/lib/paperclip/locales/zh-CN.yml +0 -18
- data/lib/paperclip/locales/zh-HK.yml +0 -18
- data/lib/paperclip/locales/zh-TW.yml +0 -18
- data/spec/paperclip/deprecations_spec.rb +0 -65
- data/spec/support/deprecations.rb +0 -9
- data/spec/support/rails_helpers.rb +0 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 569debc832514cb262986c31782d4590065f944b
|
4
|
+
data.tar.gz: b65dbae4e73cddd0c685e0c1bd4fc101ef09d251
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f0890bb495c49f763d1765015e5e7818c3d7ae6e50fbb33281226c6ccf3a316dd39b6c7e0df20fbe559522fb0c1fdc8b61c824fd954a8c5699920dcd2db88992
|
7
|
+
data.tar.gz: 7913049f0c5cbdd2b3912dfea1121fce81ea002bcdee5924ad2e48c2fc03fa5dc391922666d48310957444123d495a6e6266b6433fb51c7b512980322d527485
|
data/.codeclimate.yml
ADDED
data/.hound.yml
CHANGED
@@ -20,7 +20,8 @@ AllCops:
|
|
20
20
|
- "db/schema.rb"
|
21
21
|
- 'vendor/**/*'
|
22
22
|
- 'gemfiles/vendor/**/*'
|
23
|
-
|
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/
|
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/
|
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/
|
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,22 +1,25 @@
|
|
1
|
+
language: ruby
|
2
|
+
sudo: false
|
3
|
+
|
1
4
|
rvm:
|
2
|
-
- 2.0
|
3
5
|
- 2.1
|
4
|
-
- 2.2
|
5
|
-
-
|
6
|
-
-
|
6
|
+
- 2.2
|
7
|
+
- 2.3
|
8
|
+
- 2.4
|
7
9
|
|
8
10
|
script: "bundle exec rake clean spec cucumber"
|
9
11
|
|
12
|
+
addons:
|
13
|
+
apt:
|
14
|
+
packages:
|
15
|
+
- ghostscript
|
16
|
+
|
10
17
|
gemfile:
|
11
|
-
- gemfiles/3.2.gemfile
|
12
|
-
- gemfiles/4.1.gemfile
|
13
18
|
- gemfiles/4.2.gemfile
|
19
|
+
- gemfiles/5.0.gemfile
|
14
20
|
|
15
21
|
matrix:
|
16
22
|
fast_finish: true
|
17
|
-
|
18
|
-
-
|
19
|
-
|
20
|
-
|
21
|
-
sudo: false
|
22
|
-
cache: bundler
|
23
|
+
exclude:
|
24
|
+
- gemfile: gemfiles/5.0.gemfile
|
25
|
+
rvm: 2.1
|
data/Appraisals
CHANGED
data/CONTRIBUTING.md
CHANGED
@@ -1,20 +1,31 @@
|
|
1
1
|
Contributing
|
2
2
|
============
|
3
3
|
|
4
|
-
We love pull requests.
|
4
|
+
We love pull requests from everyone. By participating in this project, you agree
|
5
|
+
to abide by the thoughtbot [code of conduct].
|
6
|
+
|
7
|
+
[code of conduct]: https://thoughtbot.com/open-source-code-of-conduct
|
8
|
+
|
9
|
+
Here's a quick guide for contributing:
|
5
10
|
|
6
11
|
1. Fork the repo.
|
7
12
|
|
8
|
-
|
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
|
9
17
|
to know that you have a clean slate: `bundle && bundle exec rake`
|
10
18
|
|
11
|
-
|
19
|
+
1. Add a test for your change. Only refactoring and documentation changes
|
12
20
|
require no new tests. If you are adding functionality or fixing a bug, we need
|
13
21
|
a test!
|
14
22
|
|
15
|
-
|
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.
|
16
27
|
|
17
|
-
|
28
|
+
1. Push to your fork and submit a pull request.
|
18
29
|
|
19
30
|
At this point you're waiting on us. We like to at least comment on, if not
|
20
31
|
accept, pull requests within seven business days (most of the work on Paperclip
|
data/Gemfile
CHANGED
@@ -3,19 +3,14 @@ source "https://rubygems.org"
|
|
3
3
|
gemspec
|
4
4
|
|
5
5
|
gem 'sqlite3', '~> 1.3.8', :platforms => :ruby
|
6
|
-
|
7
|
-
gem 'jruby-openssl', :platforms => :jruby
|
8
|
-
gem 'activerecord-jdbcsqlite3-adapter', :platforms => :jruby
|
9
|
-
|
10
|
-
gem 'rubysl', :platforms => :rbx
|
11
|
-
gem 'racc', :platforms => :rbx
|
12
|
-
|
13
6
|
gem 'pry'
|
14
7
|
|
15
8
|
# Hinting at development dependencies
|
16
9
|
# Prevents bundler from taking a long-time to resolve
|
17
10
|
group :development, :test do
|
18
|
-
gem '
|
11
|
+
gem 'activerecord-import'
|
12
|
+
gem 'mime-types'
|
19
13
|
gem 'builder'
|
20
14
|
gem 'rubocop', require: false
|
15
|
+
gem 'rspec'
|
21
16
|
end
|
data/LICENSE
CHANGED
@@ -3,7 +3,7 @@ LICENSE
|
|
3
3
|
|
4
4
|
The MIT License
|
5
5
|
|
6
|
-
Copyright (c) 2008-
|
6
|
+
Copyright (c) 2008-2016 Jon Yurek and thoughtbot, inc.
|
7
7
|
|
8
8
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
9
9
|
of this software and associated documentation files (the "Software"), to deal
|
data/NEWS
CHANGED
@@ -1,39 +1,94 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
*
|
4
|
-
|
5
|
-
*
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
1
|
+
5.2.0 (2018-01-23):
|
2
|
+
|
3
|
+
* Security: Remove the automatic loading of URI adapters. Some of these
|
4
|
+
adapters can be specially crafted to expose your network topology. (#2435)
|
5
|
+
* Bugfix: The rake task no longer rescues `Exception`. (#2476)
|
6
|
+
* Bugfix: Handle malformed `Content-Disposition` headers (#2283)
|
7
|
+
* Bugfix: The `:only_process` option works when passed a lambda again. (#2289)
|
8
|
+
* Improvement: Added `:use_accelerate_endpoint` option when using S3 to enable
|
9
|
+
[Amazon S3 Transfer Acceleration](http://docs.aws.amazon.com/AmazonS3/latest/dev/transfer-acceleration.html)
|
10
|
+
(#2291)
|
11
|
+
* Improvement: Make the fingerprint digest configurable per attachment. The
|
12
|
+
default remains MD5. Making this configurable means it can change in a future
|
13
|
+
version because it is not considered secure anymore against intentional file
|
14
|
+
corruption. For more info, see https://en.wikipedia.org/wiki/MD5#Security
|
15
|
+
|
16
|
+
You can change the digest used for an attachment by adding the
|
17
|
+
`:adapter_options` parameter to the `has_attached_file` options like this:
|
18
|
+
`has_attached_file :avatar, adapter_options: { hash_digest: Digest::SHA256 }`
|
19
|
+
|
20
|
+
Use the rake task to regenerate fingerprints with the new digest for a given
|
21
|
+
class. Note that this does **not** check the file integrity using the old
|
22
|
+
fingerprint. Run the following command to regenerate fingerprints for all
|
23
|
+
User attachments:
|
24
|
+
`CLASS=User rake paperclip:refresh:fingerprints`
|
25
|
+
You can optionally limit the attachment that will be processed, e.g:
|
26
|
+
`CLASS=User ATTACHMENT=avatar rake paperclip:refresh:fingerprints` (#2229)
|
27
|
+
* Improvement: The new `frame_index` option on the thumbnail processor allows
|
28
|
+
you to select a specific frame from an animated upload to use as a thumbnail.
|
29
|
+
Initial support is for mkv, avi, MP4, mov, MPEG, and GIF. (#2155)
|
30
|
+
* Improvement: Instead of copying files, use hard links. This is an
|
31
|
+
optimization. (#2120)
|
32
|
+
* Improvement: S3 storage option `:s3_prefixes_in_alias`. (#2287)
|
33
|
+
* Improvement: Fog option `:fog_public` can be a lambda. (#2302)
|
34
|
+
* Improvement: One fewer warning on JRuby. (#2352)
|
35
|
+
|
36
|
+
5.1.0 (2016-08-19):
|
37
|
+
|
38
|
+
* Add default `content_type_detector` to `UploadedFileAdapter` (#2270)
|
39
|
+
* Default S3 protocol to empty string (#2038)
|
40
|
+
* Don't write original file if it wasn't reprocessed (#1993)
|
41
|
+
* Disallow trailing newlines in regular expressions (#2266)
|
42
|
+
* Support for readbyte in Paperclip attachments (#2034)
|
43
|
+
* (port from 4.3) Uri io adapter uses the content-disposition filename (#2250)
|
44
|
+
* General refactors and documentation improvements
|
45
|
+
|
46
|
+
5.0.0 (2016-07-01):
|
47
|
+
|
48
|
+
* Improvement: Add `read_timeout` configuration for URI Adapter download_content method.
|
49
|
+
* README adjustments for Ruby beginners (add links, elucidate model in Quick Start)
|
50
|
+
* Bugfix: Now it's possible to save images from URLs with special characters [#1932]
|
51
|
+
* Bugfix: Return false when file to copy is not present in cloud storage [#2173]
|
52
|
+
* Automatically close file while checking mime type [#2016]
|
53
|
+
* Add `read_timeout` option to `UriAdapter#download_content` method [#2232]
|
54
|
+
* Fix a nil error in content type validation matcher [#1910]
|
55
|
+
* Documentation improvements
|
56
|
+
|
57
|
+
5.0.0.beta2 (2016-04-01):
|
58
|
+
|
59
|
+
* Bugfix: Dynamic fog directory option is now respected
|
60
|
+
* Bugfix: Fixes cocaine duplicated paths [#2169]
|
61
|
+
* Removal of dead code (older versions of Rails and AWS SDK)
|
62
|
+
* README adjustments
|
20
63
|
|
21
|
-
|
64
|
+
5.0.0.beta1 (2016-03-13):
|
22
65
|
|
23
|
-
|
66
|
+
* Bug Fix: megabytes of mime-types info in logs when a spoofed media type is detected.
|
67
|
+
* Drop support to end-of-life'd ruby 2.0.
|
68
|
+
* Drop support for end-of-life'd Rails 3.2 and 4.1
|
69
|
+
* Drop support for AWS v1
|
70
|
+
* Remove tests for JRuby and Rubinius from Travis CI (they were failing)
|
71
|
+
* Improvement: Add `fog_options` configuration to send options to fog when
|
72
|
+
storing files.
|
73
|
+
* Extracted repository for locales only: https://github.com/thoughtbot/paperclip-i18n
|
74
|
+
* Bugfix: Original file could be unlinked during `post_process_style`, producing failures
|
75
|
+
* Bugfix for image magick scaling images up
|
76
|
+
* Memory consumption improvements
|
77
|
+
* `url` on a unpersisted record returns `default_url` rather than `nil`
|
78
|
+
* Improvement: aws-sdk v2 support
|
79
|
+
https://github.com/thoughtbot/paperclip/pull/1903
|
24
80
|
|
25
|
-
|
81
|
+
If your Gemfile contains aws-sdk (>= 2.0.0) and aws-sdk-v1, paperclip will use
|
82
|
+
aws-sdk v2. With aws-sdk v2, S3 storage requires you to set the s3_region.
|
83
|
+
s3_region may be nested in s3_credentials, and (if not nested in
|
84
|
+
s3_credentials) it may be a Proc.
|
26
85
|
|
27
|
-
4.3
|
86
|
+
4.3
|
28
87
|
|
29
|
-
|
30
|
-
|
31
|
-
* Fix GeometryParser regex for usage of '@>' flag
|
32
|
-
* `url` on a unpersisted record returns default_url
|
33
|
-
* spec deprecation warnings and failures
|
34
|
-
* README adjustments
|
88
|
+
See patch versions in v4.3 NEWS:
|
89
|
+
https://github.com/thoughtbot/paperclip/blob/v4.3/NEWS
|
35
90
|
|
36
|
-
4.3.0 (
|
91
|
+
4.3.0 (2015-06-18):
|
37
92
|
|
38
93
|
* Improvement: Update aws-sdk and cucumber gem versions.
|
39
94
|
* Improvement: Add `length` alias for `size` method in AbstractAdapter.
|
@@ -46,7 +101,7 @@
|
|
46
101
|
* Ruby Versioning: Drop support for 1.9.3 (EOL'ed)
|
47
102
|
* Rails Versioning: Drop support for 4.0.0 (EOL'ed)
|
48
103
|
|
49
|
-
4.2.4:
|
104
|
+
4.2.4 (2015-06-05):
|
50
105
|
|
51
106
|
* Rollback backwards incompatible change, allowing paperclip to run on
|
52
107
|
Ruby >= 1.9.2.
|
data/README.md
CHANGED
@@ -1,12 +1,26 @@
|
|
1
1
|
Paperclip
|
2
2
|
=========
|
3
3
|
|
4
|
-
|
4
|
+
## Documentation valid for `master` branch
|
5
|
+
|
6
|
+
Please check the documentation for the paperclip version you are using:
|
7
|
+
https://github.com/thoughtbot/paperclip/releases
|
8
|
+
|
9
|
+
---
|
10
|
+
|
11
|
+
[![Build Status](https://secure.travis-ci.org/thoughtbot/paperclip.svg?branch=master)](http://travis-ci.org/thoughtbot/paperclip)
|
12
|
+
[![Dependency Status](https://gemnasium.com/thoughtbot/paperclip.svg?travis)](https://gemnasium.com/thoughtbot/paperclip)
|
13
|
+
[![Code Climate](https://codeclimate.com/github/thoughtbot/paperclip.svg)](https://codeclimate.com/github/thoughtbot/paperclip)
|
14
|
+
[![Inline docs](http://inch-ci.org/github/thoughtbot/paperclip.svg)](http://inch-ci.org/github/thoughtbot/paperclip)
|
15
|
+
[![Security](https://hakiri.io/github/thoughtbot/paperclip/master.svg)](https://hakiri.io/github/thoughtbot/paperclip/master)
|
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 -->
|
5
19
|
|
6
20
|
- [Requirements](#requirements)
|
7
|
-
- [Ruby
|
21
|
+
- [Ruby and Rails](#ruby-and-rails)
|
8
22
|
- [Image Processor](#image-processor)
|
9
|
-
- [file](#file)
|
23
|
+
- [`file`](#file)
|
10
24
|
- [Installation](#installation)
|
11
25
|
- [Quick Start](#quick-start)
|
12
26
|
- [Models](#models)
|
@@ -14,35 +28,41 @@ Paperclip
|
|
14
28
|
- [Edit and New Views](#edit-and-new-views)
|
15
29
|
- [Edit and New Views with Simple Form](#edit-and-new-views-with-simple-form)
|
16
30
|
- [Controller](#controller)
|
17
|
-
- [
|
31
|
+
- [View Helpers](#view-helpers)
|
32
|
+
- [Checking a File Exists](#checking-a-file-exists)
|
18
33
|
- [Deleting an Attachment](#deleting-an-attachment)
|
19
34
|
- [Usage](#usage)
|
20
35
|
- [Validations](#validations)
|
36
|
+
- [Internationalization (I18n)](#internationalization-i18n)
|
21
37
|
- [Security Validations](#security-validations)
|
22
38
|
- [Defaults](#defaults)
|
23
39
|
- [Migrations](#migrations-1)
|
24
|
-
- [Table
|
40
|
+
- [Add Attachment Column To A Table](#add-attachment-column-to-a-table)
|
25
41
|
- [Schema Definition](#schema-definition)
|
26
42
|
- [Vintage Syntax](#vintage-syntax)
|
27
43
|
- [Storage](#storage)
|
28
44
|
- [Understanding Storage](#understanding-storage)
|
45
|
+
- [IO Adapters](#io-adapters)
|
29
46
|
- [Post Processing](#post-processing)
|
47
|
+
- [Custom Attachment Processors](#custom-attachment-processors)
|
30
48
|
- [Events](#events)
|
31
49
|
- [URI Obfuscation](#uri-obfuscation)
|
32
|
-
|
33
|
-
|
34
|
-
- [Custom Attachment Processors](#custom-attachment-processors)
|
50
|
+
- [Checksum / Fingerprint](#checksum--fingerprint)
|
51
|
+
- [File Preservation for Soft-Delete](#file-preservation-for-soft-delete)
|
35
52
|
- [Dynamic Configuration](#dynamic-configuration)
|
36
|
-
- [Dynamic Styles](#dynamic-styles)
|
37
|
-
- [Dynamic Processors](#dynamic-processors)
|
53
|
+
- [Dynamic Styles:](#dynamic-styles)
|
54
|
+
- [Dynamic Processors:](#dynamic-processors)
|
38
55
|
- [Logging](#logging)
|
39
56
|
- [Deployment](#deployment)
|
57
|
+
- [Attachment Styles](#attachment-styles)
|
40
58
|
- [Testing](#testing)
|
41
59
|
- [Contributing](#contributing)
|
42
60
|
- [License](#license)
|
43
61
|
- [About thoughtbot](#about-thoughtbot)
|
44
62
|
|
45
|
-
|
63
|
+
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
|
64
|
+
|
65
|
+
Paperclip is intended as an easy file attachment library for ActiveRecord. The
|
46
66
|
intent behind it was to keep setup as easy as possible and to treat files as
|
47
67
|
much like other attributes as possible. This means they aren't saved to their
|
48
68
|
final locations on disk, nor are they deleted if set to nil, until
|
@@ -54,10 +74,10 @@ packages). Attached files are saved to the filesystem and referenced in the
|
|
54
74
|
browser by an easily understandable specification, which has sensible and
|
55
75
|
useful defaults.
|
56
76
|
|
57
|
-
See the documentation for `has_attached_file` in [`Paperclip::ClassMethods`](http://rubydoc.info/gems/paperclip/Paperclip/ClassMethods) for
|
77
|
+
See the documentation for `has_attached_file` in [`Paperclip::ClassMethods`](http://www.rubydoc.info/gems/paperclip/Paperclip/ClassMethods) for
|
58
78
|
more detailed options.
|
59
79
|
|
60
|
-
The complete [RDoc](http://
|
80
|
+
The complete [RDoc](http://www.rubydoc.info/gems/paperclip) is online.
|
61
81
|
|
62
82
|
---
|
63
83
|
|
@@ -66,9 +86,8 @@ Requirements
|
|
66
86
|
|
67
87
|
### Ruby and Rails
|
68
88
|
|
69
|
-
Paperclip now requires Ruby version **>= 2.
|
70
|
-
|
71
|
-
If you're still on Ruby 1.8.7 or Ruby on Rails 2.3.x, you can still use Paperclip 2.7.x with your project. Also, everything in this README might not apply to your version of Paperclip, and you should read [the README for version 2.7](http://rubydoc.info/gems/paperclip/2.7.0) instead.
|
89
|
+
Paperclip now requires Ruby version **>= 2.1** and Rails version **>= 4.2**
|
90
|
+
(only if you're going to use Paperclip with Ruby on Rails.)
|
72
91
|
|
73
92
|
### Image Processor
|
74
93
|
|
@@ -86,7 +105,7 @@ In development mode, you might add this line to `config/environments/development
|
|
86
105
|
Paperclip.options[:command_path] = "/usr/local/bin/"
|
87
106
|
```
|
88
107
|
|
89
|
-
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):
|
90
109
|
|
91
110
|
brew install imagemagick
|
92
111
|
|
@@ -95,17 +114,14 @@ to install GhostScript. On Mac OS X, you can also install that using Homebrew:
|
|
95
114
|
|
96
115
|
brew install gs
|
97
116
|
|
98
|
-
If you
|
99
|
-
|
100
|
-
sudo apt-get install imagemagick -y
|
101
|
-
|
102
|
-
If you're on Ubuntu (or any Debian base Linux distribution), you'll want to run the following with apt-get:
|
117
|
+
If you are on Ubuntu (or any Debian base Linux distribution), you'll want to run
|
118
|
+
the following with apt-get:
|
103
119
|
|
104
120
|
sudo apt-get install imagemagick -y
|
105
121
|
|
106
122
|
### `file`
|
107
123
|
|
108
|
-
The Unix [`file` command](
|
124
|
+
The Unix [`file` command](https://en.wikipedia.org/wiki/File_(command)) is required for content-type checking.
|
109
125
|
This utility isn't available in Windows, but comes bundled with Ruby [Devkit](https://github.com/oneclick/rubyinstaller/wiki/Development-Kit),
|
110
126
|
so Windows users must make sure that the devkit is installed and added to the system `PATH`.
|
111
127
|
|
@@ -151,18 +167,18 @@ Paperclip is distributed as a gem, which is how it should be used in your app.
|
|
151
167
|
Include the gem in your Gemfile:
|
152
168
|
|
153
169
|
```ruby
|
154
|
-
gem "paperclip", "~>
|
170
|
+
gem "paperclip", "~> 5.0.0"
|
155
171
|
```
|
156
172
|
|
157
173
|
Or, if you want to get the latest, you can get master from the main paperclip repository:
|
158
174
|
|
159
175
|
```ruby
|
160
|
-
gem "paperclip", :
|
176
|
+
gem "paperclip", git: "git://github.com/thoughtbot/paperclip.git"
|
161
177
|
```
|
162
178
|
|
163
179
|
If you're trying to use features that don't seem to be in the latest released gem, but are
|
164
180
|
mentioned in this README, then you probably need to specify the master branch if you want to
|
165
|
-
use them. This README is probably ahead of the latest released version
|
181
|
+
use them. This README is probably ahead of the latest released version if you're reading it
|
166
182
|
on GitHub.
|
167
183
|
|
168
184
|
For Non-Rails usage:
|
@@ -181,22 +197,10 @@ Quick Start
|
|
181
197
|
|
182
198
|
### Models
|
183
199
|
|
184
|
-
**Rails 3**
|
185
|
-
|
186
|
-
```ruby
|
187
|
-
class User < ActiveRecord::Base
|
188
|
-
attr_accessible :avatar
|
189
|
-
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
|
190
|
-
validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/
|
191
|
-
end
|
192
|
-
```
|
193
|
-
|
194
|
-
**Rails 4**
|
195
|
-
|
196
200
|
```ruby
|
197
201
|
class User < ActiveRecord::Base
|
198
|
-
has_attached_file :avatar, :
|
199
|
-
validates_attachment_content_type :avatar, :
|
202
|
+
has_attached_file :avatar, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: "/images/:style/missing.png"
|
203
|
+
validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\z/
|
200
204
|
end
|
201
205
|
```
|
202
206
|
|
@@ -219,12 +223,13 @@ end
|
|
219
223
|
### Edit and New Views
|
220
224
|
|
221
225
|
```erb
|
222
|
-
<%= form_for @user, :
|
226
|
+
<%= form_for @user, url: users_path, html: { multipart: true } do |form| %>
|
223
227
|
<%= form.file_field :avatar %>
|
224
228
|
<% end %>
|
225
229
|
```
|
226
230
|
|
227
|
-
### Edit and New Views with Simple Form
|
231
|
+
### Edit and New Views with [Simple Form](https://github.com/plataformatec/simple_form)
|
232
|
+
|
228
233
|
```erb
|
229
234
|
<%= simple_form_for @user, url: users_path do |form| %>
|
230
235
|
<%= form.input :avatar, as: :file %>
|
@@ -233,16 +238,6 @@ end
|
|
233
238
|
|
234
239
|
### Controller
|
235
240
|
|
236
|
-
**Rails 3**
|
237
|
-
|
238
|
-
```ruby
|
239
|
-
def create
|
240
|
-
@user = User.create( params[:user] )
|
241
|
-
end
|
242
|
-
```
|
243
|
-
|
244
|
-
**Rails 4**
|
245
|
-
|
246
241
|
```ruby
|
247
242
|
def create
|
248
243
|
@user = User.create( user_params )
|
@@ -258,7 +253,7 @@ def user_params
|
|
258
253
|
end
|
259
254
|
```
|
260
255
|
|
261
|
-
###
|
256
|
+
### View Helpers
|
262
257
|
|
263
258
|
```erb
|
264
259
|
<%= image_tag @user.avatar.url %>
|
@@ -266,6 +261,16 @@ end
|
|
266
261
|
<%= image_tag @user.avatar.url(:thumb) %>
|
267
262
|
```
|
268
263
|
|
264
|
+
### Checking a File Exists
|
265
|
+
|
266
|
+
There are two methods for checking if a file exists:
|
267
|
+
|
268
|
+
- `file?` and `present?` checks if the `_file_name` field is populated
|
269
|
+
- `exists?` checks if the file exists (will perform a TCP connection if stored in the cloud)
|
270
|
+
|
271
|
+
Keep this in mind if you are checking if files are present in a loop. The first
|
272
|
+
version is significantly more performant, but has different semantics.
|
273
|
+
|
269
274
|
### Deleting an Attachment
|
270
275
|
|
271
276
|
Set the attribute to `nil` and save.
|
@@ -296,7 +301,7 @@ You'll need to add `<attachment>_content_type` in case you want to use content t
|
|
296
301
|
validation.
|
297
302
|
|
298
303
|
More information about the options passed to `has_attached_file` is available in the
|
299
|
-
documentation of [`Paperclip::ClassMethods`](http://rubydoc.info/gems/paperclip/Paperclip/ClassMethods).
|
304
|
+
documentation of [`Paperclip::ClassMethods`](http://www.rubydoc.info/gems/paperclip/Paperclip/ClassMethods).
|
300
305
|
|
301
306
|
Validations
|
302
307
|
-----------
|
@@ -310,9 +315,9 @@ For validations, Paperclip introduces several validators to validate your attach
|
|
310
315
|
Example Usage:
|
311
316
|
|
312
317
|
```ruby
|
313
|
-
validates :avatar, :
|
314
|
-
validates_with AttachmentPresenceValidator, :
|
315
|
-
validates_with AttachmentSizeValidator, :
|
318
|
+
validates :avatar, attachment_presence: true
|
319
|
+
validates_with AttachmentPresenceValidator, attributes: :avatar
|
320
|
+
validates_with AttachmentSizeValidator, attributes: :avatar, less_than: 1.megabytes
|
316
321
|
|
317
322
|
```
|
318
323
|
|
@@ -331,9 +336,9 @@ validates_attachment_presence :avatar
|
|
331
336
|
Lastly, you can also define multiple validations on a single attachment using `validates_attachment`:
|
332
337
|
|
333
338
|
```ruby
|
334
|
-
validates_attachment :avatar, :
|
335
|
-
:
|
336
|
-
:
|
339
|
+
validates_attachment :avatar, presence: true,
|
340
|
+
content_type: { content_type: "image/jpeg" },
|
341
|
+
size: { in: 0..10.kilobytes }
|
337
342
|
```
|
338
343
|
|
339
344
|
_NOTE: Post-processing will not even **start** if the attachment is not valid
|
@@ -391,7 +396,7 @@ image-y ones:
|
|
391
396
|
|
392
397
|
```ruby
|
393
398
|
validates_attachment :avatar,
|
394
|
-
:
|
399
|
+
content_type: { content_type: ["image/jpeg", "image/gif", "image/png"] }
|
395
400
|
```
|
396
401
|
|
397
402
|
`Paperclip::ContentTypeDetector` will attempt to match a file's extension to an
|
@@ -399,6 +404,12 @@ inferred content_type, regardless of the actual contents of the file.
|
|
399
404
|
|
400
405
|
---
|
401
406
|
|
407
|
+
Internationalization (I18n)
|
408
|
+
---------------------------
|
409
|
+
|
410
|
+
For using or adding locale files in different languages, check the project
|
411
|
+
https://github.com/thoughtbot/paperclip-i18n.
|
412
|
+
|
402
413
|
Security Validations
|
403
414
|
====================
|
404
415
|
|
@@ -415,9 +426,9 @@ do this.
|
|
415
426
|
class ActiveRecord::Base
|
416
427
|
has_attached_file :avatar
|
417
428
|
# Validate content type
|
418
|
-
validates_attachment_content_type :avatar, :
|
429
|
+
validates_attachment_content_type :avatar, content_type: /\Aimage/
|
419
430
|
# Validate filename
|
420
|
-
validates_attachment_file_name :avatar, :
|
431
|
+
validates_attachment_file_name :avatar, matches: [/png\z/, /jpe?g\z/]
|
421
432
|
# Explicitly do not validate
|
422
433
|
do_not_validate_attachment_file_type :avatar
|
423
434
|
end
|
@@ -437,14 +448,13 @@ will not cause errors to be raised.
|
|
437
448
|
|
438
449
|
This can sometimes cause false validation errors in applications that use custom
|
439
450
|
file extensions. In these cases you may wish to add your custom extension to the
|
440
|
-
list of
|
441
|
-
gem:
|
451
|
+
list of content type mappings by creating `config/initializers/paperclip.rb`:
|
442
452
|
|
443
453
|
```ruby
|
444
454
|
# Allow ".foo" as an extension for files with the MIME type "text/plain".
|
445
|
-
|
446
|
-
|
447
|
-
|
455
|
+
Paperclip.options[:content_type_mappings] = {
|
456
|
+
foo: %w(text/plain)
|
457
|
+
}
|
448
458
|
```
|
449
459
|
|
450
460
|
---
|
@@ -460,7 +470,7 @@ module YourApp
|
|
460
470
|
class Application < Rails::Application
|
461
471
|
# Other code...
|
462
472
|
|
463
|
-
config.paperclip_defaults = {:
|
473
|
+
config.paperclip_defaults = { storage: :fog, fog_credentials: { provider: "Local", local_root: "#{Rails.root}/public"}, fog_directory: "", fog_host: "localhost"}
|
464
474
|
end
|
465
475
|
end
|
466
476
|
```
|
@@ -471,7 +481,7 @@ An example Rails initializer would look something like this:
|
|
471
481
|
|
472
482
|
```ruby
|
473
483
|
Paperclip::Attachment.default_options[:storage] = :fog
|
474
|
-
Paperclip::Attachment.default_options[:fog_credentials] = {:
|
484
|
+
Paperclip::Attachment.default_options[:fog_credentials] = { provider: "Local", local_root: "#{Rails.root}/public"}
|
475
485
|
Paperclip::Attachment.default_options[:fog_directory] = ""
|
476
486
|
Paperclip::Attachment.default_options[:fog_host] = "http://localhost:3000"
|
477
487
|
```
|
@@ -493,6 +503,7 @@ class CreateUsersWithAttachments < ActiveRecord::Migration
|
|
493
503
|
create_table :users do |t|
|
494
504
|
t.attachment :avatar
|
495
505
|
end
|
506
|
+
end
|
496
507
|
|
497
508
|
# This is assuming you are only using the users table for Paperclip attachment. Drop with care!
|
498
509
|
def down
|
@@ -539,7 +550,7 @@ class AddAttachmentColumnsToUsers < ActiveRecord::Migration
|
|
539
550
|
end
|
540
551
|
```
|
541
552
|
|
542
|
-
### Vintage
|
553
|
+
### Vintage Syntax
|
543
554
|
|
544
555
|
Vintage syntax (such as `t.has_attached_file` and `drop_attached_file`) is still supported in
|
545
556
|
Paperclip 3.x, but you're advised to update those migration files to use this new syntax.
|
@@ -558,6 +569,7 @@ Paperclip ships with 3 storage adapters:
|
|
558
569
|
If you would like to use Paperclip with another storage, you can install these
|
559
570
|
gems along side with Paperclip:
|
560
571
|
|
572
|
+
* [paperclip-azure](https://github.com/supportify/paperclip-azure)
|
561
573
|
* [paperclip-azure-storage](https://github.com/gmontard/paperclip-azure-storage)
|
562
574
|
* [paperclip-dropbox](https://github.com/janko-m/paperclip-dropbox)
|
563
575
|
|
@@ -566,9 +578,9 @@ gems along side with Paperclip:
|
|
566
578
|
The files that are assigned as attachments are, by default, placed in the
|
567
579
|
directory specified by the `:path` option to `has_attached_file`. By default, this
|
568
580
|
location is `:rails_root/public/system/:class/:attachment/:id_partition/:style/:filename`.
|
569
|
-
This location was chosen because on standard Capistrano deployments, the
|
570
|
-
`public/system` directory
|
571
|
-
|
581
|
+
This location was chosen because, on standard Capistrano deployments, the
|
582
|
+
`public/system` directory can be symlinked to the app's shared directory, meaning it
|
583
|
+
survives between deployments. For example, using that `:path`, you may have a
|
572
584
|
file at
|
573
585
|
|
574
586
|
/data/myapp/releases/20081229172410/public/system/users/avatar/000/000/013/small/my_pic.png
|
@@ -580,12 +592,12 @@ You may also choose to store your files using Amazon's S3 service. To do so, inc
|
|
580
592
|
the `aws-sdk` gem in your Gemfile:
|
581
593
|
|
582
594
|
```ruby
|
583
|
-
gem 'aws-sdk', '~>
|
595
|
+
gem 'aws-sdk', '~> 2.3.0'
|
584
596
|
```
|
585
597
|
|
586
598
|
And then you can specify using S3 from `has_attached_file`.
|
587
599
|
You can find more information about configuring and using S3 storage in
|
588
|
-
[the `Paperclip::Storage::S3` documentation](http://rubydoc.info/gems/paperclip/Paperclip/Storage/S3).
|
600
|
+
[the `Paperclip::Storage::S3` documentation](http://www.rubydoc.info/gems/paperclip/Paperclip/Storage/S3).
|
589
601
|
|
590
602
|
Files on the local filesystem (and in the Rails app's public directory) will be
|
591
603
|
available to the internet at large. If you require access control, it's
|
@@ -596,64 +608,113 @@ variables.
|
|
596
608
|
|
597
609
|
---
|
598
610
|
|
611
|
+
IO Adapters
|
612
|
+
-----------
|
613
|
+
|
614
|
+
When a file is uploaded or attached, it can be in one of a few different input
|
615
|
+
forms, from Rails' UploadedFile object to a StringIO to a Tempfile or even a
|
616
|
+
simple String that is a URL that points to an image.
|
617
|
+
|
618
|
+
Paperclip will accept, by default, many of these sources. It also is capable of
|
619
|
+
handling even more with a little configuration. The IO Adapters that handle
|
620
|
+
images from non-local sources are not enabled by default. They can be enabled by
|
621
|
+
adding a line similar to the following into `config/initializers/paperclip.rb`:
|
622
|
+
|
623
|
+
```ruby
|
624
|
+
Paperclip::DataUriAdapter.register
|
625
|
+
```
|
626
|
+
|
627
|
+
It's best to only enable a remote-loading adapter if you need it. Otherwise
|
628
|
+
there's a chance that someone can gain insight into your internal network
|
629
|
+
structure using it as a vector.
|
630
|
+
|
631
|
+
The following adapters are *not* loaded by default:
|
632
|
+
|
633
|
+
* `Paperclip::UriAdapter` - which accepts a `URI` instance.
|
634
|
+
* `Paperclip::HttpUrlProxyAdapter` - which accepts a `http` string.
|
635
|
+
* `Paperclip::DataUriAdapter` - which accepts a Base64-encoded `data:` string.
|
636
|
+
|
637
|
+
---
|
638
|
+
|
599
639
|
Post Processing
|
600
640
|
---------------
|
601
641
|
|
602
642
|
Paperclip supports an extensible selection of post-processors. When you define
|
603
643
|
a set of styles for an attachment, by default it is expected that those
|
604
|
-
"styles" are actually "thumbnails."
|
605
|
-
|
606
|
-
|
607
|
-
|
608
|
-
automatically loaded by Paperclip, allowing you to easily define custom
|
609
|
-
processors. You can specify a processor with the `:processors` option to
|
610
|
-
`has_attached_file`:
|
644
|
+
"styles" are actually "thumbnails." These are processed by
|
645
|
+
`Paperclip::Thumbnail`. For backward compatibility reasons you can pass either
|
646
|
+
a single geometry string, or an array containing a geometry and a format that
|
647
|
+
the file will be converted to, like so:
|
611
648
|
|
612
649
|
```ruby
|
613
|
-
has_attached_file :
|
614
|
-
:processors => [:ocr]
|
650
|
+
has_attached_file :avatar, styles: { thumb: ["32x32#", :png] }
|
615
651
|
```
|
616
652
|
|
617
|
-
This
|
618
|
-
|
619
|
-
|
653
|
+
This will convert the "thumb" style to a 32x32 square in PNG format, regardless
|
654
|
+
of what was uploaded. If the format is not specified, it is kept the same (e.g.
|
655
|
+
JPGs will remain JPGs). `Paperclip::Thumbnail` uses ImageMagick to process
|
656
|
+
images; [ImageMagick's geometry documentation](http://www.imagemagick.org/script/command-line-processing.php#geometry)
|
657
|
+
has more information on the accepted style formats.
|
658
|
+
|
659
|
+
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:
|
660
|
+
|
661
|
+
* `MAGICK_MEMORY_LIMIT=128MiB`
|
662
|
+
* `MAGICK_MAP_LIMIT=64MiB`
|
663
|
+
* `MAGICK_TIME_LIMIT=30`
|
664
|
+
|
665
|
+
For a full list of variables and description, see [ImageMagick's resources documentation](http://www.imagemagick.org/script/resources.php).
|
620
666
|
|
621
|
-
|
622
|
-
|
623
|
-
|
667
|
+
---
|
668
|
+
|
669
|
+
Custom Attachment Processors
|
670
|
+
-------
|
671
|
+
|
672
|
+
You can write your own custom attachment processors to carry out tasks like
|
673
|
+
adding watermarks, compressing images, or encrypting files. Custom processors
|
674
|
+
must be defined within the `Paperclip` module, inherit from
|
675
|
+
`Paperclip::Processor` (see [`lib/paperclip/processor.rb`](https://github.com/thoughtbot/paperclip/blob/master/lib/paperclip/processor.rb)),
|
676
|
+
and implement a `make` method that returns a `File`. All files in your Rails
|
677
|
+
app's `lib/paperclip` and `lib/paperclip_processors` directories will be
|
678
|
+
automatically loaded by Paperclip. Processors are specified using the
|
679
|
+
`:processors` option to `has_attached_file`:
|
624
680
|
|
625
681
|
```ruby
|
626
|
-
has_attached_file :
|
682
|
+
has_attached_file :scan, styles: { text: { quality: :better } },
|
683
|
+
processors: [:ocr]
|
627
684
|
```
|
628
685
|
|
629
|
-
This
|
630
|
-
|
631
|
-
JPGs will remain JPGs). For more information on the accepted style formats, see
|
632
|
-
[here](http://www.imagemagick.org/script/command-line-processing.php#geometry).
|
686
|
+
This would load the hypothetical class `Paperclip::Ocr`, and pass it the
|
687
|
+
options hash `{ quality: :better }`, along with the uploaded file.
|
633
688
|
|
634
689
|
Multiple processors can be specified, and they will be invoked in the order
|
635
|
-
they are defined in the `:processors` array. Each successive processor
|
636
|
-
|
637
|
-
|
638
|
-
|
690
|
+
they are defined in the `:processors` array. Each successive processor is given
|
691
|
+
the result from the previous processor. All processors receive the same
|
692
|
+
parameters, which are defined in the `:styles` hash. For example, assuming we
|
693
|
+
had this definition:
|
639
694
|
|
640
695
|
```ruby
|
641
|
-
has_attached_file :scan, :
|
642
|
-
:
|
696
|
+
has_attached_file :scan, styles: { text: { quality: :better } },
|
697
|
+
processors: [:rotator, :ocr]
|
643
698
|
```
|
644
699
|
|
645
|
-
|
646
|
-
options `{ :
|
647
|
-
|
700
|
+
Both the `:rotator` processor and the `:ocr` processor would receive the
|
701
|
+
options `{ quality: :better }`. If a processor receives an option it doesn't
|
702
|
+
recognise, it's expected to ignore it.
|
648
703
|
|
649
704
|
_NOTE: Because processors operate by turning the original attachment into the
|
650
705
|
styles, no processors will be run if there are no styles defined._
|
651
706
|
|
652
707
|
If you're interested in caching your thumbnail's width, height and size in the
|
653
|
-
database, take a look at the [paperclip-meta](https://github.com/teeparham/paperclip-meta)
|
708
|
+
database, take a look at the [paperclip-meta](https://github.com/teeparham/paperclip-meta)
|
709
|
+
gem.
|
654
710
|
|
655
711
|
Also, if you're interested in generating the thumbnail on-the-fly, you might want
|
656
|
-
to look into the [attachment_on_the_fly](https://github.com/drpentode/Attachment-on-the-Fly)
|
712
|
+
to look into the [attachment_on_the_fly](https://github.com/drpentode/Attachment-on-the-Fly)
|
713
|
+
gem.
|
714
|
+
|
715
|
+
Paperclip's thumbnail generator (see [`lib/paperclip/thumbnail.rb`](lib/paperclip/thumbnail.rb))
|
716
|
+
is implemented as a processor, and may be a good reference for writing your own
|
717
|
+
processors.
|
657
718
|
|
658
719
|
---
|
659
720
|
|
@@ -699,8 +760,8 @@ Example Usage:
|
|
699
760
|
|
700
761
|
```ruby
|
701
762
|
has_attached_file :avatar, {
|
702
|
-
:
|
703
|
-
:
|
763
|
+
url: "/system/:hash.:extension",
|
764
|
+
hash_secret: "longSecretString"
|
704
765
|
}
|
705
766
|
```
|
706
767
|
|
@@ -712,10 +773,10 @@ is specified in `:hash_data`. The default value for `:hash_data` is `":class/:at
|
|
712
773
|
|
713
774
|
For more on this feature, read [the author's own explanation](https://github.com/thoughtbot/paperclip/pull/416)
|
714
775
|
|
715
|
-
|
776
|
+
Checksum / Fingerprint
|
716
777
|
-------
|
717
778
|
|
718
|
-
|
779
|
+
A checksum of the original file assigned will be placed in the model if it
|
719
780
|
has an attribute named fingerprint. Following the user model migration example
|
720
781
|
above, the migration would look like the following:
|
721
782
|
|
@@ -731,6 +792,17 @@ class AddAvatarFingerprintColumnToUser < ActiveRecord::Migration
|
|
731
792
|
end
|
732
793
|
```
|
733
794
|
|
795
|
+
The algorithm can be specified using a configuration option; it defaults to MD5
|
796
|
+
for backwards compatibility with Paperclip 5 and earlier.
|
797
|
+
|
798
|
+
```ruby
|
799
|
+
has_attached_file :some_attachment, adapter_options: { hash_digest: Digest::SHA256 }
|
800
|
+
```
|
801
|
+
|
802
|
+
Run `CLASS=User ATTACHMENT=avatar rake paperclip:refresh:fingerprints` after
|
803
|
+
changing the digest on existing attachments to update the fingerprints in the
|
804
|
+
database.
|
805
|
+
|
734
806
|
File Preservation for Soft-Delete
|
735
807
|
-------
|
736
808
|
|
@@ -738,7 +810,7 @@ An option is available to preserve attachments in order to play nicely with soft
|
|
738
810
|
|
739
811
|
```ruby
|
740
812
|
has_attached_file :some_attachment, {
|
741
|
-
:
|
813
|
+
preserve_files: true,
|
742
814
|
}
|
743
815
|
```
|
744
816
|
|
@@ -746,25 +818,6 @@ This will prevent ```some_attachment``` from being wiped out when the model gets
|
|
746
818
|
|
747
819
|
---
|
748
820
|
|
749
|
-
Custom Attachment Processors
|
750
|
-
-------
|
751
|
-
|
752
|
-
Custom attachment processors can be implemented and their only requirement is
|
753
|
-
to inherit from `Paperclip::Processor` (see `lib/paperclip/processor.rb`).
|
754
|
-
For example, when `:styles` are specified for an image attachment, the
|
755
|
-
thumbnail processor (see `lib/paperclip/thumbnail.rb`) is loaded without having
|
756
|
-
to specify it as a `:processor` parameter to `has_attached_file`. When any
|
757
|
-
other processor is defined, it must be called out in the `:processors`
|
758
|
-
parameter if it is to be applied to the attachment. The thumbnail processor
|
759
|
-
uses the ImageMagick `convert` command to do the work of resizing image
|
760
|
-
thumbnails. It would be easy to create a custom processor that watermarks
|
761
|
-
an image using ImageMagick's `composite` command. Following the
|
762
|
-
implementation pattern of the thumbnail processor would be a way to implement a
|
763
|
-
watermark processor. All kinds of attachment processors can be created;
|
764
|
-
a few utility examples would be compression and encryption processors.
|
765
|
-
|
766
|
-
---
|
767
|
-
|
768
821
|
Dynamic Configuration
|
769
822
|
---------------------
|
770
823
|
|
@@ -786,7 +839,7 @@ look as follows where a boss will receive a `300x300` thumbnail otherwise a
|
|
786
839
|
|
787
840
|
```ruby
|
788
841
|
class User < ActiveRecord::Base
|
789
|
-
has_attached_file :avatar, :
|
842
|
+
has_attached_file :avatar, styles: lambda { |attachment| { thumb: (attachment.instance.boss? ? "300x300>" : "100x100>") } }
|
790
843
|
end
|
791
844
|
```
|
792
845
|
|
@@ -803,7 +856,7 @@ processors, where a defined `watermark` processor is invoked after the
|
|
803
856
|
|
804
857
|
```ruby
|
805
858
|
class User < ActiveRecord::Base
|
806
|
-
has_attached_file :avatar, :
|
859
|
+
has_attached_file :avatar, processors: lambda { |instance| instance.processors }
|
807
860
|
attr_accessor :processors
|
808
861
|
end
|
809
862
|
```
|
@@ -813,7 +866,7 @@ end
|
|
813
866
|
Logging
|
814
867
|
----------
|
815
868
|
|
816
|
-
By default Paperclip outputs logging according to your logger level. If you want to disable logging (e.g. during testing) add this
|
869
|
+
By default, Paperclip outputs logging according to your logger level. If you want to disable logging (e.g. during testing) add this into your environment's configuration:
|
817
870
|
```ruby
|
818
871
|
Your::Application.configure do
|
819
872
|
...
|
@@ -822,13 +875,23 @@ Your::Application.configure do
|
|
822
875
|
end
|
823
876
|
```
|
824
877
|
|
825
|
-
More information in the [rdocs](http://
|
878
|
+
More information in the [rdocs](http://www.rubydoc.info/github/thoughtbot/paperclip/Paperclip.options)
|
826
879
|
|
827
880
|
---
|
828
881
|
|
829
882
|
Deployment
|
830
883
|
----------
|
831
884
|
|
885
|
+
To make Capistrano symlink the `public/system` directory so that attachments
|
886
|
+
survive new deployments, set the `linked_dirs` option in your `config/deploy.rb`
|
887
|
+
file:
|
888
|
+
|
889
|
+
```ruby
|
890
|
+
set :linked_dirs, fetch(:linked_dirs, []).push('public/system')
|
891
|
+
```
|
892
|
+
|
893
|
+
### Attachment Styles
|
894
|
+
|
832
895
|
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
|
833
896
|
`rake paperclip:refresh:missing_styles`. It will store current attachment styles in `RAILS_ROOT/public/system/paperclip_attachments.yml`
|
834
897
|
by default. You can change it by:
|
@@ -840,9 +903,9 @@ Paperclip.registered_attachments_styles_path = '/tmp/config/paperclip_attachment
|
|
840
903
|
Here is an example for Capistrano:
|
841
904
|
|
842
905
|
```ruby
|
843
|
-
namespace :
|
906
|
+
namespace :paperclip do
|
844
907
|
desc "build missing paperclip styles"
|
845
|
-
task :
|
908
|
+
task :build_missing_styles do
|
846
909
|
on roles(:app) do
|
847
910
|
within release_path do
|
848
911
|
with rails_env: fetch(:rails_env) do
|
@@ -853,7 +916,7 @@ namespace :deploy do
|
|
853
916
|
end
|
854
917
|
end
|
855
918
|
|
856
|
-
after("deploy:compile_assets", "
|
919
|
+
after("deploy:compile_assets", "paperclip:build_missing_styles")
|
857
920
|
```
|
858
921
|
|
859
922
|
Now you don't have to remember to refresh thumbnails in production every time you add a new style.
|
@@ -864,12 +927,12 @@ Paperclip about existing styles. Simply create a `paperclip_attachments.yml` fil
|
|
864
927
|
|
865
928
|
```ruby
|
866
929
|
class User < ActiveRecord::Base
|
867
|
-
has_attached_file :avatar, :
|
930
|
+
has_attached_file :avatar, styles: { thumb: 'x100', croppable: '600x600>', big: '1000x1000>' }
|
868
931
|
end
|
869
932
|
|
870
933
|
class Book < ActiveRecord::Base
|
871
|
-
has_attached_file :cover, :
|
872
|
-
has_attached_file :sample, :
|
934
|
+
has_attached_file :cover, styles: { small: 'x100', large: '1000x1000>' }
|
935
|
+
has_attached_file :sample, styles: { thumb: 'x100' }
|
873
936
|
end
|
874
937
|
```
|
875
938
|
|
@@ -896,7 +959,7 @@ Testing
|
|
896
959
|
-------
|
897
960
|
|
898
961
|
Paperclip provides rspec-compatible matchers for testing attachments. See the
|
899
|
-
documentation on [Paperclip::Shoulda::Matchers](http://rubydoc.info/gems/paperclip/Paperclip/Shoulda/Matchers)
|
962
|
+
documentation on [Paperclip::Shoulda::Matchers](http://www.rubydoc.info/gems/paperclip/Paperclip/Shoulda/Matchers)
|
900
963
|
for more information.
|
901
964
|
|
902
965
|
**Parallel Tests**
|
@@ -936,6 +999,17 @@ config.after(:suite) do
|
|
936
999
|
FileUtils.rm_rf(Dir["#{Rails.root}/spec/test_files/"])
|
937
1000
|
end
|
938
1001
|
```
|
1002
|
+
|
1003
|
+
**Example of test configuration with Factory Girl**
|
1004
|
+
|
1005
|
+
|
1006
|
+
```ruby
|
1007
|
+
FactoryGirl.define do
|
1008
|
+
factory :user do
|
1009
|
+
avatar { File.new("#{Rails.root}/spec/support/fixtures/image.jpg") }
|
1010
|
+
end
|
1011
|
+
end
|
1012
|
+
```
|
939
1013
|
---
|
940
1014
|
|
941
1015
|
Contributing
|
@@ -951,20 +1025,20 @@ guidelines:
|
|
951
1025
|
about writing tests for paperclip, please open a
|
952
1026
|
[GitHub issue](https://github.com/thoughtbot/paperclip/issues/new).
|
953
1027
|
|
954
|
-
Please see `CONTRIBUTING.md` for more details on contributing and running test.
|
1028
|
+
Please see [`CONTRIBUTING.md`](./CONTRIBUTING.md) for more details on contributing and running test.
|
955
1029
|
|
956
|
-
Thank you to all [the contributors](https://github.com/thoughtbot/paperclip/contributors)!
|
1030
|
+
Thank you to all [the contributors](https://github.com/thoughtbot/paperclip/graphs/contributors)!
|
957
1031
|
|
958
1032
|
License
|
959
1033
|
-------
|
960
1034
|
|
961
|
-
Paperclip is Copyright © 2008-
|
1035
|
+
Paperclip is Copyright © 2008-2017 thoughtbot, inc. It is free software, and may be
|
962
1036
|
redistributed under the terms specified in the MIT-LICENSE file.
|
963
1037
|
|
964
1038
|
About thoughtbot
|
965
1039
|
----------------
|
966
1040
|
|
967
|
-
![thoughtbot](
|
1041
|
+
![thoughtbot](http://presskit.thoughtbot.com/images/thoughtbot-logo-for-readmes.svg)
|
968
1042
|
|
969
1043
|
Paperclip is maintained and funded by thoughtbot.
|
970
1044
|
The names and logos for thoughtbot are trademarks of thoughtbot, inc.
|