shrine-cloudinary 0.1.0
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 +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +163 -0
- data/lib/shrine/storage/cloudinary.rb +128 -0
- data/shrine-cloudinary.gemspec +23 -0
- metadata +133 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b19b7d283d488e20abf633289dedc3d75ac9a7af
|
4
|
+
data.tar.gz: 46697c64a6bd24e8817980dc7e6fe751f116a98a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e0daa86740883310ba8da21250e453e5e749e10ee2eb355dcfe13e44f67a132295a6161308e0c50e1d93f439b0ab9f0b7ecab3515da017f8d1cc88eeb641ecfe
|
7
|
+
data.tar.gz: 947ead1c71d809c52bd4ee4fac40823acefd7176ee993f83f5bc45c50a904ef72027506a5fdbe7b04f19ac683b7eecb41479e00f1d6569c9f2a6b27c25c3c522
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Janko Marohnić
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,163 @@
|
|
1
|
+
# Shrine::Cloudinary
|
2
|
+
|
3
|
+
Provides [Cloudinary] storage for [Shrine].
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
gem "shrine-cloudinary"
|
9
|
+
```
|
10
|
+
|
11
|
+
## Usage
|
12
|
+
|
13
|
+
First you need to configure the Cloudinary gem:
|
14
|
+
|
15
|
+
```rb
|
16
|
+
require "cloudinary"
|
17
|
+
|
18
|
+
Cloudinary.config(
|
19
|
+
cloud_name: "...",
|
20
|
+
api_key: "...",
|
21
|
+
api_secret: "...",
|
22
|
+
)
|
23
|
+
|
24
|
+
|
25
|
+
You can now initialize your storage:
|
26
|
+
|
27
|
+
```rb
|
28
|
+
require "shrine/storage/cloudinary"
|
29
|
+
|
30
|
+
Shrine.storages[:store] = Shrine::Storage::Cloudinary.new
|
31
|
+
```
|
32
|
+
|
33
|
+
### Copying
|
34
|
+
|
35
|
+
If you're using storage as cache where files are accessible over internet,
|
36
|
+
moving the cached file to Cloudinary storage will not require another upload.
|
37
|
+
Instead only the file URL will be passed to Cloudinary, then Cloudinary will
|
38
|
+
internally download it and store the file.
|
39
|
+
|
40
|
+
### Videos and Raw
|
41
|
+
|
42
|
+
The storage defaults the resource type to "image", but you can change that
|
43
|
+
by passing the `:resource_type` option:
|
44
|
+
|
45
|
+
```rb
|
46
|
+
Shrine::Storage::Cloudinary.new(resource_type: "video") # "image", "video" or "raw"
|
47
|
+
```
|
48
|
+
|
49
|
+
### Subdirectory
|
50
|
+
|
51
|
+
You can choose to store your files in a subdirectory with the `:prefix` option:
|
52
|
+
|
53
|
+
```rb
|
54
|
+
Shrine::Storage::Cloudinary.new(prefix: "uploads")
|
55
|
+
```
|
56
|
+
|
57
|
+
### Upload options
|
58
|
+
|
59
|
+
If you want some [Cloudinary options] to be applied to all uploads, you can
|
60
|
+
specify `:upload_options`:
|
61
|
+
|
62
|
+
```rb
|
63
|
+
Shrine::Storage::Cloudinary.new(upload_options: {type: "authenticated"})
|
64
|
+
```
|
65
|
+
|
66
|
+
### Transformations
|
67
|
+
|
68
|
+
Cloudinary allows you to do incoming and eager file transformations, which means
|
69
|
+
it's possible to trigger file processing on upload. In Shrine you can leverage
|
70
|
+
that by adding `"cloudinary"` metadata, which you can do by overriding
|
71
|
+
`Shrine#extract_metadata`:
|
72
|
+
|
73
|
+
```rb
|
74
|
+
class MyUploader < Shrine
|
75
|
+
def extract_metadata(io, context)
|
76
|
+
metadata = super
|
77
|
+
metadata["cloudinary"] = {
|
78
|
+
format: "png",
|
79
|
+
eager: [
|
80
|
+
{transformation: "small"},
|
81
|
+
{transformation: "medium"},
|
82
|
+
{transformation: "large"},
|
83
|
+
]
|
84
|
+
}
|
85
|
+
metadata
|
86
|
+
end
|
87
|
+
end
|
88
|
+
```
|
89
|
+
|
90
|
+
The above example will trigger named transformations "small", "medium" and
|
91
|
+
"large", which can be configured in the Cloudinary management console. This
|
92
|
+
means that you can have different thumbnails of an image, and it's all taken
|
93
|
+
care of by Cloudinary. When displaying the URL the view, you just need to pass
|
94
|
+
in the version name to the uploaded file:
|
95
|
+
|
96
|
+
```rb
|
97
|
+
user.avatar_url(transformation: "small")
|
98
|
+
user.avatar_url(transformation: "medium")
|
99
|
+
user.avatar_url(transformation: "large")
|
100
|
+
```
|
101
|
+
|
102
|
+
### Large files
|
103
|
+
|
104
|
+
If you're uploading large files with Cloudinary (like videos), you can take
|
105
|
+
advantage of Cloudinary's special "chunked" upload API, by passing the filesize
|
106
|
+
threshold after which the special API will be used:
|
107
|
+
|
108
|
+
```rb
|
109
|
+
# Upload files larger than 100 MB using the "chunked" upload API
|
110
|
+
Shrine::Storage::Cloudinary.new(large: 100*1024*1024)
|
111
|
+
```
|
112
|
+
|
113
|
+
The default chunk size is 20 MB, but you can change that by passing
|
114
|
+
`:chunk_size` to `:upload_options`:
|
115
|
+
|
116
|
+
```rb
|
117
|
+
Shrine::Storage::Cloudinary.new(
|
118
|
+
large: 100*1024*1024 # 100 MB
|
119
|
+
upload_options: {chunk_size: 5*1024*1204} # 5 MB
|
120
|
+
)
|
121
|
+
```
|
122
|
+
|
123
|
+
### Metadata
|
124
|
+
|
125
|
+
If you decide to do incoming transformations (processing which is done on the
|
126
|
+
main file before it is saved to Cloudinary), shrine-cloudinary will automatically
|
127
|
+
update format, size, width and height metadata for the uploaded file.
|
128
|
+
|
129
|
+
### Clearing storage
|
130
|
+
|
131
|
+
You can delete all files from the Cloudinary storage in the same way as you do
|
132
|
+
with other storages:
|
133
|
+
|
134
|
+
```rb
|
135
|
+
cloudinary = Shrine::Storage::Cloudinary.new
|
136
|
+
# ...
|
137
|
+
cloudinary.clear!(:confirm)
|
138
|
+
```
|
139
|
+
|
140
|
+
## Contributing
|
141
|
+
|
142
|
+
Firstly you need to create an `.env` file with Cloudinary credentials:
|
143
|
+
|
144
|
+
```sh
|
145
|
+
# .env
|
146
|
+
CLOUDINARY_CLOUD_NAME="..."
|
147
|
+
CLOUDINARY_API_KEY="..."
|
148
|
+
CLOUDINARY_API_SECRET="..."
|
149
|
+
```
|
150
|
+
|
151
|
+
Afterwards you can run the tests:
|
152
|
+
|
153
|
+
```sh
|
154
|
+
$ bundle exec rake test
|
155
|
+
```
|
156
|
+
|
157
|
+
## License
|
158
|
+
|
159
|
+
[MIT](http://opensource.org/licenses/MIT)
|
160
|
+
|
161
|
+
[Cloudinary]: http://cloudinary.com/
|
162
|
+
[Shrine]: https://github.com/janko-m/shrine
|
163
|
+
[Cloudinary options]: http://cloudinary.com/documentation/upload_images#remote_upload
|
@@ -0,0 +1,128 @@
|
|
1
|
+
require "cloudinary"
|
2
|
+
require "down"
|
3
|
+
|
4
|
+
class Shrine
|
5
|
+
module Storage
|
6
|
+
class Cloudinary
|
7
|
+
attr_reader :prefix, :resource_type, :upload_options
|
8
|
+
|
9
|
+
def initialize(prefix: nil, large: nil, resource_type: "image", upload_options: {})
|
10
|
+
@prefix = prefix
|
11
|
+
@large = large
|
12
|
+
@resource_type = resource_type
|
13
|
+
@upload_options = upload_options.merge(resource_type: resource_type)
|
14
|
+
end
|
15
|
+
|
16
|
+
def upload(io, id, metadata = {})
|
17
|
+
options = {public_id: public_id(id)}
|
18
|
+
options.update(upload_options)
|
19
|
+
options.update(metadata.delete("cloudinary") || {})
|
20
|
+
|
21
|
+
chunk_size = options.delete(:chunk_size)
|
22
|
+
|
23
|
+
result =
|
24
|
+
if remote?(io)
|
25
|
+
uploader.upload(io.storage.url(io.id), **options)
|
26
|
+
elsif large?(io)
|
27
|
+
uploader.upload_large(io, chunk_size: chunk_size, **options)
|
28
|
+
else
|
29
|
+
uploader.upload(io, **options)
|
30
|
+
end
|
31
|
+
|
32
|
+
update_id!(result, id)
|
33
|
+
update_metadata!(result, metadata)
|
34
|
+
end
|
35
|
+
|
36
|
+
def download(id)
|
37
|
+
Down.download(url(id))
|
38
|
+
end
|
39
|
+
|
40
|
+
def move(io, id, metadata = {})
|
41
|
+
uploader.rename(io.storage.public_id(io.id), public_id(id), resource_type: resource_type)
|
42
|
+
end
|
43
|
+
|
44
|
+
def movable?(io, id)
|
45
|
+
io.is_a?(UploadedFile) && io.storage.is_a?(Storage::Cloudinary)
|
46
|
+
end
|
47
|
+
|
48
|
+
def open(id)
|
49
|
+
download(id)
|
50
|
+
end
|
51
|
+
|
52
|
+
def read(id)
|
53
|
+
downloader.download(url(id))
|
54
|
+
end
|
55
|
+
|
56
|
+
def exists?(id)
|
57
|
+
result = api.resources_by_ids([public_id(id)], resource_type: resource_type)
|
58
|
+
result.fetch("resources").any?
|
59
|
+
end
|
60
|
+
|
61
|
+
def delete(id)
|
62
|
+
uploader.destroy(public_id(id), resource_type: resource_type)
|
63
|
+
end
|
64
|
+
|
65
|
+
def multi_delete(ids)
|
66
|
+
public_ids = ids.map { |id| public_id(id) }
|
67
|
+
api.delete_resources(public_ids, resource_type: resource_type)
|
68
|
+
end
|
69
|
+
|
70
|
+
def url(id, **options)
|
71
|
+
utils.cloudinary_url(path(id), resource_type: resource_type, type: type, **options)
|
72
|
+
end
|
73
|
+
|
74
|
+
def clear!(confirm = nil, **options)
|
75
|
+
raise Shrine::Confirm unless confirm == :confirm
|
76
|
+
if prefix
|
77
|
+
api.delete_resources_by_prefix(prefix, resource_type: resource_type, **options)
|
78
|
+
else
|
79
|
+
api.delete_all_resources(resource_type: resource_type, **options)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
protected
|
84
|
+
|
85
|
+
def public_id(id)
|
86
|
+
if resource_type == "raw"
|
87
|
+
path(id)
|
88
|
+
else
|
89
|
+
path(id).chomp(File.extname(id))
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def path(id)
|
94
|
+
[*prefix, id].join("/")
|
95
|
+
end
|
96
|
+
|
97
|
+
private
|
98
|
+
|
99
|
+
def type
|
100
|
+
upload_options[:type] || "upload"
|
101
|
+
end
|
102
|
+
|
103
|
+
def remote?(io)
|
104
|
+
io.is_a?(UploadedFile) && io.storage.url(io.id) =~ /^ftp:|^https?:/
|
105
|
+
end
|
106
|
+
|
107
|
+
def large?(io)
|
108
|
+
io.size >= @large if @large
|
109
|
+
end
|
110
|
+
|
111
|
+
def update_id!(result, id)
|
112
|
+
unless resource_type == "raw" || id.frozen?
|
113
|
+
id.gsub!(/#{File.extname(id)}$/, ".#{result.fetch("format")}")
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
def update_metadata!(result, metadata)
|
118
|
+
size, width, height = result.values_at("bytes", "width", "height")
|
119
|
+
metadata.update("size" => size)
|
120
|
+
metadata.update("width" => width, "height" => height) if resource_type == "image"
|
121
|
+
end
|
122
|
+
|
123
|
+
[:Uploader, :Downloader, :Utils, :Api].each do |name|
|
124
|
+
define_method(name.downcase) { ::Cloudinary.const_get(name) }
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
Gem::Specification.new do |gem|
|
2
|
+
gem.name = "shrine-cloudinary"
|
3
|
+
gem.version = "0.1.0"
|
4
|
+
|
5
|
+
gem.required_ruby_version = ">= 2.1"
|
6
|
+
|
7
|
+
gem.summary = "Provides Cloudinary storage for Shrine."
|
8
|
+
gem.homepage = "https://github.com/janko-m/shrine-cloudinary"
|
9
|
+
gem.authors = ["Janko Marohnić"]
|
10
|
+
gem.email = ["janko.marohnic@gmail.com"]
|
11
|
+
gem.license = "MIT"
|
12
|
+
|
13
|
+
gem.files = Dir["README.md", "LICENSE.txt", "lib/**/*.rb", "shrine-cloudinary.gemspec"]
|
14
|
+
gem.require_path = "lib"
|
15
|
+
|
16
|
+
gem.add_dependency "cloudinary"
|
17
|
+
gem.add_dependency "down", ">= 1.0.3"
|
18
|
+
|
19
|
+
gem.add_development_dependency "rake"
|
20
|
+
gem.add_development_dependency "shrine"
|
21
|
+
gem.add_development_dependency "minitest"
|
22
|
+
gem.add_development_dependency "dotenv"
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,133 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: shrine-cloudinary
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Janko Marohnić
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-12-09 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: cloudinary
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: down
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.0.3
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 1.0.3
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: shrine
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: minitest
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: dotenv
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description:
|
98
|
+
email:
|
99
|
+
- janko.marohnic@gmail.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- LICENSE.txt
|
105
|
+
- README.md
|
106
|
+
- lib/shrine/storage/cloudinary.rb
|
107
|
+
- shrine-cloudinary.gemspec
|
108
|
+
homepage: https://github.com/janko-m/shrine-cloudinary
|
109
|
+
licenses:
|
110
|
+
- MIT
|
111
|
+
metadata: {}
|
112
|
+
post_install_message:
|
113
|
+
rdoc_options: []
|
114
|
+
require_paths:
|
115
|
+
- lib
|
116
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
117
|
+
requirements:
|
118
|
+
- - ">="
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: '2.1'
|
121
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
122
|
+
requirements:
|
123
|
+
- - ">="
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
126
|
+
requirements: []
|
127
|
+
rubyforge_project:
|
128
|
+
rubygems_version: 2.4.5
|
129
|
+
signing_key:
|
130
|
+
specification_version: 4
|
131
|
+
summary: Provides Cloudinary storage for Shrine.
|
132
|
+
test_files: []
|
133
|
+
has_rdoc:
|