-
Notifications
You must be signed in to change notification settings - Fork 16
/
attachments.js
114 lines (93 loc) · 3.27 KB
/
attachments.js
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
'use strict';
var rawBody = require('raw-body');
var utils = require('../utils.js');
function parseRawBody(req, res, next) {
if (req.params.id === '_design' || req.params.id === '_local') {
return next();
}
rawBody(req, {
length: req.headers['content-length']
}, function (err, string) {
if (err) {
return next(err)
}
req.rawBody = string
next()
});
}
function putAttachment(db, name, req, res) {
var attachment = req.params.attachment
var rev = req.query.rev;
var type = req.get('Content-Type') || 'application/octet-stream';
var body = new Buffer(req.rawBody || '', 'binary');
var opts = req.query;
req.db.putAttachment(name, attachment, rev, body,
type, opts, function (err, response) {
if (err) return utils.sendError(res, err);
utils.sendJSON(res, 201, response);
});
}
// Retrieve a document attachment
function getAttachment(name, req, res) {
var attachment = req.params.attachment;
var opts = req.query;
req.db.get(name, opts, function (err, info) {
if (err) return utils.sendError(res, err);
if (!info._attachments || !info._attachments[attachment]) {
return utils.sendJSON(res, 404, {
error:'not_found',
reason:'missing'
});
};
var type = info._attachments[attachment].content_type;
var md5 = info._attachments[attachment].digest.slice(4);
req.db.getAttachment(name, attachment, opts, function (err, response) {
if (err) return utils.sendError(res, err);
res.setHeader('Content-Type', type);
res.status(200).send(response);
});
});
}
// Delete a document attachment
function deleteAttachment(name, req, res) {
var name = req.params.id;
var attachment = req.params.attachment;
var rev = req.query.rev;
var opts = req.query;
req.db.removeAttachment(name, attachment, rev, function (err, response) {
if (err) return utils.sendError(res, err);
utils.sendJSON(res, 200, response);
});
}
module.exports = function(app, PouchDB) {
app.put('/:db/_design/:id/:attachment(*)', parseRawBody, function (req, res, next) {
putAttachment(req.params.db, '_design/' + req.params.id, req, res);
});
app.put('/:db/:id/:attachment(*)', parseRawBody, function (req, res, next) {
// Be careful not to catch normal design docs or local docs
if (req.params.id === '_design' || req.params.id === '_local') {
return next();
}
putAttachment(req.params.db, req.params.id, req, res);
});
app.get('/:db/_design/:id/:attachment(*)', function (req, res, next) {
getAttachment('_design/' + req.params.id, req, res);
});
app.get('/:db/:id/:attachment(*)', function (req, res, next) {
// Be careful not to catch normal design docs or local docs
if (req.params.id === '_design' || req.params.id === '_local') {
return next();
}
getAttachment(req.params.id, req, res);
});
app.delete('/:db/_design/:id/:attachment(*)', function (req, res, next) {
deleteAttachment('_design/' + req.params.id, req, res);
});
app.delete('/:db/:id/:attachment(*)', function (req, res, next) {
// Be careful not to catch normal design docs or local docs
if (req.params.id === '_design' || req.params.id === '_local') {
return next();
}
deleteAttachment(req.params.id, req, res);
});
};