1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00
fog--fog/lib/fog/storage.rb

81 lines
2 KiB
Ruby
Raw Normal View History

2013-06-14 01:55:03 -04:00
require 'mime/types'
2010-10-29 19:24:34 -04:00
module Fog
module Storage
def self.[](provider)
self.new(:provider => provider)
end
2010-10-29 19:24:34 -04:00
def self.new(attributes)
attributes = attributes.dup # prevent delete from having side effects
case provider = attributes.delete(:provider).to_s.downcase.to_sym
when :internetarchive
require 'fog/internet_archive/storage'
Fog::Storage::InternetArchive.new(attributes)
when :stormondemand
require 'fog/storm_on_demand/storage'
Fog::Storage::StormOnDemand.new(attributes)
2010-10-29 19:24:34 -04:00
else
if self.providers.include?(provider)
require "fog/#{provider}/storage"
return Fog::Storage.const_get(Fog.providers[provider]).new(attributes)
end
2010-10-29 19:24:34 -04:00
raise ArgumentError.new("#{provider} is not a recognized storage provider")
end
end
def self.directories
directories = []
for provider in self.providers
begin
directories.concat(self[provider].directories)
rescue # ignore any missing credentials/etc
end
end
directories
end
def self.get_body_size(body)
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
elsif body.respond_to?(:size)
2011-03-21 16:06:35 -04:00
body.size
elsif body.respond_to?(:stat)
2011-03-21 16:06:35 -04:00
body.stat.size
else
0
end
end
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?
mime_types.first.content_type
2011-03-10 16:04:42 -05:00
end
end
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
def self.providers
Fog.services[:storage]
end
2010-10-29 19:24:34 -04:00
end
end