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/storage/files.rb

118 lines
3.4 KiB
Ruby
Raw Normal View History

2010-10-04 17:02:08 -04:00
require 'fog/core/collection'
require 'fog/aws/models/storage/file'
2010-03-13 16:37:24 -05:00
2009-08-02 19:37:54 -04:00
module Fog
module Storage
class AWS
2010-01-14 23:44:39 -05:00
class Files < Fog::Collection
extend Fog::Deprecation
deprecate :get_url, :get_https_url
2009-08-02 19:37:54 -04:00
attribute :common_prefixes, :aliases => 'CommonPrefixes'
attribute :delimiter, :aliases => 'Delimiter'
attribute :directory
attribute :is_truncated, :aliases => 'IsTruncated'
attribute :marker, :aliases => 'Marker'
attribute :max_keys, :aliases => ['MaxKeys', 'max-keys']
attribute :prefix, :aliases => 'Prefix'
2009-08-04 13:51:54 -04:00
model Fog::Storage::AWS::File
def all(options = {})
requires :directory
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(
directory.key,
options
)
2010-03-08 19:19:48 -05:00
if parent
merge_attributes(parent.files.attributes)
2010-03-08 19:19:48 -05:00
load(parent.files.map {|file| file.attributes})
2010-01-05 01:03:24 -05:00
else
nil
end
end
alias :each_file_this_page :each
def each
if !block_given?
self
else
subset = dup.all
subset.each_file_this_page {|f| yield f}
while subset.is_truncated
subset = subset.all(:marker => subset.last.key)
subset.each_file_this_page {|f| yield f}
end
self
end
end
def get(key, options = {}, &block)
requires :directory
data = connection.get_object(directory.key, key, options, &block)
file_data = data.headers.merge({
:body => data.body,
:key => key
})
normalise_headers(file_data)
2010-01-14 23:44:39 -05:00
new(file_data)
rescue Excon::Errors::NotFound => error
case error.message
when /<Code>NoSuchKey<\/Code>/
nil
when /<Code>NoSuchBucket<\/Code>/
raise(Fog::Storage::AWS::NotFound.new("Directory #{directory.identity} does not exist."))
else
raise(error)
end
end
def get_http_url(key, expires, options = {})
requires :directory
connection.get_object_http_url(directory.key, key, expires, options)
end
def get_https_url(key, expires, options = {})
requires :directory
connection.get_object_https_url(directory.key, key, expires, options)
end
def head(key, options = {})
requires :directory
data = connection.head_object(directory.key, key, options)
file_data = data.headers.merge({
:key => key
})
normalise_headers(file_data)
2010-01-14 23:44:39 -05:00
new(file_data)
rescue Excon::Errors::NotFound
nil
end
def new(attributes = {})
requires :directory
2010-01-14 23:44:39 -05:00
super({ :directory => directory }.merge!(attributes))
end
def normalise_headers(headers)
headers['Last-Modified'] = Time.parse(headers['Last-Modified'])
headers['ETag'].gsub!('"','')
end
2009-08-02 19:37:54 -04:00
end
end
end
end