2010-10-29 19:24:34 -04:00
|
|
|
module Fog
|
2011-06-15 17:26:43 -04:00
|
|
|
module Storage
|
|
|
|
|
|
|
|
def self.[](provider)
|
|
|
|
self.new(:provider => provider)
|
|
|
|
end
|
2010-10-29 19:24:34 -04:00
|
|
|
|
|
|
|
def self.new(attributes)
|
2010-12-13 20:51:16 -05:00
|
|
|
attributes = attributes.dup # prevent delete from having side effects
|
2011-06-16 19:39:51 -04:00
|
|
|
case provider = attributes.delete(:provider).to_s.downcase.to_sym
|
2012-08-12 19:35:35 -04:00
|
|
|
when :atmos
|
|
|
|
require 'fog/atmos/storage'
|
|
|
|
Fog::Storage::Atmos.new(attributes)
|
2011-06-15 17:26:43 -04:00
|
|
|
when :aws
|
2011-08-24 14:50:42 -04:00
|
|
|
require 'fog/aws/storage'
|
2011-06-15 17:26:43 -04:00
|
|
|
Fog::Storage::AWS.new(attributes)
|
|
|
|
when :google
|
2011-08-24 14:55:35 -04:00
|
|
|
require 'fog/google/storage'
|
2011-06-15 17:26:43 -04:00
|
|
|
Fog::Storage::Google.new(attributes)
|
2011-06-15 02:10:54 -04:00
|
|
|
when :hp
|
|
|
|
require 'fog/hp/storage'
|
|
|
|
Fog::Storage::HP.new(attributes)
|
2012-01-10 17:20:56 -05:00
|
|
|
when :ibm
|
|
|
|
require 'fog/ibm/storage'
|
|
|
|
Fog::Storage::IBM.new(attributes)
|
2011-06-15 17:26:43 -04:00
|
|
|
when :local
|
2011-08-24 15:03:25 -04:00
|
|
|
require 'fog/local/storage'
|
2011-06-15 17:26:43 -04:00
|
|
|
Fog::Storage::Local.new(attributes)
|
2011-06-27 03:55:01 -04:00
|
|
|
when :ninefold
|
2011-08-24 15:07:16 -04:00
|
|
|
require 'fog/ninefold/storage'
|
2011-06-27 03:55:01 -04:00
|
|
|
Fog::Storage::Ninefold.new(attributes)
|
2011-06-15 17:26:43 -04:00
|
|
|
when :rackspace
|
2011-08-24 15:12:29 -04:00
|
|
|
require 'fog/rackspace/storage'
|
2011-06-15 17:26:43 -04:00
|
|
|
Fog::Storage::Rackspace.new(attributes)
|
2013-01-23 14:31:04 -05:00
|
|
|
when :openstack
|
|
|
|
require 'fog/openstack/storage'
|
|
|
|
Fog::Storage::OpenStack.new(attributes)
|
2010-10-29 19:24:34 -04:00
|
|
|
else
|
|
|
|
raise ArgumentError.new("#{provider} is not a recognized storage provider")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-06-15 17:26:43 -04:00
|
|
|
def self.directories
|
|
|
|
directories = []
|
2011-07-27 10:30:49 -04:00
|
|
|
for provider in self.providers
|
2011-06-15 17:26:43 -04:00
|
|
|
begin
|
|
|
|
directories.concat(self[provider].directories)
|
|
|
|
rescue # ignore any missing credentials/etc
|
|
|
|
end
|
|
|
|
end
|
|
|
|
directories
|
|
|
|
end
|
|
|
|
|
2011-03-18 16:12:55 -04:00
|
|
|
def self.get_body_size(body)
|
2011-05-25 17:37:47 -04:00
|
|
|
if body.respond_to?(:force_encoding)
|
|
|
|
body.force_encoding('BINARY')
|
|
|
|
end
|
|
|
|
if body.respond_to?(:bytesize)
|
2011-03-21 16:06:35 -04:00
|
|
|
body.bytesize
|
2011-05-25 17:37:47 -04:00
|
|
|
elsif body.respond_to?(:size)
|
2011-03-21 16:06:35 -04:00
|
|
|
body.size
|
2011-05-25 17:37:47 -04:00
|
|
|
elsif body.respond_to?(:stat)
|
2011-03-21 16:06:35 -04:00
|
|
|
body.stat.size
|
2011-03-18 16:12:55 -04:00
|
|
|
else
|
|
|
|
0
|
|
|
|
end
|
|
|
|
end
|
2011-06-27 03:55:01 -04:00
|
|
|
|
2012-03-26 20:14:02 -04:00
|
|
|
def self.get_content_type(data)
|
2011-06-25 13:48:54 -04:00
|
|
|
if data.respond_to?(:path) and !data.path.nil?
|
2011-03-10 16:04:42 -05:00
|
|
|
filename = ::File.basename(data.path)
|
|
|
|
unless (mime_types = MIME::Types.of(filename)).empty?
|
2012-03-26 20:14:02 -04:00
|
|
|
mime_types.first.content_type
|
2011-03-10 16:04:42 -05:00
|
|
|
end
|
|
|
|
end
|
2012-03-26 20:14:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.parse_data(data)
|
|
|
|
{
|
|
|
|
:body => data,
|
|
|
|
:headers => {
|
|
|
|
'Content-Length' => get_body_size(data),
|
|
|
|
'Content-Type' => get_content_type(data)
|
|
|
|
#'Content-MD5' => Base64.encode64(Digest::MD5.digest(metadata[:body])).strip
|
|
|
|
}
|
|
|
|
}
|
2011-03-10 16:04:42 -05:00
|
|
|
end
|
2011-06-27 03:55:01 -04:00
|
|
|
|
2011-07-27 10:30:49 -04:00
|
|
|
def self.providers
|
|
|
|
Fog.services[:storage]
|
|
|
|
end
|
|
|
|
|
2010-10-29 19:24:34 -04:00
|
|
|
end
|
2011-03-18 16:12:55 -04:00
|
|
|
end
|