-
Notifications
You must be signed in to change notification settings - Fork 0
/
Upload.js
141 lines (116 loc) · 3.7 KB
/
Upload.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
var http = require("http");
var url = require("url");
var multipart = require("multipart");
var sys = require("sys");
var fs = require("fs");
var server = http.createServer(function(req, res) {
// Simple path-based request dispatcher
switch (url.parse(req.url).pathname) {
case '/':
display_form(req, res);
break;
case '/upload':
upload_file(req, res);
break;
default:
show_404(req, res);
break;
}
});
// Server would listen on port 8000
server.listen(8000);
console.log("Listening on http://localhost:8000/");
/*
* Display upload form
*/
function display_form(req, res) {
res.writeHead(200, {"Content-Type": "text/html"});
res.write(
'<form action="/upload" method="post" enctype="multipart/form-data">'+
'<input type="file" name="myFile">'+
'<input type="submit" value="Upload">'+
'</form>'
);
res.end();
}
/*
* Create multipart parser to parse given request
*/
function parse_multipart(req) {
var parser = multipart.parser();
// Make parser use parsed request headers
parser.headers = req.headers;
// Add listeners to request, transfering data to parser
req.addListener("data", function(chunk) {
parser.write(chunk);
});
req.addListener("end", function() {
parser.close();
});
return parser;
}
/*
* Handle file upload
*/
function upload_file(req, res) {
// Request body is binary
req.setEncoding("binary");
// Handle request as multipart
var stream = parse_multipart(req);
var fileName = null;
var fileStream = null;
// Set handler for a request part received
stream.onPartBegin = function(part) {
sys.debug("Started part, name = " + part.name + ", filename = " + part.filename);
// Construct file name
var date = new Date();
fileName = "./uploads/"+ date + "_" + stream.part.filename;
// Construct stream used to write to file
fileStream = fs.createWriteStream(fileName);
// Add error handler
fileStream.addListener("error", function(err) {
sys.debug("Got error while writing to file '" + fileName + "': ", err);
});
// Add drain (all queued data written) handler to resume receiving request data
fileStream.addListener("drain", function() {
req.resume();
});
};
// Set handler for a request part body chunk received
stream.onData = function(chunk) {
// Pause receiving request data (until current chunk is written)
req.pause();
// Write chunk to file
// Note that it is important to write in binary mode
// Otherwise UTF-8 characters are interpreted
//sys.debug("Writing chunk");
fileStream.write(chunk, "binary");
};
// Set handler for request completed
stream.onEnd = function() {
// As this is after request completed, all writes should have been queued by now
// So following callback will be executed after all the data is written out
fileStream.addListener("drain", function() {
// Close file stream
fileStream.end();
// Handle request completion, as all chunks were already written
upload_complete(res);
});
};
}
function upload_complete(res) {
sys.debug("Request complete");
// Render response
res.writeHead(200, {"Content-Type": "text/plain"});
res.write("Thanks for playing!");
res.end();
sys.puts("\n=> Done");
}
/*
* Handles page not found error
*/
function show_404(req, res) {
res.sendHeader(404, {"Content-Type": "text/plain"});
res.write("You r doing it rong!");
res.end();
}