cloudinary 1.0.33 → 1.0.34

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.
data/lib/cloudinary.rb CHANGED
@@ -9,6 +9,7 @@ require "cloudinary/uploader"
9
9
  require "cloudinary/api"
10
10
  require "cloudinary/downloader"
11
11
  require "cloudinary/blob"
12
+ require "cloudinary/preloaded_file"
12
13
  require "cloudinary/static"
13
14
  require "cloudinary/missing"
14
15
  require "cloudinary/carrier_wave" if defined?(::CarrierWave)
@@ -3,7 +3,7 @@
3
3
  # Field value must be in the format: "image/upload/v<version>/#<public_id>.<format>#<signature>"
4
4
  # Where signature is the cloduinary API signature on the public_id and version.
5
5
  module Cloudinary::CarrierWave
6
- PRELOADED_CLOUDINARY_PATH = /^([^\/]+)\/upload\/v(\d+)\/([^\/]+)#([^\/]+)$/
6
+ PRELOADED_CLOUDINARY_PATH = Cloudinary::PreloadedFile::PRELOADED_CLOUDINARY_PATH
7
7
 
8
8
  def cache!(new_file)
9
9
  if new_file.is_a?(String) && new_file.match(PRELOADED_CLOUDINARY_PATH)
@@ -35,28 +35,21 @@ module Cloudinary::CarrierWave
35
35
  return @file.is_a?(PreloadedCloudinaryFile) ? @file.to_s : super
36
36
  end
37
37
 
38
- class PreloadedCloudinaryFile
39
- attr_reader :original_filename, :version, :public_id, :signature
38
+ class PreloadedCloudinaryFile < Cloudinary::PreloadedFile
40
39
  def initialize(file_info)
41
- resource_type, @version, @original_filename, @signature = file_info.scan(PRELOADED_CLOUDINARY_PATH).first
42
- raise "Cloudinary CarrierWave integration supports images only" if resource_type != "image"
43
- @public_id = @original_filename[0..(@original_filename.rindex(".")-1)]
44
- expected_signature = Cloudinary::Utils.api_sign_request({:public_id=>public_id, :version=>version}, Cloudinary.config.api_secret)
45
- if @signature != expected_signature
40
+ super
41
+ raise "Cloudinary CarrierWave integration only supports uploaded images" if resource_type != "image" || type != "upload"
42
+ if !valid?
46
43
  raise CarrierWave::IntegrityError, I18n.translate(:"errors.messages.cloudinary_signature_error", :public_id=>public_id, :default=>"Invalid signature for #{public_id}")
47
- end
48
- end
49
-
50
- def identifier
51
- "v#{version}/#{original_filename}"
52
- end
53
-
54
- def to_s
55
- "image/upload/v#{version}/#{original_filename}##{signature}"
56
- end
44
+ end
45
+ end
57
46
 
58
47
  def delete
59
48
  # Do nothing. This is a virtual file.
60
49
  end
61
- end
50
+
51
+ def original_filename
52
+ self.filename
53
+ end
54
+ end
62
55
  end
@@ -0,0 +1,23 @@
1
+ class Cloudinary::PreloadedFile
2
+ PRELOADED_CLOUDINARY_PATH = /^([^\/]+)\/([^\/]+)\/v(\d+)\/([^\/]+)#([^\/]+)$/
3
+
4
+ attr_reader :filename, :version, :public_id, :signature, :resource_type, :type
5
+ def initialize(file_info)
6
+ @resource_type, @type, @version, @filename, @signature = file_info.scan(PRELOADED_CLOUDINARY_PATH).first
7
+ @public_id = @resource_type == "image" ? @filename[0..(@filename.rindex(".")-1)] : @filename
8
+ end
9
+
10
+ def valid?
11
+ expected_signature = Cloudinary::Utils.api_sign_request({:public_id=>public_id, :version=>version}, Cloudinary.config.api_secret)
12
+ @signature == expected_signature
13
+ end
14
+
15
+ def identifier
16
+ "v#{version}/#{filename}"
17
+ end
18
+
19
+ def to_s
20
+ "#{resource_type}/#{type}/v#{version}/#{filename}##{signature}"
21
+ end
22
+
23
+ end
@@ -21,11 +21,14 @@ class Cloudinary::Utils
21
21
  width = options[:width]
22
22
  height = options[:height]
23
23
  has_layer = !options[:overlay].blank? || !options[:underlay].blank?
24
-
25
- options.delete(:width) if width && (width.to_f < 1 || has_layer)
26
- options.delete(:height) if height && (height.to_f < 1 || has_layer)
27
-
24
+
28
25
  crop = options.delete(:crop)
26
+ angle = build_array(options.delete(:angle)).join(".")
27
+
28
+ no_html_sizes = has_layer || !angle.blank? || crop.to_s == "fit" || crop.to_s == "limit"
29
+ options.delete(:width) if width && (width.to_f < 1 || no_html_sizes)
30
+ options.delete(:height) if height && (height.to_f < 1 || no_html_sizes)
31
+
29
32
  width=height=nil if crop.nil? && !has_layer
30
33
 
31
34
  background = options.delete(:background)
@@ -45,8 +48,6 @@ class Cloudinary::Utils
45
48
  effect = options.delete(:effect)
46
49
  effect = Array(effect).flatten.join(":") if effect.is_a?(Array) || effect.is_a?(Hash)
47
50
 
48
- angle = build_array(options.delete(:angle)).join(".")
49
-
50
51
  params = {:w=>width, :h=>height, :t=>named_transformation, :c=>crop, :b=>background, :e=>effect, :a=>angle}
51
52
  { :x=>:x, :y=>:y, :r=>:radius, :d=>:default_image, :g=>:gravity, :q=>:quality,
52
53
  :p=>:prefix, :l=>:overlay, :u=>:underlay, :f=>:fetch_format, :dn=>:density, :pg=>:page
@@ -1,4 +1,4 @@
1
1
  # Copyright Cloudinary
2
2
  module Cloudinary
3
- VERSION = "1.0.33"
3
+ VERSION = "1.0.34"
4
4
  end
data/spec/utils_spec.rb CHANGED
@@ -68,7 +68,25 @@ describe Cloudinary::Utils do
68
68
  options.should == {:width=>100, :height=>100}
69
69
  result.should == "http://res.cloudinary.com/test123/image/upload/c_crop,h_100,w_100/test"
70
70
  end
71
-
71
+
72
+ it "should not pass width and height to html in case of fit or limit crop" do
73
+ options = {:width=>100, :height=>100, :crop=>:limit}
74
+ result = Cloudinary::Utils.cloudinary_url("test", options)
75
+ options.should == {}
76
+ result.should == "http://res.cloudinary.com/test123/image/upload/c_limit,h_100,w_100/test"
77
+ options = {:width=>100, :height=>100, :crop=>:fit}
78
+ result = Cloudinary::Utils.cloudinary_url("test", options)
79
+ options.should == {}
80
+ result.should == "http://res.cloudinary.com/test123/image/upload/c_fit,h_100,w_100/test"
81
+ end
82
+
83
+ it "should not pass width and height to html in case angle was used" do
84
+ options = {:width=>100, :height=>100, :crop=>:scale, :angle=>:auto}
85
+ result = Cloudinary::Utils.cloudinary_url("test", options)
86
+ options.should == {}
87
+ result.should == "http://res.cloudinary.com/test123/image/upload/a_auto,c_scale,h_100,w_100/test"
88
+ end
89
+
72
90
  it "should use x, y, radius, prefix, gravity and quality from options" do
73
91
  options = {:x=>1, :y=>2, :radius=>3, :gravity=>:center, :quality=>0.4, :prefix=>"a"}
74
92
  result = Cloudinary::Utils.cloudinary_url("test", options)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cloudinary
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.33
4
+ version: 1.0.34
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2012-08-05 00:00:00.000000000 Z
14
+ date: 2012-08-27 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: rest-client
@@ -91,6 +91,7 @@ files:
91
91
  - lib/cloudinary/helper.rb
92
92
  - lib/cloudinary/migrator.rb
93
93
  - lib/cloudinary/missing.rb
94
+ - lib/cloudinary/preloaded_file.rb
94
95
  - lib/cloudinary/railtie.rb
95
96
  - lib/cloudinary/static.rb
96
97
  - lib/cloudinary/uploader.rb