-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
executable file
·118 lines (107 loc) · 3.74 KB
/
server.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
#!/usr/bin/env node
var express = require('express');
var formidable = require('formidable');
var files = require('./files');
var template = require('./template');
var sys = require('sys');
var MAX_FILE_SIZE = 300 * 1024 * 1024;
var HTML_HEADERS = { 'Content-Type': 'text/html; charset=UTF-8' };
var app = express();
// Don't want this public? Uncomment:
// app.use(express.basicAuth('user', 'password'));
app.get('/', function(req, res) {
res.writeHead(200, HTML_HEADERS);
res.write(template.htmlHead);
res.write(template.uploadForm);
res.write(template.downloadList(files.getFiles()));
res.write(template.htmlFoot);
res.end();
});
app.post('/upload', function(req, res) {
var out, error;
var form = new formidable.IncomingForm();
form.encoding = 'utf8';
form.handlePart = function(part) {
if (part.name === 'file' && part.filename) {
console.log({ part: { name: part.name,
filename: part.filename,
mime: part.mime
} });
out = files.addFile({ name: part.filename,
type: part.mime
});
var received = 0;
part.on('data', function(data) {
if (out) {
received += data.length;
if (received > MAX_FILE_SIZE) {
out.discard();
out = undefined;
files.freeBytes(received);
error = new Error('Exceeded maximum file size');
/* TODO: send response ASAP */
} else {
files.allocBytes(data.length);
req.pause();
out.write(data, function() {
req.resume();
});
}
}
});
part.on('end', function() {
if (out && received > 0) {
out.commit(received);
} else if (out) {
out.discard();
error = new Error('File is empty');
}
});
}
};
form.parse(req, function(err, fields, files) { if (!error) error = err; });
form.on('end', function() {
if (!out && !error)
error = new Error('No file received');
if (error) {
/* Error */
console.error(error.stack);
res.writeHead(400, HTML_HEADERS);
res.write(template.htmlHead);
res.write(template.error(error || new Error('No file received')));
res.write(template.htmlFoot);
res.end();
} else {
/* Success */
res.writeHead(200, HTML_HEADERS);
res.write(template.htmlHead);
res.write(template.thanks);
res.write(template.htmlFoot);
res.end();
}
});
});
function fileDownload(req, res, next) {
var m;
if ((m = req.url.match(/^\/file\/(\d+)\//))) {
var id = parseInt(m[1], 10);
var info = files.getFile(id);
res.writeHead(200, { 'Content-Type': info.mime,
'Content-Length': info.size });
var read = files.readFile(info.id);
sys.pump(read, res);
read.on('close', function() {
res.end();
});
} else
next();
}
app.use(fileDownload);
app.use(express.static(__dirname + '/public'));
app.use(function(req, res, next) {
res.writeHead(404, { Location: '/' });
res.end();
});
var port = parseInt(process.env.PORT || "8000", 10);
app.listen(port, '::');
console.log("Now listening on http://[::1]:" + port);