shrine-cloudinary 0.1.1 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 41dea0917908eed7217824a177712ab780ac44d8
4
- data.tar.gz: 6413d85af1e21214988f7cd5201dbf13d6a535fe
3
+ metadata.gz: b0a65cec872c9e53cd6e760407333608c4ab7cef
4
+ data.tar.gz: 4bee964dc688c03e8065ab278d14a248a799eed0
5
5
  SHA512:
6
- metadata.gz: a39bd818d6e1a60bcbaf5d5d8a8d6253e2ce8d924bdd228efc5fdc5150ebf78594343ab23fa9d9f11632ef5050b8be7c120eb8fbc6835c53645e55d371f9316e
7
- data.tar.gz: 727ad30490548dd17429afa49b0e1528b4d3d2bbaeb89a5a18a0ae52cb0baee1f4163f56693cf150f63dbc2968c29679bc1511741835e26e3794887b976d8c21
6
+ metadata.gz: 0c8a99b7cf9e1ed644715101652a50ed97d5e3c61b1a86cece3b16b0649ad8d4506a68762685e2ab3b02fd3a5b592012358f8c7754d062e2ad2bd7e32ecee922
7
+ data.tar.gz: b0fb81334fb1f5bc14862cb4a67c283f1a7de3a2af5d79fefed864a4cc60dc6da494c0196e57af69b849f1e92a13833a8b7408e2529f759fe68ada6f9b520959
data/README.md CHANGED
@@ -125,9 +125,37 @@ Shrine::Storage::Cloudinary.new(
125
125
 
126
126
  ### Metadata
127
127
 
128
- If you decide to do incoming transformations (processing which is done on the
129
- main file before it is saved to Cloudinary), shrine-cloudinary will automatically
130
- update format, size, width and height metadata for the uploaded file.
128
+ If you decide to do incoming transformations (processing on upload),
129
+ shrine-cloudinary will automatically update the extension, size, MIME type,
130
+ width and height metadata for the uploaded file.
131
+
132
+ You can choose to save the whole Cloudinary response to metadata by setting
133
+ `:store_data` to true:
134
+
135
+ ```rb
136
+ Shrine::Storage::Cloudinary.new(store_data: true, **cloudinary_options)
137
+ ```
138
+ ```rb
139
+ user = User.create(avatar: image_file)
140
+ user.avatar.metadata["cloudinary"] #=>
141
+ # {
142
+ # "public_id" => "foo",
143
+ # "version" => 1450294102,
144
+ # "signature" => "379ab45c743951abaea38d6a18ee631af599763f",
145
+ # "width" => 100,
146
+ # "height" => 67,
147
+ # "format" => "jpg",
148
+ # "resource_type" => "image",
149
+ # "created_at" => "2015-12-16T19:28:22Z",
150
+ # "tags" => [],
151
+ # "bytes" => 6147,
152
+ # "type" => "upload",
153
+ # "etag" => "54b5d33d07b1dc4084d7694825371cd7",
154
+ # "url" => "http://res.cloudinary.com/dkjm0biaa/image/upload/v14502\n94102/foo.jpg",
155
+ # "secure_url" => "https://res.cloudinary.com/dkjm0biaa/image/upload/v1450294102/foo.jpg",
156
+ # "original_filename" => "image"
157
+ # }
158
+ ```
131
159
 
132
160
  ### Clearing storage
133
161
 
@@ -6,11 +6,12 @@ class Shrine
6
6
  class Cloudinary
7
7
  attr_reader :prefix, :resource_type, :upload_options
8
8
 
9
- def initialize(prefix: nil, large: nil, resource_type: "image", upload_options: {})
9
+ def initialize(prefix: nil, large: nil, resource_type: "image", store_data: nil, upload_options: {})
10
10
  @prefix = prefix
11
11
  @large = large
12
12
  @resource_type = resource_type
13
13
  @upload_options = upload_options.merge(resource_type: resource_type)
14
+ @store_data = store_data
14
15
  end
15
16
 
16
17
  def upload(io, id, metadata = {})
@@ -112,20 +113,52 @@ class Shrine
112
113
  end
113
114
 
114
115
  def update_id!(result, id)
115
- unless resource_type == "raw" || id.frozen?
116
- id.gsub!(/#{File.extname(id)}$/, ".#{result.fetch("format")}")
117
- end
116
+ id.gsub!(/#{File.extname(id)}$/, ".#{result.fetch("format")}") unless resource_type == "raw"
118
117
  end
119
118
 
120
119
  def update_metadata!(result, metadata)
121
- size, width, height = result.values_at("bytes", "width", "height")
122
- metadata.update("size" => size)
123
- metadata.update("width" => width, "height" => height) if resource_type == "image"
120
+ retrieved_metadata = {
121
+ "size" => result["bytes"],
122
+ "mime_type" => MIME_TYPES[result["format"]],
123
+ "width" => result["width"],
124
+ "height" => result["height"],
125
+ }
126
+ retrieved_metadata["cloudinary"] = result if @store_data
127
+ retrieved_metadata.reject! { |key, value| value.nil? }
128
+
129
+ metadata.update(retrieved_metadata)
124
130
  end
125
131
 
126
132
  [:Uploader, :Downloader, :Utils, :Api].each do |name|
127
133
  define_method(name.downcase) { ::Cloudinary.const_get(name) }
128
134
  end
135
+
136
+ MIME_TYPES = {
137
+ # Images
138
+ "jpg" => "image/jpeg",
139
+ "png" => "image/png",
140
+ "gif" => "image/gif",
141
+ "bmp" => "image/bmp",
142
+ "tiff" => "image/tiff",
143
+ "ico" => "image/x-icon",
144
+ "pdf" => "application/pdf",
145
+ "eps" => "application/postscript",
146
+ "psd" => "application/octet-stream",
147
+ "svg" => "image/svg+xml",
148
+ "webp" => "image/webp",
149
+
150
+ # Videos
151
+ "mp4" => "video/mp4",
152
+ "flv" => "video/x-flv",
153
+ "mov" => "video/quicktime",
154
+ "ogv" => "video/ogg",
155
+ "webm" => "video/webm",
156
+ "3gp" => "video/3gpp",
157
+ "3g2" => "video/3gpp2",
158
+ "wmv" => "video/x-ms-wmv",
159
+ "mpeg" => "video/mpeg",
160
+ "avi" => "video/x-msvideo",
161
+ }
129
162
  end
130
163
  end
131
164
  end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |gem|
2
2
  gem.name = "shrine-cloudinary"
3
- gem.version = "0.1.1"
3
+ gem.version = "0.2.0"
4
4
 
5
5
  gem.required_ruby_version = ">= 2.1"
6
6
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shrine-cloudinary
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.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: 2015-12-10 00:00:00.000000000 Z
11
+ date: 2015-12-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: cloudinary