shrine-cloudinary 1.0.2 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 62bb38b8c926704c9eada728c344f4586c33689933308ca052fc6b40c0091931
4
- data.tar.gz: 408d13255233be4a9c90a82afe9122d855b3848c08d615f0eb1a8d78c582ea52
3
+ metadata.gz: 6fd427e7ba7152f8dcaa9d77157e5d6ab22ab48074ba06a3ed00523f81849ff7
4
+ data.tar.gz: 3b0c58833d9d2894b23633c365c9feff1fdf0e123f47cef0e8727e295ed96cfd
5
5
  SHA512:
6
- metadata.gz: 29e2db03ac2395c2bb2046f993aa4b95b087f95ae6b3ac349d79301c39ece7e782f5ecd6d9aeb89cb5895a9f454b0f97b63b597ec19e7c139fd03707437e852f
7
- data.tar.gz: fc5c41d4be4902f1eb08589ffeadfca966a121a58a4ad5de3ac2df2f1954cb2f91d1911a12a8fbb504d017ce6d35a20b615996b1333055e7984db530d3c4a297
6
+ metadata.gz: 3cf2577f5450d758fcedd96b64a10daf73316d2842a7d1bc16c0c7e44dbb96a3562af4ba77da0721e333e93e2456a0426f535d4cff6f28d224ee22069aced825
7
+ data.tar.gz: 92e263bf7b5cd8905f7834f4ed12a64b892e36adc8a3bc7235db464cd43c33edb390ba10e366068cc5b9c556b75ce004200a11be2c269f432ce5c78f1060d180
data/README.md CHANGED
@@ -9,7 +9,7 @@ an HTML widget for direct uploads.
9
9
  ## Installation
10
10
 
11
11
  ```ruby
12
- gem "shrine-cloudinary"
12
+ gem "shrine-cloudinary", "~> 1.1"
13
13
  ```
14
14
 
15
15
  ## Usage
@@ -29,47 +29,17 @@ Cloudinary.config(
29
29
 
30
30
  Shrine.storages = {
31
31
  cache: Shrine::Storage::Cloudinary.new(prefix: "cache"), # for direct uploads
32
- store: Shrine::Storage::Cloudinary.new(prefix: "store"),
32
+ store: Shrine::Storage::Cloudinary.new,
33
33
  }
34
34
  ```
35
35
 
36
36
  ### Direct uploads
37
37
 
38
- Cloudinary supports uploading files directly to their service, freeing your
39
- application from accepting file uploads. There are three ways in which you
40
- can do direct uploads:
38
+ The `Shrine::Storage::Cloudinary` class implements the `#presign` method, so it
39
+ should work with Shrine's [presign_endpoint] plugin and Uppy's [AwsS3] plugin.
41
40
 
42
- * [unsigned uploads using the widget](http://cloudinary.com/documentation/upload_widget)
43
- * [unsigned uploads using jQuery](http://cloudinary.com/blog/direct_upload_made_easy_from_browser_or_mobile_app_to_the_cloud)
44
- * [signed uploads via jQuery](http://cloudinary.com/documentation/jquery_image_upload)
45
-
46
- The first one is the simplest, you can see the [demo] app with complete
47
- implementation using shrine-cloudinary. Unsigned uploads don't require
48
- communicating with the server, you just need to set up an "upload preset".
49
-
50
- If you would prefer that the server controlls who is allowed to upload,
51
- shrine-cloudinary also supports generating presigns, which works with the
52
- direct_upload plugin in the same way that S3 does. When rendering on the server
53
- side, you can generate a presign inline:
54
-
55
- ```erb
56
- <input name="file" type="file"
57
- class="cloudinary-fileupload" data-cloudinary-field="image_id"
58
- data-form-data="<%= Shrine.storages[:cache].presign.fields.to_json %>">
59
- ```
60
-
61
- Alternatively you can add an endpoint to your app which can generate presigns
62
- on request, which is suitable for apps where templates are rendered on the
63
- client-side (see [direct_upload] documentation):
64
-
65
- ```rb
66
- Shrine.plugin :direct_upload, presign: true
67
- ```
68
- ```rb
69
- Rails.application.routes.draw do
70
- mount ImageUploader::UploadEndpoint => "/attachments/images"
71
- end
72
- ```
41
+ If that doesn't work, you can always use Shrine's [upload_endpoint] plugin with
42
+ Uppy's [XHRUpload] plugin.
73
43
 
74
44
  ### Copying
75
45
 
@@ -109,7 +79,7 @@ If you want some [Cloudinary options] to be applied to all uploads, you can
109
79
  specify `:upload_options`:
110
80
 
111
81
  ```rb
112
- Shrine::Storage::Cloudinary.new(upload_options: {backup: true})
82
+ Shrine::Storage::Cloudinary.new(upload_options: { backup: true })
113
83
  ```
114
84
 
115
85
  You can also apply upload options dynamically per upload using the
@@ -122,8 +92,8 @@ class MyUploader < Shrine
122
92
  {
123
93
  format: "png",
124
94
  eager: [
125
- {width: 500, height: 500, crop: :scale},
126
- {width: 300, height: 300, crop: :crop, gravity: :south},
95
+ { width: 500, height: 500, crop: :scale },
96
+ { width: 300, height: 300, crop: :crop, gravity: :south },
127
97
  ]
128
98
  }
129
99
  end
@@ -133,7 +103,7 @@ end
133
103
  Finally, you can pass upload options when using the uploader directly:
134
104
 
135
105
  ```rb
136
- uploader.upload(file, upload_options: {format: "png"})
106
+ uploader.upload(file, upload_options: { format: "png" })
137
107
  ```
138
108
 
139
109
  ### URLs
@@ -189,7 +159,7 @@ for images. In Shrine you can leverage this via `:upload_options` and
189
159
 
190
160
  ```rb
191
161
  Shrine::Storage::Cloudinary.new(
192
- upload_options: {responsive_breakpoints: {...}},
162
+ upload_options: { responsive_breakpoints: {...} },
193
163
  store_data: true,
194
164
  )
195
165
  ```
@@ -218,7 +188,7 @@ If the `:responsive_breakpoints` value needs to be dynamic, you can use the
218
188
 
219
189
  ```rb
220
190
  Shrine.plugin :upload_options, store: -> (io, context) do
221
- {responsive_breakpoints: {...}}
191
+ { responsive_breakpoints: {...} }
222
192
  end
223
193
  ```
224
194
 
@@ -238,8 +208,8 @@ The default chunk size is 20 MB, but you can change that by passing
238
208
 
239
209
  ```rb
240
210
  Shrine::Storage::Cloudinary.new(
241
- large: 100*1024*1024 # 100 MB
242
- upload_options: {chunk_size: 5*1024*1204} # 5 MB
211
+ large: 100*1024*1024 # 100 MB
212
+ upload_options: { chunk_size: 5*1024*1204 } # 5 MB
243
213
  )
244
214
  ```
245
215
 
@@ -298,6 +268,8 @@ This gem has been inspired by Cloudinary's [CarrierWave integration].
298
268
  [Rails image manipulation]: http://cloudinary.com/documentation/rails_image_manipulation
299
269
  [responsive breakpoints]: http://cloudinary.com/blog/introducing_intelligent_responsive_image_breakpoints_solutions
300
270
  [explicit API]: http://cloudinary.com/documentation/image_upload_api_reference#explicit
301
- [demo]: /demo
302
271
  [control access]: http://cloudinary.com/documentation/upload_images#control_access_to_images
303
- [direct_upload]: http://shrinerb.com/rdoc/classes/Shrine/Plugins/DirectUpload.html
272
+ [presign_endpoint]: https://github.com/shrinerb/shrine/blob/master/doc/plugins/presign_endpoint.md#readme
273
+ [upload_endpoint]: https://github.com/shrinerb/shrine/blob/master/doc/plugins/upload_endpoint.md#readme
274
+ [AwsS3]: https://uppy.io/docs/aws-s3/
275
+ [XHRUpload]: https://uppy.io/docs/xhr-upload/
@@ -18,9 +18,8 @@ class Shrine
18
18
 
19
19
  def upload(io, id, shrine_metadata: {}, **upload_options)
20
20
  options = { public_id: public_id(id) }
21
- options.update(default_options)
22
- options.update(@upload_options)
23
- options.update(upload_options)
21
+ options.merge!(@upload_options)
22
+ options.merge!(upload_options)
24
23
 
25
24
  result = store(io, **options)
26
25
 
@@ -31,52 +30,47 @@ class Shrine
31
30
  end
32
31
 
33
32
  def update(id, **options)
34
- ::Cloudinary::Uploader.explicit(public_id(id), default_options.merge(options))
35
- end
36
-
37
- def move(io, id, shrine_metadata: {}, **upload_options)
38
- ::Cloudinary::Uploader.rename(io.storage.public_id(io.id), public_id(id), default_options)
39
- end
40
-
41
- def movable?(io, id)
42
- io.is_a?(UploadedFile) && io.storage.is_a?(Storage::Cloudinary)
33
+ uploader.explicit(public_id(id), **options)
43
34
  end
44
35
 
45
36
  def open(id, **options)
46
37
  Down::Http.open(url(id, sign_url: true), **options)
38
+ rescue Down::NotFound
39
+ raise Shrine::FileNotFound, "file #{id.inspect} not found on storage"
47
40
  end
48
41
 
49
42
  def exists?(id)
50
- result = ::Cloudinary::Api.resources_by_ids([public_id(id)], default_options)
43
+ result = api.resources_by_ids([public_id(id)])
51
44
  result.fetch("resources").any?
52
45
  end
53
46
 
54
47
  def delete(id)
55
- ::Cloudinary::Uploader.destroy(public_id(id), default_options)
48
+ uploader.destroy(public_id(id))
56
49
  end
57
50
 
58
51
  def url(id, **options)
59
- ::Cloudinary::Utils.cloudinary_url(path(id), default_options.merge(secure: true, **options))
52
+ utils.cloudinary_url(path(id), secure: true, **options)
60
53
  end
61
54
 
62
55
  def clear!(**options)
63
56
  if prefix
64
- ::Cloudinary::Api.delete_resources_by_prefix(prefix, default_options.merge(options))
57
+ api.delete_resources_by_prefix(prefix, **options)
65
58
  else
66
- ::Cloudinary::Api.delete_all_resources(default_options.merge(options))
59
+ api.delete_all_resources(**options)
67
60
  end
68
61
  end
69
62
 
70
- def presign(id = nil, **options)
71
- upload_options = id ? { public_id: public_id(id) } : { folder: prefix }
72
- upload_options.update(@upload_options)
63
+ def presign(id = nil, **presign_options)
64
+ options = id ? { public_id: public_id(id) } : { folder: prefix }
65
+ options.merge!(@upload_options)
66
+ options.merge!(presign_options)
73
67
 
74
- fields = ::Cloudinary::Uploader.build_upload_params(upload_options.merge(options))
68
+ fields = ::Cloudinary::Uploader.build_upload_params(options)
75
69
  fields.reject! { |key, value| value.nil? || value == "" }
76
70
  fields[:signature] = ::Cloudinary::Utils.api_sign_request(fields, ::Cloudinary.config.api_secret)
77
- fields[:api_key] = ::Cloudinary.config.api_key
71
+ fields[:api_key] = ::Cloudinary.config.api_key
78
72
 
79
- url = ::Cloudinary::Utils.cloudinary_api_url("upload", default_options)
73
+ url = utils.cloudinary_api_url("upload")
80
74
 
81
75
  { method: :post, url: url, fields: fields }
82
76
  end
@@ -99,18 +93,22 @@ class Shrine
99
93
 
100
94
  def store(io, chunk_size: nil, **options)
101
95
  if remote?(io)
102
- ::Cloudinary::Uploader.upload(io.url, **options)
96
+ uploader.upload(io.url, **options)
103
97
  else
104
98
  make_rest_client_recognize_as_file!(io)
105
99
  if large?(io)
106
100
  io.instance_eval { def match(*); false; end } # https://github.com/cloudinary/cloudinary_gem/pull/260
107
- ::Cloudinary::Uploader.upload_large(io, chunk_size: chunk_size, **options)
101
+ uploader.upload_large(io, chunk_size: chunk_size, **options)
108
102
  else
109
- ::Cloudinary::Uploader.upload(io, **options)
103
+ uploader.upload(io, **options)
110
104
  end
111
105
  end
112
106
  end
113
107
 
108
+ def uploader; Delegator.new(::Cloudinary::Uploader, default_options); end
109
+ def api; Delegator.new(::Cloudinary::Api, default_options); end
110
+ def utils; Delegator.new(::Cloudinary::Utils, default_options); end
111
+
114
112
  def remote?(io)
115
113
  io.is_a?(UploadedFile) && io.url.to_s =~ /^ftp:|^https?:/
116
114
  end
@@ -175,6 +173,23 @@ class Shrine
175
173
  "mpeg" => "video/mpeg",
176
174
  "avi" => "video/x-msvideo",
177
175
  }
176
+
177
+ # Delegates each method call to the specified klass, but passing
178
+ # specified default options.
179
+ class Delegator
180
+ def initialize(klass, default_options)
181
+ @klass = klass
182
+ @default_options = default_options
183
+ end
184
+
185
+ def method_missing(name, *args, **options, &block)
186
+ @klass.public_send(name, *args, **@default_options, **options, &block)
187
+ end
188
+
189
+ def respond_to_missing?(name, include_private = false)
190
+ @klass.respond_to?(name, include_private)
191
+ end
192
+ end
178
193
  end
179
194
  end
180
195
  end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |gem|
2
2
  gem.name = "shrine-cloudinary"
3
- gem.version = "1.0.2"
3
+ gem.version = "1.1.0"
4
4
 
5
5
  gem.required_ruby_version = ">= 2.1"
6
6
 
@@ -13,10 +13,10 @@ Gem::Specification.new do |gem|
13
13
  gem.files = Dir["README.md", "LICENSE.txt", "lib/**/*.rb", "*.gemspec"]
14
14
  gem.require_path = "lib"
15
15
 
16
- gem.add_dependency "shrine", "~> 2.11"
16
+ gem.add_dependency "shrine", ">= 3.0.0.rc", "< 4"
17
17
  gem.add_dependency "cloudinary", "< 2"
18
- gem.add_dependency "down", "~> 4.4"
19
- gem.add_dependency "http", ">= 3.2"
18
+ gem.add_dependency "down", "~> 5.0"
19
+ gem.add_dependency "http", ">= 3.2", "< 5"
20
20
 
21
21
  gem.add_development_dependency "rake"
22
22
  gem.add_development_dependency "minitest"
metadata CHANGED
@@ -1,29 +1,35 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shrine-cloudinary
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Janko Marohnić
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-07-04 00:00:00.000000000 Z
11
+ date: 2019-09-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: shrine
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 3.0.0.rc
20
+ - - "<"
18
21
  - !ruby/object:Gem::Version
19
- version: '2.11'
22
+ version: '4'
20
23
  type: :runtime
21
24
  prerelease: false
22
25
  version_requirements: !ruby/object:Gem::Requirement
23
26
  requirements:
24
- - - "~>"
27
+ - - ">="
25
28
  - !ruby/object:Gem::Version
26
- version: '2.11'
29
+ version: 3.0.0.rc
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '4'
27
33
  - !ruby/object:Gem::Dependency
28
34
  name: cloudinary
29
35
  requirement: !ruby/object:Gem::Requirement
@@ -44,14 +50,14 @@ dependencies:
44
50
  requirements:
45
51
  - - "~>"
46
52
  - !ruby/object:Gem::Version
47
- version: '4.4'
53
+ version: '5.0'
48
54
  type: :runtime
49
55
  prerelease: false
50
56
  version_requirements: !ruby/object:Gem::Requirement
51
57
  requirements:
52
58
  - - "~>"
53
59
  - !ruby/object:Gem::Version
54
- version: '4.4'
60
+ version: '5.0'
55
61
  - !ruby/object:Gem::Dependency
56
62
  name: http
57
63
  requirement: !ruby/object:Gem::Requirement
@@ -59,6 +65,9 @@ dependencies:
59
65
  - - ">="
60
66
  - !ruby/object:Gem::Version
61
67
  version: '3.2'
68
+ - - "<"
69
+ - !ruby/object:Gem::Version
70
+ version: '5'
62
71
  type: :runtime
63
72
  prerelease: false
64
73
  version_requirements: !ruby/object:Gem::Requirement
@@ -66,6 +75,9 @@ dependencies:
66
75
  - - ">="
67
76
  - !ruby/object:Gem::Version
68
77
  version: '3.2'
78
+ - - "<"
79
+ - !ruby/object:Gem::Version
80
+ version: '5'
69
81
  - !ruby/object:Gem::Dependency
70
82
  name: rake
71
83
  requirement: !ruby/object:Gem::Requirement