2010-10-04 14:02:08 -07:00
|
|
|
require 'fog/core/model'
|
2010-03-13 13:37:24 -08:00
|
|
|
|
2009-08-02 16:37:54 -07:00
|
|
|
module Fog
|
|
|
|
module AWS
|
2010-09-08 14:40:02 -07:00
|
|
|
class Storage
|
2009-08-02 16:37:54 -07:00
|
|
|
|
2010-01-14 20:44:39 -08:00
|
|
|
class File < Fog::Model
|
2009-08-02 16:37:54 -07:00
|
|
|
|
2010-09-07 11:30:02 -07:00
|
|
|
identity :key, :aliases => 'Key'
|
2009-10-23 10:27:23 -07:00
|
|
|
|
2010-09-07 11:30:02 -07:00
|
|
|
attribute :content_length, :aliases => 'Content-Length'
|
|
|
|
attribute :content_type, :aliases => 'Content-Type'
|
|
|
|
attribute :etag, :aliases => ['Etag', 'ETag']
|
|
|
|
attribute :last_modified, :aliases => ['Last-Modified', 'LastModified']
|
|
|
|
attribute :owner, :aliases => 'Owner'
|
|
|
|
attribute :size, :aliases => 'Size'
|
|
|
|
attribute :storage_class, :aliases => 'StorageClass'
|
2009-08-02 16:37:54 -07:00
|
|
|
|
2010-09-23 09:38:55 -07:00
|
|
|
def acl=(new_acl)
|
|
|
|
valid_acls = ['private', 'public-read', 'public-read-write', 'authenticated-read']
|
|
|
|
unless valid_acls.include?(new_acl)
|
|
|
|
raise ArgumentError.new("acl must be one of [#{valid_acls.join(', ')}]")
|
|
|
|
end
|
|
|
|
@acl = new_acl
|
|
|
|
end
|
|
|
|
|
2010-02-19 11:30:23 -08:00
|
|
|
def body
|
2010-03-30 21:09:35 -07:00
|
|
|
@body ||= if last_modified && (file = collection.get(identity))
|
|
|
|
file.body
|
2010-02-19 11:30:23 -08:00
|
|
|
else
|
|
|
|
''
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-01-14 20:44:39 -08:00
|
|
|
def directory
|
|
|
|
@directory
|
2009-08-27 20:47:01 -07:00
|
|
|
end
|
|
|
|
|
2010-05-03 15:02:08 -07:00
|
|
|
def copy(target_directory_key, target_file_key)
|
2010-01-14 20:44:39 -08:00
|
|
|
requires :directory, :key
|
2010-05-03 15:02:08 -07: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 20:44:39 -08:00
|
|
|
target_file = target_directory.files.new(attributes.merge!(:key => target_file_key))
|
2009-08-04 20:09:08 -07:00
|
|
|
copy_data = {}
|
|
|
|
for key, value in data
|
|
|
|
if ['ETag', 'LastModified'].include?(key)
|
|
|
|
copy_data[key] = value
|
|
|
|
end
|
|
|
|
end
|
2010-01-14 20:44:39 -08:00
|
|
|
target_file.merge_attributes(copy_data)
|
|
|
|
target_file
|
2009-08-04 20:09:08 -07:00
|
|
|
end
|
|
|
|
|
2009-08-30 14:14:11 -07:00
|
|
|
def destroy
|
2010-01-14 20:44:39 -08:00
|
|
|
requires :directory, :key
|
2010-05-03 15:02:08 -07:00
|
|
|
connection.delete_object(directory.key, @key)
|
2009-08-27 20:47:01 -07:00
|
|
|
true
|
2009-09-02 20:17:53 -07:00
|
|
|
end
|
|
|
|
|
2010-10-12 16:22:12 -07:00
|
|
|
remove_method :owner=
|
2009-12-08 11:54:01 -08:00
|
|
|
def owner=(new_owner)
|
|
|
|
if new_owner
|
|
|
|
@owner = {
|
|
|
|
:display_name => new_owner['DisplayName'],
|
|
|
|
:id => new_owner['ID']
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-08-04 20:09:08 -07:00
|
|
|
def save(options = {})
|
2010-01-14 20:44:39 -08:00
|
|
|
requires :body, :directory, :key
|
2010-09-23 09:38:55 -07:00
|
|
|
if @acl
|
|
|
|
options['x-amz-acl'] = @acl
|
|
|
|
end
|
2010-05-03 15:02:08 -07:00
|
|
|
data = connection.put_object(directory.key, @key, @body, options)
|
2009-08-04 20:09:08 -07:00
|
|
|
@etag = data.headers['ETag']
|
2009-08-27 20:47:01 -07:00
|
|
|
true
|
2009-08-04 20:09:08 -07:00
|
|
|
end
|
|
|
|
|
2010-06-12 08:27:27 +08:00
|
|
|
def url(expires)
|
|
|
|
requires :key
|
|
|
|
collection.get_url(key, expires)
|
|
|
|
end
|
|
|
|
|
2009-08-04 20:09:08 -07:00
|
|
|
private
|
|
|
|
|
2010-01-14 20:44:39 -08:00
|
|
|
def directory=(new_directory)
|
|
|
|
@directory = new_directory
|
2009-08-04 20:09:08 -07:00
|
|
|
end
|
|
|
|
|
2009-08-02 16:37:54 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|