-
Notifications
You must be signed in to change notification settings - Fork 5
/
attachments.js
127 lines (111 loc) · 2.95 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/**
* Functions related to the loading and manipulation of attachments in
* Kanso apps.
*
* @module
*/
var utils = require('./utils'),
mime = require('mime'),
async = require('async'),
fs = require('fs');
/**
*
* @param {Object} doc
* @param {String} path
* @param {Buffer} content
* @returns {Object}
*/
exports.add = function (doc, ddoc_path, original_path, content) {
if (!(content instanceof Buffer)) {
content = new Buffer(content);
}
if (!doc._attachments) {
doc._attachments = {};
}
if (doc._attachments[ddoc_path]) {
throw new Error('Attachment already exists at ' + ddoc_path);
}
doc._attachments[ddoc_path] = {
'content_type': mime.lookup(original_path),
'data': content.toString('base64'),
// custom addition removed in cleanup postprocessor
'_original_path': original_path
};
return doc;
};
/**
* Searchs a path for attachments, adding them to the document.
*
* @param {String} pkgdir - path to the source package
* @param {String} p - path to a file or directory
* @param {Object} doc - the document to extend
* @param {Function} callback
*/
exports.addPath = function (pkgdir, p, doc, callback) {
p = utils.abspath(p, pkgdir);
exports.find(p, function (err, files) {
if (err) {
return callback(err);
}
async.forEach(files, function (f, cb) {
exports.addFile(pkgdir, f, doc, cb);
}, callback);
});
};
/**
* Loads an attachment file and adds its contents to the document
*
* @param {String} pkgdir
* @param {String} p
* @param {Object} doc
* @param {Function} callback
*/
exports.addFile = function (pkgdir, p, doc, callback) {
fs.readFile(p, function (err, content) {
if (err) {
return callback(err);
}
var rel = utils.relpath(p, pkgdir);
try {
exports.add(doc, rel, p, content);
}
catch (e) {
return callback(e)
}
callback();
});
};
/**
* Find all attachments below or at a given path, recursing through
* subdirectories
*
* @param {String} p - the path to search
* @param {Function} callback
*/
exports.find = async.memoize(function (p, callback) {
utils.find(p, exports.filenameFilter(p), callback);
});
/**
* Creates a filter used when searching for attachments. This function omits
* hidden dot-preceeded filenames.
*
* @param {String} p - the path to the directory being searched
* @returns {Function}
*/
exports.filenameFilter = function (p) {
return function (f) {
if (f === p) {
return true;
}
var relpath = utils.relpath(f, p);
// should not start with a '.'
if (/^\.[^\/]?/.test(relpath)) {
return false;
}
// should not contain a file or folder starting with a '.'
if (/\/\./.test(relpath)) {
return false;
}
return true;
};
};