-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
87 lines (77 loc) · 2.24 KB
/
index.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
var formidable = require('formidable'),
fs = require('fs'),
express = require('express'),
app = express.createServer().listen(process.env.PORT || 8000),
exec = require('child_process').exec,
filelist = {
'timestamp' : 0,
'files' : []
};
app.configure(function(){
app.use(express.static(__dirname + '/static'));
app.set('view engine', 'jade');
app.set('views', __dirname + '/views');
});
app.get('/', function(req, res){
child = exec('ls -s ./static/upload', function(err, stdout, stderr){
stdout = stdout.split('\n');
var content = [];
var item;
for(var i = 1; i < stdout.length - 1; ++i) {
item = stdout[i].trim().split(' ');
content.push({
'name' : item[item.length-1],
'size' : item[0]
});
}
res.render('index',{
content: content
});
})
})
app.post('/upload', function(req, res){
var form = new formidable.IncomingForm()
form.parse(req, function(err, fields, files){
if(files['file0'].name.split('/').length > 1) {
var directory = files['file0'].name.split('/')[0];
fs.mkdir(__dirname + '/static/upload/' + directory, 0777, function (err) {
if(err) console.log('Folder could not be created ' + err)
})
}
for(var i in files) {
filelist.files.push("/upload/" + files[i].name);
console.log(files[i].name);
filelist.timestamp = (new Date()).toLocaleTimeString();
fs.rename(files[i].path, __dirname + "/static/upload/" + files[i].name, function(err) {
if (err) {
fs.unlink(__dirname + "/static/upload/" + files[i].name);
fs.rename(files[i].path, __dirname + "/static/upload/" + files[i].name);
}
});
}
})
});
app.get('/update', function(req, res){
if (req.headers.accept && req.headers.accept == 'text/event-stream') {
res.writeHead(200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive'
});
sendSSE(req, res);
}
});
function sendSSE(req, res) {
var id = (new Date()).toLocaleTimsheString();
constructSSE(res, id);
}
function constructSSE(res, id) {
if(filelist.files.length) {
res.write('id: ' + id + '\n');
res.write('data: ' + JSON.stringify(filelist) + '\n\n');
}
setTimeout(function() {
constructSSE(res, id);
}, 5000);
}
console.log("Listening on port %d", app.address().port);