-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathload.js
More file actions
458 lines (407 loc) · 13.7 KB
/
load.js
File metadata and controls
458 lines (407 loc) · 13.7 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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
var path = require('path');
var fs = require('fs-ext');
var fsExtra = require('fs-extra');
var utils = require('./utils');
var filetype = require('../content/src/filetype');
var filemeta = require('./filemeta');
var DirCache = require('./dircache').DirCache;
var globalRootDirCache = {};
// Do not serve cached content older than 5 minutes.
var maxDirCacheAge = 5 * 60 * 1000;
// Rebuild cache in background after serving data older than 1 minute.
var autoRebuildCacheAge = 1 * 60 * 1000;
// Serve at most 600 usernames at a time from root directory.
var maxRootDirEntries = 600;
function getRootDirCache(dir) {
var dircache = globalRootDirCache[dir]
if (!dircache) {
dircache = new DirCache(dir);
globalRootDirCache[dir] = dircache;
}
return dircache;
}
exports.handleLoad = function(req, res, app, format) {
var filename = utils.filenameFromUri(req);
var callback = utils.param(req, 'callback');
var user = res.locals.owner;
var origfilename = filename;
if (filename == null) {
filename = '';
}
try {
// The root listing, which lists all users,
// is handled differently from other requests.
var isrootlisting = !user && filename == '' && format == 'json';
if (isrootlisting) {
var prefix = utils.param(req, 'prefix') || '';
var count = Math.max(maxRootDirEntries,
utils.param(req, 'count') || maxRootDirEntries);
// Grab the dir cache object for this root directory path.
var dircache = getRootDirCache(app.locals.config.dirs.datadir);
if (dircache.age() > maxDirCacheAge) {
// A very-old or never-built cache must be rebuilt before serving.
dircache.rebuild(sendCachedResult);
} else if (prefix) {
// When a specific prefix is requested, probe for an exact match.
dircache.update(prefix, sendCachedResult);
} else {
// Fresh cache without prefix: just send the cached result.
sendCachedResult(true);
}
function sendCachedResult(ok) {
var data;
if (!ok) {
data = {error: "Could not read file /"};
} else {
data = {
directory: "/",
list: dircache.readPrefix(prefix, count),
auth: false
};
// If the cache was sort-of-old, kick off an early rebuild.
if (dircache.age() > autoRebuildCacheAge) {
// No callback needed.
dircache.rebuild(null);
}
}
res.set('Cache-Control', 'must-revalidate');
res.set('Content-Type', 'text/javascript');
res.jsonp(data);
}
return;
}
// Validate username
if (user) {
utils.validateUserName(user);
if (filename.length > 0) {
filename = path.join(user, filename);
}
else {
filename = user + '/';
}
}
// Validate filename
if (filename.length > 0) {
if (!utils.isFileNameValid(filename, false)) {
utils.errorExit('Bad filename: ' + filename);
}
}
var assumeBinary = filetype.binaryTypeFilename(filename);
var absfile = utils.makeAbsolute(filename, app);
if (format == 'json') {
var haskey = userhaskey(user, app);
// Handle the case of a file that's present
if (utils.isPresent(absfile, 'file')) {
var m = filemeta.parseMetaString(
fs.readFileSync(absfile), assumeBinary),
data = m.data,
meta = m.meta;
var statObj = fs.statSync(absfile);
var mimetype = filetype.mimeForFilename(filename);
res.set('Cache-Control', 'must-revalidate');
resp = {
file: '/' + filename,
data: data,
auth: haskey,
mtime: statObj.mtime.getTime(),
mime: mimetype
};
if (meta != null) {
resp.meta = meta;
}
res.jsonp(resp);
return;
}
// Handle the case of a directory that's present
if (utils.isPresent(absfile, 'dir')) {
if (filename.length > 0 && filename[filename.length - 1] != '/') {
filename += '/';
}
var list = buildDirList(absfile, fs.readdirSync(absfile).sort());
var jsonRet =
{'directory': '/' + filename, 'list': list, 'auth': haskey};
res.set('Cache-Control', 'must-revalidate');
res.jsonp(jsonRet);
return;
}
// Handle the case of a new file create
if (filename.length > 0 &&
filename[filename.length - 1] != '/' &&
isValidNewFile(absfile, app)) {
res.set('Cache-Control', 'must-revalidate');
res.jsonp({'error': 'could not read file ' + filename,
'newfile': true,
'auth': haskey,
'info': absfile});
return;
}
utils.errorExit('Could not read file ' + filename);
}
else if (format == 'code') { // For loading the code only
var mt = filetype.mimeForFilename(filename),
m = filemeta.parseMetaString(
fs.readFileSync(absfile), assumeBinary),
data = m.data,
meta = m.meta;
res.set('Cache-Control', 'must-revalidate');
res.set('Content-Type', (meta && meta.type) ||
mt.replace(/\bx-pencilcode\b/, 'cofeescript'));
res.send(data);
return;
}
else if (format == 'print') { // For printing the code
var mt = filetype.mimeForFilename(filename),
m = filemeta.parseMetaString(
fs.readFileSync(absfile), assumeBinary),
data = m.data,
meta = m.meta,
needline = false,
out = [];
if (mt.indexOf('text') !== 0) {
res.set('Content-Type', mt);
res.send(data);
return;
}
res.set('Cache-Control', 'must-revalidate');
res.set('Content-Type', 'text/html;charset=utf-8');
out.push('<!doctype html>', '<html>', '<head>');
// Add a title in the form username: file.
out.push('<title>' + filename.replace('/', ': ') + '</title>');
out.push('<style>');
// For scrible - when the page is modified so that the pre is
// the second element, insert some extra top-padding to make
// space for scrible's overlay.
out.push('pre:nth-child(2) { padding-top: 100px; }');
// s for dotted-lines with columns
out.push('s { border-right: 1px dotted skyblue; margin-right: -1; '+
'text-decoration: none; }');
out.push('</style>');
out.push('</head>', '<body>', '<pre>');
if (/\S/.test(data)) {
out.push(addHTMLLineNumbers(data));
needline = true;
}
if (meta && meta.css && /\S/.test(meta.css)) {
if (needline)
out.push('<br><hr style="border:none;height:1px;background:black">');
out.push(addHTMLLineNumbers(meta.css));
needline = true;
}
if (meta && meta.html && /\S/.test(meta.html)) {
if (needline)
out.push('<br><hr style="border:none;height:1px;background:black">');
out.push(addHTMLLineNumbers(meta.html));
needline = true;
}
out.push('</pre>');
out.push('</body>', '</html>');
res.send(out.join('\n'));
return;
}
else if (format == 'run') { // File loading outside the editor
if (utils.isPresent(absfile, 'file')) {
var mt = filetype.mimeForFilename(filename),
m = filemeta.parseMetaString(
fs.readFileSync(absfile), assumeBinary);
// For turtle bits, assume it's coffeescript
if (mt.indexOf('text/x-pencilcode') == 0) {
data = filetype.wrapTurtle(m, res.locals.site);
mt = mt.replace('x-pencilcode', 'html');
} else {
data = m.data;
}
res.set('Cache-Control', 'must-revalidate');
res.set('Content-Type', mt);
res.send(data);
return;
}
else if (utils.isPresent(absfile, 'dir') ||
filename.indexOf('/') == filename.length) {
if (filename.length > 0 && filename[filename.length - 1] != '/') {
res.redirect(301, '/home' + req.path + '/');
return;
}
res.set('Cache-Control', 'must-revalidate');
res.set('Content-Type', 'text/html;charset=utf-8');
var text = '<title>' + req.path + '</title>';
text += '<style>\n';
text += 'body { font-family:Arial,sans-serif; }\n';
text += 'pre {\n';
text += '-moz-column-count:3;\n';
text += '-webkit-column-count:3;\n';
text += 'column-count:3;\n';
text += '}\n';
text += '</style>\n';
text += '<h3>' + req.host + req.path + '</h3>\n';
text += '<pre>\n';
var lastIndex = req.path.lastIndexOf('/');
if (lastIndex > 0) {
var prevIndex = req.path.substring(0, lastIndex).lastIndexOf('/');
if (prevIndex >= 0) {
var parentDir = req.path.substring(0, prevIndex + 1);
text += '<a href="/home' + parentDir;
text += '" style="background:yellow">Up to ' + parentDir;
text += '</a>\n';
}
}
var contents = (utils.isPresent(absfile, 'dir')) ?
fs.readdirSync(absfile).sort() : new Array();
for (var i = 0; i < contents.length; i++) {
if (contents[i][0] == '.') {
// Skip past any dirs starting with a '.'
continue;
}
var name = contents[i];
var af = path.join(absfile, name);
if (utils.isPresent(af, 'dir') &&
name.charAt(name.length - 1) != '/') {
name += '/';
}
var link = '<a href="/home' + req.path + name + '">' + name + '</a>';
if (utils.isPresent(af, 'file')) {
link += ' <a href="/edit' + req.path + name +
'" style="color:lightgray" rel="nofollow">edit</a>';
}
text += link + '\n';
}
text += '</pre>\n';
if (contents.length == 0) {
text += '(directory is empty)<br>\n';
}
res.send(text);
return;
}
else if (filename.charAt(filename.length - 1) == '/') {
res.redirect(301,
path.dirname(filename.substring(0, filename.length-1)) + '/');
return;
}
else {
res.set('Cache-Control', 'must-revalidate');
res.set('Content-Type', 'text/html;charset=utf-8');
var text = '<pre>\n';
text += 'No file ' + origfilename + ' found.\n\n';
var strip = (req.path.charAt(req.path.length - 1) == '/') ?
req.path.substring(0, req.path.length - 1) : req.path;
var parentDir = path.dirname(strip) + '/';
text += '<a href="' + parentDir + '">Up to ' + parentDir + '</a>\n';
var extIdx = filename.lastIndexOf('.');
if (extIdx > 0) {
var ext = filename.substring(extIdx + 1);
if (ext == 'htm' || ext == 'html' || ext == 'js' || ext == 'css') {
text += '<a href="/edit/' + origfilename + '" rel="nofollow">Create /home/' + origfilename + '</a>\n';
}
}
text += '</pre>\n';
res.send(text);
return;
}
}
}
catch (e) {
if (e instanceof utils.ImmediateReturnError) {
if (format == 'json') {
res.jsonp((e.jsonObj) ? e.jsonObj : {'error': e.toString()});
}
else {
res.set('Content-Type', 'text/html');
if (/ENOENT/.test(e.message)) {
res.status(404);
res.send('<html><body><plaintext>' +
'404: ' + filename.replace('/', ': ') + ' not found.');
} else {
res.send('<html><body><plaintext>' + e.message);
}
}
}
else {
throw e;
}
}
};
function addIndentGuides(line) {
var leading = /^(?: )*/.exec(line)[0].length;
return line.substring(0, leading).replace(/ /g, '<s> </s>') +
line.substring(leading);
}
function addHTMLLineNumbers(data) {
var out = [],
lines = data.replace(/\s*$/, '').split('\n'),
spaces = Math.max(3, ('' + lines.length).length),
j;
for (j = 0; j < lines.length; ++j) {
var line = '' + (j + 1);
while (line.length < spaces) {
line = ' ' + line;
}
line = line + '<s> </s>' + addIndentGuides(escapeHTML(lines[j]));
out.push(line);
}
return out.join('\n');
}
function escapeHTML(s) {
return s.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>');
}
function isValidNewFile(newAbsFileName, app) {
var dir = path.dirname(newAbsFileName);
while (true) {
if (dir.indexOf(app.locals.config.dirs.datadir) != 0) {
return false;
}
try {
if (fs.statSync(dir).isDirectory()) {
return true;
}
}
catch (e) { }
dir = path.dirname(dir);
}
}
function buildDirList(absdir, contents) {
var list = new Array();
for (var i = 0; i < contents.length; i++) {
// Skip over any entries starting with .
if (contents[i][0] == '.') {
continue;
}
var item = path.join(absdir, contents[i]);
var statObj = fs.statSync(item);
var modestr = '';
if (statObj.isDirectory()) {
modestr += 'd';
}
if (statObj.mode & 00400) {
modestr += 'r';
}
if (statObj.mode & 00200) {
modestr += 'w';
}
if (statObj.mode & 00100) {
modestr += 'x';
}
var mtime = statObj.mtime.getTime();
// If its an empty file, then reset mtime
if (statObj.isFile() && statObj.size == 0) {
mtime = 0;
}
list.push({name: contents[i],
mode: modestr,
size: statObj.size,
mtime: mtime});
}
return list;
}
function userhaskey(user, app) {
if (!user) {
return false;
}
var keydir = utils.getKeyDir(user, app);
if (!utils.isPresent(keydir, 'dir')) {
return false;
}
var keys = fs.readdirSync(keydir);
if (!keys || keys.length == 0) {
return false;
}
return true;
}