From 1519cb77e92b8474e114db8b89ae7fd406d8e809 Mon Sep 17 00:00:00 2001 From: Jan Holthuis Date: Wed, 5 Jan 2022 15:50:29 +0100 Subject: [PATCH] SoundSource: Fix broken file type detection if file suffix is misleading If a file is named `foo.wav` but actually contains MP3 data, `SoundSource::getTypeFromFile` should return "mp3", not "wav". This behavior is expected and already tested in `SoundSourceProxyTest.getTypeFromFile`, but due to a bug in our test file creation code, the test operated on a wrong file name and passed although the behavior was broken and the function would just return "wav" in the example above. The reason for this is that QMimeDatabase only looks at the file name when the file suffix is misleading, and thus cannot detect that the file is actually an MP3 file: > The default matching algorithm looks at both the file name and the > file contents, if necessary. The file extension has priority over the > contents, but the contents will be used if the file extension is > unknown, or matches multiple MIME types. > > Source: https://doc.qt.io/qt-5/qmimedatabase.html#mimeTypeForFile This commit fixes `SoundSource::getTypeFromFile` to work as expected by using the file *content* instead of the file name for determining the file type. --- src/sources/soundsource.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/sources/soundsource.cpp b/src/sources/soundsource.cpp index 71c4b5d3de34..e686a4c75ae8 100644 --- a/src/sources/soundsource.cpp +++ b/src/sources/soundsource.cpp @@ -48,7 +48,8 @@ QString SoundSource::getTypeFromFile(const QFileInfo& fileInfo) { const QString fileSuffix = fileInfo.suffix(); const QString fileType = fileTypeFromSuffix(fileSuffix); DEBUG_ASSERT(!fileType.isEmpty() || fileType == QString{}); - const QMimeType mimeType = QMimeDatabase().mimeTypeForFile(fileInfo); + const QMimeType mimeType = QMimeDatabase().mimeTypeForFile( + fileInfo, QMimeDatabase::MatchContent); // According to the documentation mimeTypeForFile always returns a valid // type, using the generic type application/octet-stream as a fallback. // This might also occur for missing files as seen on Qt 5.12.