mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
[storage] add public= and public_url for directory, public_url for file
This commit is contained in:
parent
c29a72ccd1
commit
93b8d32845
16 changed files with 153 additions and 12 deletions
|
@ -61,6 +61,28 @@ module Fog
|
|||
@payer = new_payer
|
||||
end
|
||||
|
||||
def public=(new_public)
|
||||
if new_public
|
||||
@acl = 'public-read'
|
||||
else
|
||||
@acl = 'private'
|
||||
end
|
||||
new_public
|
||||
end
|
||||
|
||||
def public_url
|
||||
requires :key
|
||||
if connection.get_bucket_acl(key).body['AccessControlList'].detect {|grant| grant['Grantee']['URI'] == 'http://acs.amazonaws.com/groups/global/AllUsers' && grant['Permission'] == 'READ'}
|
||||
if key.to_s =~ /^(?:[a-z]|\d(?!\d{0,2}(?:\.\d{1,3}){3}$))(?:[a-z0-9]|\.(?![\.\-])|\-(?![\.])){1,61}[a-z0-9]$/
|
||||
"https://#{key}.s3.amazonaws.com"
|
||||
else
|
||||
"https://s3.amazonaws.com/#{key}"
|
||||
end
|
||||
else
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
def save
|
||||
requires :key
|
||||
options = {}
|
||||
|
|
|
@ -60,6 +60,21 @@ module Fog
|
|||
end
|
||||
end
|
||||
|
||||
def public_url
|
||||
requires :directory, :key
|
||||
if directory.public_url
|
||||
"#{directory.public_url}/#{key}"
|
||||
elsif connection.get_object_acl(directory.key, key).body['AccessControlList'].detect {|grant| grant['Grantee']['URI'] == 'http://acs.amazonaws.com/groups/global/AllUsers' && grant['Permission'] == 'READ'}
|
||||
if directory.key.to_s =~ /^(?:[a-z]|\d(?!\d{0,2}(?:\.\d{1,3}){3}$))(?:[a-z0-9]|\.(?![\.\-])|\-(?![\.])){1,61}[a-z0-9]$/
|
||||
"https://#{directory.key}.s3.amazonaws.com/#{key}"
|
||||
else
|
||||
"https://s3.amazonaws.com/#{directory.key}/#{key}"
|
||||
end
|
||||
else
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
def save(options = {})
|
||||
requires :body, :directory, :key
|
||||
if options != {}
|
||||
|
|
|
@ -38,6 +38,13 @@ module Fog
|
|||
|
||||
module Utils
|
||||
|
||||
def cdn
|
||||
@cdn ||= Fog::AWS::CDN.new(
|
||||
:aws_access_key_id => @aws_access_key_id,
|
||||
:aws_secret_access_key => @aws_secret_access_key
|
||||
)
|
||||
end
|
||||
|
||||
def parse_data(data)
|
||||
metadata = {
|
||||
:body => nil,
|
||||
|
@ -93,6 +100,7 @@ module Fog
|
|||
def initialize(options={})
|
||||
require 'mime/types'
|
||||
@aws_access_key_id = options[:aws_access_key_id]
|
||||
@aws_secret_access_key = options[:aws_secret_access_key]
|
||||
options[:region] ||= 'us-east-1'
|
||||
@host = options[:host] || case options[:region]
|
||||
when 'eu-west-1'
|
||||
|
|
|
@ -39,6 +39,28 @@ module Fog
|
|||
end
|
||||
end
|
||||
|
||||
def public=(new_public)
|
||||
if new_public
|
||||
@acl = 'public-read'
|
||||
else
|
||||
@acl = 'private'
|
||||
end
|
||||
new_public
|
||||
end
|
||||
|
||||
def public_url
|
||||
requires :key
|
||||
if connection.get_bucket_acl(key).body['AccessControlList'].detect {|entry| entry['Scope']['type'] == 'AllUsers' && entry['Permission'] == 'READ'}
|
||||
if key.to_s =~ /^(?:[a-z]|\d(?!\d{0,2}(?:\.\d{1,3}){3}$))(?:[a-z0-9]|\.(?![\.\-])|\-(?![\.])){1,61}[a-z0-9]$/
|
||||
"https://#{key}.commondatastorage.googleapis.com"
|
||||
else
|
||||
"https://commondatastorage.googleapis.com/#{key}"
|
||||
end
|
||||
else
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
def save
|
||||
requires :key
|
||||
options = {}
|
||||
|
|
|
@ -63,6 +63,21 @@ module Fog
|
|||
end
|
||||
end
|
||||
|
||||
def public_url
|
||||
requires :directory, :key
|
||||
if directory.public_url
|
||||
"#{directory.public_url}/#{key}"
|
||||
elsif connection.get_object_acl(directory.key, key).body['AccessControlList'].detect {|entry| entry['Scope']['type'] == 'AllUsers' && entry['Permission'] == 'READ'}
|
||||
if directory.key.to_s =~ /^(?:[a-z]|\d(?!\d{0,2}(?:\.\d{1,3}){3}$))(?:[a-z0-9]|\.(?![\.\-])|\-(?![\.])){1,61}[a-z0-9]$/
|
||||
"https://#{directory.key}.commondatastorage.googleapis/#{key}"
|
||||
else
|
||||
"https://commondatastorage.googleapis.com/#{directory.key}/#{key}"
|
||||
end
|
||||
else
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
def save(options = {})
|
||||
requires :body, :directory, :key
|
||||
if options != {}
|
||||
|
|
|
@ -32,6 +32,14 @@ module Fog
|
|||
end
|
||||
end
|
||||
|
||||
def public=(new_public)
|
||||
new_public
|
||||
end
|
||||
|
||||
def public_url
|
||||
nil
|
||||
end
|
||||
|
||||
def save
|
||||
requires :key
|
||||
|
||||
|
|
|
@ -31,6 +31,10 @@ module Fog
|
|||
true
|
||||
end
|
||||
|
||||
def public_url
|
||||
nil
|
||||
end
|
||||
|
||||
def save(options = {})
|
||||
requires :body, :directory, :key
|
||||
file = ::File.new(path, 'w')
|
||||
|
|
|
@ -7,9 +7,9 @@ module Fog
|
|||
model_path 'fog/rackspace/models/cdn'
|
||||
|
||||
request_path 'fog/rackspace/requests/cdn'
|
||||
request :get_cdn_containers
|
||||
request :head_cdn_container
|
||||
request :put_cdn_container
|
||||
request :get_containers
|
||||
request :head_container
|
||||
request :put_container
|
||||
|
||||
class Mock
|
||||
|
||||
|
|
|
@ -32,9 +32,27 @@ module Fog
|
|||
end
|
||||
end
|
||||
|
||||
def public=(new_public)
|
||||
@public = new_public
|
||||
end
|
||||
|
||||
def public_url
|
||||
requires :key
|
||||
@public_url ||= begin
|
||||
begin response = connection.cdn.head_container(key)
|
||||
response.headers['X-CDN-URI']
|
||||
rescue Fog::Service::NotFound
|
||||
nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def save
|
||||
requires :key
|
||||
connection.put_container(key)
|
||||
if @public
|
||||
@public_url = connection.cdn.put_container(key).headers['X-CDN-URI']
|
||||
end
|
||||
true
|
||||
end
|
||||
|
||||
|
|
|
@ -41,6 +41,13 @@ module Fog
|
|||
end
|
||||
end
|
||||
|
||||
def public_url
|
||||
requires :directory, :key
|
||||
if @directory.public_url
|
||||
"#{@directory.public_url}/#{key}"
|
||||
end
|
||||
end
|
||||
|
||||
def save(options = {})
|
||||
requires :body, :directory, :key
|
||||
data = connection.put_object(directory.key, @key, @body, options)
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
module Fog
|
||||
module Rackspace
|
||||
class Storage
|
||||
class CDN
|
||||
class Real
|
||||
|
||||
# List existing cdn-enabled storage containers
|
||||
|
@ -15,7 +15,7 @@ module Fog
|
|||
# * response<~Excon::Response>:
|
||||
# * body<~Array>:
|
||||
# * container<~String>: Name of container
|
||||
def get_cdn_containers(options = {})
|
||||
def get_containers(options = {})
|
||||
response = request(
|
||||
:expects => [200, 204],
|
||||
:method => 'GET',
|
||||
|
@ -29,7 +29,7 @@ module Fog
|
|||
|
||||
class Mock
|
||||
|
||||
def get_cdn_containers(options = {})
|
||||
def get_containers(options = {})
|
||||
Fog::Mock.not_implemented
|
||||
end
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
module Fog
|
||||
module Rackspace
|
||||
class Storage
|
||||
class CDN
|
||||
class Real
|
||||
|
||||
# List cdn properties for a container
|
||||
|
@ -17,7 +17,7 @@ module Fog
|
|||
# * 'X-Log-Retention'<~Boolean> - ?
|
||||
# * 'X-User-Agent-ACL'<~String> - ?
|
||||
# * 'X-Referrer-ACL'<~String> - ?
|
||||
def head_cdn_container(container)
|
||||
def head_container(container)
|
||||
response = request(
|
||||
:expects => 204,
|
||||
:method => 'HEAD',
|
||||
|
@ -31,7 +31,7 @@ module Fog
|
|||
|
||||
class Mock
|
||||
|
||||
def head_cdn_container(container)
|
||||
def head_container(container)
|
||||
Fog::Mock.not_implemented
|
||||
end
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
module Fog
|
||||
module Rackspace
|
||||
class Storage
|
||||
class CDN
|
||||
class Real
|
||||
|
||||
# modify CDN properties for a container
|
||||
|
@ -14,7 +14,7 @@ module Fog
|
|||
# * 'X-Log-Retention'<~Boolean> - ?
|
||||
# * 'X-User-Agent-ACL'<~String> - ?
|
||||
# * 'X-Referrer-ACL'<~String> - ?
|
||||
def put_cdn_container(name, options = {})
|
||||
def put_container(name, options = {})
|
||||
response = request(
|
||||
:expects => [201, 202],
|
||||
:headers => options,
|
||||
|
@ -28,7 +28,7 @@ module Fog
|
|||
|
||||
class Mock
|
||||
|
||||
def put_cdn_container(name, options = {})
|
||||
def put_container(name, options = {})
|
||||
Fog::Mock.not_implemented
|
||||
end
|
||||
|
|
@ -24,6 +24,13 @@ module Fog
|
|||
|
||||
module Utils
|
||||
|
||||
def cdn
|
||||
@cdn ||= Fog::Rackspace::CDN.new(
|
||||
:rackspace_api_key => @rackspace_api_key,
|
||||
:rackspace_username => @rackspace_username
|
||||
)
|
||||
end
|
||||
|
||||
def parse_data(data)
|
||||
metadata = {
|
||||
:body => nil,
|
||||
|
@ -64,6 +71,7 @@ module Fog
|
|||
|
||||
def initialize(options={})
|
||||
require 'mime/types'
|
||||
@rackspace_api_key = options[:rackspace_api_key]
|
||||
@rackspace_username = options[:rackspace_username]
|
||||
@data = self.class.data[@rackspace_username]
|
||||
end
|
||||
|
@ -76,6 +84,8 @@ module Fog
|
|||
def initialize(options={})
|
||||
require 'mime/types'
|
||||
require 'json'
|
||||
@rackspace_api_key = options[:rackspace_api_key]
|
||||
@rackspace_username = options[:rackspace_username]
|
||||
credentials = Fog::Rackspace.authenticate(options)
|
||||
@auth_token = credentials['X-Auth-Token']
|
||||
|
||||
|
|
|
@ -16,6 +16,14 @@ def directory_tests(connection, mocks_implemented = true)
|
|||
@directory.destroy
|
||||
end
|
||||
|
||||
tests("#public=(true)").succeeds do
|
||||
@directory.public=(true)
|
||||
end
|
||||
|
||||
tests("#respond_to?(:public_url)").succeeds do
|
||||
@directory.respond_to?(:public_url)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
tests('failure') do
|
||||
|
|
|
@ -20,6 +20,10 @@ def file_tests(connection, mocks_implemented = true)
|
|||
@file.destroy
|
||||
end
|
||||
|
||||
tests("#respond_to?(:public_url)").succeeds do
|
||||
@directory.respond_to?(:public_url)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
tests('failure') do
|
||||
|
|
Loading…
Reference in a new issue