filestack-rails 2.2.0 → 3.0.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 +113 -110
- data/Rakefile +2 -11
- data/app/helpers/filestack_rails/application_helper.rb +78 -0
- data/app/helpers/filestack_rails/form_helper.rb +32 -0
- data/lib/filestack-rails.rb +7 -0
- data/lib/filestack_rails/configuration.rb +25 -0
- data/lib/filestack_rails/engine.rb +17 -0
- data/lib/filestack_rails/transform.rb +46 -0
- data/lib/filestack_rails/version.rb +3 -0
- data/lib/tasks/filestack_rails_tasks.rake +4 -0
- metadata +27 -69
- data/MIT-LICENSE +0 -20
- data/app/helpers/filepicker_rails/application_helper.rb +0 -276
- data/app/helpers/filepicker_rails/form_helper.rb +0 -72
- data/lib/filepicker-rails.rb +0 -7
- data/lib/filepicker_rails/configuration.rb +0 -77
- data/lib/filepicker_rails/engine.rb +0 -17
- data/lib/filepicker_rails/policy.rb +0 -46
- data/lib/filepicker_rails/tag.rb +0 -44
- data/lib/filepicker_rails/version.rb +0 -3
@@ -0,0 +1,25 @@
|
|
1
|
+
module FilestackRails
|
2
|
+
class Configuration
|
3
|
+
attr_accessor :api_key, :client_name, :secret_key, :security, :expiry, :app_secret
|
4
|
+
|
5
|
+
def api_key
|
6
|
+
@api_key or raise "Set config.filepicker_rails.api_key"
|
7
|
+
end
|
8
|
+
|
9
|
+
def client_name
|
10
|
+
@client_name or 'filestack_client'
|
11
|
+
end
|
12
|
+
|
13
|
+
def expiry
|
14
|
+
@expiry or ( Time.zone.now.to_i + 600 )
|
15
|
+
end
|
16
|
+
|
17
|
+
def security=(security_options = {})
|
18
|
+
if @app_secret.nil?
|
19
|
+
raise 'You must have secret key to use security'
|
20
|
+
end
|
21
|
+
@security = FilestackSecurity.new(@app_secret, options: security_options)
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module FilestackRails
|
2
|
+
class Engine < ::Rails::Engine
|
3
|
+
config.filestack_rails = FilestackRails::Configuration.new
|
4
|
+
isolate_namespace FilestackRails
|
5
|
+
|
6
|
+
initializer 'filestack_rails.action_controller' do |app|
|
7
|
+
ActiveSupport.on_load(:action_controller) do
|
8
|
+
::ActionController::Base.helper(FilestackRails::ApplicationHelper)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
initializer "filestack_rails.form_builder" do
|
13
|
+
ActionView::Helpers::FormBuilder.send(:include, FilestackRails::FormHelper)
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'filestack'
|
2
|
+
|
3
|
+
class FilestackTransform
|
4
|
+
def initialize(apikey)
|
5
|
+
security = ::Rails.application.config.filestack_rails.security
|
6
|
+
if !security.nil?
|
7
|
+
@transform = Transform.new(apikey: apikey, security: security)
|
8
|
+
else
|
9
|
+
@transform = Transform.new(apikey: apikey)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def method_missing(method_name, **args)
|
14
|
+
if defined? @transform.send(method_name)
|
15
|
+
raise "Invalid transformation for filestack_image" unless scrub_bad_transforms(method_name)
|
16
|
+
@transform = @transform.send(method_name, **args)
|
17
|
+
self
|
18
|
+
else
|
19
|
+
super
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def add_external_url(url)
|
24
|
+
@transform.instance_variable_set(:@external_url, url)
|
25
|
+
end
|
26
|
+
|
27
|
+
def fs_url
|
28
|
+
@transform.url
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
module FilestackRails
|
33
|
+
module Transform
|
34
|
+
|
35
|
+
def get_transform(apikey)
|
36
|
+
FilestackTransform.new(apikey)
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
def scrub_bad_transforms(method_name)
|
45
|
+
!['av_convert', 'debug', 'store', 'url'].include? method_name.to_s
|
46
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: filestack-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 3.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
-
-
|
7
|
+
- filestack
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-07-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -16,30 +16,30 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '4.0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '4.0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
28
|
+
name: filestack
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
34
|
-
type: :
|
33
|
+
version: 2.0.1
|
34
|
+
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - "
|
38
|
+
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
40
|
+
version: 2.0.1
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
|
-
name:
|
42
|
+
name: coveralls
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
45
|
- - ">="
|
@@ -53,7 +53,7 @@ dependencies:
|
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
|
-
name:
|
56
|
+
name: sqlite3
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
59
|
- - ">="
|
@@ -67,7 +67,7 @@ dependencies:
|
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
|
-
name:
|
70
|
+
name: rspec-rails
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
73
|
- - ">="
|
@@ -94,67 +94,25 @@ dependencies:
|
|
94
94
|
- - ">="
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '0'
|
97
|
-
|
98
|
-
|
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'
|
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'
|
139
|
-
description: Makes integrating Filestack with rails easy
|
97
|
+
description: Allows easy integraiton of Filestack's File Picker through dynamic button
|
98
|
+
tags and form helpers
|
140
99
|
email:
|
141
100
|
- dev@filestack.com
|
142
101
|
executables: []
|
143
102
|
extensions: []
|
144
103
|
extra_rdoc_files: []
|
145
104
|
files:
|
146
|
-
- MIT-LICENSE
|
147
105
|
- README.md
|
148
106
|
- Rakefile
|
149
|
-
- app/helpers/
|
150
|
-
- app/helpers/
|
151
|
-
- lib/
|
152
|
-
- lib/
|
153
|
-
- lib/
|
154
|
-
- lib/
|
155
|
-
- lib/
|
156
|
-
- lib/
|
157
|
-
homepage: https://
|
107
|
+
- app/helpers/filestack_rails/application_helper.rb
|
108
|
+
- app/helpers/filestack_rails/form_helper.rb
|
109
|
+
- lib/filestack-rails.rb
|
110
|
+
- lib/filestack_rails/configuration.rb
|
111
|
+
- lib/filestack_rails/engine.rb
|
112
|
+
- lib/filestack_rails/transform.rb
|
113
|
+
- lib/filestack_rails/version.rb
|
114
|
+
- lib/tasks/filestack_rails_tasks.rake
|
115
|
+
homepage: https://www.filestack.com
|
158
116
|
licenses:
|
159
117
|
- MIT
|
160
118
|
metadata: {}
|
@@ -174,8 +132,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
174
132
|
version: '0'
|
175
133
|
requirements: []
|
176
134
|
rubyforge_project:
|
177
|
-
rubygems_version: 2.
|
135
|
+
rubygems_version: 2.5.1
|
178
136
|
signing_key:
|
179
137
|
specification_version: 4
|
180
|
-
summary:
|
138
|
+
summary: Filestack plugin for Rails 4+
|
181
139
|
test_files: []
|
data/MIT-LICENSE
DELETED
@@ -1,20 +0,0 @@
|
|
1
|
-
Copyright 2013 YOURNAME
|
2
|
-
|
3
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
-
a copy of this software and associated documentation files (the
|
5
|
-
"Software"), to deal in the Software without restriction, including
|
6
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
-
permit persons to whom the Software is furnished to do so, subject to
|
9
|
-
the following conditions:
|
10
|
-
|
11
|
-
The above copyright notice and this permission notice shall be
|
12
|
-
included in all copies or substantial portions of the Software.
|
13
|
-
|
14
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
-
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
-
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
-
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
@@ -1,276 +0,0 @@
|
|
1
|
-
module FilepickerRails
|
2
|
-
module ApplicationHelper
|
3
|
-
|
4
|
-
include FilepickerRails::Tag
|
5
|
-
|
6
|
-
# Creates a javascript tag to the filepicker JavaScript.
|
7
|
-
#
|
8
|
-
# #### Examples
|
9
|
-
#
|
10
|
-
# filepicker_js_include_tag
|
11
|
-
# # => <script src="//api.filepicker.io/v1/filepicker.js"></script>
|
12
|
-
def filepicker_js_include_tag
|
13
|
-
javascript_include_tag "//api.filepicker.io/v2/filepicker.js", type: "text/javascript"
|
14
|
-
end
|
15
|
-
|
16
|
-
# Creates a filepicker field tag, accepts optional `options` hash for configuration.
|
17
|
-
#
|
18
|
-
# #### Options
|
19
|
-
#
|
20
|
-
# - `:button_text` - The text of the upload button.
|
21
|
-
# - `:button_class` - The class of the upload button.
|
22
|
-
# - `:extensions` - The extensions of file types you want to support for this upload. Ex: `.png,.jpg`.
|
23
|
-
# - `:mimetypes` - The file types you want to support for this upload. Ex: `image/png,text/*`.
|
24
|
-
# - `:container` - Where to show the file picker dialog can be `modal`, `window` or the id of an iframe on the page.
|
25
|
-
# - `:multiple` - (true or false) Whether or not multiple uploads can be saved at once.
|
26
|
-
# - `:services` - What services your users can upload to. Ex: `BOX, COMPUTER, FACEBOOK`.
|
27
|
-
# - `:store_path` - The path to store the file at within the specified file store.
|
28
|
-
# - `: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`.
|
29
|
-
# - `:store_container` - The bucket or container in your specified `store_location`. Defaults to the container specified in the developer portal. Does not apply to Dropbox storage.
|
30
|
-
# - `:store_access` - Should the underlying file be publicly available on its S3 link. Options are `public` and `private`, defaults to 'private'.
|
31
|
-
# - `:dragdrop` - (`true` or `false`) Whether or not to allow drag-and-drop uploads.
|
32
|
-
# - `:drag_text` - The text of the dragdrop pane.
|
33
|
-
# - `:drag_class` - The class of the dragdrop pane.
|
34
|
-
# - `:onchange` - The onchange event.
|
35
|
-
# - `:max_size` - The maximum file size allowed, in bytes.
|
36
|
-
# - `:max_files` - The maximum number of files.
|
37
|
-
# - `:open_to` - Open the picker to the given service. Ex: `COMPUTER`.
|
38
|
-
# - `:class` - Add a class to the input.
|
39
|
-
# - `:value` - Define the value of the input
|
40
|
-
# - `:language` - Open the picker to the given language. Ex: `fr`.
|
41
|
-
#
|
42
|
-
# #### Examples
|
43
|
-
#
|
44
|
-
# filepicker_field_tag('user[filepicker_url]')
|
45
|
-
# # => <input data-fp-apikey="..." id="user_filepicker_url" name="user[filepicker_url]" type="filepicker" />
|
46
|
-
#
|
47
|
-
def filepicker_field_tag(name, options = {})
|
48
|
-
define_input_options(options)
|
49
|
-
tag :input, { 'type' => type, 'name' => name, 'id' => sanitize_to_id(name) }.update(input_options.stringify_keys)
|
50
|
-
end
|
51
|
-
|
52
|
-
# Creates a button allowing the user to download a file
|
53
|
-
# (or upload it to any of the supported services). Set the content of
|
54
|
-
# the button on the `text` parameter. The `url` of the content you want the user to save.
|
55
|
-
# Define the `mimetype` of the content. Accepts a optional `options` parameter.
|
56
|
-
#
|
57
|
-
# #### Options
|
58
|
-
#
|
59
|
-
# - `:container` - Where to show the file picker dialog can be `modal`,
|
60
|
-
# `window` or the id of an iframe on the page.
|
61
|
-
# - `:services` - What services your users can upload to. Ex: `BOX, COMPUTER, FACEBOOK`.
|
62
|
-
# - `:save_as_name` - A recommended file name. The user can override this.
|
63
|
-
#
|
64
|
-
# #### Examples
|
65
|
-
#
|
66
|
-
# filepicker_save_button "Save", @user.filepicker_url, "image/jpg"
|
67
|
-
# # => <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>
|
68
|
-
#
|
69
|
-
def filepicker_save_button(text, url, mimetype, options = {})
|
70
|
-
export_widget(text, url, mimetype, options) do
|
71
|
-
button_tag(text, options)
|
72
|
-
end
|
73
|
-
end
|
74
|
-
|
75
|
-
# Creates a link allowing the user to download a file
|
76
|
-
# (or upload it to any of the supported services). Set the content of
|
77
|
-
# the link on the `text` parameter. The `url` of the content you want the user to save.
|
78
|
-
# Define the `mimetype` of the content. Accepts a optional `options` parameter.
|
79
|
-
#
|
80
|
-
# #### Options
|
81
|
-
#
|
82
|
-
# - `:container` - Where to show the file picker dialog can be `modal`,
|
83
|
-
# `window` or the id of an iframe on the page.
|
84
|
-
# - `:services` - What services your users can upload to. Ex: `BOX, COMPUTER, FACEBOOK`.
|
85
|
-
# - `:save_as_name` - A recommended file name. The user can override this.
|
86
|
-
#
|
87
|
-
# #### Examples
|
88
|
-
#
|
89
|
-
# filepicker_save_link "Save", @user.filepicker_url, "image/jpg"
|
90
|
-
# # => <a data-fp-apikey="..." data-fp-mimetype="image/jpg" data-fp-url="https://www.filepicker.io/api/file/hFHUCB3iTxyMzseuWOgG" href="#" id="filepicker_export_widget_link">save</a>
|
91
|
-
#
|
92
|
-
def filepicker_save_link(text, url, mimetype, options = {})
|
93
|
-
export_widget(text, url, mimetype, options) do
|
94
|
-
options[:id] = options.fetch(:id, 'filepicker_export_widget_link')
|
95
|
-
link_to text, '#', options
|
96
|
-
end
|
97
|
-
end
|
98
|
-
|
99
|
-
# Creates a image tag of the `url`. Accepts the options to work on filepicker.io,
|
100
|
-
# see the valid options on `filepicker_image_url` documentation. Accepts html options to the image tag,
|
101
|
-
# see the [image_tag](http://api.rubyonrails.org/classes/ActionView/Helpers/AssetTagHelper.html#method-i-image_tag)
|
102
|
-
# documentation for the valid options.
|
103
|
-
#
|
104
|
-
# #### Examples
|
105
|
-
#
|
106
|
-
# filepicker_image_tag @user.filepicker_url, w: 160, h: 160, fit: 'clip'
|
107
|
-
# # => <img src="https://www.filepicker.io/api/file/hFHUCB3iTxyMzseuWOgG/convert?w=160&h=160&fit=clip" />
|
108
|
-
#
|
109
|
-
def filepicker_image_tag(url, image_options={}, image_tag_options={})
|
110
|
-
image_tag(filepicker_image_url(url, image_options), image_tag_options)
|
111
|
-
end
|
112
|
-
|
113
|
-
# Creates the full path of the image to the specified `url` accepts optional `options`
|
114
|
-
# hash for configuration.
|
115
|
-
#
|
116
|
-
# #### Options
|
117
|
-
#
|
118
|
-
# - `:w` - Resize the image to this width.
|
119
|
-
#
|
120
|
-
# - `:h` - Resize the image to this height.
|
121
|
-
#
|
122
|
-
# - `:fit` - Specifies how to resize the image. Possible values are:
|
123
|
-
# - `:clip` - Resizes the image to fit within the specified parameters without
|
124
|
-
# distorting, cropping, or changing the aspect ratio, this is the default.
|
125
|
-
# - `:crop` - Resizes the image to fit the specified parameters exactly by
|
126
|
-
# removing any parts of the image that don't fit within the boundaries
|
127
|
-
# - `:scales` - Resizes the image to fit the specified parameters exactly by
|
128
|
-
# scaling the image to the desired size
|
129
|
-
# - `:align` - Determines how the image is aligned when resizing and using the "fit" parameter.
|
130
|
-
# Check API for details.
|
131
|
-
#
|
132
|
-
# - `:rotate` - Rotate the image. Default is no rotation. Possible values are:
|
133
|
-
# - `:exif` - will rotate the image automatically based on the exif data in the image.
|
134
|
-
# - Other valid values are integers between 0 and 359, for degrees of rotation.
|
135
|
-
#
|
136
|
-
# - `:crop` - Crops the image to a specified rectangle. The input to this parameter
|
137
|
-
# should be 4 numbers for `x,y,width,height` - for example,
|
138
|
-
# `10, 20, 200, 250` would select the 200x250 pixel rectangle starting
|
139
|
-
# from 10 pixels from the left edge and 20 pixels from the top edge of the
|
140
|
-
# image.
|
141
|
-
#
|
142
|
-
# - `:crop_first` - Makes sure the image is cropped before any other
|
143
|
-
# conversion parameters are executed.
|
144
|
-
# The only value for this parameter is `true`.
|
145
|
-
#
|
146
|
-
# - `:format` - Specifies what format the image should be converted to, if any.
|
147
|
-
# Possible values are `jpg` and `png`. For `jpg` conversions, you
|
148
|
-
# can additionally specify a quality parameter.
|
149
|
-
#
|
150
|
-
# - `:quality` - For jpeg conversion, specifies the quality of the resultant image.
|
151
|
-
# Quality should be an integer between 1 and 100
|
152
|
-
#
|
153
|
-
# - `:watermark` - Adds the specified absolute url as a watermark on the image.
|
154
|
-
#
|
155
|
-
# - `:watersize` - This size of the watermark, as a percentage of the base
|
156
|
-
# image (not the original watermark).
|
157
|
-
#
|
158
|
-
# - `:waterposition` - Where to put the watermark relative to the base image.
|
159
|
-
# Possible values for vertical position are `top`,`middle`,
|
160
|
-
# `bottom` and `left`,`center`,`right`, for horizontal
|
161
|
-
# position. The two can be combined by separating vertical
|
162
|
-
# and horizontal with a comma. The default behavior
|
163
|
-
# is bottom,right
|
164
|
-
#
|
165
|
-
# - `:cache` - Specifies if the image should be cached or not.
|
166
|
-
#
|
167
|
-
# - `:compress` - You can take advantage of Filepicker's image compression which utilizes JPEGtran and OptiPNG.
|
168
|
-
# The value for this parameter is boolean. If you want to compress your image then the parameter
|
169
|
-
# is compress:true. Compression is off/false by default.
|
170
|
-
#
|
171
|
-
# #### Examples
|
172
|
-
#
|
173
|
-
# filepicker_image_url @user.filepicker_url, w: 160, h: 160, fit: 'clip'
|
174
|
-
# # => https://www.filepicker.io/api/file/hFHUCB3iTxyMzseuWOgG/convert?w=160&h=160&fit=clip
|
175
|
-
#
|
176
|
-
def filepicker_image_url(url, options = {})
|
177
|
-
FilepickerImageUrl.new(url, options).execute
|
178
|
-
end
|
179
|
-
|
180
|
-
class FilepickerImageUrl
|
181
|
-
|
182
|
-
CONVERT_OPTIONS = [:w, :h, :fit, :align, :rotate, :crop, :format,
|
183
|
-
:quality, :watermark, :watersize, :waterposition,
|
184
|
-
:crop_first]
|
185
|
-
VALID_OPTIONS = CONVERT_OPTIONS + [:cache, :compress]
|
186
|
-
|
187
|
-
def initialize(url, options = {})
|
188
|
-
@url, @options = url, options
|
189
|
-
end
|
190
|
-
|
191
|
-
def execute
|
192
|
-
base_url = url_with_path.split('?').first
|
193
|
-
query_params = all_options.to_query
|
194
|
-
|
195
|
-
[base_url, query_params.presence].compact.join('?')
|
196
|
-
end
|
197
|
-
|
198
|
-
private
|
199
|
-
|
200
|
-
attr_reader :url, :options
|
201
|
-
|
202
|
-
def valid_options
|
203
|
-
options.select { |option| VALID_OPTIONS.include?(option) }
|
204
|
-
end
|
205
|
-
|
206
|
-
def convert_options
|
207
|
-
options.select { |option| CONVERT_OPTIONS.include?(option) }
|
208
|
-
end
|
209
|
-
|
210
|
-
def all_options
|
211
|
-
[original_url_options, valid_options, policy_config].inject(&:merge)
|
212
|
-
end
|
213
|
-
|
214
|
-
def original_url_options
|
215
|
-
query_string = url_with_path.split('?')[1]
|
216
|
-
|
217
|
-
if query_string
|
218
|
-
Rack::Utils.parse_nested_query(query_string)
|
219
|
-
else
|
220
|
-
{}
|
221
|
-
end
|
222
|
-
end
|
223
|
-
|
224
|
-
def cdn_host
|
225
|
-
@cdn_host ||= ::Rails.application.config.filepicker_rails.cdn_host
|
226
|
-
end
|
227
|
-
|
228
|
-
def cdn_url
|
229
|
-
if cdn_host
|
230
|
-
uri = URI.parse(url)
|
231
|
-
url.gsub("#{uri.scheme}://#{uri.host}", cdn_host)
|
232
|
-
else
|
233
|
-
url
|
234
|
-
end
|
235
|
-
end
|
236
|
-
|
237
|
-
def handle
|
238
|
-
url.split("/").last
|
239
|
-
end
|
240
|
-
|
241
|
-
def policy_config
|
242
|
-
Policy.apply(options: {handle: handle})
|
243
|
-
end
|
244
|
-
|
245
|
-
def url_with_path
|
246
|
-
@url_with_path ||= if append_convert_on_url_path?
|
247
|
-
"#{cdn_url}/convert"
|
248
|
-
else
|
249
|
-
cdn_url
|
250
|
-
end
|
251
|
-
end
|
252
|
-
|
253
|
-
def append_convert_on_url_path?
|
254
|
-
convert_options.any? && !cdn_url.match('/convert')
|
255
|
-
end
|
256
|
-
end
|
257
|
-
private_constant :FilepickerImageUrl
|
258
|
-
|
259
|
-
private
|
260
|
-
|
261
|
-
def export_widget(text, url, mimetype, options, &block)
|
262
|
-
options[:data] ||= {}
|
263
|
-
container = options.delete(:container)
|
264
|
-
services = options.delete(:services)
|
265
|
-
save_as = options.delete(:save_as_name)
|
266
|
-
|
267
|
-
options[:data]['fp-url'] = url
|
268
|
-
options[:data]['fp-apikey'] = ::Rails.application.config.filepicker_rails.api_key
|
269
|
-
options[:data]['fp-mimetype'] = mimetype
|
270
|
-
options[:data]['fp-container'] = container if container
|
271
|
-
options[:data]['fp-services'] = Array(services).join(",") if services
|
272
|
-
options[:data]['fp-suggestedFilename'] = save_as if save_as
|
273
|
-
block.call
|
274
|
-
end
|
275
|
-
end
|
276
|
-
end
|