middleware: improve filename extension extraction