KFileMetaData

mimeutils.cpp
1 /*
2  This file is part of KFileMetaData
3  SPDX-FileCopyrightText: 2019 Stefan Brüns <[email protected]>
4 
5  SPDX-License-Identifier: LGPL-2.1-or-later
6 */
7 
8 #include "mimeutils.h"
9 
10 namespace KFileMetaData {
11 namespace MimeUtils {
12 
13 QMimeType strictMimeType(const QString& filePath, const QMimeDatabase& db)
14 {
15  auto extensionMimes = db.mimeTypesForFileName(filePath);
16  auto contentMime = db.mimeTypeForFile(filePath, QMimeDatabase::MatchContent);
17 
18  if (extensionMimes.contains(contentMime)) {
19  // content based mime type is one of the types for the file extension, e.g.:
20  // *.ogg -> [ audio/ogg, audio/x-vorbis+ogg, ...]
21  // content -> audio/x-vorbis+ogg
22  return contentMime;
23  }
24 
25  for (const auto &mime : extensionMimes) {
26  // check if the content is generic and the extension is more specific, e.g.:
27  // *.mkv -> [ video/matroska ]
28  // content -> application/matroska
29  if (mime.inherits(contentMime.name())) {
30  return mime;
31  }
32  }
33  // content mime type does not match the extension, trust the content
34  return contentMime;
35 }
36 
37 }} // namespace KFileMetaData::MimeUtils
QList< QMimeType > mimeTypesForFileName(const QString &fileName) const const
QMimeType mimeTypeForFile(const QString &fileName, QMimeDatabase::MatchMode mode) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sun Aug 9 2020 22:56:15 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.