-
Notifications
You must be signed in to change notification settings - Fork 7
/
filestatic.js
86 lines (74 loc) · 2.67 KB
/
filestatic.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
// to quick-view files a single click on a filetree element
var eejs = require('ep_etherpad-lite/node/eejs');
var fs = require('fs');
var settings = require('ep_etherpad-lite/node/utils/Settings');
var ext = require('ep_codepad/extensions');
// abs - absolute part of the files path - the project path
var abs = '/tmp/';
// theme from settings
var theme = 'Default';
var cif = '';
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;
}
}
//https://github.com/broofa/node-mime
var mime = require('mime');
exports.expressCreateFileStaticViewServer = function(hook_name, args, cb) {
args.app.get('/s/*', function(req, res) {
// file path slice
var fps = req.url.slice(3, 250);
// file path urlencoded
var fpu = encodeURIComponent(fps);
// file path read url
var fpr = '/s/' + fpu;
var file = decodeURIComponent(fps);
var path = abs + '/' + file;
try {
fs.exists(path, function(exists) {
if (exists) {
if (!ext.getBrush(path) && mime.lookup(path) !== 'application/octet-stream') {
fs.readFile(path, function(err, data) {
if (err) throw err;
res.set('Content-Type', mime.lookup(path));
res.send(data);
});
} else {
fs.readFile(path, {
encoding: 'utf-8'
}, function(err, data) {
if (err) throw err;
res.send(data);
});
}
} else {
res.send(eejs.require("ep_codepad/templates/view.ejs", {
uri: "#",
code: "Error! No such file.",
file: file,
theme: theme,
cif: cif,
brush: "plain"
}));
}
});
} catch (e) {
console.log("FileViewServer error: " + e);
res.send(eejs.require("ep_codepad/templates/view.ejs", {
uri: "#",
code: "FileStaticViewServer - Error! " + e,
file: file,
theme: theme,
cif: cif,
brush: "plain"
}));
}
});
};