-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathfiletree.js
98 lines (86 loc) · 3.15 KB
/
filetree.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
// creates the filetree view
var eejs = require('ep_etherpad-lite/node/eejs');
var fs = require('fs');
var ext = require('ep_codepad/extensions');
var settings = require('ep_etherpad-lite/node/utils/Settings');
// abs - absolute part of the files path - the project path
var abs = '/tmp/';
// theme from settings
var theme = 'Default';
var cif = '';
var play_url = '/play';
if (settings.ep_codepad) {
if (settings.ep_codepad.project_path) {
abs = settings.ep_codepad.project_path;
}
if (settings.ep_codepad.theme) {
theme = settings.ep_codepad.theme;
}
if (settings.ep_codepad.installation_folder) {
cif = settings.ep_codepad.installation_folder;
}
if (settings.ep_codepad.play_url) {
play_url = settings.ep_codepad.play_url;
}
}
var canRead = function(path) {
if (process.getgid && process.getuid) {
// make sure files can be read ...
var stats = fs.statSync(path);
var perm = parseInt(stats.mode.toString(8), 10);
var user = (perm % 1000) >= 400;
var userx = (perm % 1000) >= 500;
var group = (perm % 100) >= 40;
var groupx = (perm % 100) >= 50;
var world = (perm % 10) >= 4;
var worldx = (perm % 10) >= 5;
var inuid = stats.uid == process.getuid();
var ingid = stats.gid == process.getgid();
// directories must have an execute
if (stats.isDirectory())
return ((userx && ingid) || (groupx && ingid) || worldx);
else
return ((user && ingid) || (group && ingid) || world);
} else {
return true;
}
};
exports.expressCreateFileTreeServer = function(hook_name, args, cb) {
args.app.get('/files', function(req, res) {
res.send(eejs.require("ep_codepad/templates/filetree.ejs", {
abs: abs,
cif: cif,
play_url: play_url,
theme: theme
}));
});
args.app.get('/files_connector', function(req, res) {
var dir = req.query.dir;
var r = '<ul class="jqueryFileTree" style="display: none;">';
try {
if (canRead(abs + dir)) {
var files = fs.readdirSync(abs + dir);
files.forEach(function(f) {
var ff = dir + f;
if (canRead(abs + ff) && f.charAt(0) !== '.') {
var stats = fs.statSync(abs + ff);
var ft = f;
if (ft.length > 25) ft = f.substring(0, 22) + "...";
if (stats.isDirectory()) {
r += '<li class="directory collapsed"><a href="' + cif + '/files" rel="' + ff + '/" title="' + ff + '">' + ft + '</a></li>';
} else {
r += '<li class="file ext_' + ext.getBrush(f) + '"><a href="' + cif + '/v' + ff + '" rel=' + ff + ' title="' + ff + '">' + ft + '</a></li>';
}
}
});
r += '</ul>';
}
} catch (e) {
console.log("FileTreeServer error: " + e);
r += 'ERR: ' + e;
r += '</ul>';
}
res.send(r);
});
};
// --