KFileMetaData

mimeutils.cpp
1/*
2 This file is part of KFileMetaData
3 SPDX-FileCopyrightText: 2019 Stefan BrĂ¼ns <stefan.bruens@rwth-aachen.de>
4
5 SPDX-License-Identifier: LGPL-2.1-or-later
6*/
7
8#include "mimeutils.h"
9
10namespace KFileMetaData {
11namespace MimeUtils {
12
13QMimeType 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
QMimeType strictMimeType(const QString &filePath, const QMimeDatabase &db)
Returns the MIME type for a file.
Definition mimeutils.cpp:13
The KFileMetaData namespace.
QMimeType mimeTypeForFile(const QFileInfo &fileInfo, MatchMode mode) const const
QList< QMimeType > mimeTypesForFileName(const QString &fileName) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:12:41 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.