paperclip 3.4.0 → 6.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.codeclimate.yml +17 -0
- data/.github/issue_template.md +3 -0
- data/.gitignore +0 -6
- data/.hound.yml +1055 -0
- data/.rubocop.yml +1 -0
- data/.travis.yml +19 -12
- data/Appraisals +4 -11
- data/CONTRIBUTING.md +29 -13
- data/Gemfile +13 -4
- data/LICENSE +1 -3
- data/MIGRATING-ES.md +317 -0
- data/MIGRATING.md +375 -0
- data/NEWS +390 -71
- data/README.md +607 -152
- data/RELEASING.md +17 -0
- data/Rakefile +6 -8
- data/UPGRADING +12 -9
- data/features/basic_integration.feature +34 -21
- data/features/migration.feature +0 -24
- data/features/rake_tasks.feature +2 -3
- data/features/step_definitions/attachment_steps.rb +44 -36
- data/features/step_definitions/html_steps.rb +2 -2
- data/features/step_definitions/rails_steps.rb +125 -26
- data/features/step_definitions/s3_steps.rb +3 -3
- data/features/step_definitions/web_steps.rb +1 -103
- data/features/support/env.rb +3 -2
- data/features/support/fakeweb.rb +4 -1
- data/features/support/file_helpers.rb +12 -2
- data/features/support/fixtures/gemfile.txt +1 -1
- data/features/support/paths.rb +1 -1
- data/features/support/rails.rb +4 -11
- data/gemfiles/4.2.gemfile +17 -0
- data/gemfiles/5.0.gemfile +17 -0
- data/lib/generators/paperclip/paperclip_generator.rb +9 -3
- data/lib/generators/paperclip/templates/paperclip_migration.rb.erb +2 -2
- data/lib/paperclip/attachment.rb +215 -82
- data/lib/paperclip/attachment_registry.rb +60 -0
- data/lib/paperclip/callbacks.rb +13 -1
- data/lib/paperclip/content_type_detector.rb +48 -24
- data/lib/paperclip/errors.rb +8 -1
- data/lib/paperclip/file_command_content_type_detector.rb +6 -8
- data/lib/paperclip/filename_cleaner.rb +15 -0
- data/lib/paperclip/geometry_detector_factory.rb +12 -5
- data/lib/paperclip/geometry_parser_factory.rb +1 -1
- data/lib/paperclip/glue.rb +1 -2
- data/lib/paperclip/has_attached_file.rb +115 -0
- data/lib/paperclip/helpers.rb +15 -20
- data/lib/paperclip/interpolations/plural_cache.rb +18 -0
- data/lib/paperclip/interpolations.rb +36 -14
- data/lib/paperclip/io_adapters/abstract_adapter.rb +42 -5
- data/lib/paperclip/io_adapters/attachment_adapter.rb +20 -9
- data/lib/paperclip/io_adapters/data_uri_adapter.rb +22 -0
- data/lib/paperclip/io_adapters/empty_string_adapter.rb +19 -0
- data/lib/paperclip/io_adapters/file_adapter.rb +13 -7
- data/lib/paperclip/io_adapters/http_url_proxy_adapter.rb +16 -0
- 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 +15 -16
- data/lib/paperclip/io_adapters/uploaded_file_adapter.rb +11 -7
- data/lib/paperclip/io_adapters/uri_adapter.rb +43 -19
- data/lib/paperclip/locales/en.yml +1 -0
- data/lib/paperclip/logger.rb +1 -1
- data/lib/paperclip/matchers/have_attached_file_matcher.rb +3 -6
- data/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb +4 -4
- data/lib/paperclip/matchers/validate_attachment_presence_matcher.rb +7 -2
- data/lib/paperclip/matchers/validate_attachment_size_matcher.rb +2 -1
- data/lib/paperclip/matchers.rb +1 -1
- data/lib/paperclip/media_type_spoof_detector.rb +93 -0
- data/lib/paperclip/missing_attachment_styles.rb +11 -16
- data/lib/paperclip/processor.rb +15 -43
- data/lib/paperclip/processor_helpers.rb +50 -0
- data/lib/paperclip/rails_environment.rb +25 -0
- data/lib/paperclip/schema.rb +10 -8
- data/lib/paperclip/storage/filesystem.rb +20 -5
- data/lib/paperclip/storage/fog.rb +49 -23
- data/lib/paperclip/storage/s3.rb +153 -82
- data/lib/paperclip/style.rb +8 -3
- data/lib/paperclip/tempfile_factory.rb +6 -4
- data/lib/paperclip/thumbnail.rb +35 -19
- data/lib/paperclip/url_generator.rb +26 -14
- data/lib/paperclip/validators/attachment_content_type_validator.rb +15 -2
- data/lib/paperclip/validators/attachment_file_name_validator.rb +80 -0
- data/lib/paperclip/validators/attachment_file_type_ignorance_validator.rb +29 -0
- data/lib/paperclip/validators/attachment_presence_validator.rb +12 -8
- data/lib/paperclip/validators/attachment_size_validator.rb +17 -10
- data/lib/paperclip/validators/media_type_spoof_detection_validator.rb +31 -0
- data/lib/paperclip/validators.rb +31 -3
- data/lib/paperclip/version.rb +3 -1
- data/lib/paperclip.rb +41 -55
- data/lib/tasks/paperclip.rake +56 -9
- data/paperclip.gemspec +18 -17
- data/shoulda_macros/paperclip.rb +13 -3
- data/spec/paperclip/attachment_definitions_spec.rb +13 -0
- data/spec/paperclip/attachment_processing_spec.rb +79 -0
- data/spec/paperclip/attachment_registry_spec.rb +158 -0
- data/{test/attachment_test.rb → spec/paperclip/attachment_spec.rb} +597 -389
- data/spec/paperclip/content_type_detector_spec.rb +48 -0
- data/spec/paperclip/file_command_content_type_detector_spec.rb +40 -0
- data/spec/paperclip/filename_cleaner_spec.rb +13 -0
- data/spec/paperclip/geometry_detector_spec.rb +39 -0
- data/{test/geometry_parser_test.rb → spec/paperclip/geometry_parser_spec.rb} +27 -27
- data/{test/geometry_test.rb → spec/paperclip/geometry_spec.rb} +50 -52
- data/spec/paperclip/glue_spec.rb +44 -0
- data/spec/paperclip/has_attached_file_spec.rb +158 -0
- data/{test/integration_test.rb → spec/paperclip/integration_spec.rb} +179 -199
- data/{test/interpolations_test.rb → spec/paperclip/interpolations_spec.rb} +79 -46
- data/spec/paperclip/io_adapters/abstract_adapter_spec.rb +160 -0
- data/{test/io_adapters/attachment_adapter_test.rb → spec/paperclip/io_adapters/attachment_adapter_spec.rb} +54 -25
- data/spec/paperclip/io_adapters/data_uri_adapter_spec.rb +89 -0
- data/spec/paperclip/io_adapters/empty_string_adapter_spec.rb +17 -0
- data/spec/paperclip/io_adapters/file_adapter_spec.rb +131 -0
- data/spec/paperclip/io_adapters/http_url_proxy_adapter_spec.rb +138 -0
- data/spec/paperclip/io_adapters/identity_adapter_spec.rb +8 -0
- data/{test/io_adapters/nil_adapter_test.rb → spec/paperclip/io_adapters/nil_adapter_spec.rb} +7 -7
- data/{test/io_adapters/registry_test.rb → spec/paperclip/io_adapters/registry_spec.rb} +12 -9
- data/spec/paperclip/io_adapters/stringio_adapter_spec.rb +64 -0
- data/spec/paperclip/io_adapters/uploaded_file_adapter_spec.rb +146 -0
- data/spec/paperclip/io_adapters/uri_adapter_spec.rb +220 -0
- data/spec/paperclip/matchers/have_attached_file_matcher_spec.rb +19 -0
- data/spec/paperclip/matchers/validate_attachment_content_type_matcher_spec.rb +109 -0
- data/spec/paperclip/matchers/validate_attachment_presence_matcher_spec.rb +69 -0
- data/spec/paperclip/matchers/validate_attachment_size_matcher_spec.rb +88 -0
- data/spec/paperclip/media_type_spoof_detector_spec.rb +120 -0
- data/spec/paperclip/meta_class_spec.rb +30 -0
- data/spec/paperclip/paperclip_missing_attachment_styles_spec.rb +84 -0
- data/spec/paperclip/paperclip_spec.rb +192 -0
- data/spec/paperclip/plural_cache_spec.rb +37 -0
- data/spec/paperclip/processor_helpers_spec.rb +57 -0
- data/{test/processor_test.rb → spec/paperclip/processor_spec.rb} +7 -7
- data/spec/paperclip/rails_environment_spec.rb +33 -0
- data/spec/paperclip/rake_spec.rb +103 -0
- data/spec/paperclip/schema_spec.rb +248 -0
- data/{test/storage/filesystem_test.rb → spec/paperclip/storage/filesystem_spec.rb} +18 -18
- data/spec/paperclip/storage/fog_spec.rb +566 -0
- data/spec/paperclip/storage/s3_live_spec.rb +188 -0
- data/spec/paperclip/storage/s3_spec.rb +1693 -0
- data/spec/paperclip/style_spec.rb +254 -0
- data/spec/paperclip/tempfile_factory_spec.rb +33 -0
- data/spec/paperclip/tempfile_spec.rb +35 -0
- data/{test/thumbnail_test.rb → spec/paperclip/thumbnail_spec.rb} +186 -141
- data/spec/paperclip/url_generator_spec.rb +221 -0
- data/spec/paperclip/validators/attachment_content_type_validator_spec.rb +322 -0
- data/spec/paperclip/validators/attachment_file_name_validator_spec.rb +160 -0
- data/{test/validators/attachment_presence_validator_test.rb → spec/paperclip/validators/attachment_presence_validator_spec.rb} +20 -20
- data/{test/validators/attachment_size_validator_test.rb → spec/paperclip/validators/attachment_size_validator_spec.rb} +87 -59
- data/spec/paperclip/validators/media_type_spoof_detection_validator_spec.rb +52 -0
- data/spec/paperclip/validators_spec.rb +164 -0
- data/spec/spec_helper.rb +46 -0
- data/spec/support/assertions.rb +82 -0
- data/spec/support/fake_model.rb +25 -0
- data/spec/support/fake_rails.rb +12 -0
- data/spec/support/fixtures/empty.html +1 -0
- data/spec/support/fixtures/empty.xlsx +0 -0
- data/spec/support/fixtures/spaced file.jpg +0 -0
- data/spec/support/matchers/accept.rb +5 -0
- data/spec/support/matchers/exist.rb +5 -0
- data/spec/support/matchers/have_column.rb +23 -0
- data/{test → spec}/support/mock_attachment.rb +2 -0
- data/{test → spec}/support/mock_url_generator_builder.rb +2 -2
- data/spec/support/model_reconstruction.rb +68 -0
- data/spec/support/reporting.rb +11 -0
- data/spec/support/test_data.rb +13 -0
- data/spec/support/version_helper.rb +9 -0
- metadata +395 -346
- data/Gemfile.lock +0 -200
- data/RUNNING_TESTS.md +0 -4
- data/cucumber/paperclip_steps.rb +0 -6
- data/gemfiles/3.0.gemfile +0 -11
- data/gemfiles/3.1.gemfile +0 -11
- data/gemfiles/3.2.gemfile +0 -11
- data/lib/paperclip/attachment_options.rb +0 -9
- data/lib/paperclip/instance_methods.rb +0 -35
- data/test/attachment_options_test.rb +0 -27
- data/test/attachment_processing_test.rb +0 -29
- data/test/content_type_detector_test.rb +0 -40
- data/test/file_command_content_type_detector_test.rb +0 -25
- data/test/generator_test.rb +0 -80
- data/test/geometry_detector_test.rb +0 -24
- data/test/helper.rb +0 -199
- data/test/io_adapters/abstract_adapter_test.rb +0 -50
- data/test/io_adapters/file_adapter_test.rb +0 -100
- data/test/io_adapters/identity_adapter_test.rb +0 -8
- data/test/io_adapters/stringio_adapter_test.rb +0 -51
- data/test/io_adapters/uploaded_file_adapter_test.rb +0 -123
- data/test/io_adapters/uri_adapter_test.rb +0 -86
- data/test/matchers/have_attached_file_matcher_test.rb +0 -24
- data/test/matchers/validate_attachment_content_type_matcher_test.rb +0 -110
- data/test/matchers/validate_attachment_presence_matcher_test.rb +0 -47
- data/test/matchers/validate_attachment_size_matcher_test.rb +0 -86
- data/test/meta_class_test.rb +0 -32
- data/test/paperclip_missing_attachment_styles_test.rb +0 -94
- data/test/paperclip_test.rb +0 -259
- data/test/schema_test.rb +0 -200
- data/test/storage/fog_test.rb +0 -453
- data/test/storage/s3_live_test.rb +0 -179
- data/test/storage/s3_test.rb +0 -1236
- data/test/style_test.rb +0 -213
- data/test/support/mock_model.rb +0 -2
- data/test/tempfile_factory_test.rb +0 -13
- data/test/url_generator_test.rb +0 -187
- data/test/validators/attachment_content_type_validator_test.rb +0 -292
- data/test/validators_test.rb +0 -25
- /data/{test → spec}/database.yml +0 -0
- /data/{test → spec/support}/fixtures/12k.png +0 -0
- /data/{test → spec/support}/fixtures/50x50.png +0 -0
- /data/{test → spec/support}/fixtures/5k.png +0 -0
- /data/{test → spec/support}/fixtures/animated +0 -0
- /data/{test → spec/support}/fixtures/animated.gif +0 -0
- /data/{test → spec/support}/fixtures/animated.unknown +0 -0
- /data/{test → spec/support}/fixtures/bad.png +0 -0
- /data/{test → spec/support}/fixtures/fog.yml +0 -0
- /data/{test → spec/support}/fixtures/rotated.jpg +0 -0
- /data/{test → spec/support}/fixtures/s3.yml +0 -0
- /data/{test → spec/support}/fixtures/spaced file.png +0 -0
- /data/{test → spec/support}/fixtures/text.txt +0 -0
- /data/{test → spec/support}/fixtures/twopage.pdf +0 -0
- /data/{test → spec/support}/fixtures/uppercase.PNG +0 -0
- /data/{test → spec}/support/mock_interpolator.rb +0 -0
data/lib/paperclip/attachment.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
|
-
# encoding: utf-8
|
2
1
|
require 'uri'
|
3
2
|
require 'paperclip/url_generator'
|
3
|
+
require 'active_support/deprecation'
|
4
|
+
require 'active_support/core_ext/string/inflections'
|
4
5
|
|
5
6
|
module Paperclip
|
6
7
|
# The Attachment class manages the files for a given attachment. It saves
|
@@ -14,6 +15,7 @@ module Paperclip
|
|
14
15
|
:default_url => "/:attachment/:style/missing.png",
|
15
16
|
:escape_url => true,
|
16
17
|
:restricted_characters => /[&$+,\/:;=?@<>\[\]\{\}\|\\\^~%# ]/,
|
18
|
+
:filename_cleaner => nil,
|
17
19
|
:hash_data => ":class/:attachment/:id/:style/:updated_at",
|
18
20
|
:hash_digest => "SHA1",
|
19
21
|
:interpolator => Paperclip::Interpolations,
|
@@ -28,16 +30,19 @@ module Paperclip
|
|
28
30
|
:url_generator => Paperclip::UrlGenerator,
|
29
31
|
:use_default_time_zone => true,
|
30
32
|
:use_timestamp => true,
|
31
|
-
:whiny => Paperclip.options[:whiny] || Paperclip.options[:whiny_thumbnails]
|
33
|
+
:whiny => Paperclip.options[:whiny] || Paperclip.options[:whiny_thumbnails],
|
34
|
+
:validate_media_type => true,
|
35
|
+
:adapter_options => { hash_digest: Digest::MD5 },
|
36
|
+
:check_validity_before_processing => true
|
32
37
|
}
|
33
38
|
end
|
34
39
|
|
35
40
|
attr_reader :name, :instance, :default_style, :convert_options, :queued_for_write, :whiny,
|
36
|
-
:options, :interpolator, :source_file_options
|
41
|
+
:options, :interpolator, :source_file_options
|
37
42
|
attr_accessor :post_processing
|
38
43
|
|
39
44
|
# Creates an Attachment object. +name+ is the name of the attachment,
|
40
|
-
# +instance+ is the
|
45
|
+
# +instance+ is the model object instance it's attached to, and
|
41
46
|
# +options+ is the same as the hash passed to +has_attached_file+.
|
42
47
|
#
|
43
48
|
# Options include:
|
@@ -45,7 +50,8 @@ module Paperclip
|
|
45
50
|
# +url+ - a relative URL of the attachment. This is interpolated using +interpolator+
|
46
51
|
# +path+ - where on the filesystem to store the attachment. This is interpolated using +interpolator+
|
47
52
|
# +styles+ - a hash of options for processing the attachment. See +has_attached_file+ for the details
|
48
|
-
# +only_process+ - style args to be run through the post-processor. This defaults to the empty list
|
53
|
+
# +only_process+ - style args to be run through the post-processor. This defaults to the empty list (which is
|
54
|
+
# a special case that indicates all styles should be processed)
|
49
55
|
# +default_url+ - a URL for the missing image
|
50
56
|
# +default_style+ - the style to use when an argument is not specified e.g. #url, #path
|
51
57
|
# +storage+ - the storage mechanism. Defaults to :filesystem
|
@@ -59,14 +65,16 @@ module Paperclip
|
|
59
65
|
# +source_file_options+ - flags passed to the +convert+ command that controls how the file is read
|
60
66
|
# +processors+ - classes that transform the attachment. Defaults to [:thumbnail]
|
61
67
|
# +preserve_files+ - whether to keep files on the filesystem when deleting or clearing the attachment. Defaults to false
|
68
|
+
# +filename_cleaner+ - An object that responds to #call(filename) that will strip unacceptable charcters from filename
|
62
69
|
# +interpolator+ - the object used to interpolate filenames and URLs. Defaults to Paperclip::Interpolations
|
63
70
|
# +url_generator+ - the object used to generate URLs, using the interpolator. Defaults to Paperclip::UrlGenerator
|
64
71
|
# +escape_url+ - Perform URI escaping to URLs. Defaults to true
|
65
72
|
def initialize(name, instance, options = {})
|
66
|
-
@name = name
|
73
|
+
@name = name.to_sym
|
74
|
+
@name_string = name.to_s
|
67
75
|
@instance = instance
|
68
76
|
|
69
|
-
options = self.class.default_options.
|
77
|
+
options = self.class.default_options.deep_merge(options)
|
70
78
|
|
71
79
|
@options = options
|
72
80
|
@post_processing = true
|
@@ -75,7 +83,7 @@ module Paperclip
|
|
75
83
|
@errors = {}
|
76
84
|
@dirty = false
|
77
85
|
@interpolator = options[:interpolator]
|
78
|
-
@url_generator = options[:url_generator].new(self
|
86
|
+
@url_generator = options[:url_generator].new(self)
|
79
87
|
@source_file_options = options[:source_file_options]
|
80
88
|
@whiny = options[:whiny]
|
81
89
|
|
@@ -83,33 +91,30 @@ module Paperclip
|
|
83
91
|
end
|
84
92
|
|
85
93
|
# What gets called when you call instance.attachment = File. It clears
|
86
|
-
# errors, assigns attributes, and processes the file. It
|
87
|
-
#
|
88
|
-
#
|
89
|
-
#
|
94
|
+
# errors, assigns attributes, and processes the file. It also queues up the
|
95
|
+
# previous file for deletion, to be flushed away on #save of its host. In
|
96
|
+
# addition to form uploads, you can also assign another Paperclip
|
97
|
+
# attachment:
|
90
98
|
# new_user.avatar = old_user.avatar
|
91
|
-
def assign
|
99
|
+
def assign(uploaded_file)
|
100
|
+
@file = Paperclip.io_adapters.for(uploaded_file,
|
101
|
+
@options[:adapter_options])
|
92
102
|
ensure_required_accessors!
|
93
|
-
|
103
|
+
ensure_required_validations!
|
94
104
|
|
95
|
-
|
96
|
-
|
105
|
+
if @file.assignment?
|
106
|
+
clear(*only_process)
|
97
107
|
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
post_process(*only_process) if post_processing && valid_assignment?
|
109
|
-
|
110
|
-
# Reset the file size if the original file was reprocessed.
|
111
|
-
instance_write(:file_size, @queued_for_write[:original].size)
|
112
|
-
instance_write(:fingerprint, @queued_for_write[:original].fingerprint) if instance_respond_to?(:fingerprint)
|
108
|
+
if @file.nil?
|
109
|
+
nil
|
110
|
+
else
|
111
|
+
assign_attributes
|
112
|
+
post_process_file
|
113
|
+
reset_file_if_original_reprocessed
|
114
|
+
end
|
115
|
+
else
|
116
|
+
nil
|
117
|
+
end
|
113
118
|
end
|
114
119
|
|
115
120
|
# Returns the public URL of the attachment with a given style. This does
|
@@ -134,9 +139,8 @@ module Paperclip
|
|
134
139
|
#
|
135
140
|
# +#new(Paperclip::Attachment, options_hash)+
|
136
141
|
# +#for(style_name, options_hash)+
|
137
|
-
def url(style_name = default_style, options = {})
|
138
|
-
default_options = {:timestamp => @options[:use_timestamp], :escape => @options[:escape_url]}
|
139
142
|
|
143
|
+
def url(style_name = default_style, options = {})
|
140
144
|
if options == true || options == false # Backwards compatibility.
|
141
145
|
@url_generator.for(style_name, default_options.merge(:timestamp => options))
|
142
146
|
else
|
@@ -144,6 +148,20 @@ module Paperclip
|
|
144
148
|
end
|
145
149
|
end
|
146
150
|
|
151
|
+
def default_options
|
152
|
+
{
|
153
|
+
:timestamp => @options[:use_timestamp],
|
154
|
+
:escape => @options[:escape_url]
|
155
|
+
}
|
156
|
+
end
|
157
|
+
|
158
|
+
# Alias to +url+ that allows using the expiring_url method provided by the cloud
|
159
|
+
# storage implementations, but keep using filesystem storage for development and
|
160
|
+
# testing.
|
161
|
+
def expiring_url(time = 3600, style_name = default_style)
|
162
|
+
url(style_name)
|
163
|
+
end
|
164
|
+
|
147
165
|
# Returns the path of the attachment as defined by the :path option. If the
|
148
166
|
# file is stored in the filesystem the path refers to the path of the file
|
149
167
|
# on disk. If the file is stored in S3, the path is the "key" part of the
|
@@ -153,6 +171,18 @@ module Paperclip
|
|
153
171
|
path.respond_to?(:unescape) ? path.unescape : path
|
154
172
|
end
|
155
173
|
|
174
|
+
# :nodoc:
|
175
|
+
def staged_path(style_name = default_style)
|
176
|
+
if staged?
|
177
|
+
@queued_for_write[style_name].path
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
# :nodoc:
|
182
|
+
def staged?
|
183
|
+
! @queued_for_write.empty?
|
184
|
+
end
|
185
|
+
|
156
186
|
# Alias to +url+
|
157
187
|
def to_s style_name = default_style
|
158
188
|
url(style_name)
|
@@ -209,6 +239,10 @@ module Paperclip
|
|
209
239
|
# the instance's errors and returns false, cancelling the save.
|
210
240
|
def save
|
211
241
|
flush_deletes unless @options[:keep_old_files]
|
242
|
+
process = only_process
|
243
|
+
if process.any? && !process.include?(:original)
|
244
|
+
@queued_for_write.except!(:original)
|
245
|
+
end
|
212
246
|
flush_writes
|
213
247
|
@dirty = false
|
214
248
|
true
|
@@ -231,10 +265,8 @@ module Paperclip
|
|
231
265
|
# nil to the attachment *and saving*. This is permanent. If you wish to
|
232
266
|
# wipe out the existing attachment but not save, use #clear.
|
233
267
|
def destroy
|
234
|
-
|
235
|
-
|
236
|
-
save
|
237
|
-
end
|
268
|
+
clear
|
269
|
+
save
|
238
270
|
end
|
239
271
|
|
240
272
|
# Returns the uploaded file if present.
|
@@ -297,12 +329,23 @@ module Paperclip
|
|
297
329
|
OpenSSL::HMAC.hexdigest(OpenSSL::Digest.const_get(@options[:hash_digest]).new, @options[:hash_secret], data)
|
298
330
|
end
|
299
331
|
|
300
|
-
# This method really shouldn't be called that often.
|
332
|
+
# This method really shouldn't be called that often. Its expected use is
|
301
333
|
# in the paperclip:refresh rake task and that's it. It will regenerate all
|
302
334
|
# thumbnails forcefully, by reobtaining the original file and going through
|
303
335
|
# the post-process again.
|
336
|
+
# NOTE: Calling reprocess WILL NOT delete existing files. This is due to
|
337
|
+
# inconsistencies in timing of S3 commands. It's possible that calling
|
338
|
+
# #reprocess! will lose data if the files are not kept.
|
304
339
|
def reprocess!(*style_args)
|
305
|
-
|
340
|
+
saved_flags = @options.slice(
|
341
|
+
:only_process,
|
342
|
+
:preserve_files,
|
343
|
+
:check_validity_before_processing
|
344
|
+
)
|
345
|
+
@options[:only_process] = style_args
|
346
|
+
@options[:preserve_files] = true
|
347
|
+
@options[:check_validity_before_processing] = false
|
348
|
+
|
306
349
|
begin
|
307
350
|
assign(self)
|
308
351
|
save
|
@@ -311,13 +354,13 @@ module Paperclip
|
|
311
354
|
warn "#{e} - skipping file."
|
312
355
|
false
|
313
356
|
ensure
|
314
|
-
@options
|
357
|
+
@options.merge!(saved_flags)
|
315
358
|
end
|
316
359
|
end
|
317
360
|
|
318
361
|
# Returns true if a file has been assigned.
|
319
362
|
def file?
|
320
|
-
|
363
|
+
original_filename.present?
|
321
364
|
end
|
322
365
|
|
323
366
|
alias :present? :file?
|
@@ -336,7 +379,7 @@ module Paperclip
|
|
336
379
|
# instance_write(:file_name, "me.jpg") will write "me.jpg" to the instance's
|
337
380
|
# "avatar_file_name" field (assuming the attachment is called avatar).
|
338
381
|
def instance_write(attr, value)
|
339
|
-
setter = :"#{
|
382
|
+
setter = :"#{@name_string}_#{attr}="
|
340
383
|
if instance.respond_to?(setter)
|
341
384
|
instance.send(setter, value)
|
342
385
|
end
|
@@ -345,7 +388,7 @@ module Paperclip
|
|
345
388
|
# Reads the attachment-specific attribute on the instance. See instance_write
|
346
389
|
# for more details.
|
347
390
|
def instance_read(attr)
|
348
|
-
getter = :"#{
|
391
|
+
getter = :"#{@name_string}_#{attr}"
|
349
392
|
if instance.respond_to?(getter)
|
350
393
|
instance.send(getter)
|
351
394
|
end
|
@@ -357,10 +400,24 @@ module Paperclip
|
|
357
400
|
@options[:path].respond_to?(:call) ? @options[:path].call(self) : @options[:path]
|
358
401
|
end
|
359
402
|
|
403
|
+
def active_validator_classes
|
404
|
+
@instance.class.validators.map(&:class)
|
405
|
+
end
|
406
|
+
|
407
|
+
def missing_required_validator?
|
408
|
+
(active_validator_classes.flat_map(&:ancestors) & Paperclip::REQUIRED_VALIDATORS).empty?
|
409
|
+
end
|
410
|
+
|
411
|
+
def ensure_required_validations!
|
412
|
+
if missing_required_validator?
|
413
|
+
raise Paperclip::Errors::MissingRequiredValidatorError
|
414
|
+
end
|
415
|
+
end
|
416
|
+
|
360
417
|
def ensure_required_accessors! #:nodoc:
|
361
418
|
%w(file_name).each do |field|
|
362
|
-
unless @instance.respond_to?("#{
|
363
|
-
raise Paperclip::Error.new("#{@instance.class} model missing required attr_accessor for '#{
|
419
|
+
unless @instance.respond_to?("#{@name_string}_#{field}") && @instance.respond_to?("#{@name_string}_#{field}=")
|
420
|
+
raise Paperclip::Error.new("#{@instance.class} model missing required attr_accessor for '#{@name_string}_#{field}'")
|
364
421
|
end
|
365
422
|
end
|
366
423
|
end
|
@@ -369,16 +426,6 @@ module Paperclip
|
|
369
426
|
Paperclip.log(message)
|
370
427
|
end
|
371
428
|
|
372
|
-
def valid_assignment? #:nodoc:
|
373
|
-
if instance.valid?
|
374
|
-
true
|
375
|
-
else
|
376
|
-
instance.errors.none? do |attr, message|
|
377
|
-
attr.to_s.start_with?(@name.to_s)
|
378
|
-
end
|
379
|
-
end
|
380
|
-
end
|
381
|
-
|
382
429
|
def initialize_storage #:nodoc:
|
383
430
|
storage_class_name = @options[:storage].to_s.downcase.camelize
|
384
431
|
begin
|
@@ -389,19 +436,73 @@ module Paperclip
|
|
389
436
|
self.extend(storage_module)
|
390
437
|
end
|
391
438
|
|
392
|
-
def
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
439
|
+
def assign_attributes
|
440
|
+
@queued_for_write[:original] = @file
|
441
|
+
assign_file_information
|
442
|
+
assign_fingerprint { @file.fingerprint }
|
443
|
+
assign_timestamps
|
444
|
+
end
|
397
445
|
|
398
|
-
|
446
|
+
def assign_file_information
|
447
|
+
instance_write(:file_name, cleanup_filename(@file.original_filename))
|
448
|
+
instance_write(:content_type, @file.content_type.to_s.strip)
|
449
|
+
instance_write(:file_size, @file.size)
|
450
|
+
end
|
451
|
+
|
452
|
+
def assign_fingerprint
|
453
|
+
if instance_respond_to?(:fingerprint)
|
454
|
+
instance_write(:fingerprint, yield)
|
455
|
+
end
|
456
|
+
end
|
457
|
+
|
458
|
+
def assign_timestamps
|
459
|
+
if has_enabled_but_unset_created_at?
|
460
|
+
instance_write(:created_at, Time.now)
|
461
|
+
end
|
462
|
+
|
463
|
+
instance_write(:updated_at, Time.now)
|
464
|
+
end
|
465
|
+
|
466
|
+
def post_process_file
|
467
|
+
dirty!
|
468
|
+
|
469
|
+
if post_processing
|
470
|
+
post_process(*only_process)
|
471
|
+
end
|
472
|
+
end
|
473
|
+
|
474
|
+
def dirty!
|
475
|
+
@dirty = true
|
476
|
+
end
|
477
|
+
|
478
|
+
def reset_file_if_original_reprocessed
|
479
|
+
instance_write(:file_size, @queued_for_write[:original].size)
|
480
|
+
assign_fingerprint { @queued_for_write[:original].fingerprint }
|
481
|
+
reset_updater
|
482
|
+
end
|
483
|
+
|
484
|
+
def reset_updater
|
485
|
+
if instance.respond_to?(updater)
|
486
|
+
instance.send(updater)
|
487
|
+
end
|
488
|
+
end
|
489
|
+
|
490
|
+
def updater
|
491
|
+
:"#{name}_file_name_will_change!"
|
492
|
+
end
|
493
|
+
|
494
|
+
def extra_options_for(style) #:nodoc:
|
495
|
+
process_options(:convert_options, style)
|
399
496
|
end
|
400
497
|
|
401
498
|
def extra_source_file_options_for(style) #:nodoc:
|
402
|
-
|
499
|
+
process_options(:source_file_options, style)
|
500
|
+
end
|
501
|
+
|
502
|
+
def process_options(options_type, style) #:nodoc:
|
503
|
+
all_options = @options[options_type][:all]
|
403
504
|
all_options = all_options.call(instance) if all_options.respond_to?(:call)
|
404
|
-
style_options = @options[
|
505
|
+
style_options = @options[options_type][style]
|
405
506
|
style_options = style_options.call(instance) if style_options.respond_to?(:call)
|
406
507
|
|
407
508
|
[ style_options, all_options ].compact.join(" ")
|
@@ -412,7 +513,9 @@ module Paperclip
|
|
412
513
|
|
413
514
|
instance.run_paperclip_callbacks(:post_process) do
|
414
515
|
instance.run_paperclip_callbacks(:"#{name}_post_process") do
|
415
|
-
|
516
|
+
if !@options[:check_validity_before_processing] || !instance.errors.any?
|
517
|
+
post_process_styles(*style_args)
|
518
|
+
end
|
416
519
|
end
|
417
520
|
end
|
418
521
|
end
|
@@ -427,13 +530,30 @@ module Paperclip
|
|
427
530
|
def post_process_style(name, style) #:nodoc:
|
428
531
|
begin
|
429
532
|
raise RuntimeError.new("Style #{name} has no processors defined.") if style.processors.blank?
|
430
|
-
|
431
|
-
|
533
|
+
intermediate_files = []
|
534
|
+
original = @queued_for_write[:original]
|
535
|
+
|
536
|
+
@queued_for_write[name] = style.processors.
|
537
|
+
reduce(original) do |file, processor|
|
538
|
+
file = Paperclip.processor(processor).make(file, style.processor_options, self)
|
539
|
+
intermediate_files << file unless file == @queued_for_write[:original]
|
540
|
+
# if we're processing the original, close + unlink the source tempfile
|
541
|
+
if name == :original
|
542
|
+
@queued_for_write[:original].close(true)
|
543
|
+
end
|
544
|
+
file
|
432
545
|
end
|
433
|
-
|
434
|
-
|
546
|
+
|
547
|
+
unadapted_file = @queued_for_write[name]
|
548
|
+
@queued_for_write[name] = Paperclip.io_adapters.
|
549
|
+
for(@queued_for_write[name], @options[:adapter_options])
|
550
|
+
unadapted_file.close if unadapted_file.respond_to?(:close)
|
551
|
+
@queued_for_write[name]
|
552
|
+
rescue Paperclip::Errors::NotIdentifiedByImageMagickError => e
|
435
553
|
log("An error was received while processing: #{e.inspect}")
|
436
554
|
(@errors[:processing] ||= []) << e.message if @options[:whiny]
|
555
|
+
ensure
|
556
|
+
unlink_files(intermediate_files)
|
437
557
|
end
|
438
558
|
end
|
439
559
|
|
@@ -452,10 +572,12 @@ module Paperclip
|
|
452
572
|
end
|
453
573
|
|
454
574
|
def queue_all_for_delete #:nodoc:
|
455
|
-
return if
|
456
|
-
@
|
457
|
-
|
458
|
-
|
575
|
+
return if !file?
|
576
|
+
unless @options[:preserve_files]
|
577
|
+
@queued_for_delete += [:original, *styles.keys].uniq.map do |style|
|
578
|
+
path(style) if exists?(style)
|
579
|
+
end.compact
|
580
|
+
end
|
459
581
|
instance_write(:file_name, nil)
|
460
582
|
instance_write(:content_type, nil)
|
461
583
|
instance_write(:file_size, nil)
|
@@ -470,22 +592,33 @@ module Paperclip
|
|
470
592
|
end
|
471
593
|
end
|
472
594
|
|
473
|
-
# called by storage after the writes are flushed and before @
|
595
|
+
# called by storage after the writes are flushed and before @queued_for_write is cleared
|
474
596
|
def after_flush_writes
|
475
|
-
|
476
|
-
file.close unless file.closed?
|
477
|
-
file.unlink if file.respond_to?(:unlink) && file.path.present? && File.exist?(file.path)
|
478
|
-
end
|
597
|
+
unlink_files(@queued_for_write.values)
|
479
598
|
end
|
480
599
|
|
481
|
-
def
|
482
|
-
|
483
|
-
|
484
|
-
|
485
|
-
|
600
|
+
def unlink_files(files)
|
601
|
+
Array(files).each do |file|
|
602
|
+
file.close unless file.closed?
|
603
|
+
|
604
|
+
begin
|
605
|
+
file.unlink if file.respond_to?(:unlink)
|
606
|
+
rescue Errno::ENOENT
|
607
|
+
end
|
486
608
|
end
|
487
609
|
end
|
488
610
|
|
611
|
+
# You can either specifiy :restricted_characters or you can define your own
|
612
|
+
# :filename_cleaner object. This object needs to respond to #call and takes
|
613
|
+
# the filename that will be cleaned. It should return the cleaned filename.
|
614
|
+
def filename_cleaner
|
615
|
+
@options[:filename_cleaner] || FilenameCleaner.new(@options[:restricted_characters])
|
616
|
+
end
|
617
|
+
|
618
|
+
def cleanup_filename(filename)
|
619
|
+
filename_cleaner.call(filename)
|
620
|
+
end
|
621
|
+
|
489
622
|
# Check if attachment database table has a created_at field
|
490
623
|
def able_to_store_created_at?
|
491
624
|
@instance.respond_to?("#{name}_created_at".to_sym)
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'singleton'
|
2
|
+
|
3
|
+
module Paperclip
|
4
|
+
class AttachmentRegistry
|
5
|
+
include Singleton
|
6
|
+
|
7
|
+
def self.register(klass, attachment_name, attachment_options)
|
8
|
+
instance.register(klass, attachment_name, attachment_options)
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.clear
|
12
|
+
instance.clear
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.names_for(klass)
|
16
|
+
instance.names_for(klass)
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.each_definition(&block)
|
20
|
+
instance.each_definition(&block)
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.definitions_for(klass)
|
24
|
+
instance.definitions_for(klass)
|
25
|
+
end
|
26
|
+
|
27
|
+
def initialize
|
28
|
+
clear
|
29
|
+
end
|
30
|
+
|
31
|
+
def register(klass, attachment_name, attachment_options)
|
32
|
+
@attachments ||= {}
|
33
|
+
@attachments[klass] ||= {}
|
34
|
+
@attachments[klass][attachment_name] = attachment_options
|
35
|
+
end
|
36
|
+
|
37
|
+
def clear
|
38
|
+
@attachments = Hash.new { |h,k| h[k] = {} }
|
39
|
+
end
|
40
|
+
|
41
|
+
def names_for(klass)
|
42
|
+
@attachments[klass].keys
|
43
|
+
end
|
44
|
+
|
45
|
+
def each_definition
|
46
|
+
@attachments.each do |klass, attachments|
|
47
|
+
attachments.each do |name, options|
|
48
|
+
yield klass, name, options
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def definitions_for(klass)
|
54
|
+
parent_classes = klass.ancestors.reverse
|
55
|
+
parent_classes.each_with_object({}) do |ancestor, inherited_definitions|
|
56
|
+
inherited_definitions.deep_merge! @attachments[ancestor]
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
data/lib/paperclip/callbacks.rb
CHANGED
@@ -7,7 +7,7 @@ module Paperclip
|
|
7
7
|
|
8
8
|
module Defining
|
9
9
|
def define_paperclip_callbacks(*callbacks)
|
10
|
-
define_callbacks
|
10
|
+
define_callbacks(*[callbacks, { terminator: hasta_la_vista_baby }].flatten)
|
11
11
|
callbacks.each do |callback|
|
12
12
|
eval <<-end_callbacks
|
13
13
|
def before_#{callback}(*args, &blk)
|
@@ -19,6 +19,18 @@ module Paperclip
|
|
19
19
|
end_callbacks
|
20
20
|
end
|
21
21
|
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def hasta_la_vista_baby
|
26
|
+
lambda do |_, result|
|
27
|
+
if result.respond_to?(:call)
|
28
|
+
result.call == false
|
29
|
+
else
|
30
|
+
result == false
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
22
34
|
end
|
23
35
|
|
24
36
|
module Running
|
@@ -1,56 +1,80 @@
|
|
1
1
|
module Paperclip
|
2
2
|
class ContentTypeDetector
|
3
|
+
# The content-type detection strategy is as follows:
|
4
|
+
#
|
5
|
+
# 1. Blank/Empty files: If there's no filepath or the file is empty,
|
6
|
+
# provide a sensible default (application/octet-stream or inode/x-empty)
|
7
|
+
#
|
8
|
+
# 2. Calculated match: Return the first result that is found by both the
|
9
|
+
# `file` command and MIME::Types.
|
10
|
+
#
|
11
|
+
# 3. Standard types: Return the first standard (without an x- prefix) entry
|
12
|
+
# in MIME::Types
|
13
|
+
#
|
14
|
+
# 4. Experimental types: If there were no standard types in MIME::Types
|
15
|
+
# list, try to return the first experimental one
|
16
|
+
#
|
17
|
+
# 5. Raw `file` command: Just use the output of the `file` command raw, or
|
18
|
+
# a sensible default. This is cached from Step 2.
|
19
|
+
|
3
20
|
EMPTY_TYPE = "inode/x-empty"
|
4
21
|
SENSIBLE_DEFAULT = "application/octet-stream"
|
5
22
|
|
6
|
-
def initialize(
|
7
|
-
@
|
23
|
+
def initialize(filepath)
|
24
|
+
@filepath = filepath
|
8
25
|
end
|
9
26
|
|
27
|
+
# Returns a String describing the file's content type
|
10
28
|
def detect
|
11
|
-
if
|
29
|
+
if blank_name?
|
12
30
|
SENSIBLE_DEFAULT
|
13
|
-
elsif
|
31
|
+
elsif empty_file?
|
14
32
|
EMPTY_TYPE
|
15
|
-
elsif
|
16
|
-
|
17
|
-
elsif !multiple?
|
18
|
-
possible_types.first
|
33
|
+
elsif calculated_type_matches.any?
|
34
|
+
calculated_type_matches.first
|
19
35
|
else
|
20
|
-
|
36
|
+
type_from_file_contents || SENSIBLE_DEFAULT
|
21
37
|
end.to_s
|
22
38
|
end
|
23
39
|
|
24
40
|
private
|
25
41
|
|
26
|
-
def
|
27
|
-
|
42
|
+
def blank_name?
|
43
|
+
@filepath.nil? || @filepath.empty?
|
28
44
|
end
|
29
45
|
|
30
|
-
def
|
31
|
-
|
46
|
+
def empty_file?
|
47
|
+
File.exist?(@filepath) && File.size(@filepath) == 0
|
32
48
|
end
|
33
49
|
|
34
|
-
|
35
|
-
|
50
|
+
alias :empty? :empty_file?
|
51
|
+
|
52
|
+
def calculated_type_matches
|
53
|
+
possible_types.select do |content_type|
|
54
|
+
content_type == type_from_file_contents
|
55
|
+
end
|
36
56
|
end
|
37
57
|
|
38
|
-
def
|
39
|
-
|
58
|
+
def possible_types
|
59
|
+
MIME::Types.type_for(@filepath).collect(&:content_type)
|
40
60
|
end
|
41
61
|
|
42
|
-
def
|
43
|
-
|
62
|
+
def type_from_file_contents
|
63
|
+
type_from_mime_magic || type_from_file_command
|
64
|
+
rescue Errno::ENOENT => e
|
65
|
+
Paperclip.log("Error while determining content type: #{e}")
|
66
|
+
SENSIBLE_DEFAULT
|
44
67
|
end
|
45
68
|
|
46
|
-
def
|
47
|
-
|
48
|
-
|
69
|
+
def type_from_mime_magic
|
70
|
+
@type_from_mime_magic ||= File.open(@filepath) do |file|
|
71
|
+
MimeMagic.by_magic(file).try(:type)
|
72
|
+
end
|
49
73
|
end
|
50
74
|
|
51
75
|
def type_from_file_command
|
52
|
-
|
76
|
+
@type_from_file_command ||=
|
77
|
+
FileCommandContentTypeDetector.new(@filepath).detect
|
53
78
|
end
|
54
|
-
|
55
79
|
end
|
56
80
|
end
|