Class: CarrierWave::Storage::UpYun::File
- Inherits:
-
CarrierWave::SanitizedFile
- Object
- CarrierWave::SanitizedFile
- CarrierWave::Storage::UpYun::File
- Defined in:
- lib/carrierwave/storage/upyun.rb
Instance Attribute Summary collapse
- #content_type ⇒ Object
-
#path ⇒ Object
readonly
Returns the current path/filename of the file on Cloud Files.
Instance Method Summary collapse
- #conn ⇒ Object
-
#delete ⇒ Object
Remove the file from Cloud Files.
- #escaped_path ⇒ Object
- #headers ⇒ Object
-
#initialize(uploader, base, path) ⇒ File
constructor
A new instance of File.
-
#read ⇒ Object
Reads the contents of the file from Cloud Files.
-
#store(new_file, headers = {}) ⇒ Object
Writes the supplied data into the object on Cloud Files.
-
#url ⇒ Object
Returns the url on the Cloud Files CDN.
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_type ⇒ Object
44 45 46 |
# File 'lib/carrierwave/storage/upyun.rb', line 44 def content_type @content_type || "" end |
#path ⇒ Object (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
#conn ⇒ Object
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 |
#delete ⇒ Object
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_path ⇒ Object
40 41 42 |
# File 'lib/carrierwave/storage/upyun.rb', line 40 def escaped_path @escaped_path ||= CGI.escape(@path) end |
#headers ⇒ Object
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 |
#read ⇒ Object
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 |
#url ⇒ Object
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 |