forked from sffc/socketio-file-upload
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
534 lines (482 loc) · 14.4 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
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
/*
* Copyright (C) 2013 Shane Carr
* X11 License
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
* Except as contained in this notice, the names of the authors or copyright
* holders shall not be used in advertising or otherwise to promote the sale,
* use or other dealings in this Software without prior written authorization
* from the authors or copyright holders.
*/
// Require Libraries
var util = require("util"),
EventEmitter = require("events").EventEmitter,
path = require("path"),
fs = require("fs");
function SocketIOFileUploadServer() {
"use strict";
EventEmitter.call(this);
var self = this; // avoids context issues
/**
* Directory in which to save uploaded files. null = do not save files
* @type {String}
*/
self.dir = null;
/**
* What mode (UNIX permissions) in which to save uploaded files
* @type {Number}
*/
self.mode = "0666";
/**
* Maximum file size, in bytes, when saving files. An "error" event will
* be emitted when this size is exceeded, and the data will not be written
* to the disk. null = allow any file size
*/
self.maxFileSize = null;
/**
* Whether or not to emit an error event if a progress chunk fails to
* finish writing. The failure could be a harmless notification that the
* file is larger than the internal buffer size, or it could mean that the
* file upload triggered an ENOSPC error.
*/
self.emitChunkFail = false;
/**
* Default validator.
* @param {Object} event Contains { file: fileInfo }
* @param {function} callback Call it with true to start upload, false to abort
*/
self.uploadValidator = function(event, callback){
callback(true);
};
var files = [];
/**
* Private function to emit the "siofu_complete" message on the socket.
* @param {Number} id The file ID as passed on the siofu_upload.
* @param {boolean} success
* @return {void}
*/
var _emitComplete = function (socket, id, success) {
var fileInfo = files[id];
// Check if the upload was aborted
if (!fileInfo) {
return;
}
socket.emit("siofu_complete", {
id: id,
success: success,
detail: fileInfo.clientDetail
});
};
/**
* Private function to recursively find a file name by incrementing "inc" until
* an empty file is found.
* @param {String} ext File extension
* @param {String} base File base name
* @param {Date} mtime File modified time
* @param {Number} inc Current number to suffix the base name. Pass -1
* to not suffix a number to the base name.
* @param {Function} next Callback function when the save is complete.
* Will be passed a possible error as well as the
* final base name.
* @return {void}
*/
var _findFileNameWorker = function (ext, base, inc, next) {
var newBase = (inc === -1) ? base : base + "-" + inc;
var pathName = path.join(self.dir, newBase + ext);
fs.exists(pathName, function (exists) {
if (exists) {
_findFileNameWorker(ext, base, inc + 1, next);
}
else {
fs.open(pathName, "w", self.mode, function (err, fd) {
if (err) {
// Oops! Pass an error to the callback function.
next(err);
return;
}
// Pass the file handler and the new name to the callback.
next(null, newBase, pathName, fd);
});
}
});
};
/**
* Private function to save an uploaded file.
* @param {Object} fileInfo Object containing file name, modified time, and
* text content.
* @return {void}
*/
var _findFileName = function (fileInfo, next) {
// Strip dangerous characters from the file name
var filesafeName = fileInfo.name
.replace(/[\/\?<>\\:\*\|":]|[\x00-\x1f\x80-\x9f]|^\.+$/g, "_");
var ext = path.extname(filesafeName);
var base = path.basename(filesafeName, ext);
// Use a recursive function to save the file under the first available filename.
_findFileNameWorker(ext, base, -1, function (err, newBase, pathName, fd) {
if (err) {
next(err);
return;
}
fs.close(fd, function (err) {
if (err) {
next(err);
return;
}
next(null, newBase, pathName);
});
});
};
var _uploadDone = function (socket) {
return function (data) {
var fileInfo = files[data.id];
// Check if the upload was aborted
if (!fileInfo) {
return;
}
try {
if (fileInfo.writeStream) {
// Update the file modified time. This doesn't seem to work; I'm not
// sure if it's my error or a bug in Node.
// IE issue
var mtime = (Object.prototype.toString.call(fileInfo.mtime) === "[object Date]" && !isNaN(fileInfo.mtime)) ? fileInfo.mtime : new Date();
fs.utimes(fileInfo.pathName, new Date(), mtime, function (err) {
// Check if the upload was aborted
if (!files[data.id]) {
return;
}
// I'm not sure what arguments the futimes callback is passed.
// Based on node_file.cc, it looks like it is passed zero
// arguments (version 0.10.6 line 140), but the docs say that
// "the first argument is always reserved for an exception".
if (err) {
fileInfo.success = false;
_emitComplete(socket, data.id, fileInfo.success);
console.log("SocketIOFileUploadServer Error (_uploadDone fs.utimes):");
console.log(err);
_cleanupFile(data.id);
return;
}
// Emit the "saved" event to the server-side listeners
self.emit("saved", {
file: fileInfo
});
_emitComplete(socket, data.id, fileInfo.success);
_cleanupFile(data.id);
});
}
else {
_emitComplete(socket, data.id, fileInfo.success);
_cleanupFile(data.id);
}
}
catch (err) {
console.log("SocketIOFileUploadServer Error (_uploadDone):");
console.log(err);
}
// Emit the "complete" event to the server-side listeners
self.emit("complete", {
file: fileInfo,
interrupt: !!data.interrupt
});
};
};
var _uploadProgress = function (socket) {
//jshint unused:false
return function (data) {
var fileInfo = files[data.id], buffer;
// Check if the upload was aborted
if (!fileInfo) {
return;
}
try {
if (data.base64) {
buffer = new Buffer(data.content, "base64");
}
else {
buffer = new Buffer(data.content);
}
fileInfo.size = data.size;
fileInfo.bytesLoaded += buffer.length;
if (self.maxFileSize !== null
&& fileInfo.bytesLoaded > self.maxFileSize) {
fileInfo.success = false;
socket.emit("siofu_error", {
id: data.id,
message: "Max allowed file size exceeded"
});
self.emit("error", {
file: fileInfo,
error: new Error("Max allowed file size exceeded"),
memo: "self-thrown from progress event"
});
_cleanupFile(data.id);
}
else {
if (fileInfo.writeStream) {
if (!fileInfo.writeStream.write(buffer) && self.emitChunkFail) {
self.emit("error", {
file: fileInfo,
error: new Error("Write of chunk failed (ENOSPC?)"),
memo: "self-thrown from progress event"
});
}
}
}
// Emit that the chunk has been received, so client starts sending the next chunk
socket.emit("siofu_chunk", { id: data.id });
self.emit("progress", {
file: fileInfo,
buffer: buffer
});
}
catch (err) {
console.log("SocketIOFileUploadServer Error (_uploadProgress):");
console.log(err);
}
};
};
/**
* Private function to handle the start of a file upload.
* @param {Socket} socket The socket on which the listener is bound
* @return {Function} A function compatible with a Socket.IO callback
*/
var _uploadStart = function (socket) {
return function (data) {
// Save the file information
var fileInfo = {
name: data.name,
mtime: new Date(data.mtime),
encoding: data.encoding,
clientDetail: {},
meta: data.meta || {},
id: data.id,
size: data.size,
bytesLoaded: 0,
success: true
};
files[data.id] = fileInfo;
// Dispatch event to listeners on the server side
self.emit("start", {
file: fileInfo
});
// Abort right now if the "start" event aborted the file upload.
if (!files[data.id]) {
return;
}
self.uploadValidator({ file: fileInfo }, function( isValid ){
if ( !isValid ) {
self.abort( data.id, socket );
} else {
// If we're not saving the file, we are ready to start receiving data now.
if (!self.dir) {
socket.emit("siofu_ready", {
id: data.id,
name: null
});
} else {
_serverReady(socket, data, fileInfo);
}
}
});
};
};
var _serverReady = function(socket, data, fileInfo){
// Find a filename and get the handler. Then tell the client that
// we're ready to start receiving data.
_findFileName(fileInfo, function (err, newBase, pathName) {
// Check if the upload was aborted
if (!files[data.id]) {
return;
}
if (err) {
_emitComplete(socket, data.id, false);
self.emit("error", {
file: fileInfo,
error: err,
memo: "computing file name"
});
_cleanupFile(data.id);
return;
}
files[data.id].base = newBase;
files[data.id].pathName = pathName;
// Create a write stream.
try {
var writeStream = fs.createWriteStream(pathName, {
mode: self.mode
});
writeStream.on("open", function () {
// Check if the upload was aborted
if (!files[data.id]) {
return;
}
socket.emit("siofu_ready", {
id: data.id,
name: newBase
});
});
writeStream.on("error", function (err) {
// Check if the upload was aborted
if (!files[data.id]) {
return;
}
_emitComplete(socket, data.id, false);
self.emit("error", {
file: fileInfo,
error: err,
memo: "from within write stream"
});
_cleanupFile(data.id);
});
files[data.id].writeStream = writeStream;
}
catch (err) {
_emitComplete(socket, data.id, false);
self.emit("error", {
file: fileInfo,
error: err,
memo: "creating write stream"
});
_cleanupFile(data.id);
return;
}
});
};
var _cleanupFile = function (id) {
var fileInfo = files[id];
if (fileInfo.writeStream) {
fileInfo.writeStream.end();
}
delete files[id];
}
/**
* Private function to handle a client disconnect event.
* @param {Socket} socket The socket on which the listener is bound
* @return {Function} A function compatible with a Socket.IO callback
*/
var _onDisconnect = function (socket) {
return function () {
for (var id in files) {
if (files.hasOwnProperty(id)) {
var fileInfo = files[id];
self.emit("error", {
file: fileInfo,
error: new Error("Client disconnected in the middle of an upload"),
memo: "disconnect during upload"
});
_cleanupFile(id);
return;
}
}
}
}
/**
* Public method. Listen to a Socket.IO socket for a file upload event
* emitted from the client-side library.
*
* @param {Socket} socket The socket on which to listen
* @return {void}
*/
this.listen = function (socket) {
socket.on("siofu_start", _uploadStart(socket));
socket.on("siofu_progress", _uploadProgress(socket));
socket.on("siofu_done", _uploadDone(socket));
socket.on("disconnect", _onDisconnect(socket));
};
/**
* Public method. Abort an upload that may be in progress. Throws an
* exception if the specified file upload is not in progress.
*
* @param {String} id The ID of the file upload to abort.
* @param {Socket} socket The socket that this instance is connected to.
* @return {void}
*/
this.abort = function (id, socket) {
if (!socket) {
throw new Error("Please pass the socket instance as the second argument to abort()");
}
var fileInfo = files[id];
if (!fileInfo) {
throw new Error("File with specified ID does not exist: " + id);
}
fileInfo.success = false;
socket.emit("siofu_error", {
id: id,
message: "File upload aborted by server"
});
_cleanupFile(id);
}
}
util.inherits(SocketIOFileUploadServer, EventEmitter);
/**
* Path at which to serve the client JavaScript file.
* @type {String}
*/
SocketIOFileUploadServer.clientPath = "/siofu/client.js";
/**
* Private function to serve the static client file.
* @param {ServerResponse} res The server response
* @return {void}
*/
var _serve = function (res) {
"use strict";
fs.readFile(__dirname + "/client.min.js", function (err, data) {
if (err) throw err;
res.writeHead(200, {
"Content-Type": "text/javascript"
});
res.write(data);
res.end();
});
};
/**
* Transmit the static client file on a vanilla HTTP server.
* @param {HTTPServer} app Your HTTP server
* @return {void}
*/
SocketIOFileUploadServer.listen = function (app) {
"use strict";
app.on("request", function (req, res) {
if (req.url === SocketIOFileUploadServer.clientPath) {
_serve(res);
}
});
};
/**
* Router to serve the static client file on the Connect middleware, including
* the Express.JS web framework. Pass this function to your application like
* this:
*
* app.use(SocketIOFileUploadServer.router)
*
* You should not need to ever call this function.
*/
SocketIOFileUploadServer.router = function (req, res, next) {
"use strict";
if (req.url === SocketIOFileUploadServer.clientPath) {
_serve(res);
}
else {
next();
}
};
// Export the object.
module.exports = SocketIOFileUploadServer;