blob: 5a4647de2b6f3f6755626bdbec0c580423480383 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
function(doc, req){
/* we expect to receive the following in `req.body`:
* {
* 'couch_rev': '<couch_rev>',
* 'u1db_rev': '<u1db_rev>',
* 'content': '<base64 encoded content>',
* 'trans_id': '<reansaction_id>'
* 'conflicts': '<base64 encoded conflicts>',
* 'update_conflicts': <boolean>
* }
*/
var body = JSON.parse(req.body);
// create a new document document
if (!doc) {
doc = {}
doc['_id'] = req['id'];
}
// or fail if couch revisions do not match
else if (doc['_rev'] != body['couch_rev']) {
// of fail if revisions do not match
return [null, 'revision conflict']
}
// store u1db rev
doc.u1db_rev = body['u1db_rev'];
// save content as attachment
if (body['content'] != null) {
// save u1db content as attachment
if (!doc._attachments)
doc._attachments = {};
doc._attachments.u1db_content = {
content_type: "application/octet-stream",
data: body['content'] // should be base64 encoded
};
}
// or delete the attachment if document is tombstone
else if (doc._attachments &&
doc._attachments.u1db_content)
delete doc._attachments.u1db_content;
// store the transaction id
if (!doc.u1db_transactions)
doc.u1db_transactions = [];
var d = new Date();
doc.u1db_transactions.push([d.getTime(), body['trans_id']]);
// save conflicts as attachment if they were sent
if (body['update_conflicts'])
if (body['conflicts'] != null) {
if (!doc._attachments)
doc._attachments = {};
doc._attachments.u1db_conflicts = {
content_type: "application/octet-stream",
data: body['conflicts'] // should be base64 encoded
}
} else {
if(doc._attachments && doc._attachments.u1db_conflicts)
delete doc._attachments.u1db_conflicts
}
return [doc, 'ok'];
}
|