summaryrefslogblamecommitdiff
path: root/user/kfilemetadata/add-mimeutils.patch
blob: 5ea2cef37188b56f7b09c210d91088b87a50eb65 (plain) (tree)

























































































































































































                                                                                                                                                
From 69c25514cf6a08ceaaacbc4092cc02ff40853228 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Stefan=20Br=C3=BCns?= <stefan.bruens@rwth-aachen.de>
Date: Mon, 25 Mar 2019 17:40:41 +0100
Subject: Add helper function to determine mime type based on content and
 extension

Summary:
The QMimeDatabase::MatchDefault only falls back to content matching
if the extension is not known. This fails for e.g. Matroska files, where
the content allows to distinguish between audio and video files.

CCBUG: 403902

Reviewers: #baloo, #frameworks, astippich, ngraham, poboiko

Reviewed By: #baloo, astippich, ngraham

Subscribers: kde-frameworks-devel

Tags: #frameworks, #baloo

Differential Revision: https://phabricator.kde.org/D20045
---
 src/CMakeLists.txt |  1 +
 src/mimeutils.cpp  | 50 +++++++++++++++++++++++++++++++++++++++++++++++++
 src/mimeutils.h    | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 106 insertions(+)
 create mode 100644 src/mimeutils.cpp
 create mode 100644 src/mimeutils.h

diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index c3cbe8c..fc4ce19 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -14,6 +14,7 @@ set(KF5FileMetaData_SRCS
     writercollection.cpp
     externalwriter.cpp
     formatstrings.cpp
+    mimeutils.cpp
     )
 ecm_qt_declare_logging_category(KF5FileMetaData_SRCS HEADER kfilemetadata_debug.h IDENTIFIER KFILEMETADATA_LOG CATEGORY_NAME kf5.kfilemetadata)
 
diff --git a/src/mimeutils.cpp b/src/mimeutils.cpp
new file mode 100644
index 0000000..7ea4dc8
--- /dev/null
+++ b/src/mimeutils.cpp
@@ -0,0 +1,50 @@
+/*
+ * This file is part of KFileMetaData
+ * Copyright (C) 2019 Stefan Brüns <stefan.bruens@rwth-aachen.de>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+#include "mimeutils.h"
+
+namespace KFileMetaData {
+namespace MimeUtils {
+
+QMimeType strictMimeType(const QString& filePath, const QMimeDatabase& db)
+{
+    auto extensionMimes = db.mimeTypesForFileName(filePath);
+    auto contentMime    = db.mimeTypeForFile(filePath, QMimeDatabase::MatchContent);
+
+    if (extensionMimes.contains(contentMime)) {
+        // content based mime type is one of the types for the file extension, e.g.:
+        // *.ogg -> [ audio/ogg, audio/x-vorbis+ogg, ...]
+        // content -> audio/x-vorbis+ogg
+        return contentMime;
+    }
+
+    for (auto mime : extensionMimes) {
+        // check if the content is generic and the extension is more specific, e.g.:
+        // *.mkv -> [ video/matroska ]
+        // content -> application/matroska
+        if (mime.inherits(contentMime.name())) {
+            return mime;
+        }
+    }
+    // content mime type does not match the extension, trust the content
+    return contentMime;
+}
+
+}} // namespace KFileMetaData::MimeUtils
diff --git a/src/mimeutils.h b/src/mimeutils.h
new file mode 100644
index 0000000..f33e100
--- /dev/null
+++ b/src/mimeutils.h
@@ -0,0 +1,55 @@
+/*
+ * This file is part of KFileMetaData
+ * Copyright (C) 2019 Stefan Brüns <stefan.bruens@rwth-aachen.de>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+#ifndef KFILEMETADATA_MIMEUTILS
+#define KFILEMETADATA_MIMEUTILS
+
+#include <QMimeDatabase>
+#include "kfilemetadata_export.h"
+
+namespace KFileMetaData
+{
+namespace MimeUtils
+{
+
+/**
+ * Returns the mimetype for a file
+ *
+ * The function uses both content and filename to determine the
+ * \c QMimeType. In case the extension mimetype is more specific
+ * than the content mimetype, and the first inherits the latter,
+ * the extension mimetype is preferred.
+ * If the extension does not match the content, the content has
+ * higher priority.
+ * The file must exist and be readable.
+ *
+ * @since 5.57
+ *
+ * \sa QMimeDatabase::mimeTypesForFileName
+ * \sa QMimeType::inherits
+ */
+KFILEMETADATA_EXPORT
+QMimeType strictMimeType(const QString& filePath, const QMimeDatabase& db);
+
+
+} // namespace MimeUtils
+} // namespace KFileMetaData
+
+#endif // KFILEMETADATA_MIMEUTILS
-- 
cgit v1.1

From b56d0cb5b90baeef2ada6ba0cbaea91506df1270 Mon Sep 17 00:00:00 2001
From: Alexander Stippich <a.stippich@gmx.net>
Date: Wed, 27 Mar 2019 20:41:49 +0100
Subject: Generate header for new MimeUtils

---
 src/CMakeLists.txt | 1 +
 1 file changed, 1 insertion(+)

diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index fc4ce19..73bf7d0 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -75,6 +75,7 @@ ecm_generate_headers(KF5FileMetaData_CamelCase_HEADERS
     WriterPlugin
     WriterCollection
     EmbeddedImageData
+    MimeUtils
 
     PREFIX kfilemetadata
     REQUIRED_HEADERS KF5FileMetaData_HEADERS
-- 
cgit v1.1