Class: CarrierWave::Storage::CloudFiles::File

Inherits:
Object
  • Object
show all
Defined in:
lib/carrierwave/storage/cloud_files.rb

Instance Method Summary collapse

Constructor Details

#initialize(uploader, base, path) ⇒ File

Returns a new instance of File.



33
34
35
36
37
# File 'lib/carrierwave/storage/cloud_files.rb', line 33

def initialize(uploader, base, path)
  @uploader = uploader
  @path = path
  @base = base
end

Instance Method Details

#content_typeObject



95
96
97
# File 'lib/carrierwave/storage/cloud_files.rb', line 95

def content_type
  cf_container.object(@path).content_type
end

#content_type=(new_content_type) ⇒ Object



99
100
101
# File 'lib/carrierwave/storage/cloud_files.rb', line 99

def content_type=(new_content_type)
  headers["content-type"] = new_content_type
end

#deleteObject

Remove the file from Cloud Files



66
67
68
69
70
71
72
73
# File 'lib/carrierwave/storage/cloud_files.rb', line 66

def delete
  begin
    cf_container.delete_object(@path)
  rescue ::CloudFiles::Exception::NoSuchObject
    # If the file's not there, don't panic
    nil
  end
end

#pathObject

Returns the current path/filename of the file on Cloud Files.

Returns

String

A path



46
47
48
# File 'lib/carrierwave/storage/cloud_files.rb', line 46

def path
  @path
end

#readObject

Reads the contents of the file from Cloud Files

Returns

String

contents of the file



57
58
59
60
61
# File 'lib/carrierwave/storage/cloud_files.rb', line 57

def read
  object = cf_container.object(@path)
  @content_type = object.content_type
  object.data
end

#store(data, headers = {}) ⇒ Object

Writes the supplied data into the object on Cloud Files.

Returns

boolean



110
111
112
113
# File 'lib/carrierwave/storage/cloud_files.rb', line 110

def store(data,headers={})
  object = cf_container.create_object(@path)
  object.write(data,headers)
end

#urlObject

Returns the url on the Cloud Files CDN. Note that the parent container must be marked as public for this to work.

Returns

String

file’s url



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/carrierwave/storage/cloud_files.rb', line 83

def url
  if @uploader.cloud_files_cdn_host
    "http://" + @uploader.cloud_files_cdn_host + "/" + @path
  else
    begin
      cf_container.object(@path).public_url
    rescue ::CloudFiles::Exception::NoSuchObject
      nil
    end
  end
end