filepicker-rails 1.1.0 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +51 -47
- data/Rakefile +14 -3
- data/app/helpers/filepicker_rails/application_helper.rb +119 -37
- data/app/helpers/filepicker_rails/form_helper.rb +82 -38
- data/lib/filepicker_rails/configuration.rb +67 -4
- data/lib/filepicker_rails/engine.rb +1 -0
- data/lib/filepicker_rails/policy.rb +12 -1
- data/lib/filepicker_rails/version.rb +1 -1
- 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: a7c6108a87b1e18899294344d21da54df7705e14
|
4
|
+
data.tar.gz: fbab8e63de76ba8773a0aa1283591223386e8827
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f445d0c3ebc0b0766d1bd034d27aa4fc5c53c5cd405c6bbf2b753263e33288834e1b6d61eeeb73c4410260bddb4f1c0a8fe45f329f91314fd1a1316480b0e3d0
|
7
|
+
data.tar.gz: f1e3d5ff4a55be3249441f7cace323ff85f4658d64220a836ec5641c090842347be91ab0706a1be4724bcde5221788630d5b14b70c4a6045d2c212d8fcb68c77
|
data/README.md
CHANGED
@@ -28,24 +28,12 @@ Add the filepicker.io javascript library to your layout:
|
|
28
28
|
<%= filepicker_js_include_tag %>
|
29
29
|
```
|
30
30
|
|
31
|
-
Set your API Key in config/application.rb
|
31
|
+
Set your API Key in `config/application.rb`:
|
32
32
|
|
33
33
|
```ruby
|
34
34
|
config.filepicker_rails.api_key = "Your filepicker.io API Key"
|
35
35
|
```
|
36
36
|
|
37
|
-
Set your Secret Key in config/application.rb for signed request support:
|
38
|
-
|
39
|
-
```ruby
|
40
|
-
config.filepicker_rails.secret_key = "Your filepicker.io Secret Key"
|
41
|
-
```
|
42
|
-
|
43
|
-
Set your CDN Path in config/production.rb ([CDN usage](https://developers.inkfilepicker.com/docs/cdn/)):
|
44
|
-
|
45
|
-
```ruby
|
46
|
-
config.filepicker_rails.cdn_host = "Your CDN host name"
|
47
|
-
```
|
48
|
-
|
49
37
|
## Usage
|
50
38
|
|
51
39
|
### First create a migration to add the field that will hold your filepicker.io URL
|
@@ -58,13 +46,9 @@ Then add a column to the model's table of type :string:
|
|
58
46
|
|
59
47
|
```ruby
|
60
48
|
class AddNameOfAttrForFilepickerUrlToUser < ActiveRecord::Migration
|
61
|
-
def
|
49
|
+
def change
|
62
50
|
add_column :user, :filepicker_url, :string
|
63
51
|
end
|
64
|
-
|
65
|
-
def down
|
66
|
-
remove_column :user, :filepicker_url
|
67
|
-
end
|
68
52
|
end
|
69
53
|
```
|
70
54
|
|
@@ -80,24 +64,15 @@ end
|
|
80
64
|
<%= f.submit %>
|
81
65
|
<% end %>
|
82
66
|
```
|
67
|
+
The `filepicker_field` accepts a options parameter, [click here to see all the valid options](http://rubydoc.info/github/Ink/filepicker-rails/master/FilepickerRails/FormHelper:filepicker_field).
|
83
68
|
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
of an iframe on the page.
|
92
|
-
* multiple - (true or false) Whether or not multiple uploads can be saved at once.
|
93
|
-
* services - What services your users can upload to. Ex: "BOX, COMPUTER, FACEBOOK".
|
94
|
-
* store_path - The path to store the file at within the specified file store.
|
95
|
-
* store_location - The file is not copied by default. It remains in the original location. If you wish you have the file copied onto your own storage, you can specify where we should put the copy. The only value at the moment is "S3".
|
96
|
-
* store_access - Should the underlying file be publicly available on its S3 link. Options are "public" and "private", defaults to 'private'.
|
97
|
-
* dragdrop - (true or false) Whether or not to allow drag-and-drop uploads.
|
98
|
-
* drag_text - The text of the dragdrop pane.
|
99
|
-
* drag_class - The class of the dragdrop pane.
|
100
|
-
* onchange - The onchange event.
|
69
|
+
### Displaying an image:
|
70
|
+
|
71
|
+
```erb
|
72
|
+
<%= filepicker_image_tag @user.filepicker_url, w: 160, h: 160, fit: 'clip' %>
|
73
|
+
```
|
74
|
+
|
75
|
+
The `filepicker_image_tag` accepts a options parameter, [click here to see all the valid options](http://rubydoc.info/github/Ink/filepicker-rails/master/FilepickerRails/ApplicationHelper:filepicker_image_url).
|
101
76
|
|
102
77
|
### Accessing FilePicker File with OnChange:
|
103
78
|
|
@@ -137,31 +112,60 @@ Example fpfiles object:
|
|
137
112
|
}]
|
138
113
|
```
|
139
114
|
|
140
|
-
###
|
115
|
+
### Allowing the user to download a file (or upload it to any of the supported services)
|
141
116
|
|
142
117
|
```erb
|
143
|
-
<%=
|
118
|
+
<%= filepicker_save_button "Save", @user.filepicker_url, "image/jpg" %>
|
144
119
|
```
|
145
120
|
|
146
|
-
|
121
|
+
The `filepicker_save_button` accepts a options parameter, [click here to see all the valid options](http://rubydoc.info/github/Ink/filepicker-rails/master/FilepickerRails/ApplicationHelper:filepicker_save_button).
|
147
122
|
|
148
|
-
###
|
123
|
+
### CDN
|
149
124
|
|
150
|
-
|
151
|
-
|
125
|
+
Set your CDN Path in `config/production.rb` ([CDN usage](https://developers.inkfilepicker.com/docs/cdn/)):
|
126
|
+
|
127
|
+
```ruby
|
128
|
+
config.filepicker_rails.cdn_host = "Your CDN host name"
|
129
|
+
```
|
130
|
+
|
131
|
+
### Policy
|
132
|
+
|
133
|
+
To use the [filepicker policies](https://developers.inkfilepicker.com/docs/security/) follow this instructions.
|
134
|
+
|
135
|
+
Set your Secret Key in `config/application.rb`
|
136
|
+
|
137
|
+
```ruby
|
138
|
+
config.filepicker_rails.secret_key = "Your filepicker.io Secret Key"
|
152
139
|
```
|
153
140
|
|
154
|
-
|
141
|
+
#### Expiry time
|
155
142
|
|
156
|
-
|
157
|
-
of an iframe on the page.
|
158
|
-
* services - What services your users can upload to. Ex: "BOX, COMPUTER, FACEBOOK".
|
159
|
-
* save_as_name - A recommended file name. The user can override this.
|
143
|
+
By default the expiry time is 10 minutes. If you need to change the expiry time this should be an integer and it is expressed in seconds since the [Epoch](http://en.wikipedia.org/wiki/Unix_time).
|
160
144
|
|
161
|
-
|
145
|
+
So you can do something like that to set the expiry time to 5 minutes.
|
146
|
+
|
147
|
+
```ruby
|
148
|
+
config.filepicker_rails.expiry = -> { (Time.zone.now + 5.minutes).to_i }
|
149
|
+
```
|
150
|
+
|
151
|
+
If you need always the same url, a static expiry time, to do some cache. You can set a date starting of the Epoch.
|
152
|
+
|
153
|
+
```ruby
|
154
|
+
-> { 100.years.since(Time.at(0)).to_i }
|
155
|
+
```
|
156
|
+
|
157
|
+
The argument need to be a [callable](http://www.rubytapas.com/episodes/35-Callable).
|
158
|
+
|
159
|
+
## Demo
|
162
160
|
|
163
161
|
See a simple demo app [repo](https://github.com/maxtilford/filepicker-rails-demo)
|
164
162
|
|
163
|
+
## RDocs
|
164
|
+
|
165
|
+
You can view the Filepicker::Rails documentation in RDoc format here:
|
166
|
+
|
167
|
+
http://rubydoc.info/github/Ink/filepicker-rails/master/frames
|
168
|
+
|
165
169
|
## Versioning
|
166
170
|
|
167
171
|
Filepicker::Rails follow the [Semantic Versioning](http://semver.org/).
|
data/Rakefile
CHANGED
@@ -5,8 +5,11 @@ rescue LoadError
|
|
5
5
|
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
6
6
|
end
|
7
7
|
|
8
|
+
require 'rspec/core/rake_task'
|
8
9
|
require 'rdoc/task'
|
9
10
|
|
11
|
+
RSpec::Core::RakeTask.new
|
12
|
+
|
10
13
|
RDoc::Task.new(:rdoc) do |rdoc|
|
11
14
|
rdoc.rdoc_dir = 'rdoc'
|
12
15
|
rdoc.title = 'FilepickerRails'
|
@@ -18,9 +21,17 @@ end
|
|
18
21
|
APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
|
19
22
|
load 'rails/tasks/engine.rake'
|
20
23
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
+
task :default do
|
25
|
+
if ENV['BUNDLE_GEMFILE'] =~ /gemfiles/
|
26
|
+
Rake::Task['spec'].invoke
|
27
|
+
else
|
28
|
+
Rake::Task['appraise'].invoke
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
task :appraise do
|
33
|
+
exec 'appraisal install && appraisal rake'
|
34
|
+
end
|
24
35
|
|
25
36
|
Bundler::GemHelper.install_tasks
|
26
37
|
|
@@ -1,10 +1,33 @@
|
|
1
1
|
module FilepickerRails
|
2
2
|
module ApplicationHelper
|
3
3
|
|
4
|
+
# Creates a javascript tag to the filepicker JavaScript.
|
5
|
+
#
|
6
|
+
# #### Examples
|
7
|
+
#
|
8
|
+
# filepicker_js_include_tag
|
9
|
+
# # => <script src="//api.filepicker.io/v1/filepicker.js"></script>
|
4
10
|
def filepicker_js_include_tag
|
5
11
|
javascript_include_tag "//api.filepicker.io/v1/filepicker.js"
|
6
12
|
end
|
7
13
|
|
14
|
+
# Creates a button allowing the user to download a file
|
15
|
+
# (or upload it to any of the supported services). Set the content of
|
16
|
+
# the button on the `text` parameter. The `url` of the content you want the user to save.
|
17
|
+
# Define the `mimetype` of the content. Accepts a optional `options` parameter.
|
18
|
+
#
|
19
|
+
# #### Options
|
20
|
+
#
|
21
|
+
# - `:container` - Where to show the file picker dialog can be `modal`,
|
22
|
+
# `window` or the id of an iframe on the page.
|
23
|
+
# - `:services` - What services your users can upload to. Ex: `BOX, COMPUTER, FACEBOOK`.
|
24
|
+
# - `:save_as_name` - A recommended file name. The user can override this.
|
25
|
+
#
|
26
|
+
# #### Examples
|
27
|
+
#
|
28
|
+
# filepicker_save_button "Save", @user.filepicker_url, "image/jpg"
|
29
|
+
# # => <button data-fp-apikey="..." data-fp-mimetype="image/jpg" data-fp-url="https://www.filepicker.io/api/file/hFHUCB3iTxyMzseuWOgG" name="button" type="submit">save</button>
|
30
|
+
#
|
8
31
|
def filepicker_save_button(text, url, mimetype, options = {})
|
9
32
|
options[:data] ||= {}
|
10
33
|
container = options.delete(:container)
|
@@ -20,71 +43,130 @@ module FilepickerRails
|
|
20
43
|
button_tag(text, options)
|
21
44
|
end
|
22
45
|
|
23
|
-
#
|
24
|
-
#
|
25
|
-
#
|
46
|
+
# Creates a image tag of the `url`. Accepts the options to work on filepicker.io,
|
47
|
+
# see the valid options on `filepicker_image_url` documentation. Accepts html options to the image tag,
|
48
|
+
# see the [image_tag](http://api.rubyonrails.org/classes/ActionView/Helpers/AssetTagHelper.html#method-i-image_tag)
|
49
|
+
# documentation for the valid options.
|
50
|
+
#
|
51
|
+
# #### Examples
|
52
|
+
#
|
53
|
+
# filepicker_image_tag @user.filepicker_url, w: 160, h: 160, fit: 'clip'
|
54
|
+
# # => <img src="https://www.filepicker.io/api/file/hFHUCB3iTxyMzseuWOgG/convert?w=160&h=160&fit=clip" />
|
55
|
+
#
|
26
56
|
def filepicker_image_tag(url, image_options={}, image_tag_options={})
|
27
57
|
image_tag(filepicker_image_url(url, image_options), image_tag_options)
|
28
58
|
end
|
29
59
|
|
30
|
-
#
|
60
|
+
# Creates the full path of the image to the specified `url` accepts optional `options`
|
61
|
+
# hash for configuration.
|
62
|
+
#
|
63
|
+
# #### Options
|
64
|
+
#
|
65
|
+
# - `:w` - Resize the image to this width.
|
31
66
|
#
|
32
|
-
# h - Resize the image to this height.
|
67
|
+
# - `:h` - Resize the image to this height.
|
33
68
|
#
|
34
|
-
# fit - Specifies how to resize the image. Possible values are:
|
35
|
-
#
|
36
|
-
# distorting, cropping, or changing the aspect ratio
|
37
|
-
#
|
69
|
+
# - `:fit` - Specifies how to resize the image. Possible values are:
|
70
|
+
# - `:clip` - Resizes the image to fit within the specified parameters without
|
71
|
+
# distorting, cropping, or changing the aspect ratio, this is the default.
|
72
|
+
# - `:crop` - Resizes the image to fit the specified parameters exactly by
|
38
73
|
# removing any parts of the image that don't fit within the boundaries
|
39
|
-
#
|
74
|
+
# - `:scales` - Resizes the image to fit the specified parameters exactly by
|
40
75
|
# scaling the image to the desired size
|
41
|
-
#
|
42
|
-
# align - Determines how the image is aligned when resizing and using the "fit" parameter.
|
76
|
+
# - `:align` - Determines how the image is aligned when resizing and using the "fit" parameter.
|
43
77
|
# Check API for details.
|
44
78
|
#
|
45
|
-
# rotate - Rotate the image. Default is no rotation.
|
46
|
-
#
|
47
|
-
#
|
79
|
+
# - `:rotate` - Rotate the image. Default is no rotation. Possible values are:
|
80
|
+
# - `:exif` - will rotate the image automatically based on the exif data in the image.
|
81
|
+
# - Other valid values are integers between 0 and 359, for degrees of rotation.
|
48
82
|
#
|
49
|
-
#
|
50
|
-
#
|
51
|
-
#
|
52
|
-
# should be 4 numbers for 'x,y,width,height' - for example,
|
53
|
-
# 'crop=10,20,200,250' would select the 200x250 pixel rectangle starting
|
83
|
+
# - `:crop` - Crops the image to a specified rectangle. The input to this parameter
|
84
|
+
# should be 4 numbers for `x,y,width,height` - for example,
|
85
|
+
# `10, 20, 200, 250` would select the 200x250 pixel rectangle starting
|
54
86
|
# from 10 pixels from the left edge and 20 pixels from the top edge of the
|
55
87
|
# image.
|
56
88
|
#
|
57
|
-
# format - Specifies what format the image should be converted to, if any.
|
58
|
-
# Possible values are
|
89
|
+
# - `:format` - Specifies what format the image should be converted to, if any.
|
90
|
+
# Possible values are `jpg` and `png`. For `jpg` conversions, you
|
59
91
|
# can additionally specify a quality parameter.
|
60
92
|
#
|
61
|
-
# quality - For jpeg conversion, specifies the quality of the resultant image.
|
93
|
+
# - `:quality` - For jpeg conversion, specifies the quality of the resultant image.
|
62
94
|
# Quality should be an integer between 1 and 100
|
63
95
|
#
|
64
|
-
# watermark - Adds the specified absolute url as a watermark on the image.
|
96
|
+
# - `:watermark` - Adds the specified absolute url as a watermark on the image.
|
65
97
|
#
|
66
|
-
# watersize - This size of the watermark, as a percentage of the base
|
98
|
+
# - `:watersize` - This size of the watermark, as a percentage of the base
|
67
99
|
# image (not the original watermark).
|
68
100
|
#
|
69
|
-
# waterposition - Where to put the watermark relative to the base image.
|
70
|
-
# Possible values for vertical position are
|
71
|
-
#
|
101
|
+
# - `:waterposition` - Where to put the watermark relative to the base image.
|
102
|
+
# Possible values for vertical position are `top`,`middle`,
|
103
|
+
# `bottom` and `left`,`center`,`right`, for horizontal
|
72
104
|
# position. The two can be combined by separating vertical
|
73
105
|
# and horizontal with a comma. The default behavior
|
74
106
|
# is bottom,right
|
107
|
+
#
|
108
|
+
# - `:cache` - Specifies if the image should be cached or not.
|
109
|
+
#
|
110
|
+
# #### Examples
|
111
|
+
#
|
112
|
+
# filepicker_image_url @user.filepicker_url, w: 160, h: 160, fit: 'clip'
|
113
|
+
# # => https://www.filepicker.io/api/file/hFHUCB3iTxyMzseuWOgG/convert?w=160&h=160&fit=clip
|
114
|
+
#
|
75
115
|
def filepicker_image_url(url, options = {})
|
76
|
-
|
116
|
+
FilepickerImageUrl.new(url, options).execute
|
117
|
+
end
|
118
|
+
|
119
|
+
class FilepickerImageUrl
|
77
120
|
|
78
|
-
|
79
|
-
|
80
|
-
|
121
|
+
CONVERT_OPTIONS = [:w, :h, :fit, :align, :rotate, :crop, :format,
|
122
|
+
:quality, :watermark, :watersize, :waterposition]
|
123
|
+
VALID_OPTIONS = CONVERT_OPTIONS + [:cache]
|
124
|
+
|
125
|
+
def initialize(url, options = {})
|
126
|
+
@url, @options = url, options
|
81
127
|
end
|
82
128
|
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
129
|
+
def execute
|
130
|
+
url_with_path = if convert_options.any?
|
131
|
+
"#{cdn_url}/convert"
|
132
|
+
else
|
133
|
+
cdn_url
|
134
|
+
end
|
135
|
+
|
136
|
+
query_params = all_options.merge(policy_config).to_query
|
137
|
+
|
138
|
+
[url_with_path, query_params.presence].compact.join('?')
|
139
|
+
end
|
140
|
+
|
141
|
+
private
|
142
|
+
|
143
|
+
attr_reader :url, :options
|
144
|
+
|
145
|
+
def all_options
|
146
|
+
options.select { |option| VALID_OPTIONS.include?(option) }
|
147
|
+
end
|
148
|
+
|
149
|
+
def convert_options
|
150
|
+
options.select { |option| CONVERT_OPTIONS.include?(option) }
|
151
|
+
end
|
152
|
+
|
153
|
+
def cdn_host
|
154
|
+
::Rails.application.config.filepicker_rails.cdn_host
|
155
|
+
end
|
156
|
+
|
157
|
+
def cdn_url
|
158
|
+
if cdn_host
|
159
|
+
uri = URI.parse(url)
|
160
|
+
url.gsub("#{uri.scheme}://#{uri.host}", cdn_host)
|
161
|
+
else
|
162
|
+
url
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
def policy_config
|
167
|
+
Policy.apply
|
168
|
+
end
|
88
169
|
end
|
170
|
+
private_constant :FilepickerImageUrl
|
89
171
|
end
|
90
172
|
end
|
@@ -1,57 +1,101 @@
|
|
1
1
|
module FilepickerRails
|
2
2
|
module FormHelper
|
3
3
|
|
4
|
-
|
5
|
-
|
4
|
+
FILEPICKER_OPTIONS_TO_DASHERIZE = [:button_text, :button_class, :mimetypes,
|
5
|
+
:extensions, :container, :services,
|
6
|
+
:drag_text, :drag_class, :store_path,
|
7
|
+
:store_location, :store_access, :multiple]
|
6
8
|
|
7
|
-
|
8
|
-
input_options['data-fp-apikey'] ||= ::Rails.application.config.filepicker_rails.api_key
|
9
|
-
input_options.merge!(secure_filepicker) unless input_options['data-fp-policy'].present?
|
10
|
-
input_options['type'] = type
|
9
|
+
FILEPICKER_OPTIONS_TO_CAMELIZE = [:max_size]
|
11
10
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
11
|
+
# Creates a filepicker field, accepts optional `options` hash for configuration.
|
12
|
+
#
|
13
|
+
# #### Options
|
14
|
+
#
|
15
|
+
# - `:button_text` - The text of the upload button.
|
16
|
+
# - `:button_class` - The class of the upload button.
|
17
|
+
# - `:extensions` - The extensions of file types you want to support for this upload. Ex: `.png,.jpg`.
|
18
|
+
# - `:mimetypes` - The file types you want to support for this upload. Ex: `image/png,text/*`.
|
19
|
+
# - `:container` - Where to show the file picker dialog can be `modal`, `window` or the id of an iframe on the page.
|
20
|
+
# - `:multiple` - (true or false) Whether or not multiple uploads can be saved at once.
|
21
|
+
# - `:services` - What services your users can upload to. Ex: `BOX, COMPUTER, FACEBOOK`.
|
22
|
+
# - `:store_path` - The path to store the file at within the specified file store.
|
23
|
+
# - `:store_location` - The file is not copied by default. It remains in the original location. If you wish you have the file copied onto your own storage, you can specify where we should put the copy. The only value at the moment is `S3`.
|
24
|
+
# - `:store_access` - Should the underlying file be publicly available on its S3 link. Options are `public` and `private`, defaults to 'private'.
|
25
|
+
# - `:dragdrop` - (`true` or `false`) Whether or not to allow drag-and-drop uploads.
|
26
|
+
# - `:drag_text` - The text of the dragdrop pane.
|
27
|
+
# - `:drag_class` - The class of the dragdrop pane.
|
28
|
+
# - `:onchange` - The onchange event.
|
29
|
+
# - `:max_size` - The maximum file size allowed, in bytes.
|
30
|
+
# - `:class` - Add a class to the input.
|
31
|
+
# - `:value` - Define the value of the input
|
32
|
+
#
|
33
|
+
# #### Examples
|
34
|
+
#
|
35
|
+
# filepicker_field(:filepicker_url)
|
36
|
+
# # => <input data-fp-apikey="..." id="user_filepicker_url" name="user[filepicker_url]" type="filepicker" />
|
37
|
+
#
|
38
|
+
# This is mixed on form for to be used like.
|
39
|
+
#
|
40
|
+
# <%= form_for @user do |f| %>
|
41
|
+
# <%= f.filepicker_field :filepicker_url %>
|
42
|
+
# <%= f.submit %>
|
43
|
+
# <% end %>
|
44
|
+
#
|
45
|
+
def filepicker_field(method, options = {})
|
46
|
+
define_input_options(options)
|
47
|
+
@method = method
|
48
|
+
if rails_greater_than_4?
|
49
|
+
rails_greater_than_4_input
|
16
50
|
else
|
17
|
-
|
51
|
+
rails_input
|
18
52
|
end
|
19
53
|
end
|
20
54
|
|
21
55
|
private
|
22
56
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
:class => 'class',
|
40
|
-
:value => 'value'
|
41
|
-
}
|
57
|
+
attr_reader :input_options, :method, :type, :object_name, :template
|
58
|
+
|
59
|
+
def define_input_options(options)
|
60
|
+
@type = options.delete(:dragdrop) ? 'filepicker-dragdrop' : 'filepicker'
|
61
|
+
@input_options = retrieve_legacy_filepicker_options(options)
|
62
|
+
@input_options['data-fp-apikey'] ||= ::Rails.application.config.filepicker_rails.api_key
|
63
|
+
@input_options.merge!(secure_filepicker) unless @input_options['data-fp-policy'].present?
|
64
|
+
@input_options['type'] = @type
|
65
|
+
@input_options
|
66
|
+
end
|
67
|
+
|
68
|
+
def rails_greater_than_4_input
|
69
|
+
tag = ActionView::Helpers::Tags::TextField.new(object_name, method, template, objectify_options(input_options))
|
70
|
+
tag.send(:add_default_name_and_id, input_options)
|
71
|
+
tag.render
|
72
|
+
end
|
42
73
|
|
74
|
+
def rails_input
|
75
|
+
ActionView::Helpers::InstanceTag.new(object_name, method, template).to_input_field_tag(type, input_options)
|
76
|
+
end
|
77
|
+
|
78
|
+
def rails_greater_than_4?
|
79
|
+
::Rails.version.to_i >= 4
|
80
|
+
end
|
81
|
+
|
82
|
+
def filepicker_prefix
|
83
|
+
'data-fp-'
|
84
|
+
end
|
85
|
+
|
86
|
+
def retrieve_legacy_filepicker_options(options)
|
87
|
+
mappings = {}
|
88
|
+
FILEPICKER_OPTIONS_TO_DASHERIZE.each do |option|
|
89
|
+
mappings[option] = "#{filepicker_prefix}#{option.to_s.dasherize}"
|
90
|
+
end
|
91
|
+
FILEPICKER_OPTIONS_TO_CAMELIZE.each do |option|
|
92
|
+
mappings[option] = "#{filepicker_prefix}#{option.to_s.camelize(:lower)}"
|
93
|
+
end
|
43
94
|
Hash[options.map {|k, v| [mappings[k] || k, v] }]
|
44
95
|
end
|
45
96
|
|
46
97
|
def secure_filepicker
|
47
|
-
|
48
|
-
grant = Policy.new
|
49
|
-
grant.call = [:pick, :store]
|
50
|
-
|
51
|
-
{
|
52
|
-
'data-fp-policy' => grant.policy,
|
53
|
-
'data-fp-signature' => grant.signature
|
54
|
-
}
|
98
|
+
Policy.apply([:pick, :store], ['data-fp-policy', 'data-fp-signature'])
|
55
99
|
end
|
56
100
|
end
|
57
101
|
end
|
@@ -1,14 +1,77 @@
|
|
1
1
|
module FilepickerRails
|
2
2
|
class Configuration
|
3
|
-
attr_writer :api_key, :default_expiry
|
4
|
-
attr_accessor :secret_key, :cdn_host
|
5
3
|
|
4
|
+
# Define your API Key to be used.
|
5
|
+
#
|
6
|
+
# #### Examples
|
7
|
+
#
|
8
|
+
# This is to be used on the `config/application.rb`:
|
9
|
+
#
|
10
|
+
# config.filepicker_rails.api_key = 'Your filepicker.io API Key'
|
11
|
+
#
|
12
|
+
attr_writer :api_key
|
13
|
+
|
14
|
+
# Define your Secret key to be used on Policy.
|
15
|
+
#
|
16
|
+
# More info about Policy on [Ink documentation](https://developers.filepicker.io/docs/security/)
|
17
|
+
#
|
18
|
+
# #### Examples
|
19
|
+
#
|
20
|
+
# This is to be used on the `config/application.rb`:
|
21
|
+
#
|
22
|
+
# config.filepicker_rails.secret_key = 'Your filepicker.io Secret Key'
|
23
|
+
#
|
24
|
+
attr_writer :secret_key
|
25
|
+
|
26
|
+
# @private
|
27
|
+
attr_reader :secret_key
|
28
|
+
|
29
|
+
# Set your CDN Path to be used
|
30
|
+
#
|
31
|
+
# More info about CDN on [Ink documentation](https://developers.filepicker.io/docs/cdn/)
|
32
|
+
#
|
33
|
+
# #### Examples
|
34
|
+
#
|
35
|
+
# This is to be used on the `config/application.rb`:
|
36
|
+
#
|
37
|
+
# config.filepicker_rails.cdn_host = 'Your CDN host name'
|
38
|
+
#
|
39
|
+
attr_writer :cdn_host
|
40
|
+
|
41
|
+
# @private
|
42
|
+
attr_reader :cdn_host
|
43
|
+
|
44
|
+
# @private
|
6
45
|
def api_key
|
7
46
|
@api_key or raise "Set config.filepicker_rails.api_key"
|
8
47
|
end
|
9
48
|
|
10
|
-
|
11
|
-
|
49
|
+
# Define the expire time when using Policy.
|
50
|
+
#
|
51
|
+
# By default the expiry time is 10 minutes.
|
52
|
+
# If you need to change the expiry time this should be an integer and
|
53
|
+
# it is expressed in seconds since the [Epoch](http://en.wikipedia.org/wiki/Unix_time).
|
54
|
+
#
|
55
|
+
# #### Examples
|
56
|
+
#
|
57
|
+
# This is to be used on the `config/application.rb`:
|
58
|
+
#
|
59
|
+
# config.filepicker_rails.expiry = -> { (Time.zone.now + 5.minutes).to_i }
|
60
|
+
# # Define the expiry time to 5 minutes
|
61
|
+
#
|
62
|
+
# If you need always the same url, a static expiry time, to do some cache.
|
63
|
+
# You can set a date starting of the Epoch.
|
64
|
+
#
|
65
|
+
# config.filepicker_rails.expiry = -> { 100.years.since(Time.at(0)).to_i }
|
66
|
+
#
|
67
|
+
def expiry=(expiry)
|
68
|
+
raise ArgumentError, 'Must be a callable' unless expiry.respond_to?(:call)
|
69
|
+
@expiry = expiry
|
70
|
+
end
|
71
|
+
|
72
|
+
# @private
|
73
|
+
def expiry
|
74
|
+
@expiry ||= -> { Time.zone.now.to_i + 600 }
|
12
75
|
end
|
13
76
|
end
|
14
77
|
end
|
@@ -19,11 +19,21 @@ module FilepickerRails
|
|
19
19
|
OpenSSL::HMAC.hexdigest('sha256', ::Rails.application.config.filepicker_rails.secret_key, policy)
|
20
20
|
end
|
21
21
|
|
22
|
+
def self.apply(call = [:read, :convert], keys = ['policy', 'signature'])
|
23
|
+
return {} unless ::Rails.application.config.filepicker_rails.secret_key.present?
|
24
|
+
grant = Policy.new
|
25
|
+
grant.call = call
|
26
|
+
{
|
27
|
+
keys[0] => grant.policy,
|
28
|
+
keys[1] => grant.signature
|
29
|
+
}
|
30
|
+
end
|
31
|
+
|
22
32
|
private
|
23
33
|
def json_policy
|
24
34
|
hash = Hash.new
|
25
35
|
|
26
|
-
@expiry ||=
|
36
|
+
@expiry ||= ::Rails.application.config.filepicker_rails.expiry.call
|
27
37
|
|
28
38
|
[:expiry, :call, :handle, :maxsize, :minsize, :path].each do |input|
|
29
39
|
hash[input] = send(input) unless send(input).nil?
|
@@ -32,4 +42,5 @@ module FilepickerRails
|
|
32
42
|
MultiJson.dump(hash)
|
33
43
|
end
|
34
44
|
end
|
45
|
+
private_constant :Policy
|
35
46
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: filepicker-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Max Tilford
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-08-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -108,6 +108,34 @@ dependencies:
|
|
108
108
|
- - ">="
|
109
109
|
- !ruby/object:Gem::Version
|
110
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'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: yard
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
111
139
|
description: Makes integrating filepicker.io with rails 4 easy
|
112
140
|
email:
|
113
141
|
- maxtilford@gmail.com
|
@@ -150,3 +178,4 @@ signing_key:
|
|
150
178
|
specification_version: 4
|
151
179
|
summary: Makes integrating filepicker.io with rails 4 easy
|
152
180
|
test_files: []
|
181
|
+
has_rdoc:
|