File indexing completed on 2024-05-05 16:09:02

0001 /*
0002     This file is part of KFileMetaData
0003     SPDX-FileCopyrightText: 2019 Stefan BrĂ¼ns <stefan.bruens@rwth-aachen.de>
0004 
0005     SPDX-License-Identifier: LGPL-2.1-or-later
0006 */
0007 
0008 #include "mimeutils.h"
0009 
0010 namespace KFileMetaData {
0011 namespace MimeUtils {
0012 
0013 QMimeType strictMimeType(const QString& filePath, const QMimeDatabase& db)
0014 {
0015     auto extensionMimes = db.mimeTypesForFileName(filePath);
0016     auto contentMime    = db.mimeTypeForFile(filePath, QMimeDatabase::MatchContent);
0017 
0018     if (extensionMimes.contains(contentMime)) {
0019         // content based mime type is one of the types for the file extension, e.g.:
0020         // *.ogg -> [ audio/ogg, audio/x-vorbis+ogg, ...]
0021         // content -> audio/x-vorbis+ogg
0022         return contentMime;
0023     }
0024 
0025     for (const auto &mime : extensionMimes) {
0026         // check if the content is generic and the extension is more specific, e.g.:
0027         // *.mkv -> [ video/matroska ]
0028         // content -> application/matroska
0029         if (mime.inherits(contentMime.name())) {
0030             return mime;
0031         }
0032     }
0033     // content mime type does not match the extension, trust the content
0034     return contentMime;
0035 }
0036 
0037 }} // namespace KFileMetaData::MimeUtils