1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00
fog--fog/lib/fog/aws/models/s3/file.rb

85 lines
2.2 KiB
Ruby
Raw Normal View History

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
identity :key, 'Key'
attr_accessor :body
attribute :content_length, 'Content-Length'
attribute :content_type, 'Content-Type'
attribute :etag, ['Etag', 'ETag']
attribute :last_modified, ['Last-Modified', 'LastModified']
attribute :owner, 'Owner'
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
@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
end
def copy(target_directory_key, target_file_key)
2010-01-14 23:44:39 -05:00
requires :directory, :key
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))
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
end
2009-08-30 17:14:11 -04:00
def destroy
2010-01-14 23:44:39 -05:00
requires :directory, :key
connection.delete_object(directory.key, @key)
true
end
def owner=(new_owner)
if new_owner
@owner = {
:display_name => new_owner['DisplayName'],
:id => new_owner['ID']
}
end
end
def save(options = {})
2010-01-14 23:44:39 -05:00
requires :body, :directory, :key
data = connection.put_object(directory.key, @key, @body, options)
@etag = data.headers['ETag']
true
end
def url(expires)
requires :key
collection.get_url(key, expires)
end
private
2010-01-14 23:44:39 -05:00
def directory=(new_directory)
@directory = new_directory
end
2009-08-02 19:37:54 -04:00
end
end
end
end