cloudinary 1.0.49 → 1.0.50

Sign up to get free protection for your applications and to get access to all the features.
@@ -81,11 +81,19 @@ module Cloudinary::CarrierWave
81
81
  @public_id ||= Cloudinary::Utils.random_public_id
82
82
  end
83
83
 
84
- def rename(to_public_id)
85
- raise "rename cannot be used if the public_id method is overridden" if self.public_id
86
- resource_type = Cloudinary::Utils.resource_type_for_format(self.filename || self.format)
87
- from_public_id = self.my_public_id
84
+ def rename(to_public_id = nil)
85
+ to_public_id ||= self.public_id
86
+ if self.public_id && to_public_id != self.public_id
87
+ raise CloudinaryException, "The public_id method was overridden and returns #{self.public_id} - can't rename to #{to_public_id}"
88
+ elsif to_public_id.nil?
89
+ raise CloudinaryException, "No to_public_id given"
90
+ end
91
+
92
+ from_public_id = @stored_public_id || self.my_public_id
93
+ return if from_public_id == to_public_id
94
+
88
95
  @public_id = @stored_public_id = to_public_id
96
+ resource_type = Cloudinary::Utils.resource_type_for_format(self.format)
89
97
  if resource_type == 'raw'
90
98
  from_public_id = [from_public_id, self.format].join(".")
91
99
  to_public_id = [to_public_id, self.format].join(".")
@@ -122,6 +130,11 @@ module Cloudinary::CarrierWave
122
130
  true
123
131
  end
124
132
 
133
+ # Rename preloaded uploads if public_id was overridden
134
+ def auto_rename_preloaded?
135
+ true
136
+ end
137
+
125
138
  class CloudinaryFile
126
139
  attr_reader :identifier, :public_id, :filename, :format, :version, :storage_type, :resource_type
127
140
  def initialize(identifier, uploader)
@@ -129,7 +142,7 @@ module Cloudinary::CarrierWave
129
142
  @identifier = identifier
130
143
 
131
144
  if @identifier.include?("/")
132
- version, @filename = @identifier.split("/")
145
+ version, @filename = @identifier.split("/", 2)
133
146
  @version = version[1..-1] # remove 'v' prefix
134
147
  else
135
148
  @filename = @identifier
@@ -4,8 +4,14 @@ class Cloudinary::CarrierWave::Storage < ::CarrierWave::Storage::Abstract
4
4
  return if !uploader.enable_processing
5
5
  if uploader.is_main_uploader?
6
6
  case file
7
- when Cloudinary::CarrierWave::PreloadedCloudinaryFile
8
- return store_cloudinary_version(file.version)
7
+ when Cloudinary::CarrierWave::PreloadedCloudinaryFile
8
+ if uploader.public_id && uploader.auto_rename_preloaded?
9
+ @stored_version = file.version
10
+ uploader.rename
11
+ else
12
+ store_cloudinary_version(file.version)
13
+ end
14
+ return
9
15
  when Cloudinary::CarrierWave::CloudinaryFile
10
16
  return nil # Nothing to do
11
17
  when Cloudinary::CarrierWave::RemoteFile
@@ -43,7 +49,7 @@ class Cloudinary::CarrierWave::Storage < ::CarrierWave::Storage::Abstract
43
49
  end
44
50
 
45
51
  def store_cloudinary_version(version)
46
- name = "v#{version}/#{identifier.split("/").last}"
52
+ name = "v#{version}/#{identifier.split("/", 2).last}"
47
53
  model_class = uploader.model.class
48
54
  column = uploader.model.send(:_mounter, uploader.mounted_as).send(:serialization_column)
49
55
  if defined?(ActiveRecord::Base) && uploader.model.is_a?(ActiveRecord::Base)
@@ -100,6 +100,7 @@ class Cloudinary::Utils
100
100
  private_cdn = config_option_consume(options, :private_cdn)
101
101
  secure_distribution = config_option_consume(options, :secure_distribution)
102
102
  cname = config_option_consume(options, :cname)
103
+ shorten = config_option_consume(options, :shorten)
103
104
  force_remote = options.delete(:force_remote)
104
105
  cdn_subdomain = config_option_consume(options, :cdn_subdomain)
105
106
 
@@ -151,6 +152,11 @@ class Cloudinary::Utils
151
152
  prefix += "/#{cloud_name}" if !private_cdn || (secure && secure_distribution == Cloudinary::AKAMAI_SHARED_CDN)
152
153
  end
153
154
 
155
+ if shorten && resource_type.to_s == "image" && type.to_s == "upload"
156
+ resource_type = "iu"
157
+ type = nil
158
+ end
159
+ version ||= 1 if source.include?("/") and type != :fetch && !source.match(/^v[0-9]+/)
154
160
  source = prefix + "/" + [resource_type,
155
161
  type, transformation, version ? "v#{version}" : nil,
156
162
  source].reject(&:blank?).join("/").gsub(%r(([^:])//), '\1/')
@@ -1,4 +1,4 @@
1
1
  # Copyright Cloudinary
2
2
  module Cloudinary
3
- VERSION = "1.0.49"
3
+ VERSION = "1.0.50"
4
4
  end
data/spec/utils_spec.rb CHANGED
@@ -390,4 +390,21 @@ describe Cloudinary::Utils do
390
390
  Cloudinary::Uploader.build_upload_params(:backup=>nil)[:backup].should be_nil
391
391
  Cloudinary::Uploader.build_upload_params({})[:backup].should be_nil
392
392
  end
393
+
394
+ it "should add version if public_id contains /" do
395
+ result = Cloudinary::Utils.cloudinary_url("folder/test")
396
+ result.should == "http://res.cloudinary.com/test123/image/upload/v1/folder/test"
397
+ result = Cloudinary::Utils.cloudinary_url("folder/test", :version=>123)
398
+ result.should == "http://res.cloudinary.com/test123/image/upload/v123/folder/test"
399
+ end
400
+
401
+ it "should not add version if public_id contains version already" do
402
+ result = Cloudinary::Utils.cloudinary_url("v1234/test")
403
+ result.should == "http://res.cloudinary.com/test123/image/upload/v1234/test"
404
+ end
405
+
406
+ it "should allow to shorted image/upload urls" do
407
+ result = Cloudinary::Utils.cloudinary_url("test", :shorten=>true)
408
+ result.should == "http://res.cloudinary.com/test123/iu/test"
409
+ end
393
410
  end
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.49
4
+ version: 1.0.50
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: 2013-03-21 00:00:00.000000000 Z
14
+ date: 2013-03-24 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: rest-client