Module: Cloudinary::CarrierWave

Defined in:
lib/cloudinary/carrier_wave/preloaded.rb,
lib/cloudinary/carrier_wave.rb,
lib/cloudinary/carrier_wave/error.rb,
lib/cloudinary/carrier_wave/remote.rb,
lib/cloudinary/carrier_wave/process.rb

Overview

Copyright Cloudinary Support for store in CarrierWave files that were preloaded to cloudinary (e.g., by javascript) Field value must be in the format: “image/upload/v<version>/#<public_id>.<format>#<signature>” Where signature is the cloduinary API signature on the public_id and version.

Defined Under Namespace

Modules: ClassMethods Classes: CloudinaryFile, PreloadedCloudinaryFile, RemoteFile, Storage, UploadError

Constant Summary collapse

SANITIZE_REGEXP =
CarrierWave::SanitizedFile.respond_to?(:sanitize_regexp) ? CarrierWave::SanitizedFile.sanitize_regexp : /[^a-zA-Z0-9\.\-\+_]/
PRELOADED_CLOUDINARY_PATH =
Cloudinary::PreloadedFile::PRELOADED_CLOUDINARY_PATH

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/cloudinary/carrier_wave.rb', line 10

def self.included(base)
  base.storage Cloudinary::CarrierWave::Storage
  base.extend ClassMethods
  base.class_attribute :storage_type, :metadata
  base.send(:attr_reader, :stored_version)
  override_in_versions(base, :blank?, :full_public_id, :all_versions_processors)
end

.override_in_versions(base, *methods) ⇒ Object

For the given methods - versions should call the main uploader method



183
184
185
186
187
188
189
190
191
192
# File 'lib/cloudinary/carrier_wave.rb', line 183

def self.override_in_versions(base, *methods)
  methods.each do
    |method|
    base.send :define_method, method do
      return super() if self.version_name.blank?
      uploader = self.model.send(self.mounted_as)
      uploader.send(method)    
    end
  end    
end

.split_format(identifier) ⇒ Object



174
175
176
177
178
179
180
# File 'lib/cloudinary/carrier_wave.rb', line 174

def self.split_format(identifier)
  last_dot = identifier.rindex(".")
  return [identifier, nil] if last_dot.nil?
  public_id = identifier[0, last_dot]
  format = identifier[last_dot+1..-1]
  return [public_id, format]    
end

Instance Method Details

#all_processorsObject



116
117
118
# File 'lib/cloudinary/carrier_wave/process.rb', line 116

def all_processors    
  (self.is_main_uploader? ? [] : all_versions_processors) + self.class.processors
end

#all_versions_processorsObject



110
111
112
113
114
# File 'lib/cloudinary/carrier_wave/process.rb', line 110

def all_versions_processors
  all_versions = self.class.instance_variable_get('@all_versions')
  
  all_versions ? all_versions.processors : []
end

#auto_rename_preloaded?Boolean

Rename preloaded uploads if public_id was overridden

Returns:

  • (Boolean)


135
136
137
# File 'lib/cloudinary/carrier_wave.rb', line 135

def auto_rename_preloaded?
  true
end

#cache!(new_file) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/cloudinary/carrier_wave/preloaded.rb', line 8

def cache!(new_file)
  if new_file.is_a?(String) && new_file.match(PRELOADED_CLOUDINARY_PATH)
    @file = PreloadedCloudinaryFile.new(new_file)
    @stored_version = @file.version
    @public_id = @stored_public_id = @file.public_id
    self.original_filename = sanitize(@file.original_filename)
    @cache_id = "unused" # must not be blank 

  else
    super
    @public_id = nil # allow overriding public_id

  end
end

#cache_nameObject



34
35
36
# File 'lib/cloudinary/carrier_wave/preloaded.rb', line 34

def cache_name
  return @file.is_a?(PreloadedCloudinaryFile) ? @file.to_s : super
end

#cache_versions!(new_file = nil) ⇒ Object



110
111
112
# File 'lib/cloudinary/carrier_wave.rb', line 110

def cache_versions!(new_file=nil)
  # Do nothing

end

#cloudinary_should_handle_remote?Boolean

Let Cloudinary download remote URLs directly

Returns:

  • (Boolean)


130
131
132
# File 'lib/cloudinary/carrier_wave.rb', line 130

def cloudinary_should_handle_remote?
  true
end

#default_public_idObject

default public_id to use if no uploaded file. Override with public_id of an uploaded image if you want a default image.



66
67
68
# File 'lib/cloudinary/carrier_wave.rb', line 66

def default_public_id
  nil
end

#delete_remote?Boolean

Should removed files be removed from Cloudinary as well. Can be overridden.

Returns:

  • (Boolean)


125
126
127
# File 'lib/cloudinary/carrier_wave.rb', line 125

def delete_remote?
  true
end

#download!(uri) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
# File 'lib/cloudinary/carrier_wave/remote.rb', line 2

def download!(uri)
  return super if !self.cloudinary_should_handle_remote?
  if respond_to?(:process_uri)
    uri = process_uri(uri)
  else # Backward compatibility with old CarrierWave

    uri = URI.parse(URI.escape(URI.unescape(uri)))
  end
  return if uri.to_s.blank?
  self.original_filename = @cache_id = @filename = File.basename(uri.path).gsub(/[^a-zA-Z0-9\.\-\+_]/, '')
  @file = RemoteFile.new(uri, @filename)
end

#eagerObject



120
121
122
# File 'lib/cloudinary/carrier_wave/process.rb', line 120

def eager
  @eager ||= self.all_processors.any?{|processor| processor[0] == :eager}
end

#filenameObject



60
61
62
63
# File 'lib/cloudinary/carrier_wave.rb', line 60

def filename
  return nil if self.blank?
  return [self.full_public_id, self.format].join(".")
end

#formatObject



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/cloudinary/carrier_wave/process.rb', line 130

def format
  format_processor = self.all_processors.find{|processor| processor[0] == :convert}
  if format_processor 
    # Explicit format is given

    format = Array(format_processor[1]).first 
  elsif self.version_name.present? 
    # No local format. The reset should be handled by main uploader

    uploader = self.model.send(self.mounted_as)
    format = uploader.format
  else
    # Try to auto-detect format

    format = Cloudinary::CarrierWave.split_format(original_filename || "").last
    format ||= "png" # TODO Default format? 

  end
  format = format.to_s.downcase
  Cloudinary::FORMAT_ALIASES[format] || format
end

#full_public_idObject



54
55
56
57
58
# File 'lib/cloudinary/carrier_wave.rb', line 54

def full_public_id
  return nil if self.blank?
  return self.my_public_id if self.stored_version.blank?
  return "v#{self.stored_version}/#{self.my_public_id}"
end

#is_main_uploader?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/cloudinary/carrier_wave.rb', line 18

def is_main_uploader?
  self.class.version_names.blank?
end

#my_public_idObject

If the user overrode public_id, that should be used, even if it’s different from current public_id in the database. Otherwise, try to use public_id from the database. Otherwise, generate a new random public_id



78
79
80
81
82
# File 'lib/cloudinary/carrier_wave.rb', line 78

def my_public_id
  @public_id ||= self.public_id 
  @public_id ||= @stored_public_id
  @public_id ||= Cloudinary::Utils.random_public_id
end

#process!(new_file = nil) ⇒ Object



114
115
116
# File 'lib/cloudinary/carrier_wave.rb', line 114

def process!(new_file=nil)
  # Do nothing

end

#public_idObject

public_id to use for uploaded file. Can be overridden by caller. Random public_id will be used otherwise.



71
72
73
# File 'lib/cloudinary/carrier_wave.rb', line 71

def public_id
  nil
end

#recreate_versions!Object



106
107
108
# File 'lib/cloudinary/carrier_wave.rb', line 106

def recreate_versions!
  # Do nothing

end

#rename(to_public_id = nil, overwrite = false) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/cloudinary/carrier_wave.rb', line 84

def rename(to_public_id = nil, overwrite=false)
  public_id_overwrite = self.public_id 
  to_public_id ||= public_id_overwrite
  if public_id_overwrite && to_public_id != public_id_overwrite 
    raise CloudinaryException, "The public_id method was overridden and returns #{public_id_overwrite} - can't rename to #{to_public_id}"
  elsif to_public_id.nil?
    raise CloudinaryException, "No to_public_id given"
  end
   
  from_public_id = @stored_public_id || self.my_public_id
  return if from_public_id == to_public_id
  
  @public_id = @stored_public_id = to_public_id
  resource_type = Cloudinary::Utils.resource_type_for_format(self.format)
  if resource_type == 'raw'
    from_public_id = [from_public_id, self.format].join(".") 
    to_public_id = [to_public_id, self.format].join(".")
  end
  Cloudinary::Uploader.rename(from_public_id, to_public_id, :type=>self.storage_type, :resource_type=>resource_type, :overwrite=>overwrite)
  storage.store_cloudinary_identifier(@stored_version, [@public_id, self.format].join("."))
end

#retrieve_from_cache!(new_file) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/cloudinary/carrier_wave/preloaded.rb', line 21

def retrieve_from_cache!(new_file)
  if new_file.is_a?(String) && new_file.match(PRELOADED_CLOUDINARY_PATH)
    @file = PreloadedCloudinaryFile.new(new_file)
    @stored_version = @file.version
    @public_id = @stored_public_id = @file.public_id
    self.original_filename = sanitize(@file.original_filename)
    @cache_id = "unused" # must not be blank 

  else
    super
    @public_id = nil # allow overriding public_id

  end
end

#retrieve_from_store!(identifier) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/cloudinary/carrier_wave.rb', line 22

def retrieve_from_store!(identifier)
  if identifier.blank?
    @file = @stored_version = @stored_public_id = nil
    self.original_filename = nil
  else
    @file = CloudinaryFile.new(identifier, self)
    @public_id = @stored_public_id = @file.public_id
    @stored_version = @file.version
    self.original_filename = sanitize(@file.filename)
  end
end

#sanitize(filename) ⇒ Object



119
120
121
122
# File 'lib/cloudinary/carrier_wave.rb', line 119

def sanitize(filename)
  return nil if filename.nil?
  filename.gsub(SANITIZE_REGEXP, '_')
end

#set_or_yell(hash, attr, value) ⇒ Object



53
54
55
56
# File 'lib/cloudinary/carrier_wave/process.rb', line 53

def set_or_yell(hash, attr, value)
  raise CloudinaryException, "conflicting transformation on #{attr} #{value}!=#{hash[attr]}" if hash[attr] && hash[attr] != value
  hash[attr] = value
end

#tagsObject



124
125
126
127
128
# File 'lib/cloudinary/carrier_wave/process.rb', line 124

def tags
  @tags ||= self.all_processors.select{|processor| processor[0] == :tags}.map(&:second).first
  raise CloudinaryException, "tags cannot be used in versions." if @tags.present? && self.version_name.present?
  @tags
end

#transformationObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/cloudinary/carrier_wave/process.rb', line 58

def transformation
  return @transformation if @transformation
  @transformation = {}
  self.all_processors.each do
    |name, args|
    case name
    when :convert # Do nothing. This is handled by format

    when :resize_to_limit 
      set_or_yell(@transformation, :width, args[0])    
      set_or_yell(@transformation, :height, args[1])
      set_or_yell(@transformation, :crop, :limit)
    when :resize_to_fit 
      set_or_yell(@transformation, :width, args[0])    
      set_or_yell(@transformation, :height, args[1])
      set_or_yell(@transformation, :crop, :fit)
    when :resize_to_fill
      set_or_yell(@transformation, :width, args[0])    
      set_or_yell(@transformation, :height, args[1])
      set_or_yell(@transformation, :gravity, args[2].to_s.downcase)
      set_or_yell(@transformation, :crop, :fill)
    when :resize_and_pad
      set_or_yell(@transformation, :width, args[0])    
      set_or_yell(@transformation, :height, args[1])
      set_or_yell(@transformation, :background, args[2].to_s.downcase)
      set_or_yell(@transformation, :gravity, args[3].to_s.downcase)
      set_or_yell(@transformation, :crop, :pad)
    when :scale 
      set_or_yell(@transformation, :width, args[0])    
      set_or_yell(@transformation, :height, args[1])
      set_or_yell(@transformation, :crop, :scale)
    when :crop
      set_or_yell(@transformation, :width, args[0])
      set_or_yell(@transformation, :height, args[1])
      set_or_yell(@transformation, :gravity, args[2].to_s.downcase)
      set_or_yell(@transformation, :crop, :crop)
    when :cloudinary_transformation
      args.each do
        |attr, value|        
        set_or_yell(@transformation, attr, value)
      end
    else
      if args.blank?
        send(name).each do
          |attr, value|        
          set_or_yell(@transformation, attr, value)
        end
      end
    end
  end
  @transformation     
end

#url(*args) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/cloudinary/carrier_wave.rb', line 34

def url(*args)
  if args.first && !args.first.is_a?(Hash)
    super
  else
    if self.blank?
      url = self.default_url
      return url if !url.blank?
      public_id = self.default_public_id
      return nil if public_id.nil?
    else
      public_id = self.full_public_id
    end      
    options = args.extract_options!
    options = self.transformation.merge(options) if self.version_name.present?
    
    resource_type = Cloudinary::Utils.resource_type_for_format(self.filename || self.format)
    Cloudinary::Utils.cloudinary_url(public_id, {:format=>self.format, :resource_type=>resource_type, :type=>self.storage_type}.merge(options))
  end
end