s3ff 0.9.1 → 0.9.2
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 +34 -52
- data/lib/s3ff/version.rb +1 -1
- data/lib/s3ff/view_helper.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 804b22add6dbaad3b44df45846e522113b9c848e
|
4
|
+
data.tar.gz: 290fe54e2b6ede4b2d914af03534eb019945b442
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1263f70cfdb4dd17e6284aebbc8d8cc9d588ba3d422f4b15c044ea7dc1503246becdfbbfb09e70ff7aa75bb74ab4e6e151e5f58ec4aed4484cbe10455264d393
|
7
|
+
data.tar.gz: 51cf9eb3149442474c227fcd93042152462cc9c46aaa4ccb8b84c890a0455f57e61a155a8b8fb373feabcab519e75612d1140cde4e20c2d1c3a56e92536e2428
|
data/README.md
CHANGED
@@ -21,35 +21,11 @@ Or install it yourself as:
|
|
21
21
|
|
22
22
|
## Usage
|
23
23
|
|
24
|
-
###
|
25
|
-
|
26
|
-
```ruby
|
27
|
-
if defined?(S3FileField) && ENV['AWS_KEY']
|
28
|
-
cdn_hostname = ENV.fetch('CDN_HOSTNAME') { ENV['S3_BUCKET'] && "#{ENV['S3_BUCKET']}.s3.amazonaws.com" }
|
29
|
-
S3FileField.config do |c|
|
30
|
-
c.access_key_id = ENV['AWS_KEY']
|
31
|
-
c.secret_access_key = ENV['AWS_SECRET']
|
32
|
-
c.bucket = ENV['S3_BUCKET']
|
33
|
-
c.region = ENV['S3_BUCKET_REGION'] || 'us-west-2'
|
34
|
-
c.url = "//#{cdn_hostname}" if cdn_hostname # S3 API endpoint (optional), eg. "https://#{c.bucket}.s3.amazonaws.com/"
|
35
|
-
# c.acl = "public-read"
|
36
|
-
# c.expiration = 10.hours.from_now.utc.iso8601
|
37
|
-
# c.max_file_size = 100.megabytes
|
38
|
-
# c.conditions = []
|
39
|
-
# c.key_starts_with = 'uploads/
|
40
|
-
# c.ssl = true # if true, force SSL connection
|
41
|
-
end
|
24
|
+
### 1. Configure s3_file_field
|
42
25
|
|
43
|
-
|
44
|
-
S3FileField::FormBuilder.class_eval do
|
45
|
-
def s3_file_field(method, options = {})
|
46
|
-
@template.file_field(@object_name, method, options)
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
50
|
-
```
|
26
|
+
add a config like `sample/config_s3_file_field.rb` into your Rails `config/initializers/` directory
|
51
27
|
|
52
|
-
###
|
28
|
+
### 2. Add javascript
|
53
29
|
|
54
30
|
in your application.js
|
55
31
|
|
@@ -57,24 +33,24 @@ in your application.js
|
|
57
33
|
//= require s3ff
|
58
34
|
```
|
59
35
|
|
60
|
-
###
|
36
|
+
### 3. Change `file_field` input to use `s3_file_field`
|
61
37
|
|
62
|
-
```
|
38
|
+
``` haml
|
63
39
|
= form_for :user do |f|
|
64
40
|
= f.s3_file_field :avatar
|
65
41
|
```
|
66
42
|
|
67
43
|
or if you're using `simple_form`
|
68
44
|
|
69
|
-
```
|
45
|
+
``` haml
|
70
46
|
= simple_form_for :user do |f|
|
71
47
|
= f.input :avatar do
|
72
48
|
= f.s3_file_field :avatar, :class => "form-control"
|
73
49
|
```
|
74
50
|
|
75
|
-
###
|
51
|
+
### 4. Add footer
|
76
52
|
|
77
|
-
```
|
53
|
+
``` haml
|
78
54
|
= include_s3ff_templates
|
79
55
|
```
|
80
56
|
|
@@ -84,22 +60,19 @@ NOTE: Feel free to modify & render the templates manually, but keep the `s3ff_`
|
|
84
60
|
|
85
61
|
To illustate, if you have a file field like this
|
86
62
|
|
87
|
-
```
|
63
|
+
``` html
|
88
64
|
<input type="file" name="user[avatar]">
|
89
65
|
```
|
90
66
|
|
91
|
-
When `s3ff` kicks in, it would upgrade the field to a `s3_file_field`. When your user chooses a file, it will be uploaded, with a progress indicator, directly into your S3 bucket (see `s3_file_field` gem for configuration). Your `form` will be disabled during the upload and re-enabled once upload completes. After this process,
|
67
|
+
When `s3ff` kicks in, it would upgrade the field to a `s3_file_field`. When your user chooses a file, it will be uploaded, with a progress indicator, directly into your S3 bucket (see `s3_file_field` gem for configuration). Your `form` will be disabled during the upload and re-enabled once upload completes. After this process, a new hidden form field will be attached to your form:
|
92
68
|
|
93
|
-
```
|
69
|
+
``` html
|
94
70
|
<input type="file" name="user[avatar_direct_url]" value="https://....">
|
95
|
-
<input type="file" name="user[avatar_file_name]" value="face.png">
|
96
|
-
<input type="file" name="user[avatar_file_size]" value="162534">
|
97
|
-
<input type="file" name="user[avatar_content_type]" value="image/png">
|
98
71
|
```
|
99
72
|
|
100
73
|
## Code changes to your app
|
101
74
|
|
102
|
-
`s3ff` designed to minimize moving parts and code changes to your Rails app - all it does is give you
|
75
|
+
`s3ff` designed to minimize moving parts and code changes to your Rails app - all it does is give you new hidden form fields in return for every direct s3 file upload that happened in your user's browser.
|
103
76
|
|
104
77
|
How you deal with these form fields are entirely up to you. Here's a simple way:
|
105
78
|
|
@@ -107,44 +80,53 @@ How you deal with these form fields are entirely up to you. Here's a simple way:
|
|
107
80
|
|
108
81
|
If your controller was specifying
|
109
82
|
|
110
|
-
```
|
83
|
+
``` ruby
|
111
84
|
params.require(:user).permit(:avatar)
|
112
85
|
```
|
113
86
|
|
114
87
|
It would need to be changed to accept the new form fields
|
115
88
|
|
116
|
-
```
|
117
|
-
params.require(:user).permit(:avatar,
|
118
|
-
:avatar_direct_url,
|
119
|
-
:avatar_file_name,
|
120
|
-
:avatar_file_size,
|
121
|
-
:avatar_content_type
|
122
|
-
)
|
89
|
+
``` ruby
|
90
|
+
params.require(:user).permit(:avatar, :avatar_direct_url)
|
123
91
|
```
|
124
92
|
|
125
93
|
#### 2. Upgrade model to also accept direct url
|
126
94
|
|
127
95
|
If your model was originally
|
128
96
|
|
129
|
-
```
|
97
|
+
``` ruby
|
130
98
|
class User < ActiveRecord::Base
|
131
99
|
has_attached_file :avatar
|
100
|
+
validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\Z/
|
132
101
|
end
|
133
102
|
```
|
134
103
|
|
135
|
-
Download the file from S3 when given `avatar_direct_url`. This
|
104
|
+
Download the file from S3 when given `avatar_direct_url`. This leaves all your existing Paperclip code and logic unchanged.
|
136
105
|
|
137
|
-
```
|
106
|
+
``` ruby
|
138
107
|
class User < ActiveRecord::Base
|
139
108
|
has_attached_file :avatar
|
109
|
+
validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\Z/
|
110
|
+
|
111
|
+
# s3ff changes
|
140
112
|
|
141
|
-
attr_accessor :avatar_direct_url
|
142
113
|
def avatar_direct_url=(value)
|
143
|
-
self.avatar =
|
114
|
+
self.avatar =
|
115
|
+
ActionDispatch::Http::UploadedFile.new(
|
116
|
+
tempfile: open(value),
|
117
|
+
filename: File.basename(value),
|
118
|
+
).tap do |upload|
|
119
|
+
upload.content_type = Paperclip::ContentTypeDetector.new(upload.path).detect
|
120
|
+
end
|
144
121
|
end
|
145
122
|
end
|
146
123
|
```
|
147
124
|
|
125
|
+
#### CAVEAT
|
126
|
+
|
127
|
+
It isn't ideal to handle your attachment processing synchronously during the web request. For usage with `Sidekiq` or `DelayedJob`, look at the reference code in `sample/user.*.rb`
|
128
|
+
|
129
|
+
|
148
130
|
## Contributing
|
149
131
|
|
150
132
|
1. Fork it ( http://github.com/jollygoodcode/s3ff/fork )
|
data/lib/s3ff/version.rb
CHANGED
data/lib/s3ff/view_helper.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
module S3FF
|
2
2
|
module ViewHelper
|
3
|
-
def include_s3ff_templates(map = { _direct_url: 'result.url', _file_name: 'result.filename', _file_size: 'result.filesize', _content_type: 'result.filetype' })
|
3
|
+
def include_s3ff_templates(map = { _direct_url: 'result.url' }) # extras: , _file_name: 'result.filename', _file_size: 'result.filesize', _content_type: 'result.filetype' })
|
4
4
|
<<-EOM
|
5
5
|
<div style="display:none;">
|
6
6
|
<script id="s3ff_label" type="text/x-tmpl">
|