2010-03-13 16:37:24 -05:00
|
|
|
require 'fog/collection'
|
|
|
|
require 'fog/aws/models/s3/file'
|
|
|
|
|
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 Files < Fog::Collection
|
2009-08-02 19:37:54 -04:00
|
|
|
|
2009-10-06 22:16:12 -04:00
|
|
|
attribute :delimiter, 'Delimiter'
|
2010-04-26 20:04:47 -04:00
|
|
|
attribute :directory
|
2009-10-06 22:16:12 -04:00
|
|
|
attribute :is_truncated, 'IsTruncated'
|
|
|
|
attribute :marker, 'Marker'
|
2010-04-02 13:40:58 -04:00
|
|
|
attribute :max_keys, ['MaxKeys', 'max-keys']
|
2009-10-06 22:16:12 -04:00
|
|
|
attribute :prefix, 'Prefix'
|
2009-08-04 13:51:54 -04:00
|
|
|
|
2010-01-14 23:44:39 -05:00
|
|
|
model Fog::AWS::S3::File
|
2009-10-30 02:35:28 -04:00
|
|
|
|
2009-08-29 14:20:54 -04:00
|
|
|
def all(options = {})
|
2010-03-31 00:04:27 -04:00
|
|
|
options = {
|
|
|
|
'delimiter' => @delimiter,
|
|
|
|
'marker' => @marker,
|
|
|
|
'max-keys' => @max_keys,
|
|
|
|
'prefix' => @prefix
|
|
|
|
}.merge!(options)
|
|
|
|
options = options.reject {|key,value| value.nil? || value.to_s.empty?}
|
2010-01-05 01:03:24 -05:00
|
|
|
merge_attributes(options)
|
2010-03-08 19:19:48 -05:00
|
|
|
parent = directory.collection.get(
|
2010-05-03 18:02:08 -04:00
|
|
|
directory.key,
|
2009-12-24 19:10:14 -05:00
|
|
|
options
|
2009-12-14 13:28:43 -05:00
|
|
|
)
|
2010-03-08 19:19:48 -05:00
|
|
|
if parent
|
|
|
|
load(parent.files.map {|file| file.attributes})
|
2010-01-05 01:03:24 -05:00
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
2009-08-04 23:09:08 -04:00
|
|
|
end
|
|
|
|
|
2009-09-10 14:30:50 -04:00
|
|
|
def get(key, options = {}, &block)
|
2010-05-03 18:02:08 -04:00
|
|
|
data = connection.get_object(directory.key, key, options, &block)
|
2010-01-14 23:44:39 -05:00
|
|
|
file_data = {
|
2009-09-02 23:17:53 -04:00
|
|
|
:body => data.body,
|
|
|
|
:key => key
|
|
|
|
}
|
2009-09-02 00:41:52 -04:00
|
|
|
for key, value in data.headers
|
|
|
|
if ['Content-Length', 'Content-Type', 'ETag', 'Last-Modified'].include?(key)
|
2010-01-14 23:44:39 -05:00
|
|
|
file_data[key] = value
|
2009-08-19 00:26:13 -04:00
|
|
|
end
|
|
|
|
end
|
2010-01-14 23:44:39 -05:00
|
|
|
new(file_data)
|
2009-11-08 15:16:52 -05:00
|
|
|
rescue Excon::Errors::NotFound
|
2009-09-02 00:41:52 -04:00
|
|
|
nil
|
2009-08-19 00:26:13 -04:00
|
|
|
end
|
|
|
|
|
2009-10-03 18:43:19 -04:00
|
|
|
def get_url(key, expires)
|
2010-05-03 18:02:08 -04:00
|
|
|
connection.get_object_url(directory.key, key, expires)
|
2009-10-03 18:43:19 -04:00
|
|
|
end
|
|
|
|
|
2009-08-29 14:20:54 -04:00
|
|
|
def head(key, options = {})
|
2010-05-03 18:02:08 -04:00
|
|
|
data = connection.head_object(directory.key, key, options)
|
2010-01-14 23:44:39 -05:00
|
|
|
file_data = {
|
2009-09-02 23:17:53 -04:00
|
|
|
:key => key
|
|
|
|
}
|
|
|
|
for key, value in data.headers
|
2009-09-10 14:55:42 -04:00
|
|
|
if ['Content-Length', 'Content-Type', 'ETag', 'Last-Modified'].include?(key)
|
2010-01-14 23:44:39 -05:00
|
|
|
file_data[key] = value
|
2009-08-19 00:26:13 -04:00
|
|
|
end
|
|
|
|
end
|
2010-01-14 23:44:39 -05:00
|
|
|
new(file_data)
|
2009-11-08 15:16:52 -05:00
|
|
|
rescue Excon::Errors::NotFound
|
2009-09-02 23:17:53 -04:00
|
|
|
nil
|
2009-08-19 00:26:13 -04:00
|
|
|
end
|
|
|
|
|
2009-08-04 23:09:08 -04:00
|
|
|
def new(attributes = {})
|
2010-01-14 23:44:39 -05:00
|
|
|
super({ :directory => directory }.merge!(attributes))
|
2009-09-02 00:41:52 -04:00
|
|
|
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
|