Class: CarrierWave::Storage::UpYun::File
- Inherits:
-
Object
- Object
- CarrierWave::Storage::UpYun::File
- Defined in:
- lib/carrierwave/storage/upyun/file.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
-
#copy_to(new_path) ⇒ CarrierWave::Storage::UpYun::File
Creates a copy of this file and returns it.
-
#delete ⇒ Object
Remove the file from Cloud Files.
- #escaped_path ⇒ Object
- #extension ⇒ Object
-
#filename ⇒ Object
Return file name, if available.
- #headers ⇒ Object
-
#initialize(uploader, base, path) ⇒ File
constructor
A new instance of File.
-
#read ⇒ Object
Reads the contents of the file from Cloud Files.
-
#size ⇒ Object
Return size of file body.
-
#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
Returns a new instance of File.
8 9 10 11 12 |
# File 'lib/carrierwave/storage/upyun/file.rb', line 8 def initialize(uploader, base, path) @uploader = uploader @path = path @base = base end |
Instance Attribute Details
#content_type ⇒ Object
27 28 29 |
# File 'lib/carrierwave/storage/upyun/file.rb', line 27 def content_type @content_type || "" end |
#path ⇒ Object (readonly)
Returns the current path/filename of the file on Cloud Files.
Returns
- String
-
A path
21 22 23 |
# File 'lib/carrierwave/storage/upyun/file.rb', line 21 def path @path end |
Instance Method Details
#copy_to(new_path) ⇒ CarrierWave::Storage::UpYun::File
Creates a copy of this file and returns it.
Parameters
- new_path (String)
-
The path where the file should be copied to.
Returns
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/carrierwave/storage/upyun/file.rb', line 153 def copy_to(new_path) escaped_new_path = CGI.escape(new_path) res = conn.put(new_path) do |req| req.headers = { "X-Upyun-Copy-Source" => "/#{@uploader.upyun_bucket}/#{self.path}", "Content-Length" => 0, "Mkdir" => "true", } end check_put_response!(res) File.new(@uploader, @base, new_path) rescue ConcurrentUploadError => e retry end |
#delete ⇒ Object
Remove the file from Cloud Files
49 50 51 52 53 54 55 |
# File 'lib/carrierwave/storage/upyun/file.rb', line 49 def delete conn.delete(escaped_path) true rescue StandardError => e puts "carrierwave-upyun delete failed: #{e.inspect}" nil end |
#escaped_path ⇒ Object
23 24 25 |
# File 'lib/carrierwave/storage/upyun/file.rb', line 23 def escaped_path @escaped_path ||= CGI.escape(@path) end |
#extension ⇒ Object
85 86 87 88 |
# File 'lib/carrierwave/storage/upyun/file.rb', line 85 def extension path_elements = path.split('.') path_elements.last if path_elements.size > 1 end |
#filename ⇒ Object
Return file name, if available
Returns
- String
-
file name
or
- NilClass
-
no file name available
80 81 82 83 |
# File 'lib/carrierwave/storage/upyun/file.rb', line 80 def filename return unless file_url = url ::File.basename(file_url.split('?').first) end |
#headers ⇒ Object
98 99 100 101 102 103 104 |
# File 'lib/carrierwave/storage/upyun/file.rb', line 98 def headers @headers ||= begin conn.head(escaped_path).headers rescue Faraday::ClientError {} end end |
#read ⇒ Object
Reads the contents of the file from Cloud Files
Returns
- String
-
contents of the file
40 41 42 43 44 |
# File 'lib/carrierwave/storage/upyun/file.rb', line 40 def read res = conn.get(escaped_path) @headers = res.headers res.body end |
#size ⇒ Object
Return size of file body
Returns
- Integer
-
size of file body
113 114 115 |
# File 'lib/carrierwave/storage/upyun/file.rb', line 113 def size headers['content-length'].to_i end |
#store(new_file, headers = {}) ⇒ Object
Writes the supplied data into the object on Cloud Files.
Returns
boolean
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/carrierwave/storage/upyun/file.rb', line 124 def store(new_file, headers = {}) # Copy from cache_path if new_file.is_a?(self.class) new_file.copy_to(self.escaped_path) return true end res = conn.put(self.escaped_path, new_file.read) do |req| req.headers = { "Expect" => "", "Mkdir" => "true" }.merge(headers) end check_put_response!(res) true rescue ConcurrentUploadError => e 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
65 66 67 68 69 |
# File 'lib/carrierwave/storage/upyun/file.rb', line 65 def url return nil unless @uploader.upyun_bucket_host [@uploader.upyun_bucket_host, @path].join("/") end |