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