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

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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_typeObject



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

def content_type
  @content_type || ""
end

#pathObject (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

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

#deleteObject

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_pathObject



23
24
25
# File 'lib/carrierwave/storage/upyun/file.rb', line 23

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

#extensionObject



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

#filenameObject

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

#headersObject



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

#readObject

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

#sizeObject

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

#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



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