core: Keep file extension when it is allowed for the file's mime type

This fixes a bug where an .mp3 file was renamed to .m2a on upload,
because mimetypes returns multiple extensions for the mime type
audio/mpeg. Instead of taking the first extension found, it checks
whether the original filename has already an extension that is
allowed.