|
| 1 | +let http = require('http'); |
| 2 | +let static = require('node-static'); |
| 3 | +let fileServer = new static.Server('.'); |
| 4 | +let path = require('path'); |
| 5 | +let fs = require('fs'); |
| 6 | +let debug = require('debug')('example:resume-upload'); |
| 7 | + |
| 8 | +let uploads = Object.create(null); |
| 9 | + |
| 10 | +function onUpload(req, res) { |
| 11 | + |
| 12 | + let fileId = req.headers['x-file-id']; |
| 13 | + let startByte = req.headers['x-start-byte']; |
| 14 | + |
| 15 | + if (!fileId) { |
| 16 | + res.writeHead(400, "No file id"); |
| 17 | + res.end(); |
| 18 | + } |
| 19 | + |
| 20 | + // we'll files "nowhere" |
| 21 | + let filePath = '/dev/null'; |
| 22 | + // could use a real path instead, e.g. |
| 23 | + // let filePath = path.join('/tmp', fileId); |
| 24 | + |
| 25 | + debug("onUpload fileId: ", fileId); |
| 26 | + |
| 27 | + // initialize a new upload |
| 28 | + if (!uploads[fileId]) uploads[fileId] = {}; |
| 29 | + let upload = uploads[fileId]; |
| 30 | + |
| 31 | + debug("bytesReceived:" + upload.bytesReceived + " startByte:" + startByte) |
| 32 | + |
| 33 | + let fileStream; |
| 34 | + |
| 35 | + // if startByte is 0 or not set, create a new file, otherwise check the size and append to existing one |
| 36 | + if (!startByte) { |
| 37 | + upload.bytesReceived = 0; |
| 38 | + fileStream = fs.createWriteStream(filePath, { |
| 39 | + flags: 'w' |
| 40 | + }); |
| 41 | + debug("New file created: " + filePath); |
| 42 | + } else { |
| 43 | + // we can check on-disk file size as well to be sure |
| 44 | + if (upload.bytesReceived != startByte) { |
| 45 | + res.writeHead(400, "Wrong start byte"); |
| 46 | + res.end(upload.bytesReceived); |
| 47 | + return; |
| 48 | + } |
| 49 | + // append to existing file |
| 50 | + fileStream = fs.createWriteStream(filePath, { |
| 51 | + flags: 'a' |
| 52 | + }); |
| 53 | + debug("File reopened: " + filePath); |
| 54 | + } |
| 55 | + |
| 56 | + |
| 57 | + req.on('data', function(data) { |
| 58 | + debug("bytes received", upload.bytesReceived); |
| 59 | + upload.bytesReceived += data.length; |
| 60 | + }); |
| 61 | + |
| 62 | + // send request body to file |
| 63 | + req.pipe(fileStream); |
| 64 | + |
| 65 | + // when the request is finished, and all its data is written |
| 66 | + fileStream.on('close', function() { |
| 67 | + if (upload.bytesReceived == req.headers['x-file-size']) { |
| 68 | + debug("Upload finished"); |
| 69 | + delete uploads[fileId]; |
| 70 | + |
| 71 | + // can do something else with the uploaded file here |
| 72 | + |
| 73 | + res.end("Success " + upload.bytesReceived); |
| 74 | + } else { |
| 75 | + // connection lost, we leave the unfinished file around |
| 76 | + debug("File unfinished, stopped at " + upload.bytesReceived); |
| 77 | + res.end(); |
| 78 | + } |
| 79 | + }); |
| 80 | + |
| 81 | + // in case of I/O error - finish the request |
| 82 | + fileStream.on('error', function(err) { |
| 83 | + debug("fileStream error"); |
| 84 | + res.writeHead(500, "File error"); |
| 85 | + res.end(); |
| 86 | + }); |
| 87 | + |
| 88 | +} |
| 89 | + |
| 90 | +function onStatus(req, res) { |
| 91 | + let fileId = req.headers['x-file-id']; |
| 92 | + let upload = uploads[fileId]; |
| 93 | + debug("onStatus fileId:", fileId, " upload:", upload); |
| 94 | + if (!upload) { |
| 95 | + res.end("0") |
| 96 | + } else { |
| 97 | + res.end(String(upload.bytesReceived)); |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | + |
| 102 | +function accept(req, res) { |
| 103 | + if (req.url == '/status') { |
| 104 | + onStatus(req, res); |
| 105 | + } else if (req.url == '/upload' && req.method == 'POST') { |
| 106 | + onUpload(req, res); |
| 107 | + } else { |
| 108 | + fileServer.serve(req, res); |
| 109 | + } |
| 110 | + |
| 111 | +} |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | + |
| 116 | +// ----------------------------------- |
| 117 | + |
| 118 | +if (!module.parent) { |
| 119 | + http.createServer(accept).listen(8080); |
| 120 | + console.log('Server listening at port 8080'); |
| 121 | +} else { |
| 122 | + exports.accept = accept; |
| 123 | +} |
0 commit comments