node.js - Calculate a file hash and save the file -


users upload files express app. need calc hash of uploaded file , write file disk using calculated hash filename. try using following code:

function storefilestream(file, next) {     createfilehash(file, function(err, hash) {         if (err) {             return next(err);         }          var filename = path.join(config.storagepath, hash),             stream = fs.createwritestream(filename);          stream.on('error', function(err) {             return next(err);         });         stream.on('finish', function() {             return next();         });          file.pipe(stream);     }); }  function createfilehash(file, next) {     var hash = crypto.createhash('sha1');     hash.setencoding('hex');      file.on('error', function(err) {         return next(err);     });     file.on('end', function(data) {         hash.end();         return next(null, hash.read());     });      file.pipe(hash); } 

the problem after calc file hash writed file size 0. best way solve task?

update according @poke suggestion try duplicate stream. code is:

function storefilestream(file, next) {     var s1 = new pass;     var s2 = new pass;     file.pipe(s1);     file.pipe(s2);              createfilehash(s1, function(err, hash) {         if (err) {             return next(err);         }          var filename = path.join(config.storagepath, hash),             stream = fs.createwritestream(filename);          stream.on('error', function(err) {             return next(err);         });         stream.on('finish', function() {             return next();         });          s2.pipe(stream);     }); }  function createfilehash(file, next) {     var hash = crypto.createhash('sha1');     hash.setencoding('hex');      file.on('error', function(err) {         return next(err);     });     file.on('end', function(data) {         hash.end();         return next(null, hash.read());     });      file.pipe(hash); } 

the problem of code events end , finish not emited. if comment file.pipe(s2); events emited, again origin problem.

you use async module (not tested should work):

async.waterfall([     function(done) {         var hash = crypto.createhash('sha1');         hash.setencoding('hex');          file.on('error', function(err) {             done(err);         });         file.on('end', function(data) {             done(null, hash.read);         });          file.pipe(hash);      },     function(hash, done) {         var filename = path.join(config.storagepath, hash),             stream = fs.createwritestream(filename);          stream.on('error', function(err) {             done(err);         });         stream.on('finish', function() {             done(null);         });          file.pipe(stream);     } ], function (err) {     console.log("everything done!"); }); 

Comments

Popular posts from this blog

javascript - Google App Script ContentService downloadAsFile not working -

javascript - Function overwritting -

php - Find a regex to take part of Email -