s3_direct_upload 0.0.3 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -22,7 +22,7 @@ end
22
22
  ```
23
23
 
24
24
  Make sure your AWS S3 CORS settings for your bucket look something like this:
25
- ```
25
+ ```xml
26
26
  <CORSConfiguration>
27
27
  <CORSRule>
28
28
  <AllowedOrigin>http://0.0.0.0:3000</AllowedOrigin>
@@ -62,7 +62,7 @@ jQuery ->
62
62
  $("#myS3Uploader").S3Uploader()
63
63
  ```
64
64
 
65
- Also place this template in the same view for the progress bars:
65
+ Optionally, you can also place this template in the same view for the progress bars:
66
66
  ```js+erb
67
67
  <script id="template-upload" type="text/x-tmpl">
68
68
  <div class="upload">
@@ -73,20 +73,14 @@ Also place this template in the same view for the progress bars:
73
73
  ```
74
74
 
75
75
  ## Options for form helper
76
- `post:` -> url in which is POST'd to after file is uploaded to S3. Example: model_url
77
-
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
-
80
- `key:` -> key on s3. defaults to `"uploads/#{SecureRandom.hex}/${filename}"`. needs to be at least `"${filename}"`.
81
-
82
- `acl:` -> acl for files uploaded to s3, defaults to "public-read"
83
-
84
- `max_file_size:` -> maximum file size, defaults to 500.megabytes
85
-
86
- `id:` -> html id for the form, its recommended that you give the form an id so you can reference with the jQuery plugin.
87
-
88
- `class:` -> optional html class for the form.
89
-
76
+ * `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.
77
+ * `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`
78
+ * `key:` key on s3. defaults to `"uploads/#{SecureRandom.hex}/${filename}"`. needs to be at least `"${filename}"`.
79
+ * `acl:` acl for files uploaded to s3, defaults to "public-read"
80
+ * `max_file_size:` maximum file size, defaults to 500.megabytes
81
+ * `id:` html id for the form, its recommended that you give the form an id so you can reference with the jQuery plugin.
82
+ * `class:` optional html class for the form.
83
+ * `data:` Optional html data
90
84
 
91
85
  ### Persisting the S3 url
92
86
  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.
@@ -120,50 +114,66 @@ Use the javascript in `s3_direct_upload` as a guide.
120
114
 
121
115
  ## Options for S3Upload jQuery Plugin
122
116
 
123
- `path` -> manual path for the files on your s3 bucket. Example: `path/to/my/files/on/s3`
124
-
125
- Note: the file path in your s3 bucket will effectively be `path + key`.
126
-
127
- `additional_data` -> You can send additional data to your rails app in the persistence POST request. Example: `{key: value}`
128
-
129
- This would be accessable in your params hash as `params[:key][:value]`
117
+ * `path:` manual path for the files on your s3 bucket. Example: `path/to/my/files/on/s3`
118
+ Note: the file path in your s3 bucket will effectively be `path + key`.
119
+ * `additional_data:` You can send additional data to your rails app in the persistence POST request. This would be accessable in your params hash as `params[:key][:value]`
120
+ Example: `{key: value}`
121
+ * `remove_completed_progress_bar:` By default, the progress bar will be removed once the file has been successfully uploaded. You can set this to `false` if you want to keep the progress bar.
122
+ * `before_add:` Callback function that executes before a file is added to the queue. It is passed file object and expects `true` or `false` to be returned. This could be useful if you would like to validate the filenames of files to be uploaded for example. If true is returned file will be uploaded as normal, false will cancel the upload.
130
123
 
131
- `before_add` -> Callback function that executes before a file is added to the que. It is passed file object and expects `true` or `false` to be returned.
132
-
133
- This could be useful if you would like to validate the filenames of files to be uploaded for example. If true is returned file will be uploaded as normal, false will cancel the upload.
124
+ ### Example with all options.
125
+ ```coffeescript
126
+ jQuery ->
127
+ $("#myS3Uploader").S3Uploader
128
+ path: 'path/to/my/files/on/s3'
129
+ additional_data: {key: 'value'}
130
+ remove_completed_progress_bar: false
131
+ before_add: myCallBackFunction() # must return true or false if set
132
+ ```
134
133
 
135
134
  ### Public methods
136
135
  You can change the settings on your form later on by accessing the jQuery instance:
137
136
 
138
- ```cofeescript
137
+ ```coffeescript
139
138
  jQuery ->
140
139
  v = $("#myS3Uploader").S3Uploader()
141
140
  ...
142
141
  v.path("new/path/")
143
142
  v.additional_data("newdata")
143
+ ```
144
144
 
145
- ### Global Event Hooks
145
+ ### Javascript Events Hooks
146
146
 
147
- When all uploads finish in a batch an `s3_uploads_complete` event will be triggered on `document`, so you could do something like:
148
- ```javascript
149
- $(document).bind('s3_uploads_complete', function(){
150
- ...
151
- alert("All Uploads completed")
152
- });
153
- ````
154
- ### Example with all options.
155
- ```cofeescript
156
- jQuery ->
157
- $("#myS3Uploader").S3Uploader
158
- path: 'path/to/my/files/on/s3'
159
- additional_data: {key: 'value'}
160
- before_add: myCallBackFunction() # must return true or false if set
147
+ #### Successfull upload
148
+ When a file has been successfully to S3, the `s3_upload_complete` is triggered on the form. A `content` object is passed along with the following attributes :
149
+
150
+ * `url` The full URL to the uploaded file on S3.
151
+ * `filename` The original name of the uploaded file.
152
+ * `filepath` The path to the file (without the filename or domain)
153
+ * `filesize` The size of the uploaded file.
154
+ * `filetype` The type of the uploaded file.
155
+
156
+ This hook could be used for example to fill a form hidden field with the returned S3 url :
157
+ ```coffeescript
158
+ $('#myS3Uploader').bind "s3_upload_complete", (e, content) ->
159
+ $('#someHiddenField').val(content.url)
161
160
  ```
162
161
 
162
+ #### Failed upload
163
+ When an error occured during the transferm the `s3_upload_failed` is triggered on the form with the same `content` object is passed for the successful upload with the addition of the `error_thrown` attribute. The most basic way to handle this error would be to display an alert message to the user in case the upload fails :
164
+ ```coffeescript
165
+ $('#myS3Uploader').bind "s3_upload_failed", (e, content) ->
166
+ alert("#{content.filename} failed to upload : #{content.error_thrown}")
167
+ ```
163
168
 
169
+ #### All uploads completed
170
+ When all uploads finish in a batch an `s3_uploads_complete` event will be triggered on `document`, so you could do something like:
171
+ ```coffeescript
172
+ $(document).bind 's3_uploads_complete', ->
173
+ alert("All Uploads completed")
174
+ ```
164
175
 
165
176
  ## Contributing / TODO
166
-
167
177
  This is just a simple gem that only really provides some javascript and a form helper.
168
178
  This gem could go all sorts of ways based on what people want and how people contribute.
169
179
  Ideas:
@@ -176,7 +186,6 @@ Ideas:
176
186
 
177
187
 
178
188
  ## Credit
179
-
180
189
  This gem is basically a small wrapper around code that [Ryan Bates](http://github.com/rbates) wrote for [Railscast#383](http://railscasts.com/episodes/383-uploading-to-amazon-s3). Most of the code in this gem was extracted from [gallery-jquery-fileupload](https://github.com/railscasts/383-uploading-to-amazon-s3/tree/master/gallery-jquery-fileupload).
181
190
 
182
191
  Thank you Ryan Bates!
@@ -18,6 +18,7 @@ $.fn.S3Uploader = (options) ->
18
18
  path: ''
19
19
  additional_data: null
20
20
  before_add: null
21
+ remove_completed_progress_bar: true
21
22
 
22
23
  $.extend settings, options
23
24
 
@@ -30,7 +31,7 @@ $.fn.S3Uploader = (options) ->
30
31
  current_files.push data
31
32
  file = data.files[0]
32
33
  unless settings.before_add and not settings.before_add(file)
33
- data.context = $(tmpl("template-upload", file))
34
+ data.context = $(tmpl("template-upload", file)) if $('#template-upload').length > 0
34
35
  $uploadForm.append(data.context)
35
36
  data.submit()
36
37
 
@@ -40,32 +41,24 @@ $.fn.S3Uploader = (options) ->
40
41
  data.context.find('.bar').css('width', progress + '%')
41
42
 
42
43
  done: (e, data) ->
43
- file = data.files[0]
44
- domain = $uploadForm.attr('action')
45
- path = settings.path + $uploadForm.find('input[name=key]').val().replace('/${filename}', '')
44
+ content = build_content_object $uploadForm, data.files[0], data.result
45
+
46
46
  to = $uploadForm.data('post')
47
- content = {}
48
- content[$uploadForm.data('as')] = domain + path + '/' + file.name
49
- content.filename = file.name
50
- content.filepath = path
51
- if settings.additional_data
52
- content = $.extend content, settings.additional_data
53
- if 'size' of file
54
- content.filesize = file.size
55
- if 'type' of file
56
- content.filetype = file.type
57
-
58
- $.post(to, content)
59
- data.context.remove() if data.context # remove progress bar
47
+ if to
48
+ content[$uploadForm.data('as')] = content.url
49
+ $.post(to, content)
50
+
51
+ data.context.remove() if data.context && settings.remove_completed_progress_bar # remove progress bar
52
+ $uploadForm.trigger("s3_upload_complete", [content])
60
53
 
61
54
  current_files.splice($.inArray(data, current_files), 1) # remove that element from the array
62
55
  if current_files.length == 0
63
56
  $(document).trigger("s3_uploads_complete")
64
57
 
65
58
  fail: (e, data) ->
66
- alert("#{data.files[0].name} failed to upload.")
67
- console.log("Upload failed:")
68
- console.log(data)
59
+ content = build_content_object $uploadForm, data.files[0], data.result
60
+ content.error_thrown = data.errorThrown
61
+ $uploadForm.trigger("s3_upload_failed", [content])
69
62
 
70
63
  formData: (form) ->
71
64
  data = form.serializeArray()
@@ -80,6 +73,26 @@ $.fn.S3Uploader = (options) ->
80
73
 
81
74
  data
82
75
 
76
+ build_content_object = ($uploadForm, file, result) ->
77
+ domain = $uploadForm.attr('action')
78
+ content = {}
79
+ if result # Use the S3 response to set the URL to avoid character encodings bugs
80
+ path = $('Key', result).text()
81
+ split_path = path.split('/')
82
+ content.url = domain + path
83
+ content.filename = split_path[split_path.length - 1]
84
+ content.filepath = split_path.slice(0, split_path.length - 1).join('/')
85
+ else # IE8 and IE9 return a null result object so we use the file object instead
86
+ path = settings.path + $uploadForm.find('input[name=key]').val().replace('/${filename}', '')
87
+ content.url = domain + path + '/' + file.name
88
+ content.filename = file.name
89
+ content.filepath = path
90
+
91
+ content.filesize = file.size if 'size' of file
92
+ content.filetype = file.type if 'type' of file
93
+ content = $.extend content, settings.additional_data if settings.additional_data
94
+ content
95
+
83
96
  #public methods
84
97
  @initialize = ->
85
98
  setUploadForm()
@@ -16,7 +16,7 @@ module S3DirectUpload
16
16
  aws_secret_access_key: S3DirectUpload.config.secret_access_key,
17
17
  bucket: S3DirectUpload.config.bucket,
18
18
  acl: "public-read",
19
- expiration: 10.hours.from_now,
19
+ expiration: 10.hours.from_now.utc.iso8601,
20
20
  max_file_size: 500.megabytes,
21
21
  as: "file",
22
22
  key: key
@@ -33,7 +33,7 @@ module S3DirectUpload
33
33
  data: {
34
34
  post: @options[:post],
35
35
  as: @options[:as]
36
- }
36
+ }.reverse_merge(@options[:data] || {})
37
37
  }
38
38
  end
39
39
 
@@ -44,6 +44,7 @@ module S3DirectUpload
44
44
  :policy => policy,
45
45
  :signature => signature,
46
46
  "AWSAccessKeyId" => @options[:aws_access_key_id],
47
+ success_action_status: "201"
47
48
  }
48
49
  end
49
50
 
@@ -68,7 +69,8 @@ module S3DirectUpload
68
69
  ["content-length-range", 0, @options[:max_file_size]],
69
70
  ["starts-with","$Content-Type",""],
70
71
  {bucket: @options[:bucket]},
71
- {acl: @options[:acl]}
72
+ {acl: @options[:acl]},
73
+ {success_action_status: "201"}
72
74
  ]
73
75
  }
74
76
  end
@@ -1,3 +1,3 @@
1
1
  module S3DirectUpload
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.5"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: s3_direct_upload
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-15 00:00:00.000000000 Z
12
+ date: 2012-12-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -66,7 +66,7 @@ dependencies:
66
66
  requirements:
67
67
  - - ~>
68
68
  - !ruby/object:Gem::Version
69
- version: 0.3.4
69
+ version: 0.3.5
70
70
  type: :runtime
71
71
  prerelease: false
72
72
  version_requirements: !ruby/object:Gem::Requirement
@@ -74,7 +74,7 @@ dependencies:
74
74
  requirements:
75
75
  - - ~>
76
76
  - !ruby/object:Gem::Version
77
- version: 0.3.4
77
+ version: 0.3.5
78
78
  description: Direct Upload to Amazon S3 With CORS and jquery-file-upload
79
79
  email:
80
80
  - w@waynehoover.com