2009-10-11 16:37:25 -04:00
|
|
|
module Fog
|
|
|
|
module Rackspace
|
|
|
|
class Files
|
|
|
|
|
|
|
|
def self.reload
|
2009-10-31 15:26:49 -04:00
|
|
|
load "fog/rackspace/requests/files/delete_container.rb"
|
2009-11-02 21:47:24 -05:00
|
|
|
load "fog/rackspace/requests/files/delete_object.rb"
|
2009-10-31 00:41:39 -04:00
|
|
|
load "fog/rackspace/requests/files/get_container.rb"
|
2009-10-11 16:37:25 -04:00
|
|
|
load "fog/rackspace/requests/files/get_containers.rb"
|
2009-10-31 00:41:39 -04:00
|
|
|
load "fog/rackspace/requests/files/head_container.rb"
|
|
|
|
load "fog/rackspace/requests/files/head_containers.rb"
|
|
|
|
load "fog/rackspace/requests/files/put_container.rb"
|
2009-11-02 21:47:24 -05:00
|
|
|
load "fog/rackspace/requests/files/put_object.rb"
|
2009-10-11 16:37:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
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
|
2010-01-09 20:32:25 -05:00
|
|
|
@cdn_connection = Fog::Connection.new("#{@cdn_scheme}://#{@cdn_host}:#{@cdn_port}")
|
2009-10-11 16:37:25 -04:00
|
|
|
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
|
2010-01-09 20:32:25 -05:00
|
|
|
@storage_connection = Fog::Connection.new("#{@storage_scheme}://#{@storage_host}:#{@storage_port}")
|
2009-10-11 16:37:25 -04:00
|
|
|
end
|
|
|
|
|
2009-10-31 13:59:50 -04:00
|
|
|
def parse_data(data)
|
|
|
|
metadata = {
|
|
|
|
:body => nil,
|
|
|
|
:headers => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
if data.is_a?(String)
|
|
|
|
metadata[:body] = data
|
|
|
|
metadata[:headers]['Content-Length'] = metadata[:body].size.to_s
|
|
|
|
else
|
|
|
|
filename = File.basename(data.path)
|
|
|
|
unless (mime_types = MIME::Types.of(filename)).empty?
|
|
|
|
metadata[:headers]['Content-Type'] = mime_types.first.content_type
|
|
|
|
end
|
|
|
|
metadata[:body] = data.read
|
|
|
|
metadata[:headers]['Content-Length'] = File.size(data.path).to_s
|
|
|
|
end
|
|
|
|
# metadata[:headers]['Content-MD5'] = Base64.encode64(Digest::MD5.digest(metadata[:body])).strip
|
|
|
|
metadata
|
|
|
|
end
|
|
|
|
|
2009-10-11 16:37:25 -04:00
|
|
|
def cdn_request(params)
|
2010-01-09 20:32:25 -05:00
|
|
|
response = @cdn_connection.request({
|
2009-10-11 16:37:25 -04:00
|
|
|
:body => params[:body],
|
|
|
|
:expects => params[:expects],
|
|
|
|
:headers => {
|
|
|
|
'X-Auth-Token' => @auth_token
|
2009-11-06 02:54:10 -05:00
|
|
|
}.merge!(params[:headers] || {}),
|
2009-10-11 16:37:25 -04:00
|
|
|
:host => @cdn_host,
|
|
|
|
:method => params[:method],
|
2009-11-05 03:02:26 -05:00
|
|
|
:path => "#{@cdn_path}/#{params[:path]}",
|
|
|
|
:query => params[:query]
|
2009-10-11 16:37:25 -04:00
|
|
|
})
|
2009-11-05 03:02:26 -05:00
|
|
|
unless response.body.empty?
|
2009-10-11 16:37:25 -04:00
|
|
|
response.body = JSON.parse(response.body)
|
|
|
|
end
|
|
|
|
response
|
|
|
|
end
|
|
|
|
|
|
|
|
def storage_request(params)
|
2010-01-09 20:32:25 -05:00
|
|
|
response = @storage_connection.request({
|
2009-10-11 16:37:25 -04:00
|
|
|
:body => params[:body],
|
|
|
|
:expects => params[:expects],
|
|
|
|
:headers => {
|
|
|
|
'X-Auth-Token' => @auth_token
|
2009-11-06 02:54:10 -05:00
|
|
|
}.merge!(params[:headers] || {}),
|
2009-10-11 16:37:25 -04:00
|
|
|
:host => @storage_host,
|
|
|
|
:method => params[:method],
|
2009-11-05 03:02:26 -05:00
|
|
|
:path => "#{@storage_path}/#{params[:path]}",
|
|
|
|
:query => params[:query]
|
2009-10-11 16:37:25 -04:00
|
|
|
})
|
2009-11-05 03:02:26 -05:00
|
|
|
unless response.body.empty?
|
2009-10-11 16:37:25 -04:00
|
|
|
response.body = JSON.parse(response.body)
|
|
|
|
end
|
|
|
|
response
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
Fog::Rackspace::Files.reload
|