s3_direct_upload 0.1.6 → 0.1.7

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: 8ac508f7f1f70b53e01a6aec5262aaa6cf0917b8
4
- data.tar.gz: 90a7b3026a49c8a7750f24fb783544474685e4a1
3
+ metadata.gz: 6055f6f92f61ced275e84e740a348ad62113b645
4
+ data.tar.gz: 4a6c469db655a9fa46393dffcb4f5d5eab39902f
5
5
  SHA512:
6
- metadata.gz: 798ccce1f9b4f316df8f1b66028dad0a46e1331f8c87410cb5a1e57d16652aef642f48a96dea008ed1a8a71f3d69f9629ba1110d54219abd75fca9f2a8c0a68e
7
- data.tar.gz: 3abaa659fe3cea8190d143904eb1b6ed7c222da37cc49451199ad601e2fa07a492e05580efb57db93f7616a86304623b2abeea078ff118aaaf8b7c855d053c16
6
+ metadata.gz: 5eb5f0027b3b157a671d5b18642ac701bef3cce5b5f040c7d356d4a6dac281cfb1ff8c8926af138bef8150ea4ef6900d51d164ff1ed7b58f60bc857e7c0271f2
7
+ data.tar.gz: b4ba9c5092b06d35018b116660e260f433a39c26186261d92d4fa90b783b57be65c84200ec49489698968d6318c2f0251e7fbfc1d036c9fe0d7ada347ce51107
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # S3DirectUpload
2
2
 
3
+ [![Build Status](https://travis-ci.org/waynehoover/s3_direct_upload.png)](https://travis-ci.org/waynehoover/s3_direct_upload)
4
+
3
5
  Easily generate a form that allows you to upload directly to Amazon S3.
4
6
  Multi file uploading supported by jquery-fileupload.
5
7
 
@@ -18,7 +20,7 @@ S3DirectUpload.config do |c|
18
20
  c.access_key_id = "" # your access key id
19
21
  c.secret_access_key = "" # your secret access key
20
22
  c.bucket = "" # your bucket name
21
- c.region = nil # region prefix of your bucket url (optional), eg. "s3-eu-west-1"
23
+ c.region = nil # region prefix of your bucket url. This is _required_ for the non-default AWS region, eg. "s3-eu-west-1"
22
24
  c.url = nil # S3 API endpoint (optional), eg. "https://#{c.bucket}.s3.amazonaws.com/"
23
25
  end
24
26
  ```
@@ -53,7 +55,7 @@ Add the following js and css to your asset pipeline:
53
55
  ## Usage
54
56
  Create a new view that uses the form helper `s3_uploader_form`:
55
57
  ```ruby
56
- <%= s3_uploader_form callback_url: model_url, callback_param: "model[image_url]", id: "myS3Uploader" do %>
58
+ <%= s3_uploader_form callback_url: model_url, callback_param: "model[image_url]", id: "s3-uploader" do %>
57
59
  <%= file_field_tag :file, multiple: true %>
58
60
  <% end %>
59
61
  ```
@@ -63,7 +65,7 @@ Note: Its required that the file_field_tag is named 'file'.
63
65
  Then in your application.js.coffee, call the S3Uploader jQuery plugin on the element you created above:
64
66
  ```coffeescript
65
67
  jQuery ->
66
- $("#myS3Uploader").S3Uploader()
68
+ $("#s3-uploader").S3Uploader()
67
69
  ```
68
70
 
69
71
  Optionally, you can also place this template in the same view for the progress bars:
@@ -77,21 +79,38 @@ Optionally, you can also place this template in the same view for the progress b
77
79
  ```
78
80
 
79
81
  ## Options for form helper
80
- * `callback_url:` url that is POST'd to after file is uploaded to S3. If you don't specify this option, no callback to the server will be made after the file has uploaded to S3.
81
- * `callback_method:` Defaults to POST. Use PUT and remove the multiple option from your file field to update a model.
82
- * `callback_param:` parameter value for the POST in which the key will be the URL of the file on S3. If for example this is set to "model[image_url]" then the data posted would be `model[image_url] : http://bucketname.s3.amazonws.com/filename.ext`
83
- * `key:` key on s3. defaults to `"uploads/{timestamp}-{unique_id}-#{SecureRandom.hex}/${filename}"`. `{timestamp}` and `{unique_id}` are special substitution params that will be populated by the javascript with values for the current upload. `${filename}` is a special s3 param that will be populated with the original upload file name. Needs to be at least `"${filename}"`. It is highly recommended to use both `{unique_id}`, which will prevent collisions when uploading files with the same name (such as from a mobile device, where every photo is named image.jpg), and a server-generated random value such as `#{SecureRandom.hex}`, which adds further collision protection with other uploaders.
84
- * `key_starts_with:` constraint on key on s3. Defaults to `uploads/`. if you change the `key` option, make sure this starts with what you put there. If you set this as a blank string the upload path to s3 can be anything - not recommended!
85
- * `acl:` acl for files uploaded to s3, defaults to "public-read"
86
- * `max_file_size:` maximum file size, defaults to 500.megabytes
87
- * `id:` html id for the form, its recommended that you give the form an id so you can reference with the jQuery plugin.
88
- * `class:` optional html class for the form.
89
- * `data:` Optional html data
90
-
91
- ### Persisting the S3 url
92
- It is recommended that you persist the image_url that is sent back from the POST request (to the url given to the `post` option and as the key given in the `as` option). So to access your files later.
93
-
94
- One way to do this is to make sure you have `resources model` in your routes file, and add the `image_url` (or whatever you would like to name it) attribute to your model, and then make sure you have the create action in your controller for that model.
82
+ * `callback_url:` No default. The url that is POST'd to after file is uploaded to S3. If you don't specify this option, no callback to the server will be made after the file has uploaded to S3.
83
+ * `callback_method:` Defaults to `POST`. Use PUT and remove the multiple option from your file field to update a model.
84
+ * `callback_param:` Defaults to `file`. Parameter key for the POST to `callback_url` the value will be the full s3 url of the file. If for example this is set to "model[image_url]" then the data posted would be `model[image_url] : http://bucketname.s3.amazonws.com/filename.ext`
85
+ * `key:` Defaults to `uploads/{timestamp}-{unique_id}-#{SecureRandom.hex}/${filename}`. It is the key, or filename used on s3. `{timestamp}` and `{unique_id}` are special substitution strings that will be populated by javascript with values for the current upload. `${filename}` is a special s3 string that will be populated with the original uploaded file name. Needs to be at least `"${filename}"`. It is highly recommended to use both `{unique_id}`, which will prevent collisions when uploading files with the same name (such as from a mobile device, where every photo is named image.jpg), and a server-generated random value such as `#{SecureRandom.hex}`, which adds further collision protection with other uploaders.
86
+ * `key_starts_with:` Defaults to `uploads/`. Constraint on the key on s3. if you change the `key` option, make sure this starts with what you put there. If you set this as a blank string the upload path to s3 can be anything - not recommended!
87
+ * `acl:` Defaults to `public-read`. The AWS acl for files uploaded to s3.
88
+ * `max_file_size:` Defaults to `500.megabytes`. Maximum file size allowed.
89
+ * `id:` Optional html id for the form, its recommended that you give the form an id so you can reference with the jQuery plugin.
90
+ * `class:` Optional html class for the form.
91
+ * `data:` Optional html data attribute hash.
92
+ * `bucket:` Optional (defaults to bucket used in config).
93
+
94
+ ### Example with all options
95
+ ```ruby
96
+ <%= s3_uploader_form callback_url: model_url,
97
+ callback_method: "POST",
98
+ callback_param: "model[image_url]",
99
+ key: "files/{timestamp}-{unique_id}-#{SecureRandom.hex}/${filename}",
100
+ key_starts_with: "files/",
101
+ acl: "public-read",
102
+ max_file_size: 50.megabytes,
103
+ id: "s3-uploader",
104
+ class: "upload-form",
105
+ data: {:key => :val} do %>
106
+ <%= file_field_tag :file, multiple: true %>
107
+ <% end %>
108
+ ```
109
+
110
+ ### Example to persist the S3 url in your rails app
111
+ It is recommended that you persist the url that is sent via the POST request (to the url given to the `callback_url` option and as the key given in the `callback_param` option).
112
+
113
+ One way to do this is to make sure you have `resources model` in your routes file, and add a `s3_url` (or something similar) attribute to your model. Then make sure you have the create action in your controller for that model that saves the url from the callback_param.
95
114
 
96
115
  You could then have your create action render a javascript file like this:
97
116
  **create.js.erb**
@@ -130,14 +149,14 @@ Use the javascript in `s3_direct_upload` as a guide.
130
149
  * `progress_bar_target:` The jQuery selector for the element where you want the progress bars to be appended to. Default is the form element.
131
150
  * `click_submit_target:` The jQuery selector for the element you wish to add a click handler to do the submitting instead of submiting on file open.
132
151
 
133
- ### Example with all options.
152
+ ### Example with all options
134
153
  ```coffeescript
135
154
  jQuery ->
136
155
  $("#myS3Uploader").S3Uploader
137
156
  path: 'path/to/my/files/on/s3'
138
157
  additional_data: {key: 'value'}
139
158
  remove_completed_progress_bar: false
140
- before_add: myCallBackFunction() # must return true or false if set
159
+ before_add: myCallBackFunction # must return true or false if set
141
160
  progress_bar_target: $('.js-progress-bars')
142
161
  click_submit_target: $('.submit-target')
143
162
  ```
@@ -230,7 +249,7 @@ rake task.
230
249
 
231
250
  First, add the fog gem to your `Gemfile` and run `bundle`:
232
251
  ```ruby
233
- require 'fog'
252
+ gem 'fog'
234
253
  ```
235
254
 
236
255
  Then, run the rake task to delete uploads older than 2 days:
@@ -254,6 +273,15 @@ Alternately, if you'd prefer for S3 to delete your old uploads automatically, yo
254
273
  so by setting your bucket's
255
274
  [Lifecycle Configuration](http://docs.aws.amazon.com/AmazonS3/latest/UG/LifecycleConfiguration.html).
256
275
 
276
+ ## A note on IE support
277
+ IE file uploads are working but with a couple caveats.
278
+
279
+ * The before_add callback doesn't work.
280
+ * The progress bar doesn't work on IE.
281
+
282
+ But IE should still upload your files fine.
283
+
284
+
257
285
  ## Contributing / TODO
258
286
  This is just a simple gem that only really provides some javascript and a form helper.
259
287
  This gem could go all sorts of ways based on what people want and how people contribute.
@@ -101,10 +101,17 @@ $.fn.S3Uploader = (options) ->
101
101
  name: "content-type"
102
102
  value: fileType
103
103
 
104
- key = $uploadForm.data("key").replace('{timestamp}', new Date().getTime()).replace('{unique_id}', @files[0].unique_id)
104
+ key = $uploadForm.data("key")
105
+ .replace('{timestamp}', new Date().getTime())
106
+ .replace('{unique_id}', @files[0].unique_id)
107
+ .replace('{extension}', @files[0].name.split('.').pop())
105
108
 
106
109
  # substitute upload timestamp and unique_id into key
107
- data[1].value = settings.path + key
110
+ key_field = $.grep data, (n) ->
111
+ n if n.name == "key"
112
+
113
+ if key_field.length > 0
114
+ key_field[0].value = settings.path + key
108
115
 
109
116
  # IE <= 9 doesn't have XHR2 hence it can't use formData
110
117
  # replace 'key' field to submit form
@@ -115,18 +122,19 @@ $.fn.S3Uploader = (options) ->
115
122
  build_content_object = ($uploadForm, file, result) ->
116
123
  content = {}
117
124
  if result # Use the S3 response to set the URL to avoid character encodings bugs
118
- content.url = $(result).find("Location").text()
119
- content.filepath = $('<a />').attr('href', content.url)[0].pathname
120
- else # IE <= 9 return a null result object so we use the file object instead
121
- domain = $uploadForm.attr('action')
122
- content.filepath = $uploadForm.find('input[name=key]').val().replace('/${filename}', '')
123
- content.url = domain + content.filepath + '/' + encodeURIComponent(file.name)
124
-
125
- content.filename = file.name
126
- content.filesize = file.size if 'size' of file
127
- content.filetype = file.type if 'type' of file
128
- content.unique_id = file.unique_id if 'unique_id' of file
129
- content.relativePath = build_relativePath(file) if has_relativePath(file)
125
+ content.url = $(result).find("Location").text()
126
+ content.filepath = $('<a />').attr('href', content.url)[0].pathname
127
+ else # IE <= 9 retu rn a null result object so we use the file object instead
128
+ domain = $uploadForm.attr('action')
129
+ content.filepath = $uploadForm.find('input[name=key]').val().replace('/${filename}', '')
130
+ content.url = domain + content.filepath + '/' + encodeURIComponent(file.name)
131
+
132
+ content.filename = file.name
133
+ content.filesize = file.size if 'size' of file
134
+ content.lastModifiedDate = file.lastModifiedDate if 'lastModifiedDate' of file
135
+ content.filetype = file.type if 'type' of file
136
+ content.unique_id = file.unique_id if 'unique_id' of file
137
+ content.relativePath = build_relativePath(file) if has_relativePath(file)
130
138
  content = $.extend content, settings.additional_data if settings.additional_data
131
139
  content
132
140
 
@@ -15,7 +15,7 @@ module S3DirectUpload
15
15
  @options = options.reverse_merge(
16
16
  aws_access_key_id: S3DirectUpload.config.access_key_id,
17
17
  aws_secret_access_key: S3DirectUpload.config.secret_access_key,
18
- bucket: S3DirectUpload.config.bucket,
18
+ bucket: options[:bucket] || S3DirectUpload.config.bucket,
19
19
  region: S3DirectUpload.config.region || "s3",
20
20
  url: S3DirectUpload.config.url,
21
21
  ssl: true,
@@ -76,7 +76,7 @@ module S3DirectUpload
76
76
  ["starts-with", "$key", @options[:key_starts_with]],
77
77
  ["starts-with", "$x-requested-with", ""],
78
78
  ["content-length-range", 0, @options[:max_file_size]],
79
- ["starts-with","$content-type",""],
79
+ ["starts-with","$content-type", @options[:content_type_starts_with] ||""],
80
80
  {bucket: @options[:bucket]},
81
81
  {acl: @options[:acl]},
82
82
  {success_action_status: "201"}
@@ -87,7 +87,7 @@ module S3DirectUpload
87
87
  def signature
88
88
  Base64.encode64(
89
89
  OpenSSL::HMAC.digest(
90
- OpenSSL::Digest::Digest.new('sha1'),
90
+ OpenSSL::Digest.new('sha1'),
91
91
  @options[:aws_secret_access_key], policy
92
92
  )
93
93
  ).gsub("\n", "")
@@ -1,3 +1,3 @@
1
1
  module S3DirectUpload
2
- VERSION = "0.1.6"
2
+ VERSION = "0.1.7"
3
3
  end
metadata CHANGED
@@ -1,69 +1,69 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: s3_direct_upload
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Wayne Hoover
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-06-07 00:00:00.000000000 Z
11
+ date: 2014-03-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '3.2'
19
+ version: '3.1'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '3.2'
26
+ version: '3.1'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: coffee-rails
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: 3.2.1
33
+ version: '3.1'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: 3.2.1
40
+ version: '3.1'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: sass-rails
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - '>='
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: 3.2.5
47
+ version: '3.1'
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - '>='
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
- version: 3.2.5
54
+ version: '3.1'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: jquery-fileupload-rails
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - ~>
59
+ - - "~>"
60
60
  - !ruby/object:Gem::Version
61
61
  version: 0.4.1
62
62
  type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - ~>
66
+ - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: 0.4.1
69
69
  description: Direct Upload to Amazon S3 With CORS and jquery-file-upload
@@ -73,17 +73,17 @@ executables: []
73
73
  extensions: []
74
74
  extra_rdoc_files: []
75
75
  files:
76
+ - LICENSE
77
+ - README.md
78
+ - app/assets/javascripts/s3_direct_upload.js.coffee
79
+ - app/assets/stylesheets/s3_direct_upload_progress_bars.css.scss
80
+ - lib/s3_direct_upload.rb
76
81
  - lib/s3_direct_upload/config_aws.rb
77
82
  - lib/s3_direct_upload/engine.rb
78
83
  - lib/s3_direct_upload/form_helper.rb
79
84
  - lib/s3_direct_upload/railtie.rb
80
85
  - lib/s3_direct_upload/version.rb
81
- - lib/s3_direct_upload.rb
82
86
  - lib/tasks/s3_direct_upload.rake
83
- - app/assets/javascripts/s3_direct_upload.js.coffee
84
- - app/assets/stylesheets/s3_direct_upload_progress_bars.css.scss
85
- - LICENSE
86
- - README.md
87
87
  homepage: ''
88
88
  licenses: []
89
89
  metadata: {}
@@ -93,17 +93,17 @@ require_paths:
93
93
  - lib
94
94
  required_ruby_version: !ruby/object:Gem::Requirement
95
95
  requirements:
96
- - - '>='
96
+ - - ">="
97
97
  - !ruby/object:Gem::Version
98
98
  version: '0'
99
99
  required_rubygems_version: !ruby/object:Gem::Requirement
100
100
  requirements:
101
- - - '>='
101
+ - - ">="
102
102
  - !ruby/object:Gem::Version
103
103
  version: '0'
104
104
  requirements: []
105
105
  rubyforge_project:
106
- rubygems_version: 2.0.2
106
+ rubygems_version: 2.2.2
107
107
  signing_key:
108
108
  specification_version: 4
109
109
  summary: Gives a form helper for Rails which allows direct uploads to s3. Based on