title: Verify mime type of uploaded files in node.js
body: <p>I'm using node and express to handle file uploads and I'm streaming them directly to conversion services using multiparty/busboy and request.</p>
  
  <p>Is there a way to verify that the streams have some certain filetypes before sending them to the corresponding providers? I tried <a href="https://github.com/mscdex/mmmagic" rel="nofollow noreferrer">https://github.com/mscdex/mmmagic</a> to get the MIME type out of the first chunk(s) and it worked nicely. I was wondering if the following workflow might work somehow:</p>
  
  <ul>
  <li>Buffer the file upload stream and check the incoming data for the Mime type.</li>
  <li>When the first few chunks are checked and the mime type is correct, empty the buffer into the request-stream.</li>
  <li>When the mime type turns out not to be correct, send an error message and return.</li>
  </ul>
  
  <p>I tried to get this working but I seem to have some stream compatibility issues (node 0.8.x vs. node 0.10.x streams, which are not supported by the request library).</p>
  
  <p>Are there any best-practices to solve this problem? Am I looking at it the wrong way?</p>
  
  <p>EDIT: Thanks to <a href="https://stackoverflow.com/a/19934861/1525722">Paul</a> I came up with this code:</p>
  
  <p><a href="https://gist.github.com/chmanie/8520572" rel="nofollow noreferrer">https://gist.github.com/chmanie/8520572</a></p>