Use a short buffer and some heuristic (xdg-mime or infer, but really probably custom heuristics) to detect file types.