yeah, detection of file types is a fun. I worked on several implementation of very precise type detection (for use in email & web proxies), and there are so many low-level details that affect that detection. The biggest fun is detection of type for container-based file formats, like, OLE-based office files (office 95-2003), Zip-based formats (MS Office > 2003, OpenOffice/LibreOffice, jar/war/ear, ...) where you need to actually look into the file to determined the type. And to add complexity, the implementation should be very fast if you run inside the web proxy where type is detected for each request (at least twice for request/response stage, or more if you process an archive).
Extraction of content is a separate fun ;-)