2010-10-04 14:02:08 -07:00
|
|
|
require 'fog/core/model'
|
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-02-27 13:39:33 -08:00
|
|
|
|
|
|
|
class File < Fog::Model
|
|
|
|
|
2010-10-18 11:09:19 -07:00
|
|
|
identity :key, :aliases => 'name'
|
2010-02-27 13:39:33 -08:00
|
|
|
|
2010-11-08 16:48:14 -08:00
|
|
|
attribute :content_length, :aliases => ['bytes', 'Content-Length'], :type => :integer
|
2010-10-18 11:09:19 -07:00
|
|
|
attribute :content_type, :aliases => ['content_type', 'Content-Type']
|
|
|
|
attribute :etag, :aliases => ['hash', 'Etag']
|
2010-11-08 16:48:14 -08:00
|
|
|
attribute :last_modified, :aliases => ['last_modified', 'Last-Modified'], :type => :time
|
2012-11-07 23:14:48 -06:00
|
|
|
attribute :access_control_allow_origin, :aliases => ['Access-Control-Allow-Origin']
|
|
|
|
attribute :origin, :aliases => ['Origin']
|
2013-02-05 10:41:59 -06:00
|
|
|
|
|
|
|
attr_writer :public
|
2010-02-27 13:39:33 -08:00
|
|
|
|
2013-02-13 14:23:55 -06:00
|
|
|
attr_accessor :directory
|
|
|
|
attr_writer :public
|
|
|
|
|
2010-02-27 13:39:33 -08:00
|
|
|
def body
|
2010-11-19 13:45:45 -08:00
|
|
|
attributes[:body] ||= if last_modified
|
2010-02-27 13:39:33 -08:00
|
|
|
collection.get(identity).body
|
|
|
|
else
|
|
|
|
''
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-11-19 13:45:45 -08:00
|
|
|
def body=(new_body)
|
|
|
|
attributes[:body] = new_body
|
|
|
|
end
|
|
|
|
|
2012-02-16 14:41:55 -05:00
|
|
|
def copy(target_directory_key, target_file_key, options={})
|
|
|
|
requires :directory, :key
|
2012-05-23 23:29:57 +02:00
|
|
|
options['Content-Type'] ||= content_type if content_type
|
2012-11-07 23:14:48 -06:00
|
|
|
options['Access-Control-Allow-Origin'] ||= access_control_allow_origin if access_control_allow_origin
|
|
|
|
options['Origin'] ||= origin if origin
|
2012-12-22 23:24:03 +00: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
|
|
|
|
|
2010-02-27 13:39:33 -08:00
|
|
|
def destroy
|
|
|
|
requires :directory, :key
|
2012-12-22 23:24:03 +00:00
|
|
|
service.delete_object(directory.key, key)
|
2010-02-27 13:39:33 -08:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2013-02-13 14:23:55 -06: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
|
|
|
|
|
2012-11-02 16:24:12 -07:00
|
|
|
def metadata
|
2013-02-13 14:23:55 -06:00
|
|
|
attributes[:metadata] ||= Fog::Storage::Rackspace::Metadata.new(self)
|
2012-11-02 16:24:12 -07:00
|
|
|
end
|
2013-02-13 14:23:55 -06:00
|
|
|
|
2010-02-27 13:39:33 -08:00
|
|
|
def owner=(new_owner)
|
|
|
|
if new_owner
|
2010-11-19 13:45:45 -08:00
|
|
|
attributes[:owner] = {
|
2010-02-27 13:39:33 -08:00
|
|
|
:display_name => new_owner['DisplayName'],
|
|
|
|
:id => new_owner['ID']
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-01 16:16:37 -06:00
|
|
|
def public?
|
|
|
|
directory.public?
|
|
|
|
end
|
2013-02-15 13:01:17 -06:00
|
|
|
|
2010-11-05 11:37:12 -07:00
|
|
|
def public_url
|
2013-02-15 13:01:17 -06:00
|
|
|
Files::file_url directory.public_url, key
|
2010-11-05 11:37:12 -07:00
|
|
|
end
|
2013-02-01 16:16:37 -06:00
|
|
|
|
2013-02-15 13:01:17 -06:00
|
|
|
def ios_url
|
|
|
|
Files::file_url directory.ios_url, key
|
|
|
|
end
|
|
|
|
|
|
|
|
def streaming_url
|
|
|
|
Files::file_url directory.streaming_url, key
|
|
|
|
end
|
|
|
|
|
2013-02-01 16:16:37 -06:00
|
|
|
def purge_from_cdn
|
|
|
|
if public?
|
|
|
|
service.cdn.purge(self)
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
2010-11-05 11:37:12 -07:00
|
|
|
|
2010-10-07 11:10:01 -07:00
|
|
|
def save(options = {})
|
2010-02-27 13:39:33 -08:00
|
|
|
requires :body, :directory, :key
|
2011-03-04 10:50:11 -08:00
|
|
|
options['Content-Type'] = content_type if content_type
|
2012-11-07 23:14:48 -06:00
|
|
|
options['Access-Control-Allow-Origin'] = access_control_allow_origin if access_control_allow_origin
|
|
|
|
options['Origin'] = origin if origin
|
2013-02-13 14:23:55 -06:00
|
|
|
options.merge!(metadata.to_headers)
|
2012-11-16 14:58:29 -08:00
|
|
|
|
2012-12-22 23:24:03 +00:00
|
|
|
data = service.put_object(directory.key, key, body, options)
|
2012-11-15 21:40:58 -08:00
|
|
|
update_attributes_from(data)
|
2013-02-13 14:23:55 -06:00
|
|
|
|
2011-05-25 14:37:47 -07:00
|
|
|
self.content_length = Fog::Storage.get_body_size(body)
|
2012-03-26 19:14:02 -05:00
|
|
|
self.content_type ||= Fog::Storage.get_content_type(body)
|
2010-02-27 13:39:33 -08:00
|
|
|
true
|
|
|
|
end
|
2012-11-15 21:40:58 -08:00
|
|
|
|
2013-02-13 14:23:55 -06:00
|
|
|
private
|
2012-11-02 16:24:12 -07:00
|
|
|
|
|
|
|
def update_attributes_from(data)
|
|
|
|
merge_attributes(data.headers.reject {|key, value| ['Content-Length', 'Content-Type'].include?(key)})
|
|
|
|
end
|
2010-02-27 13:39:33 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|