Skip to content

Instantly share code, notes, and snippets.

@ctalkington
Last active December 18, 2015 05:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ctalkington/5734444 to your computer and use it in GitHub Desktop.
Save ctalkington/5734444 to your computer and use it in GitHub Desktop.
ref ctalkington/node-archiver #29
{
"name": "test",
"dependencies" : {
"formidable": "~1.0.14",
"prettysize": "~0.0.2",
"archiver": "~0.4.3"
}
}
var fs = require("fs");
var stream = require("stream");
var http = require('http');
var formidable = require('formidable');
var archiver = require("archiver");
var prettySize = require('prettysize');
var server;
var count = 0;
var startDate;
var endDate;
var timeDiff;
var archiveSize;
var receivedSize;
server = http.createServer(function(req, res) {
if (req.url === '/') {
res.writeHead(200, {
'content-type': 'text/html'
});
res.end(
'<form action="/upload" enctype="multipart/form-data" method="post">' +
'<input type="text" name="title"><br>' +
'<input type="file" name="upload" multiple="multiple"><br>' +
'<input type="submit" value="Upload">' +
'</form>');
} else if (req.url === '/upload') {
startDate = new Date();
var feedBack = true;
var output;
var archive = archiver('zip');
archive.on('error', function(err) {
if (err) console.error(err);
});
var form = new formidable.IncomingForm();
form.on('end', function() {
endDate = new Date();
timeDiff = Math.abs((endDate.getTime() - startDate.getTime()) / 1000);
receivedSize = form.bytesReceived;
console.log('received: ' + prettySize(receivedSize) + ' (' + count + ' files) in ' + timeDiff + ' seconds');
console.log('writing archive to: ' + (feedBack ? 'browser' : 'disk'));
archive.finalize();
});
form.onPart = function(part) {
if (!part.filename) {
form.handlePart(part);
return;
}
count++;
var passThrough = new stream.PassThrough();
part.pipe(passThrough);
archive.append(passThrough, {
name: part.filename
});
};
var logThis = function(err) {
if (err) console.error(err);
endDate = new Date();
timeDiff = Math.abs((endDate.getTime() - startDate.getTime()) / 1000);
archiveSize = archive.archiver.pointer;
console.log('resulting archive size was: ' + prettySize(archiveSize));
console.log('run time: ' + timeDiff + ' seconds');
if (feedBack === false) {
res.end();
}
};
if (feedBack) {
// feed back to browser
res.on('finish', logThis);
res.writeHead(200, {
'Content-Type': 'application/zip',
'Content-Disposition': 'attachment; filename=output.zip'
});
archive.pipe(res);
} else {
output = fs.createWriteStream(__dirname + '/output.zip');
output.on('close', logThis);
archive.pipe(output);
}
form.parse(req);
} else {
res.writeHead(404, {
'content-type': 'text/plain'
});
res.end('404');
}
});
server.listen(4000);
console.log('listening on http://localhost:' + 4000 + '/');
@ctalkington
Copy link
Author

example result (huge images):

> node server.js
listening on http://localhost:4000/
received: 44.4 MB (18 files) in 5.4 seconds
writing archive to: browser
resulting archive size was: 43.9 MB
run time: 9.726 seconds

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment