2010-10-04 14:02:08 -07:00
|
|
|
require 'fog/core/collection'
|
2011-08-24 14:12:29 -05:00
|
|
|
require 'fog/rackspace/models/storage/file'
|
2010-03-19 18:29:42 -07:00
|
|
|
|
2010-02-27 13:39:33 -08:00
|
|
|
module Fog
|
2011-06-15 14:26:43 -07:00
|
|
|
module Storage
|
|
|
|
class Rackspace
|
2010-09-02 16:01:19 -07:00
|
|
|
|
2010-02-27 13:39:33 -08:00
|
|
|
class Files < Fog::Collection
|
|
|
|
|
2010-09-02 16:01:19 -07:00
|
|
|
attribute :directory
|
2010-02-27 13:39:33 -08:00
|
|
|
attribute :limit
|
|
|
|
attribute :marker
|
|
|
|
attribute :path
|
|
|
|
attribute :prefix
|
|
|
|
|
2011-06-15 14:26:43 -07:00
|
|
|
model Fog::Storage::Rackspace::File
|
2010-02-27 13:39:33 -08:00
|
|
|
|
|
|
|
def all(options = {})
|
2010-09-02 16:01:19 -07:00
|
|
|
requires :directory
|
2010-11-04 11:12:44 -07:00
|
|
|
options = {
|
2010-11-19 13:45:45 -08:00
|
|
|
'limit' => limit,
|
|
|
|
'marker' => marker,
|
|
|
|
'path' => path,
|
|
|
|
'prefix' => prefix
|
2010-11-04 11:12:44 -07:00
|
|
|
}.merge!(options)
|
2010-02-27 13:39:33 -08:00
|
|
|
merge_attributes(options)
|
2010-03-08 16:19:48 -08:00
|
|
|
parent = directory.collection.get(
|
2010-07-17 16:42:05 -05:00
|
|
|
directory.key,
|
2010-02-27 13:39:33 -08:00
|
|
|
options
|
|
|
|
)
|
2010-03-08 16:19:48 -08:00
|
|
|
if parent
|
|
|
|
load(parent.files.map {|file| file.attributes})
|
2010-02-27 13:39:33 -08:00
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-05-02 14:57:11 -07:00
|
|
|
alias :each_file_this_page :each
|
|
|
|
def each
|
|
|
|
if !block_given?
|
|
|
|
self
|
|
|
|
else
|
|
|
|
subset = dup.all
|
|
|
|
|
|
|
|
subset.each_file_this_page {|f| yield f}
|
2012-05-23 20:01:07 +02:00
|
|
|
while subset.length == (subset.limit || 10000)
|
2011-05-02 14:57:11 -07:00
|
|
|
subset = subset.all(:marker => subset.last.key)
|
|
|
|
subset.each_file_this_page {|f| yield f}
|
|
|
|
end
|
|
|
|
|
|
|
|
self
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-11-04 11:12:44 -07:00
|
|
|
def get(key, &block)
|
2010-09-02 16:01:19 -07:00
|
|
|
requires :directory
|
2010-11-04 11:12:44 -07:00
|
|
|
data = connection.get_object(directory.key, key, &block)
|
2010-11-10 12:46:50 -08:00
|
|
|
file_data = data.headers.merge({
|
2010-02-27 13:39:33 -08:00
|
|
|
:body => data.body,
|
|
|
|
:key => key
|
2010-11-10 12:46:50 -08:00
|
|
|
})
|
2010-02-27 13:39:33 -08:00
|
|
|
new(file_data)
|
2011-06-15 14:26:43 -07:00
|
|
|
rescue Fog::Storage::Rackspace::NotFound
|
2010-02-27 13:39:33 -08:00
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2011-06-10 10:53:20 -07:00
|
|
|
def get_url(key)
|
2010-09-02 16:01:19 -07:00
|
|
|
requires :directory
|
2011-06-10 10:53:20 -07:00
|
|
|
if self.directory.public_url
|
2011-09-20 16:00:57 -04:00
|
|
|
"#{self.directory.public_url}/#{Fog::Rackspace.escape(key, '/')}"
|
2011-06-10 10:53:20 -07:00
|
|
|
end
|
2010-02-27 13:39:33 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
def head(key, options = {})
|
2010-09-02 16:01:19 -07:00
|
|
|
requires :directory
|
2010-11-23 11:00:48 -08:00
|
|
|
data = connection.head_object(directory.key, key)
|
2010-11-10 12:46:50 -08:00
|
|
|
file_data = data.headers.merge({
|
|
|
|
:key => key
|
|
|
|
})
|
2010-02-27 13:39:33 -08:00
|
|
|
new(file_data)
|
2011-06-15 14:26:43 -07:00
|
|
|
rescue Fog::Storage::Rackspace::NotFound
|
2010-02-27 13:39:33 -08:00
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def new(attributes = {})
|
2010-09-02 16:01:19 -07:00
|
|
|
requires :directory
|
2010-02-27 13:39:33 -08:00
|
|
|
super({ :directory => directory }.merge!(attributes))
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|