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
|
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
|
|
|
|
connection.copy_object(directory.key, key, target_directory_key, target_file_key, options)
|
2012-02-16 14:41:55 -05:00
|
|
|
target_directory = connection.directories.new(:key => target_directory_key)
|
|
|
|
target_directory.files.get(target_file_key)
|
|
|
|
end
|
|
|
|
|
2010-02-27 16:39:33 -05:00
|
|
|
def destroy
|
|
|
|
requires :directory, :key
|
2010-11-19 16:45:45 -05:00
|
|
|
connection.delete_object(directory.key, key)
|
2010-02-27 16:39:33 -05:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
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
|
2010-11-19 16:45:45 -05:00
|
|
|
data = connection.put_object(directory.key, key, body, options)
|
2012-03-26 20:14:02 -04:00
|
|
|
merge_attributes(data.headers.reject {|key, value| ['Content-Length', 'Content-Type'].include?(key)})
|
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
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|