carrierwave_direct 0.0.13 → 0.0.14

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3ba4e8171bac7dffd59212cdb0d05567072cae76
4
- data.tar.gz: 0a1e05710c23fa992faf63773afc079187389e8b
3
+ metadata.gz: df532738055d7fe9993268699211f219bdf0eeb2
4
+ data.tar.gz: 73234daa2e001178a73ef9caec211d2aaecfd3c6
5
5
  SHA512:
6
- metadata.gz: 8263b034630c1cbc1cdf8a92a276973dc70b879273cee525246813822702c045b7aab00a933279ccfbf7828010639f227fbab3d9cf12d26137f3c95e4a2bc43a
7
- data.tar.gz: 43e72d866f555d57db0dc258116a9b23ac5358f4c7d80e7dd19e9527f8e1050c04cffb89e07436ca2b8f024ed5f63a4a7571bed707fb235fb7e46ea8410d5ae3
6
+ metadata.gz: fa994ea1bdab12573044260fc76389f0bde0866a355eb648c82726637e26247dca2aaab1e08c2122380f9704a8a59c90c4c422754e3d79fc581651fd83563452
7
+ data.tar.gz: b4c5109688c2dfe132686f9ea3f86e7ae7d08d8c7f66216d7385569b9f715e5ca0e41325b00c25bbe2e519aa864164d2483b3a400d88754ad92bbef67db04950
data/.gitignore CHANGED
@@ -4,5 +4,6 @@ Gemfile.lock
4
4
  .rvmrc
5
5
  .ruby-version
6
6
  pkg/*
7
+ /.idea/
7
8
  vendor/ruby
8
9
  .ruby-version
@@ -1,2 +1,4 @@
1
1
  rvm:
2
2
  - 1.9.3
3
+ - 2.0.0
4
+ - 2.1.0
@@ -0,0 +1,94 @@
1
+ ### 0.0.14
2
+
3
+ [Full Changes](https://github.com/dwilkie/carrierwave_direct/compare/v0.0.13...master)
4
+
5
+ Features:
6
+ * Add ability to set content type in upload form (John Kamenik @jkamenik)
7
+ * Dutch language support (Ariejan de Vroom @ariejan)
8
+
9
+ Bug Fixes:
10
+ * Escape characters in filenames (@geeky-sh)
11
+ * Use OpenSSL::Digest instead of OpenSSL::Digest::Digest (@dwiedenbruch)
12
+ * Fix signature race condition by caching policy (Louis Simoneau @lsimoneau)
13
+ * Fix multi-encoding issue when saving escaped filenames (Vincent Franco @vinniefranco)
14
+ * Use mounted-on column name for uniqueness validation (Stephan Schubert @jazen)
15
+
16
+ Misc:
17
+ * Improve readme documentation for success action status support (Rafael Macedo @rafaelmacedo)
18
+ * Increase robutness of view rpsec matchers (@sony-phoenix-dev)
19
+ * Add ruby 2.1.0 support to travis (Luciano Sousa @lucianosousa)
20
+
21
+ ### 0.0.13
22
+
23
+ [Full Changes](https://github.com/dwilkie/carrierwave_direct/compare/v0.0.12...v0.0.13)
24
+
25
+ Features:
26
+ * Add option to use success_action_status instead of success_action_redirect (Nick DeLuca @nddeluca)
27
+
28
+ Bug Fixes:
29
+ * Remove intial slash when generating key from url in order to fix updates (Enrique García @kikito)
30
+ * Fix key generation when #default_url is overriden (@dunghuynh)
31
+ * Fix policy glitch that allows other files to be overwritten (@dunghuynh)
32
+
33
+ Misc:
34
+ * Update resque url in readme (Ever Daniel Barreto @everdaniel)
35
+ * update readme (Philip Arndt @parndt)
36
+
37
+
38
+ ### 0.0.12
39
+
40
+ [Full Changes](https://github.com/dwilkie/carrierwave_direct/compare/v0.0.11...v0.0.12)
41
+
42
+ Features:
43
+ * use uuidtools gem instead of uuid gem for uid generation (Filip Tepper @filiptepper)
44
+
45
+ Bug Fixes:
46
+ * fix URI parsing issues with cetain filenames (Ricky Pai @rickypai)
47
+ * prevent double slashes in urls generated from direct_fog_url (Colin Young @colinyoung)
48
+
49
+ Misc:
50
+ * fix typo in readme (@hartator)
51
+
52
+ ### 0.0.11
53
+
54
+ [Full Changes](https://github.com/dwilkie/carrierwave_direct/compare/v0.0.10...v0.0.11)
55
+
56
+ ### 0.0.10
57
+
58
+ [Full Changes](https://github.com/dwilkie/carrierwave_direct/compare/v0.0.9...v0.0.10)
59
+
60
+ ### 0.0.9
61
+
62
+ [Full Changes](https://github.com/dwilkie/carrierwave_direct/compare/v0.0.8...v0.0.9)
63
+
64
+ ### 0.0.8
65
+
66
+ [Full Changes](https://github.com/dwilkie/carrierwave_direct/compare/v0.0.7...v0.0.8)
67
+
68
+ ### 0.0.7
69
+
70
+ [Full Changes](https://github.com/dwilkie/carrierwave_direct/compare/v0.0.6...v0.0.7)
71
+
72
+ ### 0.0.6
73
+
74
+ [Full Changes](https://github.com/dwilkie/carrierwave_direct/compare/v0.0.5...v0.0.6)
75
+
76
+ ### 0.0.5
77
+
78
+ [Full Changes](https://github.com/dwilkie/carrierwave_direct/compare/v0.0.4...v0.0.5)
79
+
80
+ ### 0.0.4
81
+
82
+ [Full Changes](https://github.com/dwilkie/carrierwave_direct/compare/v0.0.3...v0.0.4)
83
+
84
+ ### 0.0.3
85
+
86
+ [Full Changes](https://github.com/dwilkie/carrierwave_direct/compare/v0.0.2...v0.0.3)
87
+
88
+ ### 0.0.2
89
+
90
+ [Full Changes](https://github.com/dwilkie/carrierwave_direct/compare/v0.0.1...v0.0.2)
91
+
92
+ ### 0.0.1
93
+
94
+ [Full Changes](https://github.com/dwilkie/carrierwave_direct/compare/e68498587a4e4209d121512dbb0df529e15e9282...v0.0.1)
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
- source "http://rubygems.org"
1
+ source "https://rubygems.org"
2
2
 
3
3
  # Specify your gem's dependencies in carrierwave_direct.gemspec
4
4
  gemspec
data/README.md CHANGED
@@ -6,33 +6,6 @@
6
6
 
7
7
  [CarrierWaveDirect](https://github.com/dwilkie/carrierwave_direct) works on top of [CarrierWave](https://github.com/jnicklas/carrierwave) and provides a simple way to achieve this.
8
8
 
9
- ## Project Maintainer(s) Wanted
10
-
11
- Meh. [I just want to use it already.](https://github.com/dwilkie/carrierwave_direct#example-application)
12
-
13
- ### Why can't you do it?
14
-
15
- I'm no longer involved in building apps which require file uploads. Therefore I no longer feel that I can do a good job maintaining this project. I just want the community to take it where it needs to go.
16
-
17
- ### What's in it for me?
18
-
19
- * Everything
20
- * Job Applications
21
- * Not much
22
- * What you make of it
23
-
24
- ### What do I need to do?
25
-
26
- You can help by simply accepting pull requests and bumping versions. Or if you want to be more involved you could help resolve outstanding issues or develop new features.
27
-
28
- ### Who are you looking for?
29
-
30
- Anybody who has previously contributed to CarrierWaveDirect (with tests) and/or who anybody who really wants to.
31
-
32
- ### How can I apply?
33
-
34
- This is not a job application. I you want to maintain it, you've basically got the job. Just comment on this issue: https://github.com/dwilkie/carrierwave_direct/issues/83 and link to a contribution you've made on this gem, or some other gem.
35
-
36
9
  ## Example Application
37
10
 
38
11
  For a concrete example on how to use [CarrierWaveDirect](https://github.com/dwilkie/carrierwave_direct) in a Rails application check out the [Example Application](https://github.com/dwilkie/carrierwave_direct_example).
@@ -227,26 +200,45 @@ Once you've uploaded your file directly to the cloud you'll probably need a way
227
200
 
228
201
  The default amazon content-type is "binary/octet-stream" and for many cases this will work just fine. But if you are trying to stream video or audio you will need to set the mime type manually as Amazon will not calculate it for you. All mime types are supported: [http://en.wikipedia.org/wiki/Internet_media_type](http://en.wikipedia.org/wiki/Internet_media_type).
229
202
 
230
- First, tell CarrierWaveDirect that you will include your content type manually by adding to your initializer:
203
+ First, tell CarrierWaveDirect what your default content type should be:
231
204
 
232
205
  ```ruby
233
206
  CarrierWave.configure do |config|
234
207
  # ... fog configuration and other options ...
235
208
  config.will_include_content_type = true
209
+
210
+ config.default_content_type = 'video/mpeg'
211
+ config.allowed_content_types = %w(video/mpeg video/mp4 video/ogg)
212
+ end
213
+ ```
214
+
215
+ or
216
+
217
+ ```ruby
218
+ class VideoUploader < CarrierWave::Uploader::Base
219
+ include CarrierWaveDirect::Uploader
220
+
221
+ def will_include_content_type
222
+ true
223
+ end
224
+
225
+ default_content_type 'video/mpeg'
226
+ allowed_content_types %w(video/mpeg video/mp4 video/ogg)
236
227
  end
237
228
  ```
238
229
 
239
- Then, just add a content-type element to the form.
230
+ *Note: If `will_include_content_type` is `true` and `default_content_type` is nil, the content type will default to 'binary/octet-stream'.*
231
+
232
+ For the forms, no change is required to use the default_content_type.
240
233
 
241
234
  ```erb
242
235
  <%= direct_upload_form_for @uploader do |f| %>
243
- <%= text_field_tag 'Content-Type', 'video/mpeg' %><br>
244
- <%= f.file_field :avatar %>
236
+ <%= f.file_field :avatar %> # Content-Type will automatically be set to the default
245
237
  <%= f.submit %>
246
238
  <% end %>
247
239
  ```
248
240
 
249
- You could use a select as well.
241
+ You could use a manual select as well.
250
242
 
251
243
  ```erb
252
244
  <%= direct_upload_form_for @uploader do |f| %>
@@ -255,11 +247,24 @@ You could use a select as well.
255
247
  ['Audio','audio/mpeg'],
256
248
  ['Image','image/jpeg']
257
249
  ], 'video/mpeg') %><br>
258
- <%= f.file_field :avatar %>
250
+ <%= f.file_field :avatar, exclude_content_type: true %> # Must tell the file_field helper not to include content type
251
+ <%= f.submit %>
252
+ <% end %>
253
+ ```
254
+
255
+ Or you can use the helper which shows all possible content types as a select, with the default content type selected.
256
+
257
+ ```erb
258
+ <%= direct_upload_form_for @uploader do |f| %>
259
+ <%= f.content_type_label %><br>
260
+ <%= f.content_type_select %><br><br>
261
+
262
+ <%= f.file_field :avatar, exclude_content_type: true %><br> # Must tell the file_field helper not to include its own content type
259
263
  <%= f.submit %>
260
264
  <% end %>
261
265
  ```
262
266
 
267
+
263
268
  ## Processing and referencing files in a background process
264
269
 
265
270
  Processing and saving file uploads are typically long running tasks and should be done in a background process. CarrierWaveDirect gives you a few methods to help you do this with your favorite background processor such as [DelayedJob](https://github.com/collectiveidea/delayed_job) or [Resque](https://github.com/resque/resque).
@@ -407,14 +412,20 @@ CarrierWave.configure do |config|
407
412
  config.validate_filename_format = false # defaults to true
408
413
  config.validate_remote_net_url_format = false # defaults to true
409
414
 
410
- config.min_file_size = 5.kilobytes # defaults to 1.byte
411
- config.max_file_size = 10.megabytes # defaults to 5.megabytes
412
- config.upload_expiration = 1.hour # defaults to 10.hours
413
- config.will_include_content_type = true # defaults to false; if true, content-type will be set
414
- # on s3, but you must include an input field named
415
- # Content-Type on every direct upload form
415
+ config.min_file_size = 5.kilobytes # defaults to 1.byte
416
+ config.max_file_size = 10.megabytes # defaults to 5.megabytes
417
+ config.upload_expiration = 1.hour # defaults to 10.hours
418
+ config.will_include_content_type = 'video/mp4' # defaults to false; if true, content-type will be set
419
+ # on s3, but you must include an input field named
420
+ # Content-Type on every direct upload form
421
+
422
+ config.use_action_status = true # defaults to false; if true you must set in your uploader
423
+ # 'uploader.success_action_status = 201' and set
424
+ # 'f.file_field :avatar, use_action_status: true' to works
425
+ # properly
416
426
  end
417
427
  ```
428
+
418
429
  ## Testing with CarrierWaveDirect
419
430
 
420
431
  CarrierWaveDirect provides a couple of helpers to help with integration and unit testing. You don't want to contact the Internet during your tests as this is slow, expensive and unreliable. You should first put fog into mock mode by doing something like this.
@@ -538,10 +549,10 @@ After you have fixed a bug or added a feature please also use the [CarrierWaveDi
538
549
 
539
550
  ## Credits
540
551
 
541
- Thank you to everybody who has contributed to [CarrierWaveDirect](https://github.com/dwilkie/carrierwave_direct). A special thanks to [colinyoung (Colin Young)](https://github.com/colinyoung) and [nddeluca (Nicholas DeLuca)](https://github.com/nddeluca) who help maintain the gem.
552
+ Thank you to everybody who has contributed to [CarrierWaveDirect](https://github.com/dwilkie/carrierwave_direct). A special thanks to [nddeluca (Nicholas DeLuca)](https://github.com/nddeluca) who does a great job maintaining this gem. If you'd like to help him out see [#83](https://github.com/dwilkie/carrierwave_direct/issues/83)
542
553
 
543
554
  ## Contributors
544
-
555
+ * [geeky-sh](https://github.com/geeky-sh) - Allow filenames with '()+[]' characters
545
556
  * [cblunt (Chris Blunt)](https://github.com/cblunt) - Support for passing html options
546
557
  * [robyurkowski (Rob Yurkowski)](https://github.com/robyurkowski) - Fix deprecation warnings for Rails 3.2
547
558
  * [tylr (Tyler Love)](https://github.com/tylr) - Bug fix
@@ -559,4 +570,5 @@ Thank you to everybody who has contributed to [CarrierWaveDirect](https://github
559
570
  * [rsniezynski](https://github.com/rsniezynski) - Add min file size configuration
560
571
  * [philipp-spiess (Philipp Spieß)](https://github.com/philipp-spiess) - Direct fog url bugfix
561
572
  * [colinyoung (Colin Young)](https://github.com/colinyoung) - Content-Type support
573
+ * [jkamenik (John Kamenik)](https://github.com/jkamenik) - Content-Type support
562
574
  * [filiptepper (Filip Tepper)](https://github.com/filiptepper) - Autoload UUID on heroku
@@ -1,18 +1,13 @@
1
1
  # encoding: utf-8
2
2
 
3
- require "carrierwave_direct/version"
4
-
5
3
  require "carrierwave"
6
4
  require "uuidtools"
7
5
  require "fog"
8
6
 
9
7
  module CarrierWaveDirect
10
8
 
11
- autoload :Uploader, "carrierwave_direct/uploader"
12
- autoload :Mount, "carrierwave_direct/mount"
13
-
14
9
  module Uploader
15
- autoload :Configuration, 'carrierwave_direct/uploader/configuration'
10
+ require "carrierwave_direct/uploader/configuration"
16
11
 
17
12
  CarrierWave::Uploader::Base.send(:include, Configuration)
18
13
  end
@@ -43,3 +38,7 @@ if defined?(Rails)
43
38
  end
44
39
  end
45
40
 
41
+ require "carrierwave_direct/mount"
42
+ require "carrierwave_direct/uploader"
43
+ require "carrierwave_direct/version"
44
+
@@ -4,20 +4,55 @@ module CarrierWaveDirect
4
4
  class FormBuilder < ActionView::Helpers::FormBuilder
5
5
  def file_field(method, options = {})
6
6
  options.merge!(:name => "file")
7
- fields = hidden_field(:key, :name => "key")
8
- fields << hidden_field(:aws_access_key_id, :name => "AWSAccessKeyId")
9
- fields << hidden_field(:acl, :name => "acl")
10
- fields << hidden_field(:policy, :name => "policy")
11
- fields << hidden_field(:signature, :name => "signature")
12
-
13
- if options[:use_action_status]
14
- fields << hidden_field(:success_action_status, :name => "success_action_status")
7
+
8
+ fields = required_base_fields
9
+
10
+ fields << content_type_field(options)
11
+
12
+ fields << success_action_field(options)
13
+
14
+ # The file field must be the last element in the form.
15
+ # Any element after this will be ignored by Amazon.
16
+ fields << super
17
+ end
18
+
19
+ def content_type_label(content=nil)
20
+ content ||= 'Content Type'
21
+ @template.label_tag('Content-Type', content)
22
+ end
23
+
24
+ def content_type_select(choices = [], selected = nil, options = {})
25
+ @template.select_tag('Content-Type', content_choices_options(choices, selected), options)
26
+ end
27
+
28
+ private
29
+
30
+ def required_base_fields
31
+ hidden_field(:key, :name => "key") <<
32
+ hidden_field(:aws_access_key_id, :name => "AWSAccessKeyId") <<
33
+ hidden_field(:acl, :name => "acl") <<
34
+ hidden_field(:policy, :name => "policy") <<
35
+ hidden_field(:signature, :name => "signature")
36
+ end
37
+
38
+ def content_type_field(options)
39
+ return ''.html_safe unless @object.will_include_content_type
40
+
41
+ hidden_field(:content_type, :name => 'Content-Type') unless options[:exclude_content_type]
42
+ end
43
+
44
+ def success_action_field(options)
45
+ if @object.use_action_status
46
+ hidden_field(:success_action_status, :name => "success_action_status")
15
47
  else
16
- fields << hidden_field(:success_action_redirect, :name => "success_action_redirect")
48
+ hidden_field(:success_action_redirect, :name => "success_action_redirect")
17
49
  end
50
+ end
18
51
 
19
- fields << super
52
+ def content_choices_options(choices, selected = nil)
53
+ choices = @object.content_types if choices.blank?
54
+ selected ||= @object.content_type
55
+ @template.options_for_select(choices, selected)
20
56
  end
21
57
  end
22
58
  end
23
-
@@ -0,0 +1,9 @@
1
+ nl:
2
+ errors:
3
+ messages:
4
+ carrierwave_direct_filename_taken: bestandsnaam is reeds in gebruik
5
+ carrierwave_direct_upload_missing: geüpload bestand ontbreekt
6
+ carrierwave_direct_attachment_missing: bestand ontbreekt
7
+ carrierwave_direct_filename_invalid: "is ongeldig. "
8
+ carrierwave_direct_allowed_extensions: Toegestane betandstypen zijn %{extensions}
9
+ carrierwave_direct_allowed_schemes: "Toegestane schema's zijn %{schemes}"
@@ -11,11 +11,11 @@ module CarrierWaveDirect
11
11
  end
12
12
 
13
13
  def find_key
14
- key = page.find("input[@name='key']").value
14
+ key = page.find("input[name='key']", visible: false).value
15
15
  end
16
16
 
17
17
  def find_upload_path
18
- page.find("input[@name='file']").value
18
+ page.find("input[name='file']", visible: false).value
19
19
  end
20
20
 
21
21
  def upload_directly(uploader, button_locator, options = {})
@@ -26,7 +26,7 @@ module CarrierWaveDirect
26
26
  # simulate a successful upload
27
27
 
28
28
  # form's success action redirect url
29
- redirect_url = URI.parse(page.find("input[@name='success_action_redirect']").value)
29
+ redirect_url = URI.parse(page.find("input[name='success_action_redirect']", visible: false).value)
30
30
 
31
31
  unless options[:redirect_key]
32
32
  sample_key_args = [{:base => find_key, :filename => File.basename(find_upload_path)}]
@@ -1,8 +1,10 @@
1
1
  # encoding: utf-8
2
2
 
3
+ require "carrierwave_direct/uploader/content_type"
4
+ require "carrierwave_direct/uploader/direct_url"
5
+
3
6
  module CarrierWaveDirect
4
7
  module Uploader
5
-
6
8
  extend ActiveSupport::Concern
7
9
 
8
10
  FILENAME_WILDCARD = "${filename}"
@@ -20,84 +22,72 @@ module CarrierWaveDirect
20
22
  end
21
23
  end
22
24
 
23
- def direct_fog_url(options = {})
24
- fog_uri = CarrierWave::Storage::Fog::File.new(self, CarrierWave::Storage::Fog.new(self), nil).public_url
25
- if options[:with_path]
26
- uri = URI.parse(fog_uri.chomp('/'))
27
- path = "/#{key}"
28
- uri.path += URI.escape(path)
29
- fog_uri = uri.to_s
30
- end
31
- fog_uri
25
+ include CarrierWaveDirect::Uploader::ContentType
26
+ include CarrierWaveDirect::Uploader::DirectUrl
27
+
28
+ def acl
29
+ fog_public ? 'public-read' : 'private'
32
30
  end
33
31
 
34
- def guid
35
- UUIDTools::UUID.random_create
32
+ def policy(options = {})
33
+ options[:expiration] ||= upload_expiration
34
+ options[:min_file_size] ||= min_file_size
35
+ options[:max_file_size] ||= max_file_size
36
+
37
+ @policy ||= generate_policy(options)
36
38
  end
37
39
 
38
- def key=(k)
39
- @key = k
40
- update_version_keys(:with => @key)
40
+ def clear_policy!
41
+ @policy = nil
42
+ end
43
+
44
+ def signature
45
+ Base64.encode64(
46
+ OpenSSL::HMAC.digest(
47
+ OpenSSL::Digest.new('sha1'),
48
+ aws_secret_access_key, policy
49
+ )
50
+ ).gsub("\n","")
51
+ end
52
+
53
+ def url_scheme_white_list
54
+ nil
55
+ end
56
+
57
+ def persisted?
58
+ false
41
59
  end
42
60
 
43
61
  def key
44
62
  return @key if @key.present?
45
63
  if present?
46
- self.key = URI.parse(URI.encode(url)).path[1 .. -1] # explicitly set key
64
+ self.key = URI.parse(encoded_url).path[1 .. -1] # explicitly set key
47
65
  else
48
66
  @key = "#{store_dir}/#{guid}/#{FILENAME_WILDCARD}"
49
67
  end
50
68
  @key
51
69
  end
52
70
 
53
- def has_key?
54
- key !~ /#{Regexp.escape(FILENAME_WILDCARD)}\z/
71
+ def key=(k)
72
+ @key = k
73
+ update_version_keys(:with => @key)
55
74
  end
56
75
 
57
- def acl
58
- fog_public ? 'public-read' : 'private'
76
+ def guid
77
+ UUIDTools::UUID.random_create
59
78
  end
60
79
 
61
- def policy(options = {})
62
- options[:expiration] ||= self.class.upload_expiration
63
- options[:min_file_size] ||= self.class.min_file_size
64
- options[:max_file_size] ||= self.class.max_file_size
65
-
66
- conditions = [
67
- ["starts-with", "$utf8", ""],
68
- ["starts-with", "$key", key.sub(/#{Regexp.escape(FILENAME_WILDCARD)}\z/, "")]
69
- ]
70
- conditions << ["starts-with", "$Content-Type", ""] if self.class.will_include_content_type
71
- conditions << {"bucket" => fog_directory}
72
- conditions << {"acl" => acl}
73
-
74
- if self.class.use_action_status
75
- conditions << {"success_action_status" => success_action_status}
76
- else
77
- conditions << {"success_action_redirect" => success_action_redirect}
78
- end
79
-
80
- conditions << ["content-length-range", options[:min_file_size], options[:max_file_size]]
81
-
82
- Base64.encode64(
83
- {
84
- 'expiration' => Time.now.utc + options[:expiration],
85
- 'conditions' => conditions
86
- }.to_json
87
- ).gsub("\n","")
80
+ def has_key?
81
+ key !~ /#{Regexp.escape(FILENAME_WILDCARD)}\z/
88
82
  end
89
83
 
90
- def signature
91
- Base64.encode64(
92
- OpenSSL::HMAC.digest(
93
- OpenSSL::Digest::Digest.new('sha1'),
94
- aws_secret_access_key, policy
95
- )
96
- ).gsub("\n","")
84
+ def key_regexp
85
+ /\A#{store_dir}\/[a-f\d\-]+\/.+\.(?i)#{extension_regexp}(?-i)\z/
97
86
  end
98
87
 
99
- def persisted?
100
- false
88
+ def extension_regexp
89
+ allowed_file_types = extension_white_list
90
+ extension_regexp = allowed_file_types.present? && allowed_file_types.any? ? "(#{allowed_file_types.join("|")})" : "\\w+"
101
91
  end
102
92
 
103
93
  def filename
@@ -115,21 +105,12 @@ module CarrierWaveDirect
115
105
  filename_parts.join("/")
116
106
  end
117
107
 
118
- def key_regexp
119
- /\A#{store_dir}\/[a-f\d\-]+\/.+\.(?i)#{extension_regexp}(?-i)\z/
120
- end
108
+ private
121
109
 
122
- def extension_regexp
123
- allowed_file_types = extension_white_list
124
- extension_regexp = allowed_file_types.present? && allowed_file_types.any? ? "(#{allowed_file_types.join("|")})" : "\\w+"
110
+ def encoded_url
111
+ URI.encode(URI.decode(url), " []+()")
125
112
  end
126
113
 
127
- def url_scheme_white_list
128
- nil
129
- end
130
-
131
- private
132
-
133
114
  def key_from_file(fname)
134
115
  new_key_parts = key.split("/")
135
116
  new_key_parts.pop
@@ -150,5 +131,31 @@ module CarrierWaveDirect
150
131
  extname = File.extname(for_file)
151
132
  [for_file.chomp(extname), version_name].compact.join('_') << extname
152
133
  end
134
+
135
+ def generate_policy(options)
136
+ conditions = [
137
+ ["starts-with", "$utf8", ""],
138
+ ["starts-with", "$key", key.sub(/#{Regexp.escape(FILENAME_WILDCARD)}\z/, "")]
139
+ ]
140
+
141
+ conditions << ["starts-with", "$Content-Type", ""] if will_include_content_type
142
+ conditions << {"bucket" => fog_directory}
143
+ conditions << {"acl" => acl}
144
+
145
+ if use_action_status
146
+ conditions << {"success_action_status" => success_action_status}
147
+ else
148
+ conditions << {"success_action_redirect" => success_action_redirect}
149
+ end
150
+
151
+ conditions << ["content-length-range", options[:min_file_size], options[:max_file_size]]
152
+
153
+ Base64.encode64(
154
+ {
155
+ 'expiration' => Time.now.utc + options[:expiration],
156
+ 'conditions' => conditions
157
+ }.to_json
158
+ ).gsub("\n","")
159
+ end
153
160
  end
154
161
  end