Skip to content

Commit

Permalink
SoundSource: Fix broken file type detection if file suffix is misleading
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Holzhaus committed Jan 5, 2022
1 parent 420cc7f commit 1519cb7
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/sources/soundsource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 1519cb7

Please sign in to comment.