Guess mime type with python-magic if available. Fixes #1514.