diff --git a/samples/Node.js/.vscode/launch.json b/samples/Node.js/.vscode/launch.json new file mode 100644 index 00000000..901d07ca --- /dev/null +++ b/samples/Node.js/.vscode/launch.json @@ -0,0 +1,16 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "type": "node", + "request": "launch", + "name": "Launch Program", + "program": "${workspaceFolder}/app.js", + "env": { + "PORT": 3000, + "UPLOAD_DIR": "${workspaceFolder}/upload", + "RESUMEABLE_DIR": "${workspaceFolder}/../../resumable.js" + }, + } + ] +} \ No newline at end of file diff --git a/samples/Node.js/app.js b/samples/Node.js/app.js index 6ad4b264..b0d07322 100644 --- a/samples/Node.js/app.js +++ b/samples/Node.js/app.js @@ -1,5 +1,5 @@ var express = require('express'); -var resumable = require('./resumable-node.js')('/tmp/resumable.js/'); +var resumable = require('./resumable-node.js')(process.env.UPLOAD_DIR); var app = express(); var multipart = require('connect-multiparty'); var crypto = require('crypto'); @@ -51,7 +51,7 @@ app.get('/download/:identifier', function(req, res){ app.get('/resumable.js', function (req, res) { var fs = require('fs'); res.setHeader("content-type", "application/javascript"); - fs.createReadStream("../../resumable.js").pipe(res); + fs.createReadStream(process.env.RESUMEABLE_DIR).pipe(res); }); -app.listen(3000); +app.listen(process.env.PORT); diff --git a/samples/Node.js/resumable-node.js b/samples/Node.js/resumable-node.js index 4a1fd567..8f2fd06f 100644 --- a/samples/Node.js/resumable-node.js +++ b/samples/Node.js/resumable-node.js @@ -7,6 +7,7 @@ module.exports = resumable = function(temporaryFolder){ $.temporaryFolder = temporaryFolder; $.maxFileSize = null; $.fileParameterName = 'file'; + $.files = new Map(); try { fs.mkdirSync($.temporaryFolder); @@ -106,12 +107,16 @@ module.exports = resumable = function(temporaryFolder){ } var validation = validateRequest(chunkNumber, chunkSize, totalSize, identifier, files[$.fileParameterName].size); if(validation=='valid') { + if (chunkNumber === 1) { + $.files.clear(); + } var chunkFilename = getChunkFilename(chunkNumber, identifier); // Save the chunk (TODO: OVERWRITE) fs.rename(files[$.fileParameterName].path, chunkFilename, function(){ // Do we have all the chunks? + $.files[chunkNumber] = chunkFilename; var currentTestChunk = 1; var numberOfChunks = Math.max(Math.floor(totalSize/(chunkSize*1.0)), 1); var testChunkExists = function(){ @@ -119,6 +124,12 @@ module.exports = resumable = function(temporaryFolder){ if(exists){ currentTestChunk++; if(currentTestChunk>numberOfChunks) { + for (var i = 0; i < numberOfChunks; ++i) { + var file_path = $.files[i + 1]; + var content = fs.readFileSync(file_path); + fs.appendFile(path.join($.temporaryFolder, filename), content); + fs.unlinkSync(file_path); + } callback('done', filename, original_filename, identifier); } else { // Recursion @@ -127,8 +138,8 @@ module.exports = resumable = function(temporaryFolder){ } else { callback('partly_done', filename, original_filename, identifier); } - }); - } + }); + } testChunkExists(); }); } else {