s3_direct_upload 0.0.8 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -66,7 +66,7 @@ jQuery ->
66
66
  Optionally, you can also place this template in the same view for the progress bars:
67
67
  ```js+erb
68
68
  <script id="template-upload" type="text/x-tmpl">
69
- <div class="upload">
69
+ <div id="file-{%=o.unique_id%}" class="upload">
70
70
  {%=o.name%}
71
71
  <div class="progress"><div class="bar" style="width: 0%"></div></div>
72
72
  </div>
@@ -77,6 +77,7 @@ Optionally, you can also place this template in the same view for the progress b
77
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
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
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.
80
81
  * `acl:` acl for files uploaded to s3, defaults to "public-read"
81
82
  * `max_file_size:` maximum file size, defaults to 500.megabytes
82
83
  * `id:` html id for the form, its recommended that you give the form an id so you can reference with the jQuery plugin.
@@ -116,12 +117,13 @@ Use the javascript in `s3_direct_upload` as a guide.
116
117
  ## Options for S3Upload jQuery Plugin
117
118
 
118
119
  * `path:` manual path for the files on your s3 bucket. Example: `path/to/my/files/on/s3`
119
- Note: the file path in your s3 bucket will effectively be `path + key`.
120
- * `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
+ Note: Your path MUST start with the option you put in your form builder for `key_starts_with`, or else you will get S3 permission errors. The file path in your s3 bucket will be `path + key`.
121
+ * `additional_data:` You can send additional data to your rails app in the persistence POST request. This would be accessible in your params hash as `params[:key][:value]`
121
122
  Example: `{key: value}`
122
123
  * `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.
123
124
  * `remove_failed_progress_bar:` By default, the progress bar will not be removed when uploads fail. You can set this to `true` if you want to remove the progress bar.
124
125
  * `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.
126
+ * `progress_bar_target:` The jQuery selector for the element where you want the progress bars to be appended to. Default is the form element.
125
127
 
126
128
  ### Example with all options.
127
129
  ```coffeescript
@@ -131,6 +133,7 @@ jQuery ->
131
133
  additional_data: {key: 'value'}
132
134
  remove_completed_progress_bar: false
133
135
  before_add: myCallBackFunction() # must return true or false if set
136
+ progress_bar_target: $('.js-progress-bars')
134
137
  ```
135
138
 
136
139
  ### Public methods
@@ -140,7 +143,7 @@ You can change the settings on your form later on by accessing the jQuery instan
140
143
  jQuery ->
141
144
  v = $("#myS3Uploader").S3Uploader()
142
145
  ...
143
- v.path("new/path/")
146
+ v.path("new/path/") #only works when the key_starts_with option is blank. Not recommended.
144
147
  v.additional_data("newdata")
145
148
  ```
146
149
 
@@ -154,7 +157,7 @@ $('#myS3Uploader').bind 's3_uploads_start', (e) ->
154
157
  ```
155
158
 
156
159
  #### Successfull upload
157
- 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 :
160
+ When a file has been successfully uploaded to S3, the `s3_upload_complete` is triggered on the form. A `content` object is passed along with the following attributes :
158
161
 
159
162
  * `url` The full URL to the uploaded file on S3.
160
163
  * `filename` The original name of the uploaded file.
@@ -20,6 +20,7 @@ $.fn.S3Uploader = (options) ->
20
20
  before_add: null
21
21
  remove_completed_progress_bar: true
22
22
  remove_failed_progress_bar: false
23
+ progress_bar_target: null
23
24
 
24
25
  $.extend settings, options
25
26
 
@@ -31,9 +32,11 @@ $.fn.S3Uploader = (options) ->
31
32
  add: (e, data) ->
32
33
  current_files.push data
33
34
  file = data.files[0]
35
+ file.unique_id = Math.random().toString(36).substr(2,16)
36
+
34
37
  unless settings.before_add and not settings.before_add(file)
35
38
  data.context = $(tmpl("template-upload", file)) if $('#template-upload').length > 0
36
- $uploadForm.append(data.context)
39
+ $(data.context).appendTo(settings.progress_bar_target || $uploadForm)
37
40
  data.submit()
38
41
 
39
42
  start: (e) ->
@@ -100,6 +103,7 @@ $.fn.S3Uploader = (options) ->
100
103
  content.filename = file.name
101
104
  content.filesize = file.size if 'size' of file
102
105
  content.filetype = file.type if 'type' of file
106
+ content.unique_id = file.unique_id if 'unique_id' of file
103
107
  content = $.extend content, settings.additional_data if settings.additional_data
104
108
  content
105
109
 
@@ -20,6 +20,7 @@ module S3DirectUpload
20
20
  expiration: 10.hours.from_now.utc.iso8601,
21
21
  max_file_size: 500.megabytes,
22
22
  as: "file",
23
+ key_starts_with: "uploads/",
23
24
  key: key
24
25
  )
25
26
  end
@@ -67,7 +68,7 @@ module S3DirectUpload
67
68
  expiration: @options[:expiration],
68
69
  conditions: [
69
70
  ["starts-with", "$utf8", ""],
70
- ["starts-with", "$key", ""],
71
+ ["starts-with", "$key", @options[:key_starts_with]],
71
72
  ["starts-with", "$x-requested-with", ""],
72
73
  ["content-length-range", 0, @options[:max_file_size]],
73
74
  ["starts-with","$Content-Type",""],
@@ -1,3 +1,3 @@
1
1
  module S3DirectUpload
2
- VERSION = "0.0.8"
2
+ VERSION = "0.1.0"
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.8
4
+ version: 0.1.0
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: 2013-01-16 00:00:00.000000000 Z
12
+ date: 2013-02-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails