Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

merge fils after upload down #524

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions samples/Node.js/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -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"
},
}
]
}
6 changes: 3 additions & 3 deletions samples/Node.js/app.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down Expand Up @@ -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);
15 changes: 13 additions & 2 deletions samples/Node.js/resumable-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module.exports = resumable = function(temporaryFolder){
$.temporaryFolder = temporaryFolder;
$.maxFileSize = null;
$.fileParameterName = 'file';
$.files = new Map();

try {
fs.mkdirSync($.temporaryFolder);
Expand Down Expand Up @@ -106,19 +107,29 @@ 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(){
fs.exists(getChunkFilename(currentTestChunk, identifier), function(exists){
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
Expand All @@ -127,8 +138,8 @@ module.exports = resumable = function(temporaryFolder){
} else {
callback('partly_done', filename, original_filename, identifier);
}
});
}
});
}
testChunkExists();
});
} else {
Expand Down