2010-03-13 16:37:24 -05:00
|
|
|
require 'fog/model'
|
|
|
|
|
2009-08-02 19:37:54 -04:00
|
|
|
module Fog
|
|
|
|
module AWS
|
2010-03-13 16:37:24 -05:00
|
|
|
module S3
|
2009-08-02 19:37:54 -04:00
|
|
|
|
2010-01-14 23:44:39 -05:00
|
|
|
class File < Fog::Model
|
2009-08-02 19:37:54 -04:00
|
|
|
|
2009-10-23 13:27:23 -04:00
|
|
|
identity :key, 'Key'
|
|
|
|
|
2010-03-11 10:48:12 -05:00
|
|
|
attr_accessor :body
|
2009-08-30 17:43:01 -04:00
|
|
|
attribute :content_length, 'Content-Length'
|
|
|
|
attribute :content_type, 'Content-Type'
|
2009-09-02 00:41:52 -04:00
|
|
|
attribute :etag, ['Etag', 'ETag']
|
2009-08-30 17:43:01 -04:00
|
|
|
attribute :last_modified, ['Last-Modified', 'LastModified']
|
2009-12-08 14:54:01 -05:00
|
|
|
attribute :owner, 'Owner'
|
2009-08-30 17:43:01 -04:00
|
|
|
attribute :size, 'Size'
|
|
|
|
attribute :storage_class, 'StorageClass'
|
2009-08-02 19:37:54 -04:00
|
|
|
|
2010-02-19 14:30:23 -05:00
|
|
|
def body
|
2010-03-31 00:09:35 -04:00
|
|
|
@body ||= if last_modified && (file = collection.get(identity))
|
|
|
|
file.body
|
2010-02-19 14:30:23 -05:00
|
|
|
else
|
|
|
|
''
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-01-14 23:44:39 -05:00
|
|
|
def directory
|
|
|
|
@directory
|
2009-08-27 23:47:01 -04:00
|
|
|
end
|
|
|
|
|
2010-05-03 18:02:08 -04:00
|
|
|
def copy(target_directory_key, target_file_key)
|
2010-01-14 23:44:39 -05:00
|
|
|
requires :directory, :key
|
2010-05-03 18:02:08 -04:00
|
|
|
data = connection.copy_object(directory.key, @key, target_directory_key, target_file_key).body
|
|
|
|
target_directory = connection.directories.new(:key => target_directory_key)
|
2010-01-14 23:44:39 -05:00
|
|
|
target_file = target_directory.files.new(attributes.merge!(:key => target_file_key))
|
2009-08-04 23:09:08 -04:00
|
|
|
copy_data = {}
|
|
|
|
for key, value in data
|
|
|
|
if ['ETag', 'LastModified'].include?(key)
|
|
|
|
copy_data[key] = value
|
|
|
|
end
|
|
|
|
end
|
2010-01-14 23:44:39 -05:00
|
|
|
target_file.merge_attributes(copy_data)
|
|
|
|
target_file
|
2009-08-04 23:09:08 -04:00
|
|
|
end
|
|
|
|
|
2009-08-30 17:14:11 -04:00
|
|
|
def destroy
|
2010-01-14 23:44:39 -05:00
|
|
|
requires :directory, :key
|
2010-05-03 18:02:08 -04:00
|
|
|
connection.delete_object(directory.key, @key)
|
2009-08-27 23:47:01 -04:00
|
|
|
true
|
2009-09-02 23:17:53 -04:00
|
|
|
end
|
|
|
|
|
2009-12-08 14:54:01 -05:00
|
|
|
def owner=(new_owner)
|
|
|
|
if new_owner
|
|
|
|
@owner = {
|
|
|
|
:display_name => new_owner['DisplayName'],
|
|
|
|
:id => new_owner['ID']
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-08-04 23:09:08 -04:00
|
|
|
def save(options = {})
|
2010-01-14 23:44:39 -05:00
|
|
|
requires :body, :directory, :key
|
2010-05-03 18:02:08 -04:00
|
|
|
data = connection.put_object(directory.key, @key, @body, options)
|
2009-08-04 23:09:08 -04:00
|
|
|
@etag = data.headers['ETag']
|
2009-08-27 23:47:01 -04:00
|
|
|
true
|
2009-08-04 23:09:08 -04:00
|
|
|
end
|
|
|
|
|
2010-06-11 20:27:27 -04:00
|
|
|
def url(expires)
|
|
|
|
requires :key
|
|
|
|
collection.get_url(key, expires)
|
|
|
|
end
|
|
|
|
|
2009-08-04 23:09:08 -04:00
|
|
|
private
|
|
|
|
|
2010-01-14 23:44:39 -05:00
|
|
|
def directory=(new_directory)
|
|
|
|
@directory = new_directory
|
2009-08-04 23:09:08 -04:00
|
|
|
end
|
|
|
|
|
2009-08-02 19:37:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|