Class: CarrierWave::Storage::Fog::File
- Inherits:
-
Object
- Object
- CarrierWave::Storage::Fog::File
- Includes:
- Utilities::Uri
- Defined in:
- lib/carrierwave/storage/fog.rb
Constant Summary
Constants included from Utilities::Uri
Utilities::Uri::SAFE_STRING, Utilities::Uri::UNSAFE
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, Rackspace, Google and AzureRM 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).
-
#copy_to(new_path) ⇒ CarrierWave::Storage::Fog::File
Creates a copy of this file and returns it.
-
#delete ⇒ Object
Remove the file from service.
-
#exists? ⇒ Boolean
Check if the file exists on the remote service.
-
#extension ⇒ Object
Return extension of file.
-
#filename(options = {}) ⇒ Object
Return file name, if available.
-
#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.
272 273 274 |
# File 'lib/carrierwave/storage/fog.rb', line 272 def initialize(uploader, base, path) @uploader, @base, @path, @content_type = uploader, base, path, nil end |
Instance Attribute Details
#path ⇒ Object (readonly)
Current local path to file
Returns
- String
-
a path to file
165 166 167 |
# File 'lib/carrierwave/storage/fog.rb', line 165 def path @path end |
Instance Method Details
#attributes ⇒ Object
Return all attributes from file
Returns
- Hash
-
attributes from file
174 175 176 |
# File 'lib/carrierwave/storage/fog.rb', line 174 def attributes file.attributes end |
#authenticated_url(options = {}) ⇒ Object
Return a temporary authenticated url to a private file, if available Only supported for AWS, Rackspace, Google and AzureRM providers
Returns
- String
-
temporary authenticated url
or
- NilClass
-
no authenticated url available
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/carrierwave/storage/fog.rb', line 188 def authenticated_url( = {}) if ['AWS', 'Google', 'Rackspace', 'OpenStack', 'AzureRM'].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) expire_at = ::Fog::Time.now + @uploader.fog_authenticated_url_expiration case @uploader.fog_credentials[:provider] when 'AWS', 'Google' # Older versions of fog-google do not support options as a parameter if (local_file) local_file.url(expire_at, ) else warn "Options hash not supported in #{local_file.class}. You may need to upgrade your Fog provider." local_file.url(expire_at) end when 'Rackspace', 'OpenStack' connection.get_object_https_url(@uploader.fog_directory, path, expire_at, ) else local_file.url(expire_at) end end end |
#content_type ⇒ Object
Lookup value for file content-type header
Returns
- String
-
value of content-type
218 219 220 |
# File 'lib/carrierwave/storage/fog.rb', line 218 def content_type @content_type || !file.nil? && 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
229 230 231 |
# File 'lib/carrierwave/storage/fog.rb', line 229 def content_type=(new_content_type) @content_type = new_content_type end |
#copy_to(new_path) ⇒ CarrierWave::Storage::Fog::File
Creates a copy of this file and returns it.
Parameters
- new_path (String)
-
The path where the file should be copied to.
Returns
431 432 433 434 |
# File 'lib/carrierwave/storage/fog.rb', line 431 def copy_to(new_path) connection.copy_object(@uploader.fog_directory, file.key, @uploader.fog_directory, new_path, acl_header) CarrierWave::Storage::Fog::File.new(@uploader, @base, new_path) end |
#delete ⇒ Object
Remove the file from service
Returns
- Boolean
-
true for success or raises error
240 241 242 243 |
# File 'lib/carrierwave/storage/fog.rb', line 240 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
313 314 315 |
# File 'lib/carrierwave/storage/fog.rb', line 313 def exists? !!file end |
#extension ⇒ Object
Return extension of file
Returns
- String
-
extension of file or nil if the file has no extension
252 253 254 255 |
# File 'lib/carrierwave/storage/fog.rb', line 252 def extension path_elements = path.split('.') path_elements.last if path_elements.size > 1 end |
#filename(options = {}) ⇒ Object
Return file name, if available
Returns
- String
-
file name
or
- NilClass
-
no file name available
415 416 417 418 |
# File 'lib/carrierwave/storage/fog.rb', line 415 def filename( = {}) return unless file_url = url() CGI.unescape(file_url.split('?').first).gsub(/.*\/(.*?$)/, '\1') end |
#headers ⇒ Object
deprecated: All attributes from file (includes headers)
Returns
- Hash
-
attributes from file
264 265 266 267 268 269 270 |
# File 'lib/carrierwave/storage/fog.rb', line 264 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
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 |
# File 'lib/carrierwave/storage/fog.rb', line 349 def public_url encoded_path = encode_path(path) if host = @uploader.asset_host if host.respond_to? :call "#{host.call(self)}/#{encoded_path}" else "#{host}/#{encoded_path}" end else # AWS/Google optimized for speed over correctness case fog_provider when 'AWS' # check if some endpoint is set in fog_credentials if @uploader.fog_credentials.has_key?(:endpoint) "#{@uploader.fog_credentials[:endpoint]}/#{@uploader.fog_directory}/#{encoded_path}" else protocol = @uploader.fog_use_ssl_for_aws ? "https" : "http" subdomain_regex = /^(?:[a-z]|\d(?!\d{0,2}(?:\d{1,3}){3}$))(?:[a-z0-9\.]|(?![\-])|\-(?![\.])){1,61}[a-z0-9]$/ valid_subdomain = @uploader.fog_directory.to_s =~ subdomain_regex && !(protocol == 'https' && @uploader.fog_directory =~ /\./) # if directory is a valid subdomain, use that style for access if valid_subdomain s3_subdomain = @uploader.fog_aws_accelerate ? "s3-accelerate" : "s3" "#{protocol}://#{@uploader.fog_directory}.#{s3_subdomain}.amazonaws.com/#{encoded_path}" else # directory is not a valid subdomain, so use path style for access "#{protocol}://s3.amazonaws.com/#{@uploader.fog_directory}/#{encoded_path}" end end when 'Google' # https://cloud.google.com/storage/docs/access-public-data "https://storage.googleapis.com/#{@uploader.fog_directory}/#{encoded_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
282 283 284 285 286 287 288 289 290 291 292 293 294 |
# File 'lib/carrierwave/storage/fog.rb', line 282 def read file_body = file.body return if file_body.nil? return file_body unless file_body.is_a?(::File) # Fog::Storage::XXX::File#body could return the source file which was upoloaded to the remote server. read_source_file(file_body) if ::File.exist?(file_body.path) # If the source file doesn't exist, the remote content is read @file = nil # rubocop:disable Gitlab/ModuleWithInstanceVariables file.body end |
#size ⇒ Object
Return size of file body
Returns
- Integer
-
size of file body
303 304 305 |
# File 'lib/carrierwave/storage/fog.rb', line 303 def size file.nil? ? 0 : file.content_length end |
#store(new_file) ⇒ Object
Write file to service
Returns
- Boolean
-
true on success or raises error
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 |
# File 'lib/carrierwave/storage/fog.rb', line 323 def store(new_file) if new_file.is_a?(self.class) new_file.copy_to(path) else 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? end true end |
#url(options = {}) ⇒ Object
Return url to file, if avaliable
Returns
- String
-
url
or
- NilClass
-
no url available
398 399 400 401 402 403 404 |
# File 'lib/carrierwave/storage/fog.rb', line 398 def url( = {}) if !@uploader.fog_public authenticated_url() else public_url end end |