paperclip 3.5.2 → 5.2.1
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/.gitignore +0 -6
- data/.hound.yml +1055 -0
- data/.rubocop.yml +1 -0
- data/.travis.yml +19 -13
- data/Appraisals +4 -16
- data/CONTRIBUTING.md +29 -13
- data/Gemfile +10 -7
- data/LICENSE +1 -3
- data/NEWS +226 -23
- data/README.md +494 -152
- data/RELEASING.md +17 -0
- data/Rakefile +6 -8
- data/UPGRADING +12 -9
- data/features/basic_integration.feature +27 -8
- data/features/migration.feature +0 -24
- data/features/step_definitions/attachment_steps.rb +36 -28
- data/features/step_definitions/html_steps.rb +2 -2
- data/features/step_definitions/rails_steps.rb +68 -37
- data/features/step_definitions/s3_steps.rb +2 -2
- data/features/step_definitions/web_steps.rb +1 -103
- data/features/support/env.rb +3 -2
- data/features/support/file_helpers.rb +2 -2
- data/features/support/fixtures/gemfile.txt +1 -1
- data/features/support/paths.rb +1 -1
- data/features/support/rails.rb +2 -25
- data/gemfiles/4.2.gemfile +17 -0
- data/gemfiles/5.0.gemfile +17 -0
- data/lib/generators/paperclip/paperclip_generator.rb +0 -2
- data/lib/generators/paperclip/templates/paperclip_migration.rb.erb +1 -1
- data/lib/paperclip/attachment.rb +160 -47
- data/lib/paperclip/attachment_registry.rb +4 -1
- data/lib/paperclip/callbacks.rb +13 -1
- data/lib/paperclip/content_type_detector.rb +26 -24
- data/lib/paperclip/errors.rb +8 -1
- data/lib/paperclip/file_command_content_type_detector.rb +6 -8
- data/lib/paperclip/geometry_detector_factory.rb +10 -3
- data/lib/paperclip/geometry_parser_factory.rb +1 -1
- data/lib/paperclip/glue.rb +1 -1
- data/lib/paperclip/has_attached_file.rb +17 -1
- data/lib/paperclip/helpers.rb +14 -10
- data/lib/paperclip/interpolations/plural_cache.rb +6 -5
- data/lib/paperclip/interpolations.rb +27 -14
- data/lib/paperclip/io_adapters/abstract_adapter.rb +28 -4
- data/lib/paperclip/io_adapters/attachment_adapter.rb +13 -8
- data/lib/paperclip/io_adapters/data_uri_adapter.rb +11 -16
- 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 +8 -8
- 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 +10 -6
- data/lib/paperclip/io_adapters/uri_adapter.rb +41 -19
- data/lib/paperclip/locales/en.yml +1 -0
- data/lib/paperclip/matchers/have_attached_file_matcher.rb +2 -1
- data/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb +4 -4
- data/lib/paperclip/matchers/validate_attachment_presence_matcher.rb +2 -1
- data/lib/paperclip/matchers/validate_attachment_size_matcher.rb +2 -1
- data/lib/paperclip/media_type_spoof_detector.rb +89 -0
- data/lib/paperclip/processor.rb +5 -41
- data/lib/paperclip/processor_helpers.rb +50 -0
- data/lib/paperclip/rails_environment.rb +25 -0
- data/lib/paperclip/schema.rb +9 -7
- data/lib/paperclip/storage/filesystem.rb +14 -3
- data/lib/paperclip/storage/fog.rb +47 -22
- data/lib/paperclip/storage/s3.rb +144 -73
- data/lib/paperclip/style.rb +8 -2
- data/lib/paperclip/tempfile_factory.rb +6 -4
- data/lib/paperclip/thumbnail.rb +26 -14
- data/lib/paperclip/url_generator.rb +25 -14
- data/lib/paperclip/validators/attachment_content_type_validator.rb +4 -0
- 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 +4 -0
- data/lib/paperclip/validators/attachment_size_validator.rb +5 -3
- data/lib/paperclip/validators/media_type_spoof_detection_validator.rb +27 -0
- data/lib/paperclip/validators.rb +12 -3
- data/lib/paperclip/version.rb +3 -1
- data/lib/paperclip.rb +31 -11
- data/lib/tasks/paperclip.rake +34 -5
- data/paperclip.gemspec +18 -17
- data/shoulda_macros/paperclip.rb +13 -3
- data/{test → spec}/database.yml +0 -0
- data/spec/paperclip/attachment_definitions_spec.rb +13 -0
- data/{test/attachment_processing_test.rb → spec/paperclip/attachment_processing_spec.rb} +17 -20
- data/spec/paperclip/attachment_registry_spec.rb +158 -0
- data/{test/attachment_test.rb → spec/paperclip/attachment_spec.rb} +524 -400
- data/{test/content_type_detector_test.rb → spec/paperclip/content_type_detector_spec.rb} +17 -19
- data/{test/file_command_content_type_detector_test.rb → spec/paperclip/file_command_content_type_detector_spec.rb} +7 -6
- data/spec/paperclip/filename_cleaner_spec.rb +14 -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} +141 -133
- data/{test/interpolations_test.rb → spec/paperclip/interpolations_spec.rb} +70 -46
- data/spec/paperclip/io_adapters/abstract_adapter_spec.rb +101 -0
- data/{test/io_adapters/attachment_adapter_test.rb → spec/paperclip/io_adapters/attachment_adapter_spec.rb} +38 -34
- 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 +121 -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/{test/io_adapters/stringio_adapter_test.rb → spec/paperclip/io_adapters/stringio_adapter_spec.rb} +21 -18
- data/{test/io_adapters/uploaded_file_adapter_test.rb → spec/paperclip/io_adapters/uploaded_file_adapter_spec.rb} +46 -46
- data/spec/paperclip/io_adapters/uri_adapter_spec.rb +172 -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 +79 -0
- data/spec/paperclip/meta_class_spec.rb +30 -0
- data/spec/paperclip/paperclip_missing_attachment_styles_spec.rb +84 -0
- data/{test/paperclip_test.rb → spec/paperclip/paperclip_spec.rb} +46 -71
- 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} +5 -5
- data/spec/paperclip/rails_environment_spec.rb +33 -0
- data/{test/rake_test.rb → spec/paperclip/rake_spec.rb} +15 -15
- 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 +561 -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 +255 -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} +150 -131
- data/spec/paperclip/url_generator_spec.rb +222 -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} +77 -64
- 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 +47 -0
- data/spec/support/assertions.rb +82 -0
- data/spec/support/conditional_filter_helper.rb +5 -0
- data/spec/support/fake_model.rb +25 -0
- data/spec/support/fake_rails.rb +12 -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/spec/support/fixtures/empty.html +1 -0
- data/spec/support/fixtures/empty.xlsx +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/spec/support/fixtures/spaced file.jpg +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/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_interpolator.rb +0 -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 +262 -297
- 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/gemfiles/4.0.gemfile +0 -11
- data/test/attachment_definitions_test.rb +0 -12
- data/test/attachment_registry_test.rb +0 -77
- data/test/filename_cleaner_test.rb +0 -14
- data/test/generator_test.rb +0 -80
- data/test/geometry_detector_test.rb +0 -24
- data/test/has_attached_file_test.rb +0 -125
- data/test/helper.rb +0 -215
- data/test/io_adapters/abstract_adapter_test.rb +0 -58
- data/test/io_adapters/data_uri_adapter_test.rb +0 -67
- data/test/io_adapters/empty_string_adapter_test.rb +0 -17
- data/test/io_adapters/file_adapter_test.rb +0 -119
- data/test/io_adapters/http_url_proxy_adapter_test.rb +0 -102
- data/test/io_adapters/identity_adapter_test.rb +0 -8
- data/test/io_adapters/uri_adapter_test.rb +0 -102
- 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 -69
- 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 -90
- data/test/plural_cache_test.rb +0 -36
- 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 -1348
- 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 -323
- data/test/validators_test.rb +0 -32
@@ -2,7 +2,7 @@ module Paperclip
|
|
2
2
|
class ContentTypeDetector
|
3
3
|
# The content-type detection strategy is as follows:
|
4
4
|
#
|
5
|
-
# 1. Blank/Empty files: If there's no
|
5
|
+
# 1. Blank/Empty files: If there's no filepath or the file is empty,
|
6
6
|
# provide a sensible default (application/octet-stream or inode/x-empty)
|
7
7
|
#
|
8
8
|
# 2. Calculated match: Return the first result that is found by both the
|
@@ -20,8 +20,8 @@ module Paperclip
|
|
20
20
|
EMPTY_TYPE = "inode/x-empty"
|
21
21
|
SENSIBLE_DEFAULT = "application/octet-stream"
|
22
22
|
|
23
|
-
def initialize(
|
24
|
-
@
|
23
|
+
def initialize(filepath)
|
24
|
+
@filepath = filepath
|
25
25
|
end
|
26
26
|
|
27
27
|
# Returns a String describing the file's content type
|
@@ -32,47 +32,49 @@ module Paperclip
|
|
32
32
|
EMPTY_TYPE
|
33
33
|
elsif calculated_type_matches.any?
|
34
34
|
calculated_type_matches.first
|
35
|
-
elsif official_type_matches.any?
|
36
|
-
official_type_matches.first
|
37
|
-
elsif unofficial_type_matches.any?
|
38
|
-
unofficial_type_matches.first
|
39
35
|
else
|
40
|
-
|
36
|
+
type_from_file_contents || SENSIBLE_DEFAULT
|
41
37
|
end.to_s
|
42
38
|
end
|
43
39
|
|
44
40
|
private
|
45
41
|
|
46
|
-
def empty_file?
|
47
|
-
File.exists?(@filename) && File.size(@filename) == 0
|
48
|
-
end
|
49
|
-
|
50
42
|
def blank_name?
|
51
|
-
@
|
43
|
+
@filepath.nil? || @filepath.empty?
|
52
44
|
end
|
53
45
|
|
54
|
-
def
|
55
|
-
File.
|
46
|
+
def empty_file?
|
47
|
+
File.exist?(@filepath) && File.size(@filepath) == 0
|
56
48
|
end
|
57
49
|
|
58
|
-
|
59
|
-
MIME::Types.type_for(@filename).collect(&:content_type)
|
60
|
-
end
|
50
|
+
alias :empty? :empty_file?
|
61
51
|
|
62
52
|
def calculated_type_matches
|
63
|
-
possible_types.select
|
53
|
+
possible_types.select do |content_type|
|
54
|
+
content_type == type_from_file_contents
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def possible_types
|
59
|
+
MIME::Types.type_for(@filepath).collect(&:content_type)
|
64
60
|
end
|
65
61
|
|
66
|
-
def
|
67
|
-
|
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
|
68
67
|
end
|
69
68
|
|
70
|
-
def
|
71
|
-
|
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
|
72
73
|
end
|
73
74
|
|
74
75
|
def type_from_file_command
|
75
|
-
@type_from_file_command ||=
|
76
|
+
@type_from_file_command ||=
|
77
|
+
FileCommandContentTypeDetector.new(@filepath).detect
|
76
78
|
end
|
77
79
|
end
|
78
80
|
end
|
data/lib/paperclip/errors.rb
CHANGED
@@ -13,8 +13,15 @@ module Paperclip
|
|
13
13
|
class CommandNotFoundError < Paperclip::Error
|
14
14
|
end
|
15
15
|
|
16
|
+
# Attachments require a content_type or file_name validator,
|
17
|
+
# or to have explicitly opted out of them.
|
18
|
+
class MissingRequiredValidatorError < Paperclip::Error
|
19
|
+
end
|
20
|
+
|
16
21
|
# Will be thrown when ImageMagic cannot determine the uploaded file's
|
17
|
-
# metadata, usually this would mean the file is not an image.
|
22
|
+
# metadata, usually this would mean the file is not an image. If you are
|
23
|
+
# consistently receiving this error on PDFs make sure that you have
|
24
|
+
# installed Ghostscript.
|
18
25
|
class NotIdentifiedByImageMagickError < Paperclip::Error
|
19
26
|
end
|
20
27
|
|
@@ -13,20 +13,18 @@ module Paperclip
|
|
13
13
|
private
|
14
14
|
|
15
15
|
def type_from_file_command
|
16
|
+
# On BSDs, `file` doesn't give a result code of 1 if the file doesn't exist.
|
16
17
|
type = begin
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
end
|
18
|
+
Paperclip.run("file", "-b --mime :file", file: @filename)
|
19
|
+
rescue Cocaine::CommandLineError => e
|
20
|
+
Paperclip.log("Error while determining content type: #{e}")
|
21
|
+
SENSIBLE_DEFAULT
|
22
|
+
end
|
23
23
|
|
24
24
|
if type.nil? || type.match(/\(.*?\)/)
|
25
25
|
type = SENSIBLE_DEFAULT
|
26
26
|
end
|
27
27
|
type.split(/[:;\s]+/)[0]
|
28
28
|
end
|
29
|
-
|
30
29
|
end
|
31
30
|
end
|
32
|
-
|
@@ -14,9 +14,16 @@ module Paperclip
|
|
14
14
|
|
15
15
|
def geometry_string
|
16
16
|
begin
|
17
|
-
|
18
|
-
|
19
|
-
|
17
|
+
orientation = Paperclip.options[:use_exif_orientation] ?
|
18
|
+
"%[exif:orientation]" : "1"
|
19
|
+
Paperclip.run(
|
20
|
+
"identify",
|
21
|
+
"-format '%wx%h,#{orientation}' :file", {
|
22
|
+
:file => "#{path}[0]"
|
23
|
+
}, {
|
24
|
+
:swallow_stderr => true
|
25
|
+
}
|
26
|
+
)
|
20
27
|
rescue Cocaine::ExitStatusError
|
21
28
|
""
|
22
29
|
rescue Cocaine::CommandNotFoundError => e
|
data/lib/paperclip/glue.rb
CHANGED
@@ -8,7 +8,7 @@ module Paperclip
|
|
8
8
|
base.extend ClassMethods
|
9
9
|
base.send :include, Callbacks
|
10
10
|
base.send :include, Validators
|
11
|
-
base.send :include, Schema if defined? ActiveRecord
|
11
|
+
base.send :include, Schema if defined? ActiveRecord::Base
|
12
12
|
|
13
13
|
locale_path = Dir.glob(File.dirname(__FILE__) + "/locales/*.{rb,yml}")
|
14
14
|
I18n.load_path += locale_path unless I18n.load_path.include?(locale_path)
|
@@ -18,6 +18,7 @@ module Paperclip
|
|
18
18
|
register_new_attachment
|
19
19
|
add_active_record_callbacks
|
20
20
|
add_paperclip_callbacks
|
21
|
+
add_required_validations
|
21
22
|
end
|
22
23
|
|
23
24
|
private
|
@@ -77,11 +78,26 @@ module Paperclip
|
|
77
78
|
Paperclip::AttachmentRegistry.register(@klass, @name, @options)
|
78
79
|
end
|
79
80
|
|
81
|
+
def add_required_validations
|
82
|
+
options = Paperclip::Attachment.default_options.deep_merge(@options)
|
83
|
+
if options[:validate_media_type] != false
|
84
|
+
name = @name
|
85
|
+
@klass.validates_media_type_spoof_detection name,
|
86
|
+
:if => ->(instance){ instance.send(name).dirty? }
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
80
90
|
def add_active_record_callbacks
|
81
91
|
name = @name
|
82
92
|
@klass.send(:after_save) { send(name).send(:save) }
|
83
93
|
@klass.send(:before_destroy) { send(name).send(:queue_all_for_delete) }
|
84
|
-
@klass.
|
94
|
+
if @klass.respond_to?(:after_commit)
|
95
|
+
@klass.send(:after_commit, on: :destroy) do
|
96
|
+
send(name).send(:flush_deletes)
|
97
|
+
end
|
98
|
+
else
|
99
|
+
@klass.send(:after_destroy) { send(name).send(:flush_deletes) }
|
100
|
+
end
|
85
101
|
end
|
86
102
|
|
87
103
|
def add_paperclip_callbacks
|
data/lib/paperclip/helpers.rb
CHANGED
@@ -8,23 +8,27 @@ module Paperclip
|
|
8
8
|
Paperclip::Interpolations[key] = block
|
9
9
|
end
|
10
10
|
|
11
|
-
# The run method takes the name of a binary to run, the arguments
|
12
|
-
# and some options
|
11
|
+
# The run method takes the name of a binary to run, the arguments
|
12
|
+
# to that binary, the values to interpolate and some local options.
|
13
13
|
#
|
14
|
-
#
|
15
|
-
# on the filesystem. Colon-separated, just like $PATH.
|
14
|
+
# :cmd -> The name of a binary to run.
|
16
15
|
#
|
17
|
-
#
|
18
|
-
# of the binary. Defaults to [0].
|
16
|
+
# :arguments -> The command line arguments to that binary.
|
19
17
|
#
|
20
|
-
#
|
21
|
-
# This will only log if logging in general is set to true as well.
|
18
|
+
# :interpolation_values -> Values to be interpolated into the arguments.
|
22
19
|
#
|
23
|
-
#
|
20
|
+
# :local_options -> The options to be used by Cocain::CommandLine.
|
21
|
+
# These could be: runner
|
22
|
+
# logger
|
23
|
+
# swallow_stderr
|
24
|
+
# expected_outcodes
|
25
|
+
# environment
|
26
|
+
# runner_options
|
24
27
|
#
|
25
28
|
def run(cmd, arguments = "", interpolation_values = {}, local_options = {})
|
26
29
|
command_path = options[:command_path]
|
27
|
-
|
30
|
+
cocaine_path_array = Cocaine::CommandLine.path.try(:split, Cocaine::OS.path_separator)
|
31
|
+
Cocaine::CommandLine.path = [cocaine_path_array, command_path].flatten.compact.uniq
|
28
32
|
if logging? && (options[:log_command] || local_options[:log_command])
|
29
33
|
local_options = local_options.merge(:logger => logger)
|
30
34
|
end
|
@@ -2,15 +2,16 @@ module Paperclip
|
|
2
2
|
module Interpolations
|
3
3
|
class PluralCache
|
4
4
|
def initialize
|
5
|
-
@
|
5
|
+
@symbol_cache = {}.compare_by_identity
|
6
|
+
@klass_cache = {}.compare_by_identity
|
6
7
|
end
|
7
8
|
|
8
|
-
def
|
9
|
-
@
|
9
|
+
def pluralize_symbol(symbol)
|
10
|
+
@symbol_cache[symbol] ||= symbol.to_s.downcase.pluralize
|
10
11
|
end
|
11
12
|
|
12
|
-
def
|
13
|
-
@
|
13
|
+
def underscore_and_pluralize_class(klass)
|
14
|
+
@klass_cache[klass] ||= klass.name.underscore.pluralize
|
14
15
|
end
|
15
16
|
end
|
16
17
|
end
|
@@ -10,6 +10,7 @@ module Paperclip
|
|
10
10
|
# and is not intended for normal use.
|
11
11
|
def self.[]= name, block
|
12
12
|
define_method(name, &block)
|
13
|
+
@interpolators_cache = nil
|
13
14
|
end
|
14
15
|
|
15
16
|
# Hash access of interpolations. Included only for compatibility,
|
@@ -20,7 +21,7 @@ module Paperclip
|
|
20
21
|
|
21
22
|
# Returns a sorted list of all interpolations.
|
22
23
|
def self.all
|
23
|
-
self.instance_methods(false).sort
|
24
|
+
self.instance_methods(false).sort!
|
24
25
|
end
|
25
26
|
|
26
27
|
# Perform the actual interpolation. Takes the pattern to interpolate
|
@@ -29,11 +30,15 @@ module Paperclip
|
|
29
30
|
# an interpolation pattern for Paperclip to use.
|
30
31
|
def self.interpolate pattern, *args
|
31
32
|
pattern = args.first.instance.send(pattern) if pattern.kind_of? Symbol
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
end
|
33
|
+
result = pattern.dup
|
34
|
+
interpolators_cache.each do |method, token|
|
35
|
+
result.gsub!(token) { send(method, *args) } if result.include?(token)
|
36
36
|
end
|
37
|
+
result
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.interpolators_cache
|
41
|
+
@interpolators_cache ||= all.reverse!.map! { |method| [method, ":#{method}"] }
|
37
42
|
end
|
38
43
|
|
39
44
|
def self.plural_cache
|
@@ -42,13 +47,13 @@ module Paperclip
|
|
42
47
|
|
43
48
|
# Returns the filename, the same way as ":basename.:extension" would.
|
44
49
|
def filename attachment, style_name
|
45
|
-
[ basename(attachment, style_name), extension(attachment, style_name) ].
|
50
|
+
[ basename(attachment, style_name), extension(attachment, style_name) ].delete_if(&:empty?).join(".".freeze)
|
46
51
|
end
|
47
52
|
|
48
53
|
# Returns the interpolated URL. Will raise an error if the url itself
|
49
54
|
# contains ":url" to prevent infinite recursion. This interpolation
|
50
55
|
# is used in the default :path to ease default specifications.
|
51
|
-
RIGHT_HERE = "#{__FILE__.gsub(%r{
|
56
|
+
RIGHT_HERE = "#{__FILE__.gsub(%r{\A\./}, "")}:#{__LINE__ + 3}"
|
52
57
|
def url attachment, style_name
|
53
58
|
raise Errors::InfiniteInterpolationError if caller.any?{|b| b.index(RIGHT_HERE) }
|
54
59
|
attachment.url(style_name, :timestamp => false, :escape => false)
|
@@ -85,12 +90,12 @@ module Paperclip
|
|
85
90
|
# all class names. Calling #class will return the expected class.
|
86
91
|
def class attachment = nil, style_name = nil
|
87
92
|
return super() if attachment.nil? && style_name.nil?
|
88
|
-
plural_cache.
|
93
|
+
plural_cache.underscore_and_pluralize_class(attachment.instance.class)
|
89
94
|
end
|
90
95
|
|
91
96
|
# Returns the basename of the file. e.g. "file" for "file.jpg"
|
92
97
|
def basename attachment, style_name
|
93
|
-
|
98
|
+
File.basename(attachment.original_filename, ".*".freeze)
|
94
99
|
end
|
95
100
|
|
96
101
|
# Returns the extension of the file. e.g. "jpg" for "file.jpg"
|
@@ -98,7 +103,15 @@ module Paperclip
|
|
98
103
|
# of the actual extension.
|
99
104
|
def extension attachment, style_name
|
100
105
|
((style = attachment.styles[style_name.to_s.to_sym]) && style[:format]) ||
|
101
|
-
File.extname(attachment.original_filename).
|
106
|
+
File.extname(attachment.original_filename).sub(/\A\.+/, "".freeze)
|
107
|
+
end
|
108
|
+
|
109
|
+
# Returns the dot+extension of the file. e.g. ".jpg" for "file.jpg"
|
110
|
+
# If the style has a format defined, it will return the format instead
|
111
|
+
# of the actual extension. If the extension is empty, no dot is added.
|
112
|
+
def dotextension attachment, style_name
|
113
|
+
ext = extension(attachment, style_name)
|
114
|
+
ext.empty? ? ext : ".#{ext}"
|
102
115
|
end
|
103
116
|
|
104
117
|
# Returns an extension based on the content type. e.g. "jpeg" for
|
@@ -128,7 +141,7 @@ module Paperclip
|
|
128
141
|
# It's possible, though unlikely, that the mime type is not in the
|
129
142
|
# database, so just use the part after the '/' in the mime type as the
|
130
143
|
# extension.
|
131
|
-
%r{/([^/]*)
|
144
|
+
%r{/([^/]*)\z}.match(attachment.content_type)[1]
|
132
145
|
end
|
133
146
|
end
|
134
147
|
|
@@ -162,9 +175,9 @@ module Paperclip
|
|
162
175
|
def id_partition attachment, style_name
|
163
176
|
case id = attachment.instance.id
|
164
177
|
when Integer
|
165
|
-
("%09d" % id).scan(/\d{3}/).join("/")
|
178
|
+
("%09d".freeze % id).scan(/\d{3}/).join("/".freeze)
|
166
179
|
when String
|
167
|
-
id.scan(/.{3}/).first(3).join("/")
|
180
|
+
id.scan(/.{3}/).first(3).join("/".freeze)
|
168
181
|
else
|
169
182
|
nil
|
170
183
|
end
|
@@ -173,7 +186,7 @@ module Paperclip
|
|
173
186
|
# Returns the pluralized form of the attachment name. e.g.
|
174
187
|
# "avatars" for an attachment of :avatar
|
175
188
|
def attachment attachment, style_name
|
176
|
-
plural_cache.
|
189
|
+
plural_cache.pluralize_symbol(attachment.name)
|
177
190
|
end
|
178
191
|
|
179
192
|
# Returns the style, or the default style if nil is supplied.
|
@@ -5,10 +5,23 @@ module Paperclip
|
|
5
5
|
OS_RESTRICTED_CHARACTERS = %r{[/:]}
|
6
6
|
|
7
7
|
attr_reader :content_type, :original_filename, :size
|
8
|
-
delegate :close, :closed?, :eof?, :path, :rewind, :unlink, :to => :@tempfile
|
8
|
+
delegate :binmode, :binmode?, :close, :close!, :closed?, :eof?, :path, :readbyte, :rewind, :unlink, :to => :@tempfile
|
9
|
+
alias :length :size
|
10
|
+
|
11
|
+
def initialize(target, options = {})
|
12
|
+
@target = target
|
13
|
+
@options = options
|
14
|
+
end
|
9
15
|
|
10
16
|
def fingerprint
|
11
|
-
@fingerprint ||=
|
17
|
+
@fingerprint ||= begin
|
18
|
+
digest = @options.fetch(:hash_digest).new
|
19
|
+
File.open(path, "rb") do |f|
|
20
|
+
buf = ""
|
21
|
+
digest.update(buf) while f.read(16384, buf)
|
22
|
+
end
|
23
|
+
digest.hexdigest
|
24
|
+
end
|
12
25
|
end
|
13
26
|
|
14
27
|
def read(length = nil, buffer = nil)
|
@@ -20,6 +33,7 @@ module Paperclip
|
|
20
33
|
end
|
21
34
|
|
22
35
|
def original_filename=(new_filename)
|
36
|
+
return unless new_filename
|
23
37
|
@original_filename = new_filename.gsub(OS_RESTRICTED_CHARACTERS, "_")
|
24
38
|
end
|
25
39
|
|
@@ -34,12 +48,22 @@ module Paperclip
|
|
34
48
|
private
|
35
49
|
|
36
50
|
def destination
|
37
|
-
@destination ||= TempfileFactory.new.generate(original_filename)
|
51
|
+
@destination ||= TempfileFactory.new.generate(@original_filename.to_s)
|
38
52
|
end
|
39
53
|
|
40
54
|
def copy_to_tempfile(src)
|
41
|
-
|
55
|
+
link_or_copy_file(src.path, destination.path)
|
42
56
|
destination
|
43
57
|
end
|
58
|
+
|
59
|
+
def link_or_copy_file(src, dest)
|
60
|
+
Paperclip.log("Trying to link #{src} to #{dest}")
|
61
|
+
FileUtils.ln(src, dest, force: true) # overwrite existing
|
62
|
+
@destination.close
|
63
|
+
@destination.open.binmode
|
64
|
+
rescue Errno::EXDEV, Errno::EPERM, Errno::ENOENT, Errno::EEXIST => e
|
65
|
+
Paperclip.log("Link failed with #{e.message}; copying link #{src} to #{dest}")
|
66
|
+
FileUtils.cp(src, dest)
|
67
|
+
end
|
44
68
|
end
|
45
69
|
end
|
@@ -1,6 +1,13 @@
|
|
1
1
|
module Paperclip
|
2
2
|
class AttachmentAdapter < AbstractAdapter
|
3
|
-
def
|
3
|
+
def self.register
|
4
|
+
Paperclip.io_adapters.register self do |target|
|
5
|
+
Paperclip::Attachment === target || Paperclip::Style === target
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
def initialize(target, options = {})
|
10
|
+
super
|
4
11
|
@target, @style = case target
|
5
12
|
when Paperclip::Attachment
|
6
13
|
[target, :original]
|
@@ -20,17 +27,15 @@ module Paperclip
|
|
20
27
|
@size = @tempfile.size || @target.size
|
21
28
|
end
|
22
29
|
|
23
|
-
def copy_to_tempfile(
|
24
|
-
if
|
25
|
-
|
30
|
+
def copy_to_tempfile(source)
|
31
|
+
if source.staged?
|
32
|
+
link_or_copy_file(source.staged_path(@style), destination.path)
|
26
33
|
else
|
27
|
-
|
34
|
+
source.copy_to_local_file(@style, destination.path)
|
28
35
|
end
|
29
36
|
destination
|
30
37
|
end
|
31
38
|
end
|
32
39
|
end
|
33
40
|
|
34
|
-
Paperclip.
|
35
|
-
Paperclip::Attachment === target || Paperclip::Style === target
|
36
|
-
end
|
41
|
+
Paperclip::AttachmentAdapter.register
|
@@ -1,27 +1,22 @@
|
|
1
1
|
module Paperclip
|
2
2
|
class DataUriAdapter < StringioAdapter
|
3
|
+
def self.register
|
4
|
+
Paperclip.io_adapters.register self do |target|
|
5
|
+
String === target && target =~ REGEXP
|
6
|
+
end
|
7
|
+
end
|
3
8
|
|
4
|
-
REGEXP =
|
9
|
+
REGEXP = /\Adata:([-\w]+\/[-\w\+\.]+)?;base64,(.*)/m
|
5
10
|
|
6
|
-
def initialize(target_uri)
|
7
|
-
|
8
|
-
cache_current_values
|
9
|
-
@tempfile = copy_to_tempfile
|
11
|
+
def initialize(target_uri, options = {})
|
12
|
+
super(extract_target(target_uri), options)
|
10
13
|
end
|
11
14
|
|
12
15
|
private
|
13
16
|
|
14
|
-
def
|
15
|
-
|
16
|
-
|
17
|
-
@content_type = data_uri_parts[1] || 'text/plain'
|
18
|
-
@target = StringIO.new(Base64.decode64(data_uri_parts[2] || ''))
|
19
|
-
@size = @target.size
|
17
|
+
def extract_target(uri)
|
18
|
+
data_uri_parts = uri.match(REGEXP) || []
|
19
|
+
StringIO.new(Base64.decode64(data_uri_parts[2] || ""))
|
20
20
|
end
|
21
|
-
|
22
21
|
end
|
23
22
|
end
|
24
|
-
|
25
|
-
Paperclip.io_adapters.register Paperclip::DataUriAdapter do |target|
|
26
|
-
String === target && target =~ Paperclip::DataUriAdapter::REGEXP
|
27
|
-
end
|
@@ -1,6 +1,9 @@
|
|
1
1
|
module Paperclip
|
2
2
|
class EmptyStringAdapter < AbstractAdapter
|
3
|
-
def
|
3
|
+
def self.register
|
4
|
+
Paperclip.io_adapters.register self do |target|
|
5
|
+
target.is_a?(String) && target.empty?
|
6
|
+
end
|
4
7
|
end
|
5
8
|
|
6
9
|
def nil?
|
@@ -13,6 +16,4 @@ module Paperclip
|
|
13
16
|
end
|
14
17
|
end
|
15
18
|
|
16
|
-
Paperclip.
|
17
|
-
target.is_a?(String) && target.empty?
|
18
|
-
end
|
19
|
+
Paperclip::EmptyStringAdapter.register
|
@@ -1,14 +1,22 @@
|
|
1
1
|
module Paperclip
|
2
2
|
class FileAdapter < AbstractAdapter
|
3
|
-
def
|
4
|
-
|
3
|
+
def self.register
|
4
|
+
Paperclip.io_adapters.register self do |target|
|
5
|
+
File === target || ::Tempfile === target
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
def initialize(target, options = {})
|
10
|
+
super
|
5
11
|
cache_current_values
|
6
12
|
end
|
7
13
|
|
8
14
|
private
|
9
15
|
|
10
16
|
def cache_current_values
|
11
|
-
|
17
|
+
if @target.respond_to?(:original_filename)
|
18
|
+
self.original_filename = @target.original_filename
|
19
|
+
end
|
12
20
|
self.original_filename ||= File.basename(@target.path)
|
13
21
|
@tempfile = copy_to_tempfile(@target)
|
14
22
|
@content_type = ContentTypeDetector.new(@target.path).detect
|
@@ -17,6 +25,4 @@ module Paperclip
|
|
17
25
|
end
|
18
26
|
end
|
19
27
|
|
20
|
-
Paperclip.
|
21
|
-
File === target || Tempfile === target
|
22
|
-
end
|
28
|
+
Paperclip::FileAdapter.register
|
@@ -1,15 +1,15 @@
|
|
1
1
|
module Paperclip
|
2
2
|
class HttpUrlProxyAdapter < UriAdapter
|
3
|
+
def self.register
|
4
|
+
Paperclip.io_adapters.register self do |target|
|
5
|
+
String === target && target =~ REGEXP
|
6
|
+
end
|
7
|
+
end
|
3
8
|
|
4
|
-
REGEXP =
|
9
|
+
REGEXP = /\Ahttps?:\/\//
|
5
10
|
|
6
|
-
def initialize(target)
|
7
|
-
super(URI(target))
|
11
|
+
def initialize(target, options = {})
|
12
|
+
super(URI(URI.escape(target)), options)
|
8
13
|
end
|
9
|
-
|
10
14
|
end
|
11
15
|
end
|
12
|
-
|
13
|
-
Paperclip.io_adapters.register Paperclip::HttpUrlProxyAdapter do |target|
|
14
|
-
String === target && target =~ Paperclip::HttpUrlProxyAdapter::REGEXP
|
15
|
-
end
|
@@ -1,12 +1,18 @@
|
|
1
1
|
module Paperclip
|
2
2
|
class IdentityAdapter < AbstractAdapter
|
3
|
-
def
|
4
|
-
|
3
|
+
def self.register
|
4
|
+
Paperclip.io_adapters.register Paperclip::IdentityAdapter.new do |target|
|
5
|
+
Paperclip.io_adapters.registered?(target)
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
def initialize
|
5
10
|
end
|
6
|
-
end
|
7
|
-
end
|
8
11
|
|
9
|
-
|
10
|
-
|
12
|
+
def new(target, _)
|
13
|
+
target
|
14
|
+
end
|
15
|
+
end
|
11
16
|
end
|
12
17
|
|
18
|
+
Paperclip::IdentityAdapter.register
|
@@ -1,8 +1,13 @@
|
|
1
1
|
module Paperclip
|
2
2
|
class NilAdapter < AbstractAdapter
|
3
|
-
def
|
3
|
+
def self.register
|
4
|
+
Paperclip.io_adapters.register self do |target|
|
5
|
+
target.nil? || ((Paperclip::Attachment === target) && !target.present?)
|
6
|
+
end
|
4
7
|
end
|
5
8
|
|
9
|
+
def initialize(_target, _options = {}); end
|
10
|
+
|
6
11
|
def original_filename
|
7
12
|
""
|
8
13
|
end
|
@@ -19,7 +24,7 @@ module Paperclip
|
|
19
24
|
true
|
20
25
|
end
|
21
26
|
|
22
|
-
def read(*
|
27
|
+
def read(*_args)
|
23
28
|
nil
|
24
29
|
end
|
25
30
|
|
@@ -29,6 +34,4 @@ module Paperclip
|
|
29
34
|
end
|
30
35
|
end
|
31
36
|
|
32
|
-
Paperclip.
|
33
|
-
target.nil? || ( (Paperclip::Attachment === target) && !target.present? )
|
34
|
-
end
|
37
|
+
Paperclip::NilAdapter.register
|