summaryrefslogtreecommitdiff
path: root/user/kfilemetadata/add-mimeutils.patch
blob: 5ea2cef37188b56f7b09c210d91088b87a50eb65 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
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