s3_direct_upload 0.1.1 → 0.1.2

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: d19e2b36016e3829e3f284c860489d83aaacee7e
4
- data.tar.gz: b30b925f450271bc8e6980df7c780056a26f40fc
3
+ metadata.gz: e1ed6e265b4f74392b3fc0ab3c58b22a19041e61
4
+ data.tar.gz: 60545e543bac0c5feadf0abd5e5ce268b276e214
5
5
  SHA512:
6
- metadata.gz: ed39347ebba94448f41ca01a13fc92bcd4c2bfd4bc572b33ffd5e1838a83cda3bba8cc5a819f4727e298db9fcaa298404c80ca0f2e7492403519097b47e08ee0
7
- data.tar.gz: c4c4a4c93ed56e2a97725c453f67c40465f3962dcf86b75f36d4c9990b5f4729a03ecdde0faa0c6fe311164dcf7ba3c40aaad111be338ae89314dda06fe2a34c
6
+ metadata.gz: 4550009b9508b104ec290b3c70a7a6f1263b183c262e1e2e519beb7c8d4ee1b69c747c67c51e0d3e8e6e0619e8de98b3daf666ebe1a536b824934800a41a6c59
7
+ data.tar.gz: 60c03de6349abdcceadc9e7a03212d26fad2a5c31b50e8d79d78800522e628607a6d3ef343e7801b0739f1ce46eafd9503479e4a14aba6e503a37f967d6da01a
data/README.md CHANGED
@@ -52,7 +52,7 @@ Add the following js and css to your asset pipeline:
52
52
  ## Usage
53
53
  Create a new view that uses the form helper `s3_uploader_form`:
54
54
  ```ruby
55
- <%= s3_uploader_form post: model_url, as: "model[image_url]", id: "myS3Uploader" do %>
55
+ <%= s3_uploader_form callback_url: model_url, callback_param: "model[image_url]", id: "myS3Uploader" do %>
56
56
  <%= file_field_tag :file, multiple: true %>
57
57
  <% end %>
58
58
  ```
@@ -74,10 +74,11 @@ Optionally, you can also place this template in the same view for the progress b
74
74
  ```
75
75
 
76
76
  ## Options for form helper
77
- * `post:` url in which 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.
78
- * `as:` 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`
79
- * `key:` key on s3. defaults to `"uploads/#{SecureRandom.hex}/${filename}"`. needs to be at least `"${filename}"`.
80
- * `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, so be careful.
77
+ * `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.
78
+ * `callback_method:` Defaults to POST. Use PUT and remove the multiple option from your file field to update a model.
79
+ * `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`
80
+ * `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.
81
+ * `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!
81
82
  * `acl:` acl for files uploaded to s3, defaults to "public-read"
82
83
  * `max_file_size:` maximum file size, defaults to 500.megabytes
83
84
  * `id:` html id for the form, its recommended that you give the form an id so you can reference with the jQuery plugin.
@@ -223,6 +224,10 @@ S3DirectUpload.config do |c|
223
224
  end
224
225
  ```
225
226
 
227
+ Alternately, if you'd prefer for S3 to delete your old uploads automatically, you can do
228
+ so by setting your bucket's
229
+ [Lifecycle Configuration](http://docs.aws.amazon.com/AmazonS3/latest/UG/LifecycleConfiguration.html).
230
+
226
231
  ## Contributing / TODO
227
232
  This is just a simple gem that only really provides some javascript and a form helper.
228
233
  This gem could go all sorts of ways based on what people want and how people contribute.
@@ -28,25 +28,25 @@ $.fn.S3Uploader = (options) ->
28
28
  current_files = []
29
29
  forms_for_submit = []
30
30
  if settings.click_submit_target
31
- settings.click_submit_target.click ->
31
+ settings.click_submit_target.click ->
32
32
  form.submit() for form in forms_for_submit
33
- false
33
+ false
34
34
 
35
35
  setUploadForm = ->
36
36
  $uploadForm.fileupload
37
37
 
38
38
  add: (e, data) ->
39
- current_files.push data
40
39
  file = data.files[0]
41
40
  file.unique_id = Math.random().toString(36).substr(2,16)
42
41
 
43
42
  unless settings.before_add and not settings.before_add(file)
44
- data.context = $(tmpl("template-upload", file).trim()) if $('#template-upload').length > 0
43
+ current_files.push data
44
+ data.context = $($.trim(tmpl("template-upload", file))) if $('#template-upload').length > 0
45
45
  $(data.context).appendTo(settings.progress_bar_target || $uploadForm)
46
46
  if settings.click_submit_target
47
47
  forms_for_submit.push data
48
48
  else
49
- data.submit()
49
+ data.submit()
50
50
 
51
51
  start: (e) ->
52
52
  $uploadForm.trigger("s3_uploads_start", [e])
@@ -59,12 +59,12 @@ $.fn.S3Uploader = (options) ->
59
59
  done: (e, data) ->
60
60
  content = build_content_object $uploadForm, data.files[0], data.result
61
61
 
62
- to = $uploadForm.data('post')
62
+ to = $uploadForm.data('callback-url')
63
63
  if to
64
- content[$uploadForm.data('as')] = content.url
64
+ content[$uploadForm.data('callback-param')] = content.url
65
65
 
66
66
  $.ajax
67
- type: 'POST'
67
+ type: $uploadForm.data('callback-method')
68
68
  url: to
69
69
  data: content
70
70
  beforeSend: ( xhr, settings ) -> $uploadForm.trigger( 'ajax:beforeSend', [xhr, settings] )
@@ -96,7 +96,9 @@ $.fn.S3Uploader = (options) ->
96
96
  name: "Content-Type"
97
97
  value: fileType
98
98
 
99
- data[1].value = settings.path + data[1].value #the key
99
+ # substitute upload timestamp and unique_id into key
100
+ key = data[1].value.replace('{timestamp}', new Date().getTime()).replace('{unique_id}', @files[0].unique_id)
101
+ data[1].value = settings.path + key
100
102
  data
101
103
 
102
104
  build_content_object = ($uploadForm, file, result) ->
@@ -11,6 +11,7 @@ module S3DirectUpload
11
11
 
12
12
  class S3Uploader
13
13
  def initialize(options)
14
+ @key_starts_with = options[:key_starts_with] || "uploads/"
14
15
  @options = options.reverse_merge(
15
16
  aws_access_key_id: S3DirectUpload.config.access_key_id,
16
17
  aws_secret_access_key: S3DirectUpload.config.secret_access_key,
@@ -20,8 +21,9 @@ module S3DirectUpload
20
21
  acl: "public-read",
21
22
  expiration: 10.hours.from_now.utc.iso8601,
22
23
  max_file_size: 500.megabytes,
23
- as: "file",
24
- key_starts_with: "uploads/",
24
+ callback_method: "POST",
25
+ callback_param: "file",
26
+ key_starts_with: @key_starts_with,
25
27
  key: key
26
28
  )
27
29
  end
@@ -34,8 +36,9 @@ module S3DirectUpload
34
36
  authenticity_token: false,
35
37
  multipart: true,
36
38
  data: {
37
- post: @options[:post],
38
- as: @options[:as]
39
+ callback_url: @options[:callback_url],
40
+ callback_method: @options[:callback_method],
41
+ callback_param: @options[:callback_param]
39
42
  }.reverse_merge(@options[:data] || {})
40
43
  }
41
44
  end
@@ -53,7 +56,7 @@ module S3DirectUpload
53
56
  end
54
57
 
55
58
  def key
56
- @key ||= "uploads/#{DateTime.now.utc.strftime("%Y%m%dT%H%MZ")}_#{SecureRandom.hex}/${filename}"
59
+ @key ||= "#{@key_starts_with}{timestamp}-{unique_id}-#{SecureRandom.hex}/${filename}"
57
60
  end
58
61
 
59
62
  def url
@@ -1,3 +1,3 @@
1
1
  module S3DirectUpload
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: s3_direct_upload
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
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-02-27 00:00:00.000000000 Z
11
+ date: 2013-04-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails