2010-10-04 17:02:08 -04:00
require 'fog/core/model'
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-02-27 16:39:33 -05:00
class File < Fog :: Model
2013-03-07 17:01:42 -05:00
# @!attribute [r] key
# @return [String] The name of the file
2010-10-18 14:09:19 -04:00
identity :key , :aliases = > 'name'
2010-02-27 16:39:33 -05:00
2013-03-07 17:01:42 -05:00
# @!attribute [r] content_length
# @return [Integer] The content length of the file
2010-11-08 19:48:14 -05:00
attribute :content_length , :aliases = > [ 'bytes' , 'Content-Length' ] , :type = > :integer
2013-03-07 17:01:42 -05:00
# @!attribute [rw] content_type
# @return [String] The MIME Media Type of the file
# @see http://www.iana.org/assignments/media-types
2010-10-18 14:09:19 -04:00
attribute :content_type , :aliases = > [ 'content_type' , 'Content-Type' ]
2013-03-07 17:01:42 -05:00
2013-03-13 07:12:46 -04:00
attribute :content_disposition , :aliases = > 'Content-Disposition'
2013-03-07 17:01:42 -05:00
# @!attribute [rw] etag
# The MD5 checksum of file. If included file creation request, will ensure integrity of the file.
# @return [String] MD5 checksum of file.
2010-10-18 14:09:19 -04:00
attribute :etag , :aliases = > [ 'hash' , 'Etag' ]
2013-03-07 17:01:42 -05:00
# @!attribute [r] last_modified
# The last time the file was modified
# @return [Time] The last time the file was modified
2010-11-08 19:48:14 -05:00
attribute :last_modified , :aliases = > [ 'last_modified' , 'Last-Modified' ] , :type = > :time
2013-03-07 17:01:42 -05:00
# @!attribute [rw] access_control_allow_origin
# A space delimited list of URLs allowed to make Cross Origin Requests. Format is http://www.example.com. An asterisk (*) allows all.
# @return [String] string containing a list of space delimited URLs
# @see http://docs.rackspace.com/files/api/v1/cf-devguide/content/CORS_Container_Header-d1e1300.html
2012-11-08 00:14:48 -05:00
attribute :access_control_allow_origin , :aliases = > [ 'Access-Control-Allow-Origin' ]
2013-02-05 11:41:59 -05:00
2013-03-07 17:01:42 -05:00
# @!attribute [rw] origin
# @return [String] The origin is the URI of the object's host.
# @see http://docs.rackspace.com/files/api/v1/cf-devguide/content/CORS_Container_Header-d1e1300.html
attribute :origin , :aliases = > [ 'Origin' ]
2010-02-27 16:39:33 -05:00
2013-03-07 17:01:42 -05:00
# @!attribute [r] directory
# @return [Fog::Storage::Rackspace::Directory] directory containing file
2013-02-13 15:23:55 -05:00
attr_accessor :directory
2013-03-07 17:01:42 -05:00
# @!attribute [w] public
# @note Required for compatibility with other Fog providers. Not Used.
2013-02-13 15:23:55 -05:00
attr_writer :public
2013-03-07 17:01:42 -05:00
# Returns the body/contents of 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-07 17:01:42 -05:00
# @example Retrieve and download contents of Cloud Files object to file system
# file_object = directory.files.get('germany.jpg')
# File.open('germany.jpg', 'w') {|f| f.write(file_object.body) }
2013-03-27 10:50:30 -04:00
# @see Fog::Storage::Rackspace::Files#get
2010-02-27 16:39:33 -05:00
def body
2010-11-19 16:45:45 -05:00
attributes [ :body ] || = if last_modified
2010-02-27 16:39:33 -05:00
collection . get ( identity ) . body
else
''
end
end
2013-03-07 17:01:42 -05:00
# Sets the body/contents of file
# @param [String,File] new_body contents of file
2010-11-19 16:45:45 -05:00
def body = ( new_body )
attributes [ :body ] = new_body
end
2013-03-07 17:01:42 -05:00
# Copy file to another directory or directory
# @param [String] target_directory_key
# @param [String] target_file_key
# @param options [Hash] used to pass in file attributes
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/Copy_Object-d1e2241.html
2012-02-16 14:41:55 -05:00
def copy ( target_directory_key , target_file_key , options = { } )
requires :directory , :key
2012-05-23 17:29:57 -04:00
options [ 'Content-Type' ] || = content_type if content_type
2012-11-08 00:14:48 -05:00
options [ 'Access-Control-Allow-Origin' ] || = access_control_allow_origin if access_control_allow_origin
options [ 'Origin' ] || = origin if origin
2012-12-22 18:24:03 -05:00
service . copy_object ( directory . key , key , target_directory_key , target_file_key , options )
target_directory = service . directories . new ( :key = > target_directory_key )
2012-02-16 14:41:55 -05:00
target_directory . files . get ( target_file_key )
end
2013-03-07 17:01:42 -05:00
# Destroy the file
# @return [Boolean] returns true if file is destroyed
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/Delete_Object-d1e2264.html
2010-02-27 16:39:33 -05:00
def destroy
requires :directory , :key
2012-12-22 18:24:03 -05:00
service . delete_object ( directory . key , key )
2010-02-27 16:39:33 -05:00
true
end
2013-03-07 17:01:42 -05:00
# Set file metadata
# @param [Hash,Fog::Storage::Rackspace::Metadata] hash contains key value pairs
2013-02-13 15:23:55 -05:00
def metadata = ( hash )
if hash . is_a? Fog :: Storage :: Rackspace :: Metadata
attributes [ :metadata ] = hash
else
attributes [ :metadata ] = Fog :: Storage :: Rackspace :: Metadata . new ( self , hash )
end
attributes [ :metadata ]
end
2013-03-07 17:01:42 -05:00
# File metadata
# @return [Fog::Storage::Rackspace::Metadata] metadata key value pairs.
2012-11-02 19:24:12 -04:00
def metadata
2013-02-13 15:23:55 -05:00
attributes [ :metadata ] || = Fog :: Storage :: Rackspace :: Metadata . new ( self )
2012-11-02 19:24:12 -04:00
end
2013-02-13 15:23:55 -05:00
2013-03-07 17:01:42 -05:00
# Required for compatibility with other Fog providers. Not Used.
2010-02-27 16:39:33 -05:00
def owner = ( new_owner )
if new_owner
2010-11-19 16:45:45 -05:00
attributes [ :owner ] = {
2010-02-27 16:39:33 -05:00
:display_name = > new_owner [ 'DisplayName' ] ,
:id = > new_owner [ 'ID' ]
}
end
end
2013-03-07 17:01:42 -05:00
# Is file published to CDN
# @return [Boolean] return true if published to CDN
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-02-01 17:16:37 -05:00
def public?
directory . public?
end
2013-02-15 14:01:17 -05:00
2013-03-07 17:01:42 -05:00
# Returns the public url for the file.
2013-03-11 11:05:23 -04:00
# If the file has not been published to the CDN, this method will return nil as it is not publically accessible. This method will return the approprate
2013-03-07 17:01:42 -05:00
# url in the following order:
#
2013-03-27 10:50:30 -04:00
# 1. If the service used to access this file was created with the option :rackspace_cdn_ssl => true, this method will return the SSL-secured URL.
# 2. If the directory's cdn_cname attribute is populated this method will return the cname.
# 3. return the default CDN url.
2013-03-07 17:01:42 -05:00
#
# @return [String] public url for 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-11-05 14:37:12 -04:00
def public_url
2013-02-15 14:01:17 -05:00
Files :: file_url directory . public_url , key
2010-11-05 14:37:12 -04:00
end
2013-02-01 17:16:37 -05:00
2013-03-07 17:01:42 -05:00
# URL used to stream video to iOS devices without needing to convert your video
# @return [String] iOS URL
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/iOS-Streaming-d1f3725.html
2013-02-15 14:01:17 -05:00
def ios_url
Files :: file_url directory . ios_url , key
end
2013-03-07 17:01:42 -05:00
# URL used to stream resources
2013-03-11 11:05:23 -04:00
# @return [String] streaming url
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/Streaming-CDN-Enabled_Containers-d1f3721.html
2013-02-15 14:01:17 -05:00
def streaming_url
Files :: file_url directory . streaming_url , key
end
2013-03-07 17:01:42 -05:00
# Immediately purge file from the CDN network
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 You may only PURGE up to 25 objects per day. Any attempt to purge more than this will result in a 498 status code error (Rate Limited).
# @see http://docs.rackspace.com/files/api/v1/cf-devguide/content/Purge_CDN-Enabled_Objects-d1e3858.html
2013-02-01 17:16:37 -05:00
def purge_from_cdn
if public ?
service . cdn . purge ( self )
else
false
end
end
2010-11-05 14:37:12 -04:00
2013-03-07 17:01:42 -05:00
# Create or updates file and associated metadata
# @param options [Hash] additional parameters to pass to Cloud 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/Create_Update_Object-d1e1965.html
2010-10-07 14:10:01 -04:00
def save ( options = { } )
2010-02-27 16:39:33 -05:00
requires :body , :directory , :key
2011-03-04 13:50:11 -05:00
options [ 'Content-Type' ] = content_type if content_type
2012-11-08 00:14:48 -05:00
options [ 'Access-Control-Allow-Origin' ] = access_control_allow_origin if access_control_allow_origin
options [ 'Origin' ] = origin if origin
2013-03-13 07:12:46 -04:00
options [ 'Content-Disposition' ] = content_disposition if content_disposition
2013-02-13 15:23:55 -05:00
options . merge! ( metadata . to_headers )
2012-11-16 17:58:29 -05:00
2012-12-22 18:24:03 -05:00
data = service . put_object ( directory . key , key , body , options )
2012-11-16 00:40:58 -05:00
update_attributes_from ( data )
2013-02-13 15:23:55 -05:00
2011-05-25 17:37:47 -04:00
self . content_length = Fog :: Storage . get_body_size ( body )
2012-03-26 20:14:02 -04:00
self . content_type || = Fog :: Storage . get_content_type ( body )
2010-02-27 16:39:33 -05:00
true
end
2012-11-16 00:40:58 -05:00
2013-02-13 15:23:55 -05:00
private
2012-11-02 19:24:12 -04:00
def update_attributes_from ( data )
merge_attributes ( data . headers . reject { | key , value | [ 'Content-Length' , 'Content-Type' ] . include? ( key ) } )
end
2010-02-27 16:39:33 -05:00
end
end
end
end