lib/fog/storage/openstack/models/file.rb in fog-openstack-0.1.21 vs lib/fog/storage/openstack/models/file.rb in fog-openstack-0.1.22

- old
+ new

@@ -31,20 +31,26 @@ # This metadata enables you to indicate that the object content is compressed without losing the identity of the # underlying media type (Content-Type) of the file, such as a video. # @see http://docs.openstack.org/developer/swift/api/use_content-encoding_metadata.html#use-content-encoding-metadata attribute :content_encoding, :aliases => 'Content-Encoding' + def initialize(new_attributes = {}) + super + @dirty = if last_modified then false else true end + end + def body attributes[:body] ||= if last_modified collection.get(identity).body else '' end end def body=(new_body) attributes[:body] = new_body + @dirty = true end attr_reader :directory def copy(target_directory_key, target_file_key, options = {}) @@ -59,10 +65,11 @@ end def destroy requires :directory, :key service.delete_object(directory.key, key) + @dirty = true true end def metadata attributes[:metadata] ||= headers_to_metadata @@ -98,25 +105,30 @@ requires :key collection.get_url(key) end def save(options = {}) - requires :body, :directory, :key + requires :directory, :key options['Content-Type'] = content_type if content_type options['Content-Disposition'] = content_disposition if content_disposition options['Access-Control-Allow-Origin'] = access_control_allow_origin if access_control_allow_origin options['Origin'] = origin if origin options['X-Delete-At'] = delete_at if delete_at options['X-Delete-After'] = delete_after if delete_after options['Content-Encoding'] = content_encoding if content_encoding options.merge!(metadata_to_headers) - data = service.put_object(directory.key, key, body, options) + if not @dirty + data = service.post_object(directory.key, key, options) + else + requires :body + data = service.put_object(directory.key, key, body, options) + self.content_length = Fog::Storage.get_body_size(body) + self.content_type ||= Fog::Storage.get_content_type(body) + end update_attributes_from(data) refresh_metadata - self.content_length = Fog::Storage.get_body_size(body) - self.content_type ||= Fog::Storage.get_content_type(body) true end private