Class: CarrierWave::Storage::UCloud::File

Inherits:
CarrierWave::SanitizedFile
  • Object
show all
Defined in:
lib/carrierwave/storage/ucloud.rb

Instance Method Summary collapse

Constructor Details

#initialize(uploader, base, path) ⇒ File

Returns a new instance of File.



19
20
21
22
23
# File 'lib/carrierwave/storage/ucloud.rb', line 19

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

Instance Method Details

#connObject



112
113
114
115
116
117
118
119
# File 'lib/carrierwave/storage/ucloud.rb', line 112

def conn
  return @conn if defined?(@conn)

  @conn = Faraday.new(url: @uploader.ucloud_bucket_host) do |req|
    req.request :url_encoded
    req.adapter Faraday.default_adapter
  end
end

#content_typeObject



80
81
82
# File 'lib/carrierwave/storage/ucloud.rb', line 80

def content_type
  headers[:content_type]
end

#content_type=(new_content_type) ⇒ Object



84
85
86
# File 'lib/carrierwave/storage/ucloud.rb', line 84

def content_type=(new_content_type)
  headers[:content_type] = new_content_type
end

#deleteObject

Remove the file from Cloud Files



56
57
58
59
60
61
62
63
64
# File 'lib/carrierwave/storage/ucloud.rb', line 56

def delete
  begin
    conn.delete(escaped_path)
    true
  rescue => e
    puts "carrierwave-ucloud delete failed: #{e.inspect}"
    nil
  end
end

#escaped_pathObject



36
37
38
# File 'lib/carrierwave/storage/ucloud.rb', line 36

def escaped_path
  @escaped_path ||= CGI.escape(@path)
end

#headersObject



104
105
106
107
108
109
110
# File 'lib/carrierwave/storage/ucloud.rb', line 104

def headers
  @headers ||= begin
    conn.get(@path).headers
  rescue Faraday::ClientError
    {}
  end
end

#pathObject

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

Returns

String

A path



32
33
34
# File 'lib/carrierwave/storage/ucloud.rb', line 32

def path
  @path
end

#readObject

Reads the contents of the file from Cloud Files

Returns

String

contents of the file



47
48
49
50
51
# File 'lib/carrierwave/storage/ucloud.rb', line 47

def read
  res = conn.get(escaped_path)
  @headers = res.headers
  res.body
end

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

Writes the supplied data into the object on Cloud Files.

Returns

boolean



95
96
97
98
99
100
101
102
# File 'lib/carrierwave/storage/ucloud.rb', line 95

def store(data, headers = {})
  r = conn.put(escaped_path, data) do |req|
    req.headers = headers
    token = CarrierWave::UCloud::Digest.authorization(@uploader, req)
    req.headers['Authorization'] = token
  end
  r
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



74
75
76
77
78
# File 'lib/carrierwave/storage/ucloud.rb', line 74

def url
  host = @uploader.ucloud_cdn_host || @uploader.ucloud_bucket_host
  return nil unless host
  [host, @path].join("/")
end