2013-02-13 10:11:10 -05:00
|
|
|
module Fog
|
|
|
|
module Storage
|
|
|
|
class Rackspace
|
|
|
|
|
|
|
|
class Metadata
|
|
|
|
|
2013-02-13 15:23:55 -05:00
|
|
|
OBJECT_META_PREFIX = "X-Object-Meta-"
|
|
|
|
OBJECT_REMOVE_META_PREFIX = "X-Remove-Object-Meta-"
|
|
|
|
CONTAINER_META_PREFIX = "X-Container-Meta-"
|
|
|
|
CONTAINER_REMOVE_META_PREFIX = "X-Remove-Container-Meta-"
|
2013-02-13 10:11:10 -05:00
|
|
|
|
2013-02-13 10:56:39 -05:00
|
|
|
# Cloud Files will ignore headers without a value
|
|
|
|
DUMMY_VALUE = 1
|
|
|
|
|
2013-02-13 15:23:55 -05:00
|
|
|
CONTAINER_KEY_REGEX = /^#{CONTAINER_META_PREFIX}(.*)/
|
|
|
|
OBJECT_KEY_REGEX = /^#{OBJECT_META_PREFIX}(.*)/
|
2013-02-13 10:11:10 -05:00
|
|
|
|
|
|
|
|
2013-02-13 15:23:55 -05:00
|
|
|
attr_reader :data, :parent
|
|
|
|
|
|
|
|
def initialize(parent, hash={})
|
2013-02-13 10:11:10 -05:00
|
|
|
@data = hash || {}
|
2013-02-13 11:15:48 -05:00
|
|
|
@deleted_hash = {}
|
2013-02-13 15:23:55 -05:00
|
|
|
@parent = parent
|
2013-02-13 11:15:48 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def delete(key)
|
|
|
|
data.delete(key)
|
|
|
|
@deleted_hash[key] = nil
|
2013-02-13 10:11:10 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def to_headers
|
2013-02-13 11:15:48 -05:00
|
|
|
headers = {}
|
|
|
|
h = data.merge(@deleted_hash)
|
|
|
|
h.each_pair do |k,v|
|
2013-02-13 10:11:10 -05:00
|
|
|
key = to_header_key(k,v)
|
2013-02-13 10:56:39 -05:00
|
|
|
headers[key] = v || DUMMY_VALUE
|
|
|
|
end
|
|
|
|
|
2013-02-13 10:11:10 -05:00
|
|
|
headers
|
|
|
|
end
|
|
|
|
|
2013-02-13 15:23:55 -05:00
|
|
|
def self.from_headers(parent, headers)
|
|
|
|
metadata = Metadata.new(parent)
|
2013-02-13 10:11:10 -05:00
|
|
|
headers.each_pair do |k, v|
|
2013-02-13 15:23:55 -05:00
|
|
|
key = metadata.send(:to_key, k)
|
2013-02-13 10:11:10 -05:00
|
|
|
next unless key
|
2013-02-25 10:54:34 -05:00
|
|
|
metadata.data[key] = v
|
2013-02-13 10:11:10 -05:00
|
|
|
end
|
|
|
|
metadata
|
|
|
|
end
|
|
|
|
|
|
|
|
def respond_to?(method_sym, include_private = false)
|
2013-02-14 11:34:14 -05:00
|
|
|
super(method_sym, include_private) || data.respond_to?(method_sym, include_private)
|
2013-02-13 10:11:10 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def method_missing(method, *args, &block)
|
2013-02-13 11:15:48 -05:00
|
|
|
data.send(method, *args, &block)
|
2013-02-13 10:11:10 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2013-02-13 15:23:55 -05:00
|
|
|
def meta_prefix
|
|
|
|
if parent.is_a? Fog::Storage::Rackspace::Directory
|
|
|
|
CONTAINER_META_PREFIX
|
|
|
|
elsif parent.is_a? Fog::Storage::Rackspace::File
|
|
|
|
OBJECT_META_PREFIX
|
|
|
|
else
|
|
|
|
raise "Metadata prefix is unknown for #{parent.class}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove_meta_prefix
|
|
|
|
if parent.is_a? Fog::Storage::Rackspace::Directory
|
|
|
|
CONTAINER_REMOVE_META_PREFIX
|
|
|
|
elsif parent.is_a? Fog::Storage::Rackspace::File
|
|
|
|
OBJECT_REMOVE_META_PREFIX
|
|
|
|
else
|
|
|
|
raise "Remove Metadata prefix is unknown for #{parent.class}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def meta_prefix_regex
|
|
|
|
if parent.is_a? Fog::Storage::Rackspace::Directory
|
|
|
|
CONTAINER_KEY_REGEX
|
|
|
|
elsif parent.is_a? Fog::Storage::Rackspace::File
|
|
|
|
OBJECT_KEY_REGEX
|
|
|
|
else
|
|
|
|
raise "Metadata prefix is unknown for #{parent.class}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_key(key)
|
|
|
|
m = key.match meta_prefix_regex
|
2013-02-13 10:11:10 -05:00
|
|
|
return nil unless m && m[1]
|
|
|
|
|
|
|
|
a = m[1].split('-')
|
|
|
|
a.collect!(&:downcase)
|
|
|
|
str = a.join('_')
|
|
|
|
str.to_sym
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_header_key(key, value)
|
2013-02-13 15:23:55 -05:00
|
|
|
prefix = value.nil? ? remove_meta_prefix : meta_prefix
|
2013-02-13 10:11:10 -05:00
|
|
|
prefix + key.to_s.split(/[-_]/).collect(&:capitalize).join('-')
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|