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.
(cherry picked from commit f301dd34b508be0ac90d01e2b5b91fbdb07f56bd)