-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcombo.js
More file actions
67 lines (58 loc) · 1.91 KB
/
Copy pathcombo.js
File metadata and controls
67 lines (58 loc) · 1.91 KB
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
var libmime = require('mime');
var fs = require('fs');
var path = require('path');
module.exports = Combo;
function Combo(config) {
return function(req, res, next) {
var url = req.originalUrl;
var pathStart,
files = [];
if(url.indexOf(config.comboBase) > 0) {
var __tmp = url.split(config.comboBase);
pathStart = __tmp[0];
files = __tmp[1].split(config.comboSep);
files = files.map(function(file) {
if(file.indexOf('?') > 0) {
file = file.substr(0, file.indexOf('?'));
}
return file;
});
if(files.length > 1) {
res.set('Content-Type', getContentType(files[0]));
res.send(getFiles(pathStart, files));
} else {
res.sendFile(path.join(config.root, pathStart, files[0]));
}
} else {
next();
}
}
function getContentType(url) {
var ext,
mt,
cs;
ext = path.extname(url).toLowerCase();
// removing the . when posible
ext = ext.indexOf('.') === 0 ? ext.slice(1) : ext;
// computing mime type based on the extension
mt = (ext && libmime.types[ext]) || 'text/plain';
// computing charset based on the mime type
cs = libmime.charsets.lookup(mt, 'UTF-8');
return (mt + ';charset=' + cs).toLowerCase();
}
function getFiles(pathStart, files) {
var result = '';
var absfiles = files.map(function(file) {
return path.join(config.root, pathStart, file);
})
absfiles.forEach(function(file, index) {
try {
fs.statSync(file).isFile();
result += fs.readFileSync(file) + '\n';
} catch(e) {
console.log(e);
}
})
return result;
}
}