Class: Cloudinary::CarrierWave::CloudinaryFile

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(identifier, uploader) ⇒ CloudinaryFile

Returns a new instance of CloudinaryFile.



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/cloudinary/carrier_wave.rb', line 152

def initialize(identifier, uploader)
  @uploader = uploader
  @identifier = identifier

  if @identifier.match(%r(^(image|raw|video)/(upload|private|authenticated)(?:/v([0-9]+))?/(.*)))
    @resource_type = $1
    @storage_type = $2
    @version = $3.presence
    @filename = $4
  elsif @identifier.match(%r(^v([0-9]+)/(.*)))
    @version = $1
    @filename = $2
  else
    @filename = @identifier
    @version = nil
  end

  @storage_type ||= uploader.class.storage_type
  @resource_type ||= Cloudinary::Utils.resource_type_for_format(@filename)
  @public_id, @format = Cloudinary::PreloadedFile.split_format(@filename)
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



151
152
153
# File 'lib/cloudinary/carrier_wave.rb', line 151

def filename
  @filename
end

#formatObject (readonly)

Returns the value of attribute format.



151
152
153
# File 'lib/cloudinary/carrier_wave.rb', line 151

def format
  @format
end

#identifierObject (readonly)

Returns the value of attribute identifier.



151
152
153
# File 'lib/cloudinary/carrier_wave.rb', line 151

def identifier
  @identifier
end

#public_idObject (readonly)

Returns the value of attribute public_id.



151
152
153
# File 'lib/cloudinary/carrier_wave.rb', line 151

def public_id
  @public_id
end

#resource_typeObject (readonly)

Returns the value of attribute resource_type.



151
152
153
# File 'lib/cloudinary/carrier_wave.rb', line 151

def resource_type
  @resource_type
end

#storage_typeObject (readonly)

Returns the value of attribute storage_type.



151
152
153
# File 'lib/cloudinary/carrier_wave.rb', line 151

def storage_type
  @storage_type
end

#versionObject (readonly)

Returns the value of attribute version.



151
152
153
# File 'lib/cloudinary/carrier_wave.rb', line 151

def version
  @version
end

Instance Method Details

#deleteObject



178
179
180
181
# File 'lib/cloudinary/carrier_wave.rb', line 178

def delete
  public_id = @resource_type == "raw" ? self.filename : self.public_id
  Cloudinary::Uploader.destroy(public_id, :type=>self.storage_type, :resource_type=>self.resource_type) if @uploader.delete_remote?
end

#exists?Boolean

Returns:

  • (Boolean)


183
184
185
186
# File 'lib/cloudinary/carrier_wave.rb', line 183

def exists?
  public_id = @resource_type == "raw" ? self.filename : self.public_id
  Cloudinary::Uploader.exists?(public_id, :version=>self.version, :type=>self.storage_type, :resource_type=>self.resource_type)
end

#read(options = {}) ⇒ Object



188
189
190
191
# File 'lib/cloudinary/carrier_wave.rb', line 188

def read(options={})
  parameters={:type=>self.storage_type, :resource_type=>self.resource_type}.merge(options)
  Cloudinary::Downloader.download(self.identifier, parameters)
end

#storage_identifierObject



174
175
176
# File 'lib/cloudinary/carrier_wave.rb', line 174

def storage_identifier
  identifier
end