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
|
|
|
|
module Rackspace
|
2010-09-08 16:50:23 -04:00
|
|
|
class Storage
|
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
|
|
|
|
|
|
|
|
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
|
|
|
|
requires :directory, :key
|
|
|
|
if @directory.public_url
|
|
|
|
"#{@directory.public_url}/#{key}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-10-07 14:10:01 -04:00
|
|
|
def save(options = {})
|
2010-02-27 16:39:33 -05:00
|
|
|
requires :body, :directory, :key
|
2010-11-19 16:45:45 -05:00
|
|
|
data = connection.put_object(directory.key, key, body, options)
|
2010-11-10 15:46:50 -05:00
|
|
|
merge_attributes(data.headers)
|
2010-11-17 17:54:48 -05:00
|
|
|
if body.is_a?(String)
|
|
|
|
self.content_length = body.size
|
|
|
|
else
|
|
|
|
self.content_length = ::File.size(body.path)
|
|
|
|
end
|
2010-02-27 16:39:33 -05:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def directory=(new_directory)
|
|
|
|
@directory = new_directory
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|