2009-10-11 16:37:25 -04:00
|
|
|
module Fog
|
|
|
|
module Rackspace
|
2010-03-19 21:29:42 -04:00
|
|
|
module Files
|
2010-02-12 02:41:40 -05:00
|
|
|
|
2010-03-19 21:29:42 -04:00
|
|
|
def self.new(options={})
|
2010-04-03 03:33:35 -04:00
|
|
|
|
2010-04-13 15:40:46 -04:00
|
|
|
unless @required
|
|
|
|
require 'fog/rackspace/models/files/directory'
|
|
|
|
require 'fog/rackspace/models/files/directories'
|
|
|
|
require 'fog/rackspace/models/files/file'
|
|
|
|
require 'fog/rackspace/models/files/files'
|
|
|
|
require 'fog/rackspace/requests/files/delete_container'
|
|
|
|
require 'fog/rackspace/requests/files/delete_object'
|
|
|
|
require 'fog/rackspace/requests/files/get_container'
|
|
|
|
require 'fog/rackspace/requests/files/get_containers'
|
|
|
|
require 'fog/rackspace/requests/files/get_object'
|
|
|
|
require 'fog/rackspace/requests/files/head_container'
|
|
|
|
require 'fog/rackspace/requests/files/head_containers'
|
|
|
|
require 'fog/rackspace/requests/files/head_object'
|
|
|
|
require 'fog/rackspace/requests/files/put_container'
|
|
|
|
require 'fog/rackspace/requests/files/put_object'
|
|
|
|
@required = true
|
|
|
|
end
|
2010-04-03 03:33:35 -04:00
|
|
|
|
2010-03-19 21:29:42 -04:00
|
|
|
if Fog.mocking?
|
|
|
|
Fog::Rackspace::Files::Mock.new(options)
|
|
|
|
else
|
|
|
|
Fog::Rackspace::Files::Real.new(options)
|
|
|
|
end
|
2009-10-11 16:37:25 -04:00
|
|
|
end
|
|
|
|
|
2010-03-19 21:29:42 -04:00
|
|
|
def self.parse_data(data)
|
2009-10-31 13:59:50 -04:00
|
|
|
metadata = {
|
|
|
|
:body => nil,
|
|
|
|
:headers => {}
|
|
|
|
}
|
2010-03-19 21:29:42 -04:00
|
|
|
|
2009-10-31 13:59:50 -04:00
|
|
|
if data.is_a?(String)
|
|
|
|
metadata[:body] = data
|
|
|
|
metadata[:headers]['Content-Length'] = metadata[:body].size.to_s
|
|
|
|
else
|
2010-02-27 16:39:33 -05:00
|
|
|
filename = ::File.basename(data.path)
|
2009-10-31 13:59:50 -04:00
|
|
|
unless (mime_types = MIME::Types.of(filename)).empty?
|
|
|
|
metadata[:headers]['Content-Type'] = mime_types.first.content_type
|
|
|
|
end
|
|
|
|
metadata[:body] = data.read
|
2010-02-27 16:39:33 -05:00
|
|
|
metadata[:headers]['Content-Length'] = ::File.size(data.path).to_s
|
2009-10-31 13:59:50 -04:00
|
|
|
end
|
|
|
|
# metadata[:headers]['Content-MD5'] = Base64.encode64(Digest::MD5.digest(metadata[:body])).strip
|
|
|
|
metadata
|
|
|
|
end
|
|
|
|
|
2010-03-19 21:29:42 -04:00
|
|
|
def self.reset_data(keys=Mock.data.keys)
|
|
|
|
Mock.reset_data(keys)
|
2009-10-11 16:37:25 -04:00
|
|
|
end
|
|
|
|
|
2010-03-19 21:29:42 -04:00
|
|
|
class Mock
|
|
|
|
|
|
|
|
def self.data
|
|
|
|
@data ||= Hash.new do |hash, key|
|
|
|
|
hash[key] = {}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.reset_data(keys=data.keys)
|
|
|
|
for key in [*keys]
|
|
|
|
data.delete(key)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(options={})
|
|
|
|
@rackspace_username = options[:rackspace_username]
|
|
|
|
@data = self.class.data[@rackspace_username]
|
2009-10-11 16:37:25 -04:00
|
|
|
end
|
2010-03-19 21:29:42 -04:00
|
|
|
|
2009-10-11 16:37:25 -04:00
|
|
|
end
|
|
|
|
|
2010-03-19 21:29:42 -04:00
|
|
|
class Real
|
|
|
|
|
|
|
|
def initialize(options={})
|
|
|
|
credentials = Fog::Rackspace.authenticate(options)
|
|
|
|
@auth_token = credentials['X-Auth-Token']
|
|
|
|
cdn_uri = URI.parse(credentials['X-CDN-Management-Url'])
|
|
|
|
@cdn_host = cdn_uri.host
|
|
|
|
@cdn_path = cdn_uri.path
|
|
|
|
@cdn_port = cdn_uri.port
|
|
|
|
@cdn_scheme = cdn_uri.scheme
|
|
|
|
storage_uri = URI.parse(credentials['X-Storage-Url'])
|
|
|
|
@storage_host = storage_uri.host
|
|
|
|
@storage_path = storage_uri.path
|
|
|
|
@storage_port = storage_uri.port
|
|
|
|
@storage_scheme = storage_uri.scheme
|
|
|
|
end
|
|
|
|
|
|
|
|
def cdn_request(params)
|
|
|
|
@cdn_connection = Fog::Connection.new("#{@cdn_scheme}://#{@cdn_host}:#{@cdn_port}")
|
|
|
|
response = @cdn_connection.request({
|
|
|
|
:body => params[:body],
|
|
|
|
:expects => params[:expects],
|
|
|
|
:headers => {
|
|
|
|
'Content-Type' => 'application/json',
|
|
|
|
'X-Auth-Token' => @auth_token
|
|
|
|
}.merge!(params[:headers] || {}),
|
|
|
|
:host => @cdn_host,
|
|
|
|
:method => params[:method],
|
|
|
|
:path => "#{@cdn_path}/#{params[:path]}",
|
|
|
|
:query => params[:query]
|
|
|
|
})
|
|
|
|
unless response.body.empty?
|
|
|
|
response.body = JSON.parse(response.body)
|
|
|
|
end
|
|
|
|
response
|
|
|
|
end
|
|
|
|
|
|
|
|
def storage_request(params, parse_json = true)
|
|
|
|
@storage_connection = Fog::Connection.new("#{@storage_scheme}://#{@storage_host}:#{@storage_port}")
|
|
|
|
response = @storage_connection.request({
|
|
|
|
:body => params[:body],
|
|
|
|
:expects => params[:expects],
|
|
|
|
:headers => {
|
|
|
|
'Content-Type' => 'application/json',
|
|
|
|
'X-Auth-Token' => @auth_token
|
|
|
|
}.merge!(params[:headers] || {}),
|
|
|
|
:host => @storage_host,
|
|
|
|
:method => params[:method],
|
|
|
|
:path => "#{@storage_path}/#{params[:path]}",
|
|
|
|
:query => params[:query]
|
|
|
|
})
|
|
|
|
if !response.body.empty? && parse_json
|
|
|
|
response.body = JSON.parse(response.body)
|
|
|
|
end
|
|
|
|
response
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2009-10-11 16:37:25 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|