s3_file_field 1.0.4 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +30 -23
- data/lib/s3_file_field/form_helper.rb +4 -80
- data/lib/s3_file_field/s3_uploader.rb +93 -0
- data/lib/s3_file_field/version.rb +1 -1
- data/lib/s3_file_field.rb +2 -0
- metadata +31 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0477893ffeba70b2ce4e91f5ef57dfa5be7097f9
|
4
|
+
data.tar.gz: aeee37b0690800a1fa341fdb1435c67f2efd64db
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4cc2f163a26a087323d7732e8d0d6c74967f41a6ca18bb36f340fb73b5243de23e00cdd5c908161a21e0b23472ff4a22289d85b40a26554916f8a7c3c4614719
|
7
|
+
data.tar.gz: 3f4adacd07301e307c67c2779ca9547ba8cad406174bf435a7609add0deee99baa01939b1a62f6801d5bd8872f6cdc5d6fbd8eed486173c55a80ef6162e78d3d
|
data/README.md
CHANGED
@@ -8,13 +8,15 @@ Note: Since 1.0.2 gem works with both Rails 3 and Rails 4.
|
|
8
8
|
|
9
9
|
## Installation
|
10
10
|
|
11
|
-
|
12
|
-
|
11
|
+
**Gemfile**
|
13
12
|
```ruby
|
14
13
|
gem 's3_file_field'
|
15
14
|
```
|
16
15
|
|
17
|
-
|
16
|
+
**application.coffee**
|
17
|
+
```coffeescript
|
18
|
+
#= require s3_file_field
|
19
|
+
```
|
18
20
|
|
19
21
|
**config/initializers/s3_file_field.rb**
|
20
22
|
```ruby
|
@@ -22,9 +24,16 @@ S3FileField.config do |c|
|
|
22
24
|
c.access_key_id = ENV['AWS_ACCESS_KEY_ID']
|
23
25
|
c.secret_access_key = ENV['AWS_SECRET_ACCESS_KEY']
|
24
26
|
c.bucket = ENV['AWS_BUCKET']
|
27
|
+
# c.acl = "public-read"
|
28
|
+
# c.expiration = 10.hours.from_now.utc.iso8601
|
29
|
+
# c.max_file_size = 500.megabytes
|
30
|
+
# c.conditions = []
|
31
|
+
# c.key_starts_with = 'uploads/
|
25
32
|
end
|
26
33
|
```
|
27
34
|
|
35
|
+
### S3 configuration
|
36
|
+
|
28
37
|
Make sure your AWS S3 CORS Settings for your bucket look like this:
|
29
38
|
```xml
|
30
39
|
<CORSConfiguration>
|
@@ -63,31 +72,31 @@ Also ensure you've added `PutObject` and `PutObjectAcl` permission in your bucke
|
|
63
72
|
}
|
64
73
|
```
|
65
74
|
|
66
|
-
|
75
|
+
## Basic usage
|
67
76
|
|
68
|
-
**application.js.coffee**
|
69
|
-
```coffeescript
|
70
|
-
#= require s3_file_field
|
71
|
-
```
|
72
|
-
|
73
|
-
## Usage
|
74
|
-
|
75
|
-
Create a new view that uses the form helper `s3_uploader_form`:
|
76
77
|
```haml
|
77
|
-
= form_for :user do |
|
78
|
-
=
|
78
|
+
= form_for :user do |f|
|
79
|
+
= f.s3_file_field :avatar, :class => 'js-s3_file_field'
|
79
80
|
.progress
|
80
81
|
.meter{ :style => "width: 0%" }
|
81
82
|
```
|
82
83
|
|
83
|
-
Then in your application.js.coffee something like:
|
84
|
-
|
85
84
|
```coffeescript
|
86
85
|
jQuery.ready ->
|
87
|
-
$('.js-s3_file_field').S3FileField
|
86
|
+
$('.js-s3_file_field').S3FileField
|
87
|
+
done: (e, data) -> console.log(data.result.url)
|
88
88
|
```
|
89
89
|
|
90
|
-
|
90
|
+
# Advanced usage
|
91
|
+
|
92
|
+
```haml
|
93
|
+
= form_for :user do |f|
|
94
|
+
= f.s3_file_field :avatar,
|
95
|
+
:class => 'js-s3_file_field',
|
96
|
+
:key => "/uploads/${timestamp}/${filename}"
|
97
|
+
.progress
|
98
|
+
.meter{ :style => "width: 0%" }
|
99
|
+
```
|
91
100
|
|
92
101
|
```coffeescript
|
93
102
|
ready = ->
|
@@ -113,20 +122,18 @@ $(document).ready(ready)
|
|
113
122
|
$(document).on('page:load', ready)
|
114
123
|
```
|
115
124
|
|
116
|
-
Notice that after upload you have to re-fetch file field from DOM by its ID. That's becase
|
117
|
-
jQuery File Upload clones it somewhere else and `$(this)` reference points to the original input.
|
118
|
-
|
119
|
-
|
120
125
|
## Advanced customization
|
121
126
|
|
122
127
|
You can use any options / API available for jQuery File Upload plugin.
|
123
128
|
|
129
|
+
For full list of options reference [jQuery File Field wiki page](https://github.com/blueimp/jQuery-File-Upload/wiki/Options)
|
130
|
+
|
124
131
|
After successful upload, you'll find file data in `data.result` field:
|
125
132
|
|
126
133
|
```json
|
127
134
|
{
|
128
|
-
"filepath": "uploads/v3w3qzcb1d78pvi/something.gif",
|
129
135
|
"url": "https://foobar.s3.amazonaws.com/uploads/v3w3qzcb1d78pvi/something.gif",
|
136
|
+
"filepath": "uploads/v3w3qzcb1d78pvi/something.gif",
|
130
137
|
"filename": "something.gif",
|
131
138
|
"filesize": 184387,
|
132
139
|
"filetype": "image\/gif",
|
@@ -14,92 +14,16 @@ module S3FileField
|
|
14
14
|
end
|
15
15
|
|
16
16
|
def s3_file_field(object_name, method, options = {})
|
17
|
-
|
18
|
-
|
17
|
+
options = S3Uploader.new(options).field_options
|
18
|
+
|
19
19
|
if ::Rails.version.to_i >= 4
|
20
20
|
ActionView::Helpers::Tags::FileField.new(
|
21
21
|
object_name, method, self, options
|
22
22
|
).render
|
23
23
|
else
|
24
24
|
ActionView::Helpers::InstanceTag.new(
|
25
|
-
object_name, method, self,
|
26
|
-
|
27
|
-
).to_input_field_tag("file", options.update({:size => nil}))
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
class S3Uploader
|
32
|
-
def initialize(options)
|
33
|
-
@options = options.reverse_merge(
|
34
|
-
aws_access_key_id: S3FileField.config.access_key_id,
|
35
|
-
aws_secret_access_key: S3FileField.config.secret_access_key,
|
36
|
-
bucket: S3FileField.config.bucket,
|
37
|
-
acl: "public-read",
|
38
|
-
expiration: 10.hours.from_now.utc.iso8601,
|
39
|
-
max_file_size: 500.megabytes,
|
40
|
-
key_starts_with: options[:key_starts_with] || "uploads/"
|
41
|
-
)
|
42
|
-
|
43
|
-
unless @options[:aws_access_key_id]
|
44
|
-
raise Error.new("Please configure aws_access_key_id option.")
|
45
|
-
end
|
46
|
-
|
47
|
-
unless @options[:aws_secret_access_key]
|
48
|
-
raise Error.new("Please configure aws_secret_access_key option.")
|
49
|
-
end
|
50
|
-
|
51
|
-
unless @options[:bucket]
|
52
|
-
raise Error.new("Please configure bucket name.")
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
def field_options
|
57
|
-
{
|
58
|
-
data: {
|
59
|
-
:url => url,
|
60
|
-
:key => @options[:key] || key,
|
61
|
-
:acl => @options[:acl],
|
62
|
-
:'aws-access-key-id' => @options[:aws_access_key_id],
|
63
|
-
:policy => policy,
|
64
|
-
:signature => signature
|
65
|
-
}.reverse_merge(@options[:data] || {})
|
66
|
-
}
|
67
|
-
end
|
68
|
-
|
69
|
-
def key
|
70
|
-
@key ||= "#{@options[:key_starts_with]}{timestamp}-{unique_id}-#{SecureRandom.hex}/${filename}"
|
71
|
-
end
|
72
|
-
|
73
|
-
def url
|
74
|
-
"//#{@options[:bucket]}.s3.amazonaws.com/"
|
75
|
-
end
|
76
|
-
|
77
|
-
def policy
|
78
|
-
Base64.encode64(policy_data.to_json).gsub("\n", "")
|
79
|
-
end
|
80
|
-
|
81
|
-
def policy_data
|
82
|
-
{
|
83
|
-
expiration: @options[:expiration],
|
84
|
-
conditions: [
|
85
|
-
["starts-with", "$key", @options[:key_starts_with]],
|
86
|
-
["starts-with", "$x-requested-with", ""],
|
87
|
-
["content-length-range", 0, @options[:max_file_size]],
|
88
|
-
["starts-with","$Content-Type",""],
|
89
|
-
{bucket: @options[:bucket]},
|
90
|
-
{acl: @options[:acl]},
|
91
|
-
{success_action_status: "201"}
|
92
|
-
] + (@options[:conditions] || [])
|
93
|
-
}
|
94
|
-
end
|
95
|
-
|
96
|
-
def signature
|
97
|
-
Base64.encode64(
|
98
|
-
OpenSSL::HMAC.digest(
|
99
|
-
OpenSSL::Digest::Digest.new('sha1'),
|
100
|
-
@options[:aws_secret_access_key], policy
|
101
|
-
)
|
102
|
-
).gsub("\n", "")
|
25
|
+
object_name, method, self, options.delete(:object)
|
26
|
+
).to_input_field_tag("file", options.update(:size => nil))
|
103
27
|
end
|
104
28
|
end
|
105
29
|
end
|
@@ -0,0 +1,93 @@
|
|
1
|
+
module S3FileField
|
2
|
+
class S3Uploader # :nodoc:
|
3
|
+
attr_accessor :options
|
4
|
+
|
5
|
+
def initialize(original_options = {})
|
6
|
+
|
7
|
+
default_options = {
|
8
|
+
access_key_id: S3FileField.config.access_key_id,
|
9
|
+
secret_access_key: S3FileField.config.secret_access_key,
|
10
|
+
bucket: S3FileField.config.bucket,
|
11
|
+
acl: "public-read",
|
12
|
+
expiration: 10.hours.from_now.utc.iso8601,
|
13
|
+
max_file_size: 500.megabytes,
|
14
|
+
conditions: [],
|
15
|
+
key_starts_with: 'uploads/'
|
16
|
+
}
|
17
|
+
|
18
|
+
@key = original_options[:key]
|
19
|
+
@original_options = original_options
|
20
|
+
|
21
|
+
# Remove s3_file_field specific options from original options
|
22
|
+
extracted_options = @original_options.extract!(*default_options.keys).
|
23
|
+
reject { |k, v| v.nil? }
|
24
|
+
|
25
|
+
@options = default_options.merge(extracted_options)
|
26
|
+
|
27
|
+
unless @options[:access_key_id]
|
28
|
+
raise Error.new("Please configure access_key_id option.")
|
29
|
+
end
|
30
|
+
|
31
|
+
unless @options[:secret_access_key]
|
32
|
+
raise Error.new("Please configure secret_access_key option.")
|
33
|
+
end
|
34
|
+
|
35
|
+
unless @options[:bucket]
|
36
|
+
raise Error.new("Please configure bucket name.")
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def field_options
|
41
|
+
@original_options.merge(data: field_data_options)
|
42
|
+
end
|
43
|
+
|
44
|
+
def field_data_options
|
45
|
+
{
|
46
|
+
url: url,
|
47
|
+
key: key,
|
48
|
+
acl: @options[:acl],
|
49
|
+
aws_access_key_id: @options[:access_key_id],
|
50
|
+
policy: policy,
|
51
|
+
signature: signature
|
52
|
+
}.merge(@options[:data] || {})
|
53
|
+
end
|
54
|
+
|
55
|
+
private
|
56
|
+
|
57
|
+
def key
|
58
|
+
@key ||= "#{@options[:key_starts_with]}{timestamp}-{unique_id}-#{SecureRandom.hex}/${filename}"
|
59
|
+
end
|
60
|
+
|
61
|
+
def url
|
62
|
+
"//#{@options[:bucket]}.s3.amazonaws.com/"
|
63
|
+
end
|
64
|
+
|
65
|
+
def policy
|
66
|
+
Base64.encode64(policy_data.to_json).gsub("\n", '')
|
67
|
+
end
|
68
|
+
|
69
|
+
def policy_data
|
70
|
+
{
|
71
|
+
expiration: @options[:expiration],
|
72
|
+
conditions: [
|
73
|
+
["starts-with", "$key", @options[:key_starts_with]],
|
74
|
+
["starts-with", "$x-requested-with", ""],
|
75
|
+
["content-length-range", 0, @options[:max_file_size]],
|
76
|
+
["starts-with","$Content-Type",""],
|
77
|
+
{bucket: @options[:bucket]},
|
78
|
+
{acl: @options[:acl]},
|
79
|
+
{success_action_status: "201"}
|
80
|
+
] + @options[:conditions]
|
81
|
+
}
|
82
|
+
end
|
83
|
+
|
84
|
+
def signature
|
85
|
+
Base64.encode64(
|
86
|
+
OpenSSL::HMAC.digest(
|
87
|
+
OpenSSL::Digest::Digest.new('sha1'),
|
88
|
+
@options[:secret_access_key], policy
|
89
|
+
)
|
90
|
+
).gsub("\n", '')
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
data/lib/s3_file_field.rb
CHANGED
@@ -4,8 +4,10 @@ require 'jquery-fileupload-rails'
|
|
4
4
|
require 'base64'
|
5
5
|
require 'openssl'
|
6
6
|
require 'digest/sha1'
|
7
|
+
require 'active_support/core_ext'
|
7
8
|
|
8
9
|
require 's3_file_field/config_aws'
|
10
|
+
require 's3_file_field/s3_uploader'
|
9
11
|
require 's3_file_field/form_helper'
|
10
12
|
require 's3_file_field/railtie'
|
11
13
|
require 's3_file_field/engine'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: s3_file_field
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Stankiewicz
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-11-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -94,6 +94,34 @@ dependencies:
|
|
94
94
|
- - '>='
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rspec
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - '>='
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - '>='
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: pry
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - '>='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - '>='
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
97
125
|
description: jQuery File Upload extension for direct uploading to Amazon S3 using
|
98
126
|
CORS
|
99
127
|
email:
|
@@ -106,6 +134,7 @@ files:
|
|
106
134
|
- lib/s3_file_field/engine.rb
|
107
135
|
- lib/s3_file_field/form_helper.rb
|
108
136
|
- lib/s3_file_field/railtie.rb
|
137
|
+
- lib/s3_file_field/s3_uploader.rb
|
109
138
|
- lib/s3_file_field/version.rb
|
110
139
|
- lib/s3_file_field.rb
|
111
140
|
- app/assets/javascripts/s3_file_field.js.coffee
|