1 |
|
2 | (function() {
|
3 |
|
4 | var URL, attachments, dc, ec, proxy;
|
5 |
|
6 | ec = encodeURIComponent;
|
7 |
|
8 | dc = decodeURIComponent;
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | attachments = function(our_proxy) {
|
17 | var download_uri, handler, target, thru, upload_uri;
|
18 | target = our_proxy.target;
|
19 | our_proxy.target = function(pathname) {
|
20 | var $;
|
21 | if (!($ = pathname.match(/^\/([^\/]+)\/(.+)$/))) {
|
22 | return null;
|
23 | }
|
24 | return typeof target === "function" ? target(pathname, dc($[1]), $[2]) : void 0;
|
25 | };
|
26 | ({handler, download_uri, upload_uri} = proxy(our_proxy));
|
27 | thru = function(stream) {
|
28 | return stream.map(function(msg) {
|
29 | var ec_id, id, rev;
|
30 | rev = msg.getIn(['value', 'rev']);
|
31 | attachments = msg.getIn(['doc', '_attachments']);
|
32 | if (!((rev != null) && (attachments != null))) {
|
33 | return msg;
|
34 | }
|
35 | id = msg.get('id');
|
36 | ec_id = ec(id);
|
37 | return msg.withMutations(function(msg) {
|
38 | attachments.forEach(function(rec, name) {
|
39 | var path;
|
40 | path = `/${ec_id}/${ec(name)}`;
|
41 | if (rec.has('stub')) {
|
42 | msg = msg.setIn(['doc', '_attachments', name, 'download_uri'], download_uri(path, rev));
|
43 | }
|
44 | msg = msg.setIn(['doc', '_attachments', name, 'upload_uri'], upload_uri(path, rev));
|
45 | });
|
46 | return msg;
|
47 | });
|
48 | });
|
49 | };
|
50 | return {handler, thru};
|
51 | };
|
52 |
|
53 |
|
54 |
|
55 | module.exports = attachments;
|
56 |
|
57 | proxy = require('./util/attachments-proxy');
|
58 |
|
59 | ({URL} = require('url'));
|
60 |
|
61 | }).call(this);
|