-
Notifications
You must be signed in to change notification settings - Fork 3
/
util.js
222 lines (195 loc) · 5.89 KB
/
util.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
'use strict';
const path = require('path');
const fs = require('fs-extra');
const async = require('async');
const chalk = require('chalk');
var users = require('./users');
var errCtr = 0;
var idCtr = 0;
function resourceError(err, res, msg) {
console.warn(chalk.red(msg));
res.status(501).send('request error: ' + msg);
}
function createSinglePathInfoGetter(abspath) {
return function (callback) {
fs.stat(abspath, function (err, stats) {
var fkind = FileKind.Invalid;
if (!err) {
if (stats.isFile()) {
fkind = FileKind.File;
} else if (stats.isDirectory()) {
fkind = FileKind.Directory;
} else {
fkind = FileKind.Other;
}
}
callback(err, {
name: abspath,
kind: fkind
});
});
};
}
function processForDisplay(info, root) {
info.sort(function (a, b) {
if (a.isfile !== b.isfile) {
return a.isfile ? 1 : -1;
} else {
return a.shortname.localeCompare(b.shortname);
}
});
}
var FileKind;
(function (FileKind) {
FileKind[(FileKind['Invalid'] = 0)] = 'Invalid';
FileKind[(FileKind['File'] = 1)] = 'File';
FileKind[(FileKind['Directory'] = 2)] = 'Directory';
FileKind[(FileKind['Other'] = 3)] = 'Other';
})(FileKind || (FileKind = {}));
function canonicalPath() {
const args = Array.prototype.slice.call(arguments, 0);
return path.normalize(path.join.apply(path, args));
}
function htmlEncodeContent(unsafe_str) {
var escaped = unsafe_str
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/\'/g, '"')
.replace(/\'/g, ''');
var wstrans = escaped
.replace(/ /g, ' ')
.replace(/(\r\n)|(\n)/g, '<br />')
.replace(/\t/g, ' ');
return wstrans;
}
function processFileLoadForDisplay(file_path, data) {
var output = '';
if (data === '') {
output = '<em>The file \'' + path.basename(file_path) + '\' is empty!</em>';
} else if (
path.extname(file_path) === '.js' ||
path.extname(file_path) === '.html'
) {
var htmlstr = htmlEncodeContent(data.toString());
output = '<code>' + htmlstr + '</code>';
} else if (
path.extname(file_path) === '.txt' ||
path.extname(file_path) === '.log'
) {
var htmlstr = htmlEncodeContent(data.toString());
output = '<div class="plaintext">' + htmlstr + '</div>';
} else {
output =
'<em>Cannot display the content of \'' +
path.basename(file_path) +
'\'.</em>';
}
return output;
}
function loadFileInfo(rootDir, req, res) {
const user = users.getUser(req.params.userName);
const subPath = req.params.subpath ? decodeURI(req.params.subpath) : '';
var file_path = canonicalPath(rootDir, user.dataDirectory, subPath);
var fileRequestTime = new Date().toISOString();
console.log(`(${fileRequestTime}) -- start reading file ${file_path}`);
fs.readFile(file_path, 'utf8', function (err, data) {
if (err) {
var fileErrorTime = new Date().toISOString();
resourceError(
err,
res,
`(${fileErrorTime}) -- File not found ${file_path}`
);
} else {
var fileResultTime = new Date().toISOString();
console.log(`(${fileResultTime}) -- complete reading file ${file_path}`);
var output = processFileLoadForDisplay(file_path, data);
res.send(output);
}
});
}
function loadDirectoryInfo(rootDir, req, res, viewName, extraDir) {
const userData = users.getUsers();
const userName = req.params.userName || '';
if (!userName) {
res.render(viewName, {
files: [],
port: req.app.get('port'),
users: userData,
currentUser: undefined
});
return;
}
const badDir = path.join(rootDir, userName);
const goodDir = path.join(rootDir, users.getUser(userName).dataDirectory);
console.log(`reading directory from ${goodDir}`);
var directoryRequestTime = new Date().toISOString();
console.log(
`(${directoryRequestTime}) -- start reading directory ${goodDir}`
);
fs.readdir(badDir, function (err, files) {
if (err) {
var directoryErrorTime = new Date().toISOString();
resourceError(
err,
res,
`(${directoryErrorTime}) -- Directory not found ${goodDir}`
);
return;
}
var flist = files.filter(function (value) {
var fullpath = canonicalPath(badDir, value);
var fps = fullpath.substr(0, badDir.length);
return (
fullpath.length > badDir.length &&
fullpath.substr(0, badDir.length) === badDir
);
});
var cblist = flist.map(function (value) {
return createSinglePathInfoGetter(canonicalPath(badDir, value));
});
async.parallel(cblist, function (err, results) {
var tresults = [];
if (!err) {
tresults = results
.filter(function (value) {
return (
value.kind === FileKind.File || value.kind === FileKind.Directory
);
})
.map(function (value) {
var isfile = value.kind === FileKind.File;
var id = '_eid' + idCtr++;
var shortname =
path.basename(value.name) + (isfile ? '' : path.sep);
var encodedname = encodeURIComponent(
value.name.substr(goodDir.length + 1) + (isfile ? '' : path.sep)
);
return {
isfile: isfile,
elemid: id,
shortname: shortname,
encodedname: encodedname
};
});
}
var directoryResultTime = new Date().toISOString();
console.log(
`(${directoryResultTime}) -- complete reading directory ${goodDir}`
);
processForDisplay(tresults, extraDir);
res.render(viewName, {
files: tresults,
port: req.app.get('port'),
users: userData,
currentUser: userName
});
});
});
}
exports = module.exports = {
canonicalPath,
loadFileInfo,
loadDirectoryInfo
};