paperclip 3.4.0 → 6.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (220) hide show
  1. checksums.yaml +7 -0
  2. data/.codeclimate.yml +17 -0
  3. data/.github/issue_template.md +3 -0
  4. data/.gitignore +0 -6
  5. data/.hound.yml +1055 -0
  6. data/.rubocop.yml +1 -0
  7. data/.travis.yml +19 -12
  8. data/Appraisals +4 -11
  9. data/CONTRIBUTING.md +29 -13
  10. data/Gemfile +13 -4
  11. data/LICENSE +1 -3
  12. data/MIGRATING-ES.md +317 -0
  13. data/MIGRATING.md +375 -0
  14. data/NEWS +390 -71
  15. data/README.md +607 -152
  16. data/RELEASING.md +17 -0
  17. data/Rakefile +6 -8
  18. data/UPGRADING +12 -9
  19. data/features/basic_integration.feature +34 -21
  20. data/features/migration.feature +0 -24
  21. data/features/rake_tasks.feature +2 -3
  22. data/features/step_definitions/attachment_steps.rb +44 -36
  23. data/features/step_definitions/html_steps.rb +2 -2
  24. data/features/step_definitions/rails_steps.rb +125 -26
  25. data/features/step_definitions/s3_steps.rb +3 -3
  26. data/features/step_definitions/web_steps.rb +1 -103
  27. data/features/support/env.rb +3 -2
  28. data/features/support/fakeweb.rb +4 -1
  29. data/features/support/file_helpers.rb +12 -2
  30. data/features/support/fixtures/gemfile.txt +1 -1
  31. data/features/support/paths.rb +1 -1
  32. data/features/support/rails.rb +4 -11
  33. data/gemfiles/4.2.gemfile +17 -0
  34. data/gemfiles/5.0.gemfile +17 -0
  35. data/lib/generators/paperclip/paperclip_generator.rb +9 -3
  36. data/lib/generators/paperclip/templates/paperclip_migration.rb.erb +2 -2
  37. data/lib/paperclip/attachment.rb +215 -82
  38. data/lib/paperclip/attachment_registry.rb +60 -0
  39. data/lib/paperclip/callbacks.rb +13 -1
  40. data/lib/paperclip/content_type_detector.rb +48 -24
  41. data/lib/paperclip/errors.rb +8 -1
  42. data/lib/paperclip/file_command_content_type_detector.rb +6 -8
  43. data/lib/paperclip/filename_cleaner.rb +15 -0
  44. data/lib/paperclip/geometry_detector_factory.rb +12 -5
  45. data/lib/paperclip/geometry_parser_factory.rb +1 -1
  46. data/lib/paperclip/glue.rb +1 -2
  47. data/lib/paperclip/has_attached_file.rb +115 -0
  48. data/lib/paperclip/helpers.rb +15 -20
  49. data/lib/paperclip/interpolations/plural_cache.rb +18 -0
  50. data/lib/paperclip/interpolations.rb +36 -14
  51. data/lib/paperclip/io_adapters/abstract_adapter.rb +42 -5
  52. data/lib/paperclip/io_adapters/attachment_adapter.rb +20 -9
  53. data/lib/paperclip/io_adapters/data_uri_adapter.rb +22 -0
  54. data/lib/paperclip/io_adapters/empty_string_adapter.rb +19 -0
  55. data/lib/paperclip/io_adapters/file_adapter.rb +13 -7
  56. data/lib/paperclip/io_adapters/http_url_proxy_adapter.rb +16 -0
  57. data/lib/paperclip/io_adapters/identity_adapter.rb +12 -6
  58. data/lib/paperclip/io_adapters/nil_adapter.rb +8 -5
  59. data/lib/paperclip/io_adapters/registry.rb +6 -2
  60. data/lib/paperclip/io_adapters/stringio_adapter.rb +15 -16
  61. data/lib/paperclip/io_adapters/uploaded_file_adapter.rb +11 -7
  62. data/lib/paperclip/io_adapters/uri_adapter.rb +43 -19
  63. data/lib/paperclip/locales/en.yml +1 -0
  64. data/lib/paperclip/logger.rb +1 -1
  65. data/lib/paperclip/matchers/have_attached_file_matcher.rb +3 -6
  66. data/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb +4 -4
  67. data/lib/paperclip/matchers/validate_attachment_presence_matcher.rb +7 -2
  68. data/lib/paperclip/matchers/validate_attachment_size_matcher.rb +2 -1
  69. data/lib/paperclip/matchers.rb +1 -1
  70. data/lib/paperclip/media_type_spoof_detector.rb +93 -0
  71. data/lib/paperclip/missing_attachment_styles.rb +11 -16
  72. data/lib/paperclip/processor.rb +15 -43
  73. data/lib/paperclip/processor_helpers.rb +50 -0
  74. data/lib/paperclip/rails_environment.rb +25 -0
  75. data/lib/paperclip/schema.rb +10 -8
  76. data/lib/paperclip/storage/filesystem.rb +20 -5
  77. data/lib/paperclip/storage/fog.rb +49 -23
  78. data/lib/paperclip/storage/s3.rb +153 -82
  79. data/lib/paperclip/style.rb +8 -3
  80. data/lib/paperclip/tempfile_factory.rb +6 -4
  81. data/lib/paperclip/thumbnail.rb +35 -19
  82. data/lib/paperclip/url_generator.rb +26 -14
  83. data/lib/paperclip/validators/attachment_content_type_validator.rb +15 -2
  84. data/lib/paperclip/validators/attachment_file_name_validator.rb +80 -0
  85. data/lib/paperclip/validators/attachment_file_type_ignorance_validator.rb +29 -0
  86. data/lib/paperclip/validators/attachment_presence_validator.rb +12 -8
  87. data/lib/paperclip/validators/attachment_size_validator.rb +17 -10
  88. data/lib/paperclip/validators/media_type_spoof_detection_validator.rb +31 -0
  89. data/lib/paperclip/validators.rb +31 -3
  90. data/lib/paperclip/version.rb +3 -1
  91. data/lib/paperclip.rb +41 -55
  92. data/lib/tasks/paperclip.rake +56 -9
  93. data/paperclip.gemspec +18 -17
  94. data/shoulda_macros/paperclip.rb +13 -3
  95. data/spec/paperclip/attachment_definitions_spec.rb +13 -0
  96. data/spec/paperclip/attachment_processing_spec.rb +79 -0
  97. data/spec/paperclip/attachment_registry_spec.rb +158 -0
  98. data/{test/attachment_test.rb → spec/paperclip/attachment_spec.rb} +597 -389
  99. data/spec/paperclip/content_type_detector_spec.rb +48 -0
  100. data/spec/paperclip/file_command_content_type_detector_spec.rb +40 -0
  101. data/spec/paperclip/filename_cleaner_spec.rb +13 -0
  102. data/spec/paperclip/geometry_detector_spec.rb +39 -0
  103. data/{test/geometry_parser_test.rb → spec/paperclip/geometry_parser_spec.rb} +27 -27
  104. data/{test/geometry_test.rb → spec/paperclip/geometry_spec.rb} +50 -52
  105. data/spec/paperclip/glue_spec.rb +44 -0
  106. data/spec/paperclip/has_attached_file_spec.rb +158 -0
  107. data/{test/integration_test.rb → spec/paperclip/integration_spec.rb} +179 -199
  108. data/{test/interpolations_test.rb → spec/paperclip/interpolations_spec.rb} +79 -46
  109. data/spec/paperclip/io_adapters/abstract_adapter_spec.rb +160 -0
  110. data/{test/io_adapters/attachment_adapter_test.rb → spec/paperclip/io_adapters/attachment_adapter_spec.rb} +54 -25
  111. data/spec/paperclip/io_adapters/data_uri_adapter_spec.rb +89 -0
  112. data/spec/paperclip/io_adapters/empty_string_adapter_spec.rb +17 -0
  113. data/spec/paperclip/io_adapters/file_adapter_spec.rb +131 -0
  114. data/spec/paperclip/io_adapters/http_url_proxy_adapter_spec.rb +138 -0
  115. data/spec/paperclip/io_adapters/identity_adapter_spec.rb +8 -0
  116. data/{test/io_adapters/nil_adapter_test.rb → spec/paperclip/io_adapters/nil_adapter_spec.rb} +7 -7
  117. data/{test/io_adapters/registry_test.rb → spec/paperclip/io_adapters/registry_spec.rb} +12 -9
  118. data/spec/paperclip/io_adapters/stringio_adapter_spec.rb +64 -0
  119. data/spec/paperclip/io_adapters/uploaded_file_adapter_spec.rb +146 -0
  120. data/spec/paperclip/io_adapters/uri_adapter_spec.rb +220 -0
  121. data/spec/paperclip/matchers/have_attached_file_matcher_spec.rb +19 -0
  122. data/spec/paperclip/matchers/validate_attachment_content_type_matcher_spec.rb +109 -0
  123. data/spec/paperclip/matchers/validate_attachment_presence_matcher_spec.rb +69 -0
  124. data/spec/paperclip/matchers/validate_attachment_size_matcher_spec.rb +88 -0
  125. data/spec/paperclip/media_type_spoof_detector_spec.rb +120 -0
  126. data/spec/paperclip/meta_class_spec.rb +30 -0
  127. data/spec/paperclip/paperclip_missing_attachment_styles_spec.rb +84 -0
  128. data/spec/paperclip/paperclip_spec.rb +192 -0
  129. data/spec/paperclip/plural_cache_spec.rb +37 -0
  130. data/spec/paperclip/processor_helpers_spec.rb +57 -0
  131. data/{test/processor_test.rb → spec/paperclip/processor_spec.rb} +7 -7
  132. data/spec/paperclip/rails_environment_spec.rb +33 -0
  133. data/spec/paperclip/rake_spec.rb +103 -0
  134. data/spec/paperclip/schema_spec.rb +248 -0
  135. data/{test/storage/filesystem_test.rb → spec/paperclip/storage/filesystem_spec.rb} +18 -18
  136. data/spec/paperclip/storage/fog_spec.rb +566 -0
  137. data/spec/paperclip/storage/s3_live_spec.rb +188 -0
  138. data/spec/paperclip/storage/s3_spec.rb +1693 -0
  139. data/spec/paperclip/style_spec.rb +254 -0
  140. data/spec/paperclip/tempfile_factory_spec.rb +33 -0
  141. data/spec/paperclip/tempfile_spec.rb +35 -0
  142. data/{test/thumbnail_test.rb → spec/paperclip/thumbnail_spec.rb} +186 -141
  143. data/spec/paperclip/url_generator_spec.rb +221 -0
  144. data/spec/paperclip/validators/attachment_content_type_validator_spec.rb +322 -0
  145. data/spec/paperclip/validators/attachment_file_name_validator_spec.rb +160 -0
  146. data/{test/validators/attachment_presence_validator_test.rb → spec/paperclip/validators/attachment_presence_validator_spec.rb} +20 -20
  147. data/{test/validators/attachment_size_validator_test.rb → spec/paperclip/validators/attachment_size_validator_spec.rb} +87 -59
  148. data/spec/paperclip/validators/media_type_spoof_detection_validator_spec.rb +52 -0
  149. data/spec/paperclip/validators_spec.rb +164 -0
  150. data/spec/spec_helper.rb +46 -0
  151. data/spec/support/assertions.rb +82 -0
  152. data/spec/support/fake_model.rb +25 -0
  153. data/spec/support/fake_rails.rb +12 -0
  154. data/spec/support/fixtures/empty.html +1 -0
  155. data/spec/support/fixtures/empty.xlsx +0 -0
  156. data/spec/support/fixtures/spaced file.jpg +0 -0
  157. data/spec/support/matchers/accept.rb +5 -0
  158. data/spec/support/matchers/exist.rb +5 -0
  159. data/spec/support/matchers/have_column.rb +23 -0
  160. data/{test → spec}/support/mock_attachment.rb +2 -0
  161. data/{test → spec}/support/mock_url_generator_builder.rb +2 -2
  162. data/spec/support/model_reconstruction.rb +68 -0
  163. data/spec/support/reporting.rb +11 -0
  164. data/spec/support/test_data.rb +13 -0
  165. data/spec/support/version_helper.rb +9 -0
  166. metadata +395 -346
  167. data/Gemfile.lock +0 -200
  168. data/RUNNING_TESTS.md +0 -4
  169. data/cucumber/paperclip_steps.rb +0 -6
  170. data/gemfiles/3.0.gemfile +0 -11
  171. data/gemfiles/3.1.gemfile +0 -11
  172. data/gemfiles/3.2.gemfile +0 -11
  173. data/lib/paperclip/attachment_options.rb +0 -9
  174. data/lib/paperclip/instance_methods.rb +0 -35
  175. data/test/attachment_options_test.rb +0 -27
  176. data/test/attachment_processing_test.rb +0 -29
  177. data/test/content_type_detector_test.rb +0 -40
  178. data/test/file_command_content_type_detector_test.rb +0 -25
  179. data/test/generator_test.rb +0 -80
  180. data/test/geometry_detector_test.rb +0 -24
  181. data/test/helper.rb +0 -199
  182. data/test/io_adapters/abstract_adapter_test.rb +0 -50
  183. data/test/io_adapters/file_adapter_test.rb +0 -100
  184. data/test/io_adapters/identity_adapter_test.rb +0 -8
  185. data/test/io_adapters/stringio_adapter_test.rb +0 -51
  186. data/test/io_adapters/uploaded_file_adapter_test.rb +0 -123
  187. data/test/io_adapters/uri_adapter_test.rb +0 -86
  188. data/test/matchers/have_attached_file_matcher_test.rb +0 -24
  189. data/test/matchers/validate_attachment_content_type_matcher_test.rb +0 -110
  190. data/test/matchers/validate_attachment_presence_matcher_test.rb +0 -47
  191. data/test/matchers/validate_attachment_size_matcher_test.rb +0 -86
  192. data/test/meta_class_test.rb +0 -32
  193. data/test/paperclip_missing_attachment_styles_test.rb +0 -94
  194. data/test/paperclip_test.rb +0 -259
  195. data/test/schema_test.rb +0 -200
  196. data/test/storage/fog_test.rb +0 -453
  197. data/test/storage/s3_live_test.rb +0 -179
  198. data/test/storage/s3_test.rb +0 -1236
  199. data/test/style_test.rb +0 -213
  200. data/test/support/mock_model.rb +0 -2
  201. data/test/tempfile_factory_test.rb +0 -13
  202. data/test/url_generator_test.rb +0 -187
  203. data/test/validators/attachment_content_type_validator_test.rb +0 -292
  204. data/test/validators_test.rb +0 -25
  205. /data/{test → spec}/database.yml +0 -0
  206. /data/{test → spec/support}/fixtures/12k.png +0 -0
  207. /data/{test → spec/support}/fixtures/50x50.png +0 -0
  208. /data/{test → spec/support}/fixtures/5k.png +0 -0
  209. /data/{test → spec/support}/fixtures/animated +0 -0
  210. /data/{test → spec/support}/fixtures/animated.gif +0 -0
  211. /data/{test → spec/support}/fixtures/animated.unknown +0 -0
  212. /data/{test → spec/support}/fixtures/bad.png +0 -0
  213. /data/{test → spec/support}/fixtures/fog.yml +0 -0
  214. /data/{test → spec/support}/fixtures/rotated.jpg +0 -0
  215. /data/{test → spec/support}/fixtures/s3.yml +0 -0
  216. /data/{test → spec/support}/fixtures/spaced file.png +0 -0
  217. /data/{test → spec/support}/fixtures/text.txt +0 -0
  218. /data/{test → spec/support}/fixtures/twopage.pdf +0 -0
  219. /data/{test → spec/support}/fixtures/uppercase.PNG +0 -0
  220. /data/{test → spec}/support/mock_interpolator.rb +0 -0
@@ -5,26 +5,26 @@ module Paperclip
5
5
  module Schema
6
6
  COLUMNS = {:file_name => :string,
7
7
  :content_type => :string,
8
- :file_size => :integer,
8
+ :file_size => :bigint,
9
9
  :updated_at => :datetime}
10
10
 
11
11
  def self.included(base)
12
12
  ActiveRecord::ConnectionAdapters::Table.send :include, TableDefinition
13
13
  ActiveRecord::ConnectionAdapters::TableDefinition.send :include, TableDefinition
14
14
  ActiveRecord::ConnectionAdapters::AbstractAdapter.send :include, Statements
15
-
16
- if defined?(ActiveRecord::Migration::CommandRecorder) # Rails 3.1+
17
- ActiveRecord::Migration::CommandRecorder.send :include, CommandRecorder
18
- end
15
+ ActiveRecord::Migration::CommandRecorder.send :include, CommandRecorder
19
16
  end
20
17
 
21
18
  module Statements
22
19
  def add_attachment(table_name, *attachment_names)
23
20
  raise ArgumentError, "Please specify attachment name in your add_attachment call in your migration." if attachment_names.empty?
24
21
 
22
+ options = attachment_names.extract_options!
23
+
25
24
  attachment_names.each do |attachment_name|
26
25
  COLUMNS.each_pair do |column_name, column_type|
27
- add_column(table_name, "#{attachment_name}_#{column_name}", column_type)
26
+ column_options = options.merge(options[column_name.to_sym] || {})
27
+ add_column(table_name, "#{attachment_name}_#{column_name}", column_type, column_options)
28
28
  end
29
29
  end
30
30
  end
@@ -33,7 +33,7 @@ module Paperclip
33
33
  raise ArgumentError, "Please specify attachment name in your remove_attachment call in your migration." if attachment_names.empty?
34
34
 
35
35
  attachment_names.each do |attachment_name|
36
- COLUMNS.each_pair do |column_name, column_type|
36
+ COLUMNS.keys.each do |column_name|
37
37
  remove_column(table_name, "#{attachment_name}_#{column_name}")
38
38
  end
39
39
  end
@@ -47,9 +47,11 @@ module Paperclip
47
47
 
48
48
  module TableDefinition
49
49
  def attachment(*attachment_names)
50
+ options = attachment_names.extract_options!
50
51
  attachment_names.each do |attachment_name|
51
52
  COLUMNS.each_pair do |column_name, column_type|
52
- column("#{attachment_name}_#{column_name}", column_type)
53
+ column_options = options.merge(options[column_name.to_sym] || {})
54
+ column("#{attachment_name}_#{column_name}", column_type, column_options)
53
55
  end
54
56
  end
55
57
  end
@@ -36,13 +36,17 @@ module Paperclip
36
36
  def flush_writes #:nodoc:
37
37
  @queued_for_write.each do |style_name, file|
38
38
  FileUtils.mkdir_p(File.dirname(path(style_name)))
39
- File.open(path(style_name), "wb") do |new_file|
40
- while chunk = file.read(16 * 1024)
41
- new_file.write(chunk)
39
+ begin
40
+ move_file(file.path, path(style_name))
41
+ rescue SystemCallError
42
+ File.open(path(style_name), "wb") do |new_file|
43
+ while chunk = file.read(16 * 1024)
44
+ new_file.write(chunk)
45
+ end
42
46
  end
43
47
  end
44
48
  unless @options[:override_file_permissions] == false
45
- resolved_chmod = (@options[:override_file_permissions] &~ 0111) || (0666 &~ File.umask)
49
+ resolved_chmod = (@options[:override_file_permissions] & ~0111) || (0666 & ~File.umask)
46
50
  FileUtils.chmod( resolved_chmod, path(style_name) )
47
51
  end
48
52
  file.rewind
@@ -65,7 +69,7 @@ module Paperclip
65
69
  while(true)
66
70
  path = File.dirname(path)
67
71
  FileUtils.rmdir(path)
68
- break if File.exists?(path) # Ruby 1.9.2 does not raise if the removal failed.
72
+ break if File.exist?(path) # Ruby 1.9.2 does not raise if the removal failed.
69
73
  end
70
74
  rescue Errno::EEXIST, Errno::ENOTEMPTY, Errno::ENOENT, Errno::EINVAL, Errno::ENOTDIR, Errno::EACCES
71
75
  # Stop trying to remove parent directories
@@ -80,6 +84,17 @@ module Paperclip
80
84
  def copy_to_local_file(style, local_dest_path)
81
85
  FileUtils.cp(path(style), local_dest_path)
82
86
  end
87
+
88
+ private
89
+
90
+ def move_file(src, dest)
91
+ # Support hardlinked files
92
+ if File.identical?(src, dest)
93
+ File.unlink(src)
94
+ else
95
+ FileUtils.mv(src, dest)
96
+ end
97
+ end
83
98
  end
84
99
 
85
100
  end
@@ -14,11 +14,12 @@ module Paperclip
14
14
  # aws_secret_access_key: '<your aws_secret_access_key>'
15
15
  # provider: 'AWS'
16
16
  # region: 'eu-west-1'
17
+ # scheme: 'https'
17
18
  # * +fog_directory+: This is the name of the S3 bucket that will
18
19
  # store your files. Remember that the bucket must be unique across
19
20
  # all of Amazon S3. If the bucket does not exist, Paperclip will
20
21
  # attempt to create it.
21
- # * +fog_file*: This can be hash or lambda returning hash. The
22
+ # * +fog_file+: This can be hash or lambda returning hash. The
22
23
  # value is used as base properties for new uploaded file.
23
24
  # * +path+: This is the key under the bucket in which the file will
24
25
  # be stored. The URL will be constructed from the bucket and the
@@ -32,6 +33,10 @@ module Paperclip
32
33
  # that is the alias to the S3 domain of your bucket, e.g.
33
34
  # 'http://images.example.com'. This can also be used in
34
35
  # conjunction with Cloudfront (http://aws.amazon.com/cloudfront)
36
+ # * +fog_options+: (optional) A hash of options that are passed
37
+ # to fog when the file is created. For example, you could set
38
+ # the multipart-chunk size to 100MB with a hash:
39
+ # { :multipart_chunk_size => 104857600 }
35
40
 
36
41
  module Fog
37
42
  def self.extended base
@@ -43,8 +48,8 @@ module Paperclip
43
48
  end unless defined?(Fog)
44
49
 
45
50
  base.instance_eval do
46
- unless @options[:url].to_s.match(/^:fog.*url$/)
47
- @options[:path] = @options[:path].gsub(/:url/, @options[:url]).gsub(/^:rails_root\/public\/system\//, '')
51
+ unless @options[:url].to_s.match(/\A:fog.*url\z/)
52
+ @options[:path] = @options[:path].gsub(/:url/, @options[:url]).gsub(/\A:rails_root\/public\/system\//, '')
48
53
  @options[:url] = ':fog_public_url'
49
54
  end
50
55
  Paperclip.interpolates(:fog_public_url) do |attachment, style|
@@ -53,7 +58,7 @@ module Paperclip
53
58
  end
54
59
  end
55
60
 
56
- AWS_BUCKET_SUBDOMAIN_RESTRICTON_REGEX = /^(?:[a-z]|\d(?!\d{0,2}(?:\.\d{1,3}){3}$))(?:[a-z0-9]|\.(?![\.\-])|\-(?![\.])){1,61}[a-z0-9]$/
61
+ AWS_BUCKET_SUBDOMAIN_RESTRICTON_REGEX = /\A(?:[a-z]|\d(?!\d{0,2}(?:\.\d{1,3}){3}\z))(?:[a-z0-9]|\.(?![\.\-])|\-(?![\.])){1,61}[a-z0-9]\z/
57
62
 
58
63
  def exists?(style = default_style)
59
64
  if original_filename
@@ -81,11 +86,14 @@ module Paperclip
81
86
  end
82
87
 
83
88
  def fog_public(style = default_style)
84
- if @options.has_key?(:fog_public)
85
- if @options[:fog_public].respond_to?(:has_key?) && @options[:fog_public].has_key?(style)
86
- @options[:fog_public][style]
89
+ if @options.key?(:fog_public)
90
+ value = @options[:fog_public]
91
+ if value.respond_to?(:key?) && value.key?(style)
92
+ value[style]
93
+ elsif value.respond_to?(:call)
94
+ value.call(self)
87
95
  else
88
- @options[:fog_public]
96
+ value
89
97
  end
90
98
  else
91
99
  true
@@ -97,16 +105,19 @@ module Paperclip
97
105
  log("saving #{path(style)}")
98
106
  retried = false
99
107
  begin
100
- directory.files.create(fog_file.merge(
108
+ attributes = fog_file.merge(
101
109
  :body => file,
102
110
  :key => path(style),
103
111
  :public => fog_public(style),
104
112
  :content_type => file.content_type
105
- ))
113
+ )
114
+ attributes.merge!(@options[:fog_options]) if @options[:fog_options]
115
+ directory.files.create(attributes)
106
116
  rescue Excon::Errors::NotFound
107
117
  raise if retried
108
118
  retried = true
109
119
  directory.save
120
+ file.rewind
110
121
  retry
111
122
  ensure
112
123
  file.rewind
@@ -131,22 +142,24 @@ module Paperclip
131
142
  "#{dynamic_fog_host_for_style(style)}/#{path(style)}"
132
143
  else
133
144
  if fog_credentials[:provider] == 'AWS'
134
- "https://#{host_name_for_directory}/#{path(style)}"
145
+ "#{scheme}://#{host_name_for_directory}/#{path(style)}"
135
146
  else
136
147
  directory.files.new(:key => path(style)).public_url
137
148
  end
138
149
  end
139
150
  end
140
151
 
141
- def expiring_url(time = (Time.now + 3600), style = default_style)
142
- if directory.files.respond_to?(:get_http_url)
143
- expiring_url = directory.files.get_http_url(path(style), time)
152
+ def expiring_url(time = (Time.now + 3600), style_name = default_style)
153
+ time = convert_time(time)
154
+ http_url_method = "get_#{scheme}_url"
155
+ if path(style_name) && directory.files.respond_to?(http_url_method)
156
+ expiring_url = directory.files.public_send(http_url_method, path(style_name), time)
144
157
 
145
158
  if @options[:fog_host]
146
- expiring_url.gsub!(/#{host_name_for_directory}/, dynamic_fog_host_for_style(style))
159
+ expiring_url.gsub!(/#{host_name_for_directory}/, dynamic_fog_host_for_style(style_name))
147
160
  end
148
161
  else
149
- expiring_url = public_url
162
+ expiring_url = url(style_name)
150
163
  end
151
164
 
152
165
  return expiring_url
@@ -154,14 +167,14 @@ module Paperclip
154
167
 
155
168
  def parse_credentials(creds)
156
169
  creds = find_credentials(creds).stringify_keys
157
- env = Object.const_defined?(:Rails) ? Rails.env : nil
158
- (creds[env] || creds).symbolize_keys
170
+ (creds[RailsEnvironment.get] || creds).symbolize_keys
159
171
  end
160
172
 
161
173
  def copy_to_local_file(style, local_dest_path)
162
174
  log("copying #{path(style)} to local file #{local_dest_path}")
163
175
  ::File.open(local_dest_path, 'wb') do |local_file|
164
176
  file = directory.files.get(path(style))
177
+ return false unless file
165
178
  local_file.write(file.body)
166
179
  end
167
180
  rescue ::Fog::Errors::Error => e
@@ -171,6 +184,13 @@ module Paperclip
171
184
 
172
185
  private
173
186
 
187
+ def convert_time(time)
188
+ if time.is_a?(Integer)
189
+ time = Time.now + time
190
+ end
191
+ time
192
+ end
193
+
174
194
  def dynamic_fog_host_for_style(style)
175
195
  if @options[:fog_host].respond_to?(:call)
176
196
  @options[:fog_host].call(self)
@@ -180,10 +200,10 @@ module Paperclip
180
200
  end
181
201
 
182
202
  def host_name_for_directory
183
- if @options[:fog_directory].to_s =~ Fog::AWS_BUCKET_SUBDOMAIN_RESTRICTON_REGEX
184
- "#{@options[:fog_directory]}.s3.amazonaws.com"
203
+ if directory_name.to_s =~ Fog::AWS_BUCKET_SUBDOMAIN_RESTRICTON_REGEX
204
+ "#{directory_name}.s3.amazonaws.com"
185
205
  else
186
- "s3.amazonaws.com/#{@options[:fog_directory]}"
206
+ "s3.amazonaws.com/#{directory_name}"
187
207
  end
188
208
  end
189
209
 
@@ -209,13 +229,19 @@ module Paperclip
209
229
  end
210
230
 
211
231
  def directory
212
- dir = if @options[:fog_directory].respond_to?(:call)
232
+ @directory ||= connection.directories.new(key: directory_name)
233
+ end
234
+
235
+ def directory_name
236
+ if @options[:fog_directory].respond_to?(:call)
213
237
  @options[:fog_directory].call(self)
214
238
  else
215
239
  @options[:fog_directory]
216
240
  end
241
+ end
217
242
 
218
- @directory ||= connection.directories.new(:key => dir)
243
+ def scheme
244
+ @scheme ||= fog_credentials[:scheme] || 'https'
219
245
  end
220
246
  end
221
247
  end