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
2013-03-07 17:01:42 -05:00
# @!attribute [rw] directory
# @return [String] The name of the directory
# @note Methods in this class require this attribute to be set
2010-09-02 19:01:19 -04:00
attribute :directory
2013-03-07 17:01:42 -05:00
# @!attribute [rw] limit
# @return [Integer] For an integer value n, limits the number of results to at most n values.
# @see http://docs.rackspace.com/files/api/v1/cf-devguide/content/List_Large_Number_of_Objects-d1e1521.html
2010-02-27 16:39:33 -05:00
attribute :limit
2013-03-07 17:01:42 -05:00
# @!attribute [rw] marker
# @return [String] Given a string value x, return object names greater in value than the specified marker.
# @see http://docs.rackspace.com/files/api/v1/cf-devguide/content/List_Large_Number_of_Objects-d1e1521.html
2010-02-27 16:39:33 -05:00
attribute :marker
2013-03-07 17:01:42 -05:00
# @!attribute [rw] path
# @return [String] For a string value x, return the object names nested in the pseudo path.
# Equivalent to setting delimiter to '/' and prefix to the path with a '/' on the end.
2010-02-27 16:39:33 -05:00
attribute :path
2013-03-07 17:01:42 -05:00
# @!attribute [rw] prefix
# @return [String] For a string value x, causes the results to be limited to object names beginning with the substring x.
2010-02-27 16:39:33 -05:00
attribute :prefix
2011-06-15 17:26:43 -04:00
model Fog :: Storage :: Rackspace :: File
2010-02-27 16:39:33 -05:00
2013-03-07 17:01:42 -05:00
# Returns list of files
2013-03-27 10:50:30 -04:00
# @return [Fog::Storage::Rackspace::Files] Retrieves a list files.
2013-04-15 14:12:14 -04:00
# @raise [Fog::Storage::Rackspace::NotFound] - HTTP 404
# @raise [Fog::Storage::Rackspace::BadRequest] - HTTP 400
# @raise [Fog::Storage::Rackspace::InternalServerError] - HTTP 500
# @raise [Fog::Storage::Rackspace::ServiceError]
2013-03-07 17:01:42 -05:00
# @see http://docs.rackspace.com/files/api/v1/cf-devguide/content/List_Objects-d1e1284.html
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
2013-03-07 17:01:42 -05:00
# Calls block for each file in the directory
2013-03-27 10:50:30 -04:00
# @yieldparam [Fog::Storage::Rackspace::File]
2013-03-07 17:01:42 -05:00
# @return [Fog::Storage::Rackspace::Directory] returns itself
2013-04-15 14:12:14 -04:00
# @raise [Fog::Storage::Rackspace::NotFound] - HTTP 404
# @raise [Fog::Storage::Rackspace::BadRequest] - HTTP 400
# @raise [Fog::Storage::Rackspace::InternalServerError] - HTTP 500
# @raise [Fog::Storage::Rackspace::ServiceError]
2013-03-07 17:01:42 -05:00
# @note This method retrieves files in pages. Page size is defined by the limit attribute
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
2013-03-07 17:01:42 -05:00
# Retrieves file
# @param [String] key of file
# @yield get yields to block after chunk of data has been received (Optional)
# @yieldparam [String] data
# @yieldparam [Integer] remaining
# @yieldparam [Integer] content_length
# @return [Fog::Storage::Rackspace:File]
2013-04-15 14:12:14 -04:00
# @raise [Fog::Storage::Rackspace::NotFound] - HTTP 404
# @raise [Fog::Storage::Rackspace::BadRequest] - HTTP 400
# @raise [Fog::Storage::Rackspace::InternalServerError] - HTTP 500
# @raise [Fog::Storage::Rackspace::ServiceError]
2013-03-11 11:05:23 -04:00
# @note If a block is provided, the body attribute will be empty. By default chunk size is 1 MB. This value can be changed by passing the parameter :chunk_size
2013-03-07 17:01:42 -05:00
# into the :connection_options hash in the service constructor.
# @example Download an image from Cloud Files and save it to file
#
# File.open('download.jpg', 'w') do | f |
# my_directory.files.get("europe.jpg") do |data, remaing, content_length|
# f.syswrite data
# end
# end
#
# @see http://docs.rackspace.com/files/api/v1/cf-devguide/content/Retrieve_Object-d1e1856.html
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
2013-03-07 17:01:42 -05:00
# Returns the public_url for the given object key
# @param key of the object
# @return [String] url for object
2013-04-15 14:12:14 -04:00
# @raise [Fog::Storage::Rackspace::NotFound] - HTTP 404
# @raise [Fog::Storage::Rackspace::BadRequest] - HTTP 400
# @raise [Fog::Storage::Rackspace::InternalServerError] - HTTP 500
# @raise [Fog::Storage::Rackspace::ServiceError]
2013-03-07 17:01:42 -05:00
# @see Directory#public_url
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
2013-03-07 17:01:42 -05:00
# View directory detail without loading file contents
2013-03-11 11:05:23 -04:00
# @param key of the object
2013-03-07 17:01:42 -05:00
# @param options Required for compatibility with other Fog providers. Not Used.
# @return [Fog::Storage::Rackspace::File]
2013-04-15 14:12:14 -04:00
# @raise [Fog::Storage::Rackspace::NotFound] - HTTP 404
# @raise [Fog::Storage::Rackspace::BadRequest] - HTTP 400
# @raise [Fog::Storage::Rackspace::InternalServerError] - HTTP 500
# @raise [Fog::Storage::Rackspace::ServiceError]
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
2013-03-07 17:01:42 -05:00
# Create a new file object
# @param [Hash] attributes of object
# @return [Fog::Storage::Rackspace::File]
2010-02-27 16:39:33 -05:00
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-03-07 17:01:42 -05:00
# Returns an escaped object url
# @param [String] path of the url
# @param [String] key of the object
# @return [String] escaped file url
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