title: Calculate a file hash and save the file
body: <p>Users upload files into my express app. I need to calc hash of the uploaded file and then write file to disk using calculated hash as a filename. I try to do it using the following code:</p>
  
  <pre><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);
  }
  </code></pre>
  
  <p>The problem is that after I calc file hash the writed file size is 0. What is the best way do solve this task?</p>
  
  <p><strong>Update</strong>
  According @poke suggestion I try to duplicate my stream. Now my code is:</p>
  
  <pre><code>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);
  }
  </code></pre>
  
  <p>The problem of this code is that events <code>end</code> and <code>finish</code> are not emited. If I comment <code>file.pipe(s2);</code> events are emited, but I again get my origin problem.</p>