Permalink
Browse files

Added get_url for Files class

  • Loading branch information...
shadysayed committed Sep 17, 2012
1 parent 3630815 commit 7aead998112a8dbcefe6aabbc8d825efafdc3410
@@ -54,12 +54,24 @@ def get(key, options = {})
rescue Excon::Errors::NotFound => error
end
- def get_url(key)
+ def get_url(key, expires = nil, options={})
requires :directory
+ # if self.directory.public_url
+ # ::File.join(self.directory.public_url, key)
+ # end
+ connection.get_blob_http_url(directory.key, key, expires, options)
end
def head(key, options = {})
requires :directory
+ data = connection.head_blob(directory.key, key)
+ file_data = data.headers.merge({
+ :key => key
+ })
+ normalize_headers(file_attributes)
+ new(file_data)
+ rescue Excon::Errors::NotFound => error
+ nil
end
alias :each_file_this_page :each
@@ -27,6 +27,8 @@ def get_blob(container, path, options = {}, &block)
end
end
class Mock
+ def get_blob(container, path, options = {}, &block)
+ end
end
end
end
@@ -0,0 +1,40 @@
+module Fog
+ module Storage
+ class AWS
+
+ module GetBlobHttpUrl
+
+ def get_blob_http_url(container_name, blob_name, expires = nil, options = {})
+ unless container_name
+ raise ArgumentError.new('container_name is required')
+ end
+ unless blob_name
+ raise ArgumentError.new('blob_name is required')
+ end
+ host, path = [@host, ::File.join(container_name, blob_name)]
+ http_url({
+ :headers => {},
+ :host => host,
+ :port => 80,
+ :method => 'GET',
+ :path => path,
+ :query => options[:query]
+ }, expires)
+ end
+
+ end
+
+ class Real
+
+ include GetBlobHttpUrl
+
+ end
+
+ class Mock # :nodoc:all
+
+ include GetBlobHttpUrl
+
+ end
+ end
+ end
+end
@@ -0,0 +1,40 @@
+module Fog
+ module Storage
+ class AWS
+
+ module GetBlobHttpsUrl
+
+ def get_blob_https_url(container_name, blob_name, expires = nil, options = {})
+ unless container_name
+ raise ArgumentError.new('container_name is required')
+ end
+ unless blob_name
+ raise ArgumentError.new('blob_name is required')
+ end
+ host, path = [@host, ::File.join(container_name, blob_name)]
+ https_url({
+ :headers => {},
+ :host => host,
+ :port => 443,
+ :method => 'GET',
+ :path => path,
+ :query => options[:query]
+ }, expires)
+ end
+
+ end
+
+ class Real
+
+ include GetBlobHttpsUrl
+
+ end
+
+ class Mock # :nodoc:all
+
+ include GetBlobHttpsUrl
+
+ end
+ end
+ end
+end
@@ -0,0 +1,35 @@
+module Fog
+ module Storage
+ class Azure
+ class Real
+ def head_blob(container, path, options = {}, &block)
+ unless container
+ raise ArgumentError.new("container is required")
+ end
+ unless path
+ raise ArgumentError.new("path is required")
+ end
+ params = {headers: {}}
+ if snapshot = options.delete('snapshot')
+ params[:query] = {'snapshot' => snapshot}
+ end
+
+ if block_given?
+ params[:response_block] = Proc.new
+ end
+
+ request(params.merge!({
+ expects: [200],
+ method: 'HEAD',
+ idempotent: true,
+ path: File.join(container, path) # Fog::Rackspace.escape(path, '/'))
+ }))
+ end
+ end
+ class Mock
+ def head_blob(container, path, options = {}, &block)
+ end
+ end
+ end
+ end
+end
View
@@ -80,6 +80,40 @@ def extract_ms_metadata(dict)
end
result
end
+
+ def http_url(params, expires)
+ scheme_host_path_query(params.merge(:scheme => 'http', :port => 80), expires)
+ end
+
+ def https_url(params, expires)
+ scheme_host_path_query(params.merge(:scheme => 'https', :port => 443), expires)
+ end
+
+
+ private
+ def scheme_host_path_query(params, expires)
+ params[:scheme] ||= @scheme
+ if params[:port] == 80 && params[:scheme] == 'http'
+ params.delete(:port)
+ end
+ if params[:port] == 443 && params[:scheme] == 'https'
+ params.delete(:port)
+ end
+ params[:headers] ||= {}
+ #TODO: add signing for shared access signatures
+ #call AWS escape, don't reinvent the wheel
+ params[:path] = Fog::AWS.escape(params[:path]).gsub('%2F', '/')
+ query = []
+ if params[:query]
+ for key, value in params[:query]
+ #call the AWS escape, there is no need to reimplement
+ query << "#{key}=#{Fog::AWS.escape(value)}"
+ end
+ end
+ port_part = params[:port] && ":#{params[:port]}"
+ "#{params[:scheme]}://#{params[:host]}#{port_part}/#{params[:path]}?#{query.join('&')}"
+ end
+
end
class Real

0 comments on commit 7aead99

Please sign in to comment.