2010-10-04 17:02:08 -04:00
|
|
|
require 'fog/core/model'
|
2010-03-19 21:29:42 -04:00
|
|
|
|
2010-02-27 16:39:33 -05:00
|
|
|
module Fog
|
2011-06-15 17:26:43 -04:00
|
|
|
module Storage
|
|
|
|
class Rackspace
|
2010-02-27 16:39:33 -05:00
|
|
|
|
|
|
|
class File < Fog::Model
|
|
|
|
|
2010-10-18 14:09:19 -04:00
|
|
|
identity :key, :aliases => 'name'
|
2010-02-27 16:39:33 -05:00
|
|
|
|
2010-11-08 19:48:14 -05:00
|
|
|
attribute :content_length, :aliases => ['bytes', 'Content-Length'], :type => :integer
|
2010-10-18 14:09:19 -04:00
|
|
|
attribute :content_type, :aliases => ['content_type', 'Content-Type']
|
|
|
|
attribute :etag, :aliases => ['hash', 'Etag']
|
2010-11-08 19:48:14 -05:00
|
|
|
attribute :last_modified, :aliases => ['last_modified', 'Last-Modified'], :type => :time
|
2012-11-08 00:14:48 -05:00
|
|
|
attribute :access_control_allow_origin, :aliases => ['Access-Control-Allow-Origin']
|
|
|
|
attribute :origin, :aliases => ['Origin']
|
2010-02-27 16:39:33 -05:00
|
|
|
|
|
|
|
def body
|
2010-11-19 16:45:45 -05:00
|
|
|
attributes[:body] ||= if last_modified
|
2010-02-27 16:39:33 -05:00
|
|
|
collection.get(identity).body
|
|
|
|
else
|
|
|
|
''
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-11-19 16:45:45 -05:00
|
|
|
def body=(new_body)
|
|
|
|
attributes[:body] = new_body
|
|
|
|
end
|
|
|
|
|
2010-02-27 16:39:33 -05:00
|
|
|
def directory
|
|
|
|
@directory
|
|
|
|
end
|
|
|
|
|
2012-02-16 14:41:55 -05:00
|
|
|
def copy(target_directory_key, target_file_key, options={})
|
|
|
|
requires :directory, :key
|
2012-05-23 17:29:57 -04:00
|
|
|
options['Content-Type'] ||= content_type if content_type
|
2012-11-08 00:14:48 -05:00
|
|
|
options['Access-Control-Allow-Origin'] ||= access_control_allow_origin if access_control_allow_origin
|
|
|
|
options['Origin'] ||= origin if origin
|
2012-12-22 18:24:03 -05:00
|
|
|
service.copy_object(directory.key, key, target_directory_key, target_file_key, options)
|
|
|
|
target_directory = service.directories.new(:key => target_directory_key)
|
2012-02-16 14:41:55 -05:00
|
|
|
target_directory.files.get(target_file_key)
|
|
|
|
end
|
|
|
|
|
2010-02-27 16:39:33 -05:00
|
|
|
def destroy
|
|
|
|
requires :directory, :key
|
2012-12-22 18:24:03 -05:00
|
|
|
service.delete_object(directory.key, key)
|
2010-02-27 16:39:33 -05:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2012-11-02 19:24:12 -04:00
|
|
|
def metadata
|
2012-11-16 00:40:58 -05:00
|
|
|
@metadata ||= headers_to_metadata
|
2012-11-02 19:24:12 -04:00
|
|
|
end
|
|
|
|
|
2010-02-27 16:39:33 -05:00
|
|
|
def owner=(new_owner)
|
|
|
|
if new_owner
|
2010-11-19 16:45:45 -05:00
|
|
|
attributes[:owner] = {
|
2010-02-27 16:39:33 -05:00
|
|
|
:display_name => new_owner['DisplayName'],
|
|
|
|
:id => new_owner['ID']
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-11-18 14:18:46 -05:00
|
|
|
def public=(new_public)
|
|
|
|
new_public
|
|
|
|
end
|
2013-02-01 17:16:37 -05:00
|
|
|
|
|
|
|
def public?
|
|
|
|
directory.public?
|
|
|
|
end
|
2010-11-18 14:18:46 -05:00
|
|
|
|
2010-11-05 14:37:12 -04:00
|
|
|
def public_url
|
2011-06-10 13:53:20 -04:00
|
|
|
requires :key
|
|
|
|
self.collection.get_url(self.key)
|
2010-11-05 14:37:12 -04:00
|
|
|
end
|
2013-02-01 17:16:37 -05:00
|
|
|
|
|
|
|
def purge_from_cdn
|
|
|
|
if public?
|
|
|
|
service.cdn.purge(self)
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
2010-11-05 14:37:12 -04:00
|
|
|
|
2010-10-07 14:10:01 -04:00
|
|
|
def save(options = {})
|
2010-02-27 16:39:33 -05:00
|
|
|
requires :body, :directory, :key
|
2011-03-04 13:50:11 -05:00
|
|
|
options['Content-Type'] = content_type if content_type
|
2012-11-08 00:14:48 -05:00
|
|
|
options['Access-Control-Allow-Origin'] = access_control_allow_origin if access_control_allow_origin
|
|
|
|
options['Origin'] = origin if origin
|
2012-11-16 00:40:58 -05:00
|
|
|
options.merge!(metadata_to_headers)
|
2012-11-16 17:58:29 -05:00
|
|
|
|
2012-12-22 18:24:03 -05:00
|
|
|
data = service.put_object(directory.key, key, body, options)
|
2012-11-16 00:40:58 -05:00
|
|
|
update_attributes_from(data)
|
|
|
|
refresh_metadata
|
2012-11-16 17:58:29 -05:00
|
|
|
|
2011-05-25 17:37:47 -04:00
|
|
|
self.content_length = Fog::Storage.get_body_size(body)
|
2012-03-26 20:14:02 -04:00
|
|
|
self.content_type ||= Fog::Storage.get_content_type(body)
|
2010-02-27 16:39:33 -05:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def directory=(new_directory)
|
|
|
|
@directory = new_directory
|
|
|
|
end
|
|
|
|
|
2012-11-16 00:40:58 -05:00
|
|
|
def refresh_metadata
|
|
|
|
metadata.reject! {|k, v| v.nil? }
|
2012-11-02 19:24:12 -04:00
|
|
|
end
|
|
|
|
|
2012-11-16 00:40:58 -05:00
|
|
|
def headers_to_metadata
|
|
|
|
key_map = key_mapping
|
|
|
|
Hash[metadata_attributes.map {|k, v| [key_map[k], v] }]
|
|
|
|
end
|
|
|
|
|
|
|
|
def key_mapping
|
|
|
|
key_map = metadata_attributes
|
|
|
|
key_map.each_pair {|k, v| key_map[k] = header_to_key(k)}
|
|
|
|
end
|
|
|
|
|
|
|
|
def header_to_key(opt)
|
|
|
|
opt.gsub(metadata_prefix, '').split('-').map {|k| k[0, 1].downcase + k[1..-1]}.join('_').to_sym
|
|
|
|
end
|
2013-02-11 10:37:05 -05:00
|
|
|
|
2012-11-16 00:40:58 -05:00
|
|
|
def metadata_to_headers
|
2013-02-11 10:37:05 -05:00
|
|
|
hash = {}
|
|
|
|
metadata.each_pair do |k,v|
|
|
|
|
key = metakey(k,v)
|
|
|
|
hash[key] = v
|
|
|
|
end
|
|
|
|
hash
|
|
|
|
end
|
|
|
|
|
|
|
|
def metakey(key, value)
|
|
|
|
prefix = value.nil? ? "X-Remove-Object-Meta-" : "X-Object-Meta-"
|
|
|
|
prefix + key.to_s.split(/[-_]/).map(&:capitalize).join('-')
|
2012-11-16 00:40:58 -05:00
|
|
|
end
|
2013-02-11 10:37:05 -05:00
|
|
|
|
2012-11-16 00:40:58 -05:00
|
|
|
def metadata_attributes
|
2012-12-17 13:08:19 -05:00
|
|
|
if last_modified
|
2012-12-22 18:24:03 -05:00
|
|
|
headers = service.head_object(directory.key, self.key).headers
|
2012-11-16 00:40:58 -05:00
|
|
|
headers.reject! {|k, v| !metadata_attribute?(k)}
|
|
|
|
else
|
|
|
|
{}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def metadata_attribute?(key)
|
|
|
|
key.to_s =~ /^#{metadata_prefix}/
|
2012-11-02 19:24:12 -04:00
|
|
|
end
|
|
|
|
|
2012-11-16 00:40:58 -05:00
|
|
|
def metadata_prefix
|
|
|
|
"X-Object-Meta-"
|
2012-11-02 19:24:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def update_attributes_from(data)
|
|
|
|
merge_attributes(data.headers.reject {|key, value| ['Content-Length', 'Content-Type'].include?(key)})
|
|
|
|
end
|
2010-02-27 16:39:33 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|