Class: CarrierWave::Storage::Fog::File

Inherits:
Object
  • Object
show all
Includes:
Utilities::Uri
Defined in:
lib/carrierwave/storage/fog.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uploader, base, path) ⇒ File

Returns a new instance of File.



216
217
218
# File 'lib/carrierwave/storage/fog.rb', line 216

def initialize(uploader, base, path)
  @uploader, @base, @path = uploader, base, path
end

Instance Attribute Details

#pathObject (readonly)

Current local path to file

Returns

String

a path to file



116
117
118
# File 'lib/carrierwave/storage/fog.rb', line 116

def path
  @path
end

Instance Method Details

#attributesObject

Return all attributes from file

Returns

Hash

attributes from file



125
126
127
# File 'lib/carrierwave/storage/fog.rb', line 125

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 and Google providers

Returns

String

temporary authenticated url

or
NilClass

no authenticated url available



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/carrierwave/storage/fog.rb', line 139

def authenticated_url(options = {})
  if ['AWS', 'Google', 'Rackspace'].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, options)
    elsif @uploader.fog_credentials[:provider] == "Rackspace"
      connection.get_object_https_url(@uploader.fog_directory, path, ::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_typeObject

Lookup value for file content-type header

Returns

String

value of content-type



163
164
165
# File 'lib/carrierwave/storage/fog.rb', line 163

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



174
175
176
# File 'lib/carrierwave/storage/fog.rb', line 174

def content_type=(new_content_type)
  @content_type = new_content_type
end

#deleteObject

Remove the file from service

Returns

Boolean

true for success or raises error



185
186
187
188
# File 'lib/carrierwave/storage/fog.rb', line 185

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

Returns:

  • (Boolean)


247
248
249
# File 'lib/carrierwave/storage/fog.rb', line 247

def exists?
  !!directory.files.head(path)
end

#extensionObject

Return extension of file

Returns

String

extension of file



197
198
199
# File 'lib/carrierwave/storage/fog.rb', line 197

def extension
  path.split('.').last
end

#filename(options = {}) ⇒ Object

Return file name, if available

Returns

String

file name

or
NilClass

no file name available



339
340
341
342
343
# File 'lib/carrierwave/storage/fog.rb', line 339

def filename(options = {})
  if file_url = url(options)
    URI.decode(file_url).gsub(/.*\/(.*?$)/, '\1')
  end
end

#headersObject

deprecated: All attributes from file (includes headers)

Returns

Hash

attributes from file



208
209
210
211
212
213
214
# File 'lib/carrierwave/storage/fog.rb', line 208

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_urlObject

Return a url to a public file, if available

Returns

String

public url

or
NilClass

no public url available



279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/carrierwave/storage/fog.rb', line 279

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 @uploader.fog_credentials[: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"
        # 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]$/
          "#{protocol}://#{@uploader.fog_directory}.s3.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://commondatastorage.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

#readObject

Read content of file from service

Returns

String

contents of file



226
227
228
# File 'lib/carrierwave/storage/fog.rb', line 226

def read
  file.body
end

#sizeObject

Return size of file body

Returns

Integer

size of file body



237
238
239
# File 'lib/carrierwave/storage/fog.rb', line 237

def size
  file.content_length
end

#store(new_file) ⇒ Object

Write file to service

Returns

Boolean

true on success or raises error



257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/carrierwave/storage/fog.rb', line 257

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



322
323
324
325
326
327
328
# File 'lib/carrierwave/storage/fog.rb', line 322

def url(options = {})
  if !@uploader.fog_public
    authenticated_url(options)
  else
    public_url
  end
end