Class: CarrierWave::Storage::Fog::File
- Inherits:
-
Object
- Object
- CarrierWave::Storage::Fog::File
- Defined in:
- lib/carrierwave/storage/fog.rb
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Current local path to file.
Instance Method Summary collapse
-
#attributes ⇒ Object
Return all attributes from file.
-
#authenticated_url(options = {}) ⇒ Object
Return a temporary authenticated url to a private file, if available Only supported for AWS and Google providers.
-
#content_type ⇒ Object
Lookup value for file content-type header.
-
#content_type=(new_content_type) ⇒ Object
Set non-default content-type header (default is file.content_type).
-
#delete ⇒ Object
Remove the file from service.
-
#exists? ⇒ Boolean
Check if the file exists on the remote service.
-
#headers ⇒ Object
deprecated: All attributes from file (includes headers).
-
#initialize(uploader, base, path) ⇒ File
constructor
A new instance of File.
-
#public_url ⇒ Object
Return a url to a public file, if available.
-
#read ⇒ Object
Read content of file from service.
-
#size ⇒ Object
Return size of file body.
-
#store(new_file) ⇒ Object
Write file to service.
-
#url(options = {}) ⇒ Object
Return url to file, if avaliable.
Constructor Details
#initialize(uploader, base, path) ⇒ File
Returns a new instance of File.
206 207 208 |
# File 'lib/carrierwave/storage/fog.rb', line 206 def initialize(uploader, base, path) @uploader, @base, @path = uploader, base, path end |
Instance Attribute Details
#path ⇒ Object (readonly)
Current local path to file
Returns
- String
-
a path to file
119 120 121 |
# File 'lib/carrierwave/storage/fog.rb', line 119 def path @path end |
Instance Method Details
#attributes ⇒ Object
Return all attributes from file
Returns
- Hash
-
attributes from file
128 129 130 |
# File 'lib/carrierwave/storage/fog.rb', line 128 def attributes file.attributes end |
#authenticated_url(options = {}) ⇒ Object
Return a temporary authenticated url to a private file, if available Only supported for AWS and Google providers
Returns
- String
-
temporary authenticated url
or
- NilClass
-
no authenticated url available
142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/carrierwave/storage/fog.rb', line 142 def authenticated_url( = {}) if ['AWS', 'Google'].include?(@uploader.fog_credentials[:provider]) # avoid a get by using local references local_directory = connection.directories.new(:key => @uploader.fog_directory) local_file = local_directory.files.new(:key => path) if @uploader.fog_credentials[:provider] == "AWS" local_file.url(::Fog::Time.now + @uploader.fog_authenticated_url_expiration, ) else local_file.url(::Fog::Time.now + @uploader.fog_authenticated_url_expiration) end else nil end end |
#content_type ⇒ Object
Lookup value for file content-type header
Returns
- String
-
value of content-type
164 165 166 |
# File 'lib/carrierwave/storage/fog.rb', line 164 def content_type @content_type || file.content_type end |
#content_type=(new_content_type) ⇒ Object
Set non-default content-type header (default is file.content_type)
Returns
- String
-
returns new content type value
175 176 177 |
# File 'lib/carrierwave/storage/fog.rb', line 175 def content_type=(new_content_type) @content_type = new_content_type end |
#delete ⇒ Object
Remove the file from service
Returns
- Boolean
-
true for success or raises error
186 187 188 189 |
# File 'lib/carrierwave/storage/fog.rb', line 186 def delete # avoid a get by just using local reference directory.files.new(:key => path).destroy end |
#exists? ⇒ Boolean
Check if the file exists on the remote service
Returns
- Boolean
-
true if file exists or false
237 238 239 |
# File 'lib/carrierwave/storage/fog.rb', line 237 def exists? !!directory.files.head(path) end |
#headers ⇒ Object
deprecated: All attributes from file (includes headers)
Returns
- Hash
-
attributes from file
198 199 200 201 202 203 204 |
# File 'lib/carrierwave/storage/fog.rb', line 198 def headers location = caller.first warning = "[yellow][WARN] headers is deprecated, use attributes instead[/]" warning << " [light_black](#{location})[/]" Formatador.display_line(warning) attributes end |
#public_url ⇒ Object
Return a url to a public file, if available
Returns
- String
-
public url
or
- NilClass
-
no public url available
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
# File 'lib/carrierwave/storage/fog.rb', line 269 def public_url if host = @uploader.fog_host if host.respond_to? :call "#{host.call(self)}/#{path}" else "#{host}/#{path}" end else # AWS/Google optimized for speed over correctness case @uploader.fog_credentials[:provider] when 'AWS' # if directory is a valid subdomain, use that style for access if @uploader.fog_directory.to_s =~ /^(?:[a-z]|\d(?!\d{0,2}(?:\d{1,3}){3}$))(?:[a-z0-9]|(?![\-])|\-(?![\.])){1,61}[a-z0-9]$/ "https://#{@uploader.fog_directory}.s3.amazonaws.com/#{path}" else # directory is not a valid subdomain, so use path style for access "https://s3.amazonaws.com/#{@uploader.fog_directory}/#{path}" end when 'Google' "https://commondatastorage.googleapis.com/#{@uploader.fog_directory}/#{path}" else # avoid a get by just using local reference directory.files.new(:key => path).public_url end end end |
#read ⇒ Object
Read content of file from service
Returns
- String
-
contents of file
216 217 218 |
# File 'lib/carrierwave/storage/fog.rb', line 216 def read file.body end |
#size ⇒ Object
Return size of file body
Returns
- Integer
-
size of file body
227 228 229 |
# File 'lib/carrierwave/storage/fog.rb', line 227 def size file.content_length end |
#store(new_file) ⇒ Object
Write file to service
Returns
- Boolean
-
true on success or raises error
247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/carrierwave/storage/fog.rb', line 247 def store(new_file) fog_file = new_file.to_file @content_type ||= new_file.content_type @file = directory.files.create({ :body => fog_file ? fog_file : new_file.read, :content_type => @content_type, :key => path, :public => @uploader.fog_public }.merge(@uploader.fog_attributes)) fog_file.close if fog_file && !fog_file.closed? true end |
#url(options = {}) ⇒ Object
Return url to file, if avaliable
Returns
- String
-
url
or
- NilClass
-
no url available
305 306 307 308 309 310 311 |
# File 'lib/carrierwave/storage/fog.rb', line 305 def url( = {}) if !@uploader.fog_public authenticated_url() else public_url end end |