diff options
author | Jan Holthuis <jholthuis@mixxx.org> | 2022-01-05 15:50:29 +0100 |
---|---|---|
committer | Jan Holthuis <jholthuis@mixxx.org> | 2022-01-06 11:17:11 +0100 |
commit | c376317a43463d11bdce6c7c1f646e78fa7bcaf4 (patch) | |
tree | 89cadb7a20df4c9dc7cdcdf52f44887e45d77bbe | |
parent | 67dc7dc6c58b7408aee3282d6093d44180cb548c (diff) |
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.
-rw-r--r-- | src/sources/soundsource.cpp | 3 |
1 files changed, 2 insertions, 1 deletions
diff --git a/src/sources/soundsource.cpp b/src/sources/soundsource.cpp index 71c4b5d3de..e686a4c75a 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. |