From 79f614aee78ad21e7b86e18d3318dfc8d99d1826 Mon Sep 17 00:00:00 2001 From: GuDuJian-J-Zhang Date: Tue, 2 Apr 2019 17:59:00 +0800 Subject: [PATCH 1/7] debug node --- .vscode/launch.json | 19 +++++++++++++++++++ samples/Node.js/app.js | 6 +++--- 2 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 00000000..ea742fe1 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,19 @@ +{ + // 使用 IntelliSense 了解相关属性。 + // 悬停以查看现有属性的描述。 + // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "node", + "request": "launch", + "name": "Launch Program", + "program": "${workspaceFolder}/samples/Node.js/app.js", + "env": { + "PORT": 3000, + "UPLOAD_DIR": "/Users/zhangjun/work/simright/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); From 17fe394f6b52983683c2dc04d243eb6eade3cac4 Mon Sep 17 00:00:00 2001 From: GuDuJian-J-Zhang Date: Tue, 2 Apr 2019 18:15:33 +0800 Subject: [PATCH 2/7] file name --- samples/Node.js/resumable-node.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/samples/Node.js/resumable-node.js b/samples/Node.js/resumable-node.js index 4a1fd567..7a330e8b 100644 --- a/samples/Node.js/resumable-node.js +++ b/samples/Node.js/resumable-node.js @@ -21,7 +21,7 @@ module.exports = resumable = function(temporaryFolder){ // Clean up the identifier identifier = cleanIdentifier(identifier); // What would the file name be? - return path.join($.temporaryFolder, './resumable-'+identifier+'.'+chunkNumber); + return path.join($.temporaryFolder, identifier+'.'+chunkNumber); } var validateRequest = function(chunkNumber, chunkSize, totalSize, identifier, filename, fileSize){ @@ -106,7 +106,7 @@ module.exports = resumable = function(temporaryFolder){ } var validation = validateRequest(chunkNumber, chunkSize, totalSize, identifier, files[$.fileParameterName].size); if(validation=='valid') { - var chunkFilename = getChunkFilename(chunkNumber, identifier); + var chunkFilename = getChunkFilename(chunkNumber, filename); // Save the chunk (TODO: OVERWRITE) fs.rename(files[$.fileParameterName].path, chunkFilename, function(){ From f09e9a5e63de18ac25a3b21ae06d6fecd4700ef3 Mon Sep 17 00:00:00 2001 From: GuDuJian-J-Zhang Date: Tue, 2 Apr 2019 18:38:07 +0800 Subject: [PATCH 3/7] merge files --- samples/Node.js/resumable-node.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/samples/Node.js/resumable-node.js b/samples/Node.js/resumable-node.js index 7a330e8b..06c925a4 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 = []; try { fs.mkdirSync($.temporaryFolder); @@ -112,27 +113,34 @@ module.exports = resumable = function(temporaryFolder){ fs.rename(files[$.fileParameterName].path, chunkFilename, function(){ // Do we have all the chunks? + $.files.push(chunkFilename); var currentTestChunk = 1; var numberOfChunks = Math.max(Math.floor(totalSize/(chunkSize*1.0)), 1); var testChunkExists = function(){ - fs.exists(getChunkFilename(currentTestChunk, identifier), function(exists){ + fs.exists(getChunkFilename(currentTestChunk, filename), function(exists){ if(exists){ currentTestChunk++; if(currentTestChunk>numberOfChunks) { - callback('done', filename, original_filename, identifier); + let num_files = $.files.length; + for (let i = 0; i < num_files; ++i) { + let content = fs.readFileSync($.files[i]) + fs.appendFile(path.join($.temporaryFolder, filename), content); + fs.unlinkSync($.files[i]); + } + callback('done', filename, original_filename, filename); } else { // Recursion testChunkExists(); } } else { - callback('partly_done', filename, original_filename, identifier); + callback('partly_done', filename, original_filename, filename); } }); } testChunkExists(); }); } else { - callback(validation, filename, original_filename, identifier); + callback(validation, filename, original_filename, filename); } } From c699533e0a31c3508b48caf88278ba0fbda83463 Mon Sep 17 00:00:00 2001 From: GuDuJian-J-Zhang Date: Wed, 3 Apr 2019 14:39:40 +0800 Subject: [PATCH 4/7] code cleanup --- samples/Node.js/resumable-node.js | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/samples/Node.js/resumable-node.js b/samples/Node.js/resumable-node.js index 06c925a4..f074eeaa 100644 --- a/samples/Node.js/resumable-node.js +++ b/samples/Node.js/resumable-node.js @@ -7,7 +7,7 @@ module.exports = resumable = function(temporaryFolder){ $.temporaryFolder = temporaryFolder; $.maxFileSize = null; $.fileParameterName = 'file'; - $.files = []; + $.files = new Map(); try { fs.mkdirSync($.temporaryFolder); @@ -22,7 +22,7 @@ module.exports = resumable = function(temporaryFolder){ // Clean up the identifier identifier = cleanIdentifier(identifier); // What would the file name be? - return path.join($.temporaryFolder, identifier+'.'+chunkNumber); + return path.join($.temporaryFolder, './resumable-'+identifier+'.'+chunkNumber); } var validateRequest = function(chunkNumber, chunkSize, totalSize, identifier, filename, fileSize){ @@ -107,36 +107,39 @@ module.exports = resumable = function(temporaryFolder){ } var validation = validateRequest(chunkNumber, chunkSize, totalSize, identifier, files[$.fileParameterName].size); if(validation=='valid') { - var chunkFilename = getChunkFilename(chunkNumber, filename); + 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.push(chunkFilename); + $.files[chunkNumber] = chunkFilename; var currentTestChunk = 1; var numberOfChunks = Math.max(Math.floor(totalSize/(chunkSize*1.0)), 1); var testChunkExists = function(){ - fs.exists(getChunkFilename(currentTestChunk, filename), function(exists){ + fs.exists(getChunkFilename(currentTestChunk, identifier), function(exists){ if(exists){ currentTestChunk++; if(currentTestChunk>numberOfChunks) { - let num_files = $.files.length; - for (let i = 0; i < num_files; ++i) { - let content = fs.readFileSync($.files[i]) + 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($.files[i]); + fs.unlinkSync(file_path); } - callback('done', filename, original_filename, filename); + callback('done', filename, original_filename, identifier); } else { // Recursion testChunkExists(); } } else { - callback('partly_done', filename, original_filename, filename); + callback('partly_done', filename, original_filename, identifier); } - }); - } + }); + } testChunkExists(); }); } else { From d82b3674a4900240eec2c2e751a87421bfc2896f Mon Sep 17 00:00:00 2001 From: GuDuJian-J-Zhang Date: Wed, 3 Apr 2019 14:40:58 +0800 Subject: [PATCH 5/7] cleanup launch.json --- .vscode/launch.json | 3 --- 1 file changed, 3 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index ea742fe1..3a535bff 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -1,7 +1,4 @@ { - // 使用 IntelliSense 了解相关属性。 - // 悬停以查看现有属性的描述。 - // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { From 002060b78264a0453a45f0e5eb2f73e2a7908868 Mon Sep 17 00:00:00 2001 From: GuDuJian-J-Zhang Date: Wed, 3 Apr 2019 14:48:15 +0800 Subject: [PATCH 6/7] cleanup --- samples/Node.js/resumable-node.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/samples/Node.js/resumable-node.js b/samples/Node.js/resumable-node.js index f074eeaa..8f2fd06f 100644 --- a/samples/Node.js/resumable-node.js +++ b/samples/Node.js/resumable-node.js @@ -126,7 +126,7 @@ module.exports = resumable = function(temporaryFolder){ if(currentTestChunk>numberOfChunks) { for (var i = 0; i < numberOfChunks; ++i) { var file_path = $.files[i + 1]; - var content = fs.readFileSync(file_path) + var content = fs.readFileSync(file_path); fs.appendFile(path.join($.temporaryFolder, filename), content); fs.unlinkSync(file_path); } @@ -143,7 +143,7 @@ module.exports = resumable = function(temporaryFolder){ testChunkExists(); }); } else { - callback(validation, filename, original_filename, filename); + callback(validation, filename, original_filename, identifier); } } From 7ae27f493ad2da7ae11f4a086bf1c7c00308d749 Mon Sep 17 00:00:00 2001 From: GuDuJian-J-Zhang Date: Wed, 3 Apr 2019 15:49:43 +0800 Subject: [PATCH 7/7] move launch.json to Nodejs --- {.vscode => samples/Node.js/.vscode}/launch.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) rename {.vscode => samples/Node.js/.vscode}/launch.json (54%) diff --git a/.vscode/launch.json b/samples/Node.js/.vscode/launch.json similarity index 54% rename from .vscode/launch.json rename to samples/Node.js/.vscode/launch.json index 3a535bff..901d07ca 100644 --- a/.vscode/launch.json +++ b/samples/Node.js/.vscode/launch.json @@ -5,11 +5,11 @@ "type": "node", "request": "launch", "name": "Launch Program", - "program": "${workspaceFolder}/samples/Node.js/app.js", + "program": "${workspaceFolder}/app.js", "env": { "PORT": 3000, - "UPLOAD_DIR": "/Users/zhangjun/work/simright/upload", - "RESUMEABLE_DIR": "${workspaceFolder}/resumable.js" + "UPLOAD_DIR": "${workspaceFolder}/upload", + "RESUMEABLE_DIR": "${workspaceFolder}/../../resumable.js" }, } ]