Sha256: 53d17abb9b9f37e759d3ffab2bba31cab9e72ec5411949a391b769c35dddf2a9

Contents?: true

Size: 1.62 KB

Versions: 1

Compression:

Stored size: 1.62 KB

Contents

module Fog
  class Storage

    def self.new(attributes)
      attributes = attributes.dup # prevent delete from having side effects
      case provider = attributes[:provider] # attributes.delete(:provider)
      when 'AWS'
        require 'fog/storage/aws'
        Fog::AWS::Storage.new(attributes)
      when 'Google'
        require 'fog/storage/google'
        Fog::Google::Storage.new(attributes)
      when 'Local'
        require 'fog/storage/local'
        Fog::Local::Storage.new(attributes)
      when 'Rackspace'
        require 'fog/storage/rackspace'
        Fog::Rackspace::Storage.new(attributes)
      else
        raise ArgumentError.new("#{provider} is not a recognized storage provider")
      end
    end

    def self.get_body_size(body)
      if body.respond_to?(:force_encoding)
        body.force_encoding('BINARY')
      end
      if body.respond_to?(:bytesize)
        body.bytesize
      elsif body.respond_to?(:size)
        body.size
      elsif body.respond_to?(:stat)
        body.stat.size
      else
        0
      end
    end
    
    def self.parse_data(data)
      metadata = {
        :body => nil,
        :headers => {}
      }
      
      metadata[:body] = data
      metadata[:headers]['Content-Length'] = get_body_size(data)
      
      if data.respond_to?(:path)
        filename = ::File.basename(data.path)
        unless (mime_types = MIME::Types.of(filename)).empty?
          metadata[:headers]['Content-Type'] = mime_types.first.content_type
        end
      end
      # metadata[:headers]['Content-MD5'] = Base64.encode64(Digest::MD5.digest(metadata[:body])).strip
      metadata
    end
    
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fog-0.8.2 lib/fog/storage.rb