Last active
December 18, 2015 05:39
-
-
Save ctalkington/5734444 to your computer and use it in GitHub Desktop.
ref ctalkington/node-archiver #29
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "test", | |
"dependencies" : { | |
"formidable": "~1.0.14", | |
"prettysize": "~0.0.2", | |
"archiver": "~0.4.3" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 + '/'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
example result (huge images):