2010-10-29 19:24:34 -04:00
|
|
|
module Fog
|
|
|
|
class Storage
|
|
|
|
|
|
|
|
def self.new(attributes)
|
2010-12-13 20:51:16 -05:00
|
|
|
attributes = attributes.dup # prevent delete from having side effects
|
2011-01-07 21:14:39 -05:00
|
|
|
case provider = attributes[:provider] # attributes.delete(:provider)
|
2010-11-03 20:31:07 -04:00
|
|
|
when 'AWS'
|
2011-01-07 21:12:05 -05:00
|
|
|
require 'fog/storage/aws'
|
2010-10-29 19:24:34 -04:00
|
|
|
Fog::AWS::Storage.new(attributes)
|
2010-11-03 20:31:07 -04:00
|
|
|
when 'Google'
|
2011-01-07 21:12:05 -05:00
|
|
|
require 'fog/storage/google'
|
2010-10-29 19:24:34 -04:00
|
|
|
Fog::Google::Storage.new(attributes)
|
2010-11-03 20:31:07 -04:00
|
|
|
when 'Local'
|
2011-01-07 21:12:05 -05:00
|
|
|
require 'fog/storage/local'
|
2010-10-29 19:24:34 -04:00
|
|
|
Fog::Local::Storage.new(attributes)
|
2010-11-03 20:31:07 -04:00
|
|
|
when 'Rackspace'
|
2011-01-07 21:12:05 -05:00
|
|
|
require 'fog/storage/rackspace'
|
2010-10-29 19:24:34 -04:00
|
|
|
Fog::Rackspace::Storage.new(attributes)
|
|
|
|
else
|
|
|
|
raise ArgumentError.new("#{provider} is not a recognized storage provider")
|
|
|
|
end
|
|
|
|
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-03-10 16:04:42 -05:00
|
|
|
def self.parse_data(data)
|
|
|
|
metadata = {
|
|
|
|
:body => nil,
|
|
|
|
:headers => {}
|
|
|
|
}
|
2011-03-18 16:12:55 -04:00
|
|
|
|
|
|
|
metadata[:body] = data
|
|
|
|
metadata[:headers]['Content-Length'] = get_body_size(data)
|
|
|
|
|
|
|
|
if data.respond_to?(:path)
|
2011-03-10 16:04:42 -05:00
|
|
|
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
|
2011-03-18 16:12:55 -04:00
|
|
|
|
2010-10-29 19:24:34 -04:00
|
|
|
end
|
2011-03-18 16:12:55 -04:00
|
|
|
end
|