Class: Cloudinary::Uploader

Inherits:
Object show all
Defined in:
lib/cloudinary/uploader.rb

Constant Summary collapse

TEXT_PARAMS =
[:public_id, :font_family, :font_size, :font_color, :text_align, :font_weight, :font_style, :background, :opacity, :text_decoration]

Class Method Summary collapse

Class Method Details

.add_tag(tag, public_ids = [], options = {}) ⇒ Object

options may include ‘exclusive’ (boolean) which causes clearing this tag from all other resources



157
158
159
160
161
# File 'lib/cloudinary/uploader.rb', line 157

def self.add_tag(tag, public_ids = [], options = {})
  exclusive = options.delete(:exclusive)
  command = exclusive ? "set_exclusive" : "add"
  return self.call_tags_api(tag, command, public_ids, options)    
end

.build_eager(eager) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/cloudinary/uploader.rb', line 7

def self.build_eager(eager)
  return nil if eager.nil?
  Cloudinary::Utils.build_array(eager).map do
    |transformation, format|
    transformation = transformation.clone
    format = transformation.delete(:format) || format
    [Cloudinary::Utils.generate_transformation_string(transformation), format].compact.join("/")
  end.join("|")
end

.build_upload_params(options) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/cloudinary/uploader.rb', line 17

def self.build_upload_params(options)
  params = {:timestamp=>Time.now.to_i,
            :transformation => Cloudinary::Utils.generate_transformation_string(options.clone),
            :public_id=> options[:public_id],
            :callback=> options[:callback],
            :format=>options[:format],
            :type=>options[:type],
            :backup=>Cloudinary::Utils.as_safe_bool(options[:backup]),
            :faces=>Cloudinary::Utils.as_safe_bool(options[:faces]),
            :exif=>Cloudinary::Utils.as_safe_bool(options[:exif]),
            :colors=>Cloudinary::Utils.as_safe_bool(options[:colors]),
            :image_metadata=>Cloudinary::Utils.as_safe_bool(options[:image_metadata]),
            :invalidate=>Cloudinary::Utils.as_safe_bool(options[:invalidate]),
            :eager=>build_eager(options[:eager]),
            :headers=>build_custom_headers(options[:headers]),
            :use_filename=>Cloudinary::Utils.as_safe_bool(options[:use_filename]),
            :notification_url=>options[:notification_url],
            :eager_notification_url=>options[:eager_notification_url],
            :eager_async=>Cloudinary::Utils.as_safe_bool(options[:eager_async]),
            :tags=>options[:tags] && Cloudinary::Utils.build_array(options[:tags]).join(",")}    
  params    
end

.destroy(public_id, options = {}) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/cloudinary/uploader.rb', line 52

def self.destroy(public_id, options={})
  call_api("destroy", options) do    
    {
      :timestamp=>Time.now.to_i,
      :type=>options[:type],
      :public_id=> public_id,
      :invalidate=>options[:invalidate],
    }
  end              
end

.exists?(public_id, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
78
79
80
81
82
83
# File 'lib/cloudinary/uploader.rb', line 75

def self.exists?(public_id, options={})
  cloudinary_url = Cloudinary::Utils.cloudinary_url(public_id, options)
  begin
    RestClient::Request.execute(:method => :head, :url => cloudinary_url, :timeout=>5).code.to_s =~ /2\d{2}/
  rescue RestClient::ResourceNotFound => e
    return false
  end
  
end

.explicit(public_id, options = {}) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/cloudinary/uploader.rb', line 85

def self.explicit(public_id, options={})
  call_api("explicit", options) do    
    {
      :timestamp=>Time.now.to_i,
      :type=>options[:type],
      :public_id=> public_id,
      :callback=> options[:callback],
      :eager=>build_eager(options[:eager]),
      :headers=>build_custom_headers(options[:headers]),
      :tags=>options[:tags] && Cloudinary::Utils.build_array(options[:tags]).join(",")    
    }
  end              
end

.explode(public_id, options = {}) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/cloudinary/uploader.rb', line 143

def self.explode(public_id, options={})    
  call_api("explode", options) do
    {
      :timestamp=>Time.now.to_i,
      :public_id=>public_id,
      :type=>options[:type],
      :format=>options[:format],
      :notification_url=>options[:notification_url],
      :transformation => Cloudinary::Utils.generate_transformation_string(options.clone)        
    }    
  end
end

.generate_sprite(tag, options = {}) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/cloudinary/uploader.rb', line 108

def self.generate_sprite(tag, options={})
  version_store = options.delete(:version_store)
  
  result = call_api("sprite", options) do
    {
      :timestamp=>Time.now.to_i,
      :tag=>tag,
      :async=>options[:async],
      :notification_url=>options[:notification_url],
      :transformation => Cloudinary::Utils.generate_transformation_string(options.merge(:fetch_format=>options[:format]))        
    }    
  end
  
  if version_store == :file && result && result["version"]
    if defined?(Rails) && defined?(Rails.root)
      FileUtils.mkdir_p("#{Rails.root}/tmp/cloudinary")
      File.open("#{Rails.root}/tmp/cloudinary/cloudinary_sprite_#{tag}.version", "w"){|file| file.print result["version"].to_s}                      
    end  
  end      
  return result
end

.multi(tag, options = {}) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/cloudinary/uploader.rb', line 130

def self.multi(tag, options={})
  call_api("multi", options) do
    {
      :timestamp=>Time.now.to_i,
      :tag=>tag,
      :format=>options[:format],
      :async=>options[:async],
      :notification_url=>options[:notification_url],
      :transformation => Cloudinary::Utils.generate_transformation_string(options.clone)        
    }    
  end
end

.remove_tag(tag, public_ids = [], options = {}) ⇒ Object



163
164
165
# File 'lib/cloudinary/uploader.rb', line 163

def self.remove_tag(tag, public_ids = [], options = {})
  return self.call_tags_api(tag, "remove", public_ids, options)    
end

.rename(from_public_id, to_public_id, options = {}) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/cloudinary/uploader.rb', line 63

def self.rename(from_public_id, to_public_id, options={})
  call_api("rename", options) do    
    {
      :timestamp=>Time.now.to_i,
      :type=>options[:type],
      :overwrite=>options[:overwrite],
      :from_public_id=>from_public_id,
      :to_public_id=>to_public_id,
    }
  end              
end

.replace_tag(tag, public_ids = [], options = {}) ⇒ Object



167
168
169
# File 'lib/cloudinary/uploader.rb', line 167

def self.replace_tag(tag, public_ids = [], options = {})
  return self.call_tags_api(tag, "replace", public_ids, options)    
end

.text(text, options = {}) ⇒ Object



100
101
102
103
104
105
106
# File 'lib/cloudinary/uploader.rb', line 100

def self.text(text, options={})
  call_api("text", options) do
    params = {:timestamp => Time.now.to_i, :text=>text}
    TEXT_PARAMS.each{|k| params[k] = options[k] if !options[k].nil?}
    params
  end
end

.upload(file, options = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/cloudinary/uploader.rb', line 40

def self.upload(file, options={})
  call_api("upload", options) do    
    params = build_upload_params(options)
    if file.respond_to?(:read) || file =~ /^https?:/ || file =~ /^data:image\/\w*;base64,([a-zA-Z0-9\/+\n=]+)$/
      params[:file] = file
    else 
      params[:file] = File.open(file, "rb")
    end
    [params, [:file]]
  end              
end