put attachments
This commit is contained in:
parent
902e1bed26
commit
2b7e49c9c6
|
@ -59,7 +59,18 @@ module CouchRest
|
|||
RestClient.get "#{@root}/#{doc}/#{name}"
|
||||
end
|
||||
|
||||
|
||||
# PUT an attachment directly to CouchDB
|
||||
def put_attachment doc, name, file, options = {}
|
||||
docid = CGI.escape(doc['_id'])
|
||||
name = CGI.escape(name)
|
||||
uri = if doc['_rev']
|
||||
"#{@root}/#{docid}/#{name}?rev=#{doc['_rev']}"
|
||||
else
|
||||
"#{@root}/#{docid}/#{name}"
|
||||
end
|
||||
|
||||
JSON.parse(RestClient.put(uri, file, options))
|
||||
end
|
||||
|
||||
# Save a document to CouchDB. This will use the <tt>_id</tt> field from the document as the id for PUT, or request a new UUID from CouchDB, if no <tt>_id</tt> is present on the document. IDs are attached to documents on the client side because POST has the curious property of being automatically retried by proxies in the event of network segmentation and lost responses.
|
||||
def save doc
|
||||
|
|
|
@ -205,6 +205,22 @@ describe CouchRest::Database do
|
|||
|
||||
end
|
||||
|
||||
describe "PUT attachment from file" do
|
||||
before(:each) do
|
||||
filename = File.dirname(__FILE__) + '/../../fixtures/attachments/couchdb.png'
|
||||
@file = File.open(filename)
|
||||
end
|
||||
after(:each) do
|
||||
@file.close
|
||||
end
|
||||
it "should save the attachment to a new doc" do
|
||||
r = @db.put_attachment({'_id' => 'attach-this'}, 'couchdb.png', image = @file.read, {:content_type => 'image/png'})
|
||||
r['ok'].should == true
|
||||
attachment = @db.fetch_attachment("attach-this","couchdb.png")
|
||||
attachment.should == image
|
||||
end
|
||||
end
|
||||
|
||||
describe "PUT document with attachment" do
|
||||
before(:each) do
|
||||
@attach = "<html><head><title>My Doc</title></head><body><p>Has words.</p></body></html>"
|
||||
|
|
BIN
spec/fixtures/attachments/couchdb.png
vendored
Normal file
BIN
spec/fixtures/attachments/couchdb.png
vendored
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.9 KiB |
Loading…
Reference in a new issue