Class: CarrierWave::Storage::UpYun::File

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uploader, base, path) ⇒ File



25
26
27
28
29
# File 'lib/carrierwave/storage/upyun.rb', line 25

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

Instance Attribute Details

#content_typeObject



44
45
46
# File 'lib/carrierwave/storage/upyun.rb', line 44

def content_type
  @content_type || ""
end

#pathObject (readonly)

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

Returns

String

A path



38
39
40
# File 'lib/carrierwave/storage/upyun.rb', line 38

def path
  @path
end

Instance Method Details

#connObject



131
132
133
134
135
136
137
138
139
140
# File 'lib/carrierwave/storage/upyun.rb', line 131

def conn
  @conn ||= begin
    api_host = @uploader.upyun_api_host || DEFAULT_API_URL
    Faraday.new(url: "#{api_host}/#{@uploader.upyun_bucket}") do |req|
      req.request :basic_auth, @uploader.upyun_username, @uploader.upyun_password
      req.request :url_encoded
      req.adapter Faraday.default_adapter
    end
  end
end

#deleteObject

Remove the file from Cloud Files



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

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

#escaped_pathObject



40
41
42
# File 'lib/carrierwave/storage/upyun.rb', line 40

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

#headersObject



123
124
125
126
127
128
129
# File 'lib/carrierwave/storage/upyun.rb', line 123

def headers
  @headers ||= begin
    conn.get(@path).headers
               rescue Faraday::ClientError
                 {}
  end
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/upyun.rb', line 57

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

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

Writes the supplied data into the object on Cloud Files.

Returns

boolean



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/carrierwave/storage/upyun.rb', line 103

def store(new_file, headers = {})
  res = conn.put(escaped_path, new_file.read) do |req|
    req.headers = { "Expect" => "", "Mkdir" => "true" }.merge(headers)
  end

  if res.status != 200
    # code: 42900007 -> concurrent put or delete
    json = JSON.parse(res.body)
    # retry upload
    raise ConcurrentUploadError, res.body if json["code"] == 42_900_007

    raise UploadError, res.body
  end

  true
rescue ConcurrentUploadError => e
  puts "Warning: UpYun error #{e.message}, retry again."
  retry
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



82
83
84
85
86
# File 'lib/carrierwave/storage/upyun.rb', line 82

def url
  return nil unless @uploader.upyun_bucket_host

  [@uploader.upyun_bucket_host, @path].join("/")
end