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/rackspace/models/storage/file.rb
2013-03-11 10:05:23 -05:00

192 lines
7.9 KiB
Ruby

require 'fog/core/model'
module Fog
module Storage
class Rackspace
class File < Fog::Model
# @!attribute [r] key
# @return [String] The name of the file
identity :key, :aliases => 'name'
# @!attribute [r] content_length
# @return [Integer] The content length of the file
attribute :content_length, :aliases => ['bytes', 'Content-Length'], :type => :integer
# @!attribute [rw] content_type
# @return [String] The MIME Media Type of the file
# @see http://www.iana.org/assignments/media-types
attribute :content_type, :aliases => ['content_type', 'Content-Type']
# @!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.
attribute :etag, :aliases => ['hash', 'Etag']
# @!attribute [r] last_modified
# The last time the file was modified
# @return [Time] The last time the file was modified
attribute :last_modified, :aliases => ['last_modified', 'Last-Modified'], :type => :time
# @!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
attribute :access_control_allow_origin, :aliases => ['Access-Control-Allow-Origin']
# @!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']
# @!attribute [r] directory
# @return [Fog::Storage::Rackspace::Directory] directory containing file
attr_accessor :directory
# @!attribute [w] public
# @note Required for compatibility with other Fog providers. Not Used.
attr_writer :public
# Returns the body/contents of file
# @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) }
# @see Fog::Storage::Rackspace:Files#get
def body
attributes[:body] ||= if last_modified
collection.get(identity).body
else
''
end
end
# Sets the body/contents of file
# @param [String,File] new_body contents of file
def body=(new_body)
attributes[:body] = new_body
end
# 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
# @see http://docs.rackspace.com/files/api/v1/cf-devguide/content/Copy_Object-d1e2241.html
def copy(target_directory_key, target_file_key, options={})
requires :directory, :key
options['Content-Type'] ||= content_type if content_type
options['Access-Control-Allow-Origin'] ||= access_control_allow_origin if access_control_allow_origin
options['Origin'] ||= origin if origin
service.copy_object(directory.key, key, target_directory_key, target_file_key, options)
target_directory = service.directories.new(:key => target_directory_key)
target_directory.files.get(target_file_key)
end
# Destroy the file
# @return [Boolean] returns true if file is destroyed
# @see http://docs.rackspace.com/files/api/v1/cf-devguide/content/Delete_Object-d1e2264.html
def destroy
requires :directory, :key
service.delete_object(directory.key, key)
true
end
# Set file metadata
# @param [Hash,Fog::Storage::Rackspace::Metadata] hash contains key value pairs
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
# File metadata
# @return [Fog::Storage::Rackspace::Metadata] metadata key value pairs.
def metadata
attributes[:metadata] ||= Fog::Storage::Rackspace::Metadata.new(self)
end
# Required for compatibility with other Fog providers. Not Used.
def owner=(new_owner)
if new_owner
attributes[:owner] = {
:display_name => new_owner['DisplayName'],
:id => new_owner['ID']
}
end
end
# Is file published to CDN
# @return [Boolean] return true if published to CDN
def public?
directory.public?
end
# Returns the public url for the file.
# 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
# url in the following order:
#
# 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.
#
# @return [String] public url for file
def public_url
Files::file_url directory.public_url, key
end
# URL used to stream video to iOS devices without needing to convert your video
# @return [String] iOS URL
# @see http://docs.rackspace.com/files/api/v1/cf-devguide/content/iOS-Streaming-d1f3725.html
def ios_url
Files::file_url directory.ios_url, key
end
# URL used to stream resources
# @return [String] streaming url
# @see http://docs.rackspace.com/files/api/v1/cf-devguide/content/Streaming-CDN-Enabled_Containers-d1f3721.html
def streaming_url
Files::file_url directory.streaming_url, key
end
# Immediately purge file from the CDN network
# @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
def purge_from_cdn
if public?
service.cdn.purge(self)
else
false
end
end
# Create or updates file and associated metadata
# @param options [Hash] additional parameters to pass to Cloud Files
# @see http://docs.rackspace.com/files/api/v1/cf-devguide/content/Create_Update_Object-d1e1965.html
def save(options = {})
requires :body, :directory, :key
options['Content-Type'] = content_type if content_type
options['Access-Control-Allow-Origin'] = access_control_allow_origin if access_control_allow_origin
options['Origin'] = origin if origin
options.merge!(metadata.to_headers)
data = service.put_object(directory.key, key, body, options)
update_attributes_from(data)
self.content_length = Fog::Storage.get_body_size(body)
self.content_type ||= Fog::Storage.get_content_type(body)
true
end
private
def update_attributes_from(data)
merge_attributes(data.headers.reject {|key, value| ['Content-Length', 'Content-Type'].include?(key)})
end
end
end
end
end