Fix mimetype detection

@DeepDiver1975 @VicDeo 

Fixes #5516
Fixes #5474