2010-10-04 14:02:08 -07:00
|
|
|
require 'fog/core/model'
|
2011-08-24 14:12:29 -05:00
|
|
|
require 'fog/rackspace/models/storage/files'
|
2013-02-13 09:11:10 -06:00
|
|
|
require 'fog/rackspace/models/storage/metadata'
|
2010-03-19 18:29:42 -07:00
|
|
|
|
2010-02-27 13:39:33 -08:00
|
|
|
module Fog
|
2011-06-15 14:26:43 -07:00
|
|
|
module Storage
|
|
|
|
class Rackspace
|
2010-02-27 13:39:33 -08:00
|
|
|
|
|
|
|
class Directory < Fog::Model
|
|
|
|
|
2010-09-09 17:50:38 -07:00
|
|
|
identity :key, :aliases => 'name'
|
2010-02-27 13:39:33 -08:00
|
|
|
|
2013-02-13 09:11:10 -06:00
|
|
|
attribute :bytes, :aliases => 'X-Container-Bytes-Used', :type => :integer
|
|
|
|
attribute :count, :aliases => 'X-Container-Object-Count', :type => :integer
|
2011-06-16 16:22:37 -04:00
|
|
|
attribute :cdn_cname
|
2013-02-04 09:02:30 -06:00
|
|
|
|
2013-02-05 10:41:59 -06:00
|
|
|
attr_writer :public, :public_url
|
2010-02-27 13:39:33 -08:00
|
|
|
|
2013-02-13 09:11:10 -06:00
|
|
|
def metadata=(hash)
|
|
|
|
if hash.is_a? Fog::Storage::Rackspace::Metadata
|
2013-02-13 14:23:55 -06:00
|
|
|
attributes[:metadata] = hash
|
2013-02-13 09:11:10 -06:00
|
|
|
else
|
2013-02-13 14:23:55 -06:00
|
|
|
attributes[:metadata] = Fog::Storage::Rackspace::Metadata.new(self, hash)
|
2013-02-13 09:11:10 -06:00
|
|
|
end
|
2013-02-13 14:23:55 -06:00
|
|
|
attributes[:metadata]
|
2013-02-13 09:11:10 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def metadata
|
2013-02-13 14:23:55 -06:00
|
|
|
unless attributes[:metadata]
|
2013-02-14 09:56:57 -06:00
|
|
|
response = service.head_container(key)
|
2013-02-13 14:23:55 -06:00
|
|
|
attributes[:metadata] = Fog::Storage::Rackspace::Metadata.from_headers(self, response.headers)
|
2013-02-14 09:56:57 -06:00
|
|
|
end
|
2013-02-13 14:23:55 -06:00
|
|
|
attributes[:metadata]
|
2013-02-13 09:11:10 -06:00
|
|
|
end
|
|
|
|
|
2010-02-27 13:39:33 -08:00
|
|
|
def destroy
|
2010-05-02 19:59:15 -07:00
|
|
|
requires :key
|
2012-12-22 23:24:03 +00:00
|
|
|
service.delete_container(key)
|
2013-02-15 13:01:17 -06:00
|
|
|
service.cdn.publish_container(self, false) if cdn_enabled?
|
2010-02-27 13:39:33 -08:00
|
|
|
true
|
|
|
|
rescue Excon::Errors::NotFound
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
def files
|
|
|
|
@files ||= begin
|
2011-06-15 14:26:43 -07:00
|
|
|
Fog::Storage::Rackspace::Files.new(
|
2010-02-27 13:39:33 -08:00
|
|
|
:directory => self,
|
2012-12-22 23:24:03 +00:00
|
|
|
:service => service
|
2010-02-27 13:39:33 -08:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-01 16:16:37 -06:00
|
|
|
def public?
|
2013-02-11 14:39:48 -06:00
|
|
|
if @public.nil?
|
|
|
|
@public ||= (key && public_url) ? true : false
|
|
|
|
end
|
|
|
|
@public
|
2010-11-05 11:37:12 -07:00
|
|
|
end
|
2013-02-01 16:16:37 -06:00
|
|
|
|
2013-02-11 14:39:48 -06:00
|
|
|
def reload
|
|
|
|
@public = nil
|
2013-02-15 13:01:17 -06:00
|
|
|
@urls = nil
|
2013-02-11 14:39:48 -06:00
|
|
|
@files = nil
|
|
|
|
super
|
2013-02-01 16:16:37 -06:00
|
|
|
end
|
2013-02-15 13:01:17 -06:00
|
|
|
|
2013-02-04 09:02:30 -06:00
|
|
|
def public_url
|
2013-02-15 13:01:17 -06:00
|
|
|
return nil if urls.empty?
|
|
|
|
return urls[:ssl_uri] if service.ssl?
|
|
|
|
cdn_cname || urls[:uri]
|
|
|
|
end
|
|
|
|
|
|
|
|
def ios_url
|
|
|
|
urls[:ios_uri]
|
|
|
|
end
|
|
|
|
|
|
|
|
def streaming_url
|
|
|
|
urls[:streaming_uri]
|
2010-11-05 11:37:12 -07:00
|
|
|
end
|
|
|
|
|
2010-02-27 13:39:33 -08:00
|
|
|
def save
|
2010-05-02 19:59:15 -07:00
|
|
|
requires :key
|
2013-02-22 09:07:27 -06:00
|
|
|
create_container
|
2013-02-15 13:01:17 -06:00
|
|
|
raise Fog::Storage::Rackspace::Error.new("Directory can not be set as :public without a CDN provided") if public? && !cdn_enabled?
|
|
|
|
@urls = service.cdn.publish_container(self, public?)
|
2010-02-27 13:39:33 -08:00
|
|
|
true
|
|
|
|
end
|
2013-02-05 10:41:59 -06:00
|
|
|
|
2013-02-15 13:01:17 -06:00
|
|
|
private
|
|
|
|
|
|
|
|
def cdn_enabled?
|
|
|
|
service.cdn && service.cdn.enabled?
|
|
|
|
end
|
|
|
|
|
|
|
|
def urls
|
|
|
|
requires :key
|
|
|
|
return {} unless cdn_enabled?
|
|
|
|
@urls ||= service.cdn.urls(self)
|
|
|
|
end
|
|
|
|
|
2013-02-22 09:07:27 -06:00
|
|
|
def create_container
|
2013-02-25 08:26:15 -06:00
|
|
|
headers = attributes[:metadata].nil? ? {} : metadata.to_headers
|
2013-02-22 09:07:27 -06:00
|
|
|
service.put_container(key, headers)
|
2013-02-05 10:41:59 -06:00
|
|
|
end
|
2010-02-27 13:39:33 -08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|