def add_attachment(file, options = {})
assert_attachments_property
filename = File.basename(file.path)
content_type = options[:content_type] || begin
mime_types = MIME::Types.of(filename)
mime_types.empty? ? 'application/octet-stream' : mime_types.first.content_type
end
name = options[:name] || filename
data = file.read
if new_record? || !model.properties.has_property?(:rev)
self.attachments ||= {}
self.attachments[name] = {
'content_type' => content_type,
'data' => Base64.encode64(data).chomp,
}
else
adapter = repository.adapter
http = Net::HTTP.new(adapter.uri.host, adapter.uri.port)
uri = Addressable::URI.encode_component("#{attachment_path(name)}?rev=#{self.rev}")
headers = {
'Content-Length' => data.size.to_s,
'Content-Type' => content_type,
}
http.put(uri, data, headers)
self.reload
end
end