Skip to content
Snippets Groups Projects
Commit 9adddd0d authored by Sébastien Blin's avatar Sébastien Blin
Browse files

messagesadapter: better file's detection

use QMimeDatabase to detect file's extension.

GitLab: #738
Change-Id: I47c74ef3011881d9694c9c54ab4de7f8a2af86e3
parent d490e840
No related branches found
No related tags found
No related merge requests found
...@@ -29,16 +29,18 @@ ...@@ -29,16 +29,18 @@
#include <api/datatransfermodel.h> #include <api/datatransfermodel.h>
#include <QApplication> #include <QApplication>
#include <QBuffer>
#include <QClipboard> #include <QClipboard>
#include <QDesktopServices> #include <QDesktopServices>
#include <QDir>
#include <QFileInfo> #include <QFileInfo>
#include <QImageReader> #include <QImageReader>
#include <QList> #include <QList>
#include <QUrl>
#include <QMimeData> #include <QMimeData>
#include <QBuffer> #include <QMimeDatabase>
#include <QUrl>
#include <QtMath> #include <QtMath>
#include <QDir> #include <QRegExp>
MessagesAdapter::MessagesAdapter(AppSettingsManager* settingsManager, MessagesAdapter::MessagesAdapter(AppSettingsManager* settingsManager,
PreviewEngine* previewEngine, PreviewEngine* previewEngine,
...@@ -476,21 +478,17 @@ MessagesAdapter::conversationTypersUrlToName(const QSet<QString>& typersSet) ...@@ -476,21 +478,17 @@ MessagesAdapter::conversationTypersUrlToName(const QSet<QString>& typersSet)
} }
QVariantMap QVariantMap
MessagesAdapter::isLocalImage(const QString& msg) MessagesAdapter::isLocalImage(const QString& mimename)
{ {
QImageReader reader; if (mimename.startsWith("image/")) {
reader.setDecideFormatFromContent(true); QString fileFormat = mimename;
reader.setFileName(msg); fileFormat.replace("image/", "");
QByteArray fileFormat = reader.format(); QImageReader reader;
if (fileFormat == "gif") { QList<QByteArray> supportedFormats = reader.supportedImageFormats();
return {{"isAnimatedImage", true}}; auto iterator = std::find_if(supportedFormats.begin(),
} supportedFormats.end(),
QList<QByteArray> supportedFormats = reader.supportedImageFormats(); [fileFormat](QByteArray format) { return format == fileFormat; });
auto iterator = std::find_if(supportedFormats.begin(), return {{"isImage", iterator != supportedFormats.end()}};
supportedFormats.end(),
[fileFormat](QByteArray format) { return format == fileFormat; });
if (iterator != supportedFormats.end()) {
return {{"isImage", true}};
} }
return {{"isImage", false}}; return {{"isImage", false}};
} }
...@@ -504,26 +502,28 @@ MessagesAdapter::getMediaInfo(const QString& msg) ...@@ -504,26 +502,28 @@ MessagesAdapter::getMediaInfo(const QString& msg)
"<%1 style='width:100%;height:%2;outline:none;background-color:#f1f3f4;" "<%1 style='width:100%;height:%2;outline:none;background-color:#f1f3f4;"
"object-fit:cover;' " "object-fit:cover;' "
"controls controlsList='nodownload' src='file://%3' type='%4'/></body>"; "controls controlsList='nodownload' src='file://%3' type='%4'/></body>";
QVariantMap fileInfo = isLocalImage(msg); QMimeDatabase db;
QMimeType mime = db.mimeTypeForFile(filePath);
QVariantMap fileInfo = isLocalImage(mime.name());
if (fileInfo["isImage"].toBool() || fileInfo["isAnimatedImage"].toBool()) { if (fileInfo["isImage"].toBool() || fileInfo["isAnimatedImage"].toBool()) {
return fileInfo; return fileInfo;
} }
QRegularExpression vPattern("[^\\s]+(.*?)\\.(avi|mov|webm|webp|rmvb)$", static const QRegExp vPattern("(video/)(avi|mov|webm|webp|rmvb)$", Qt::CaseInsensitive);
QRegularExpression::CaseInsensitiveOption); auto match = vPattern.indexIn(mime.name());
QString type = vPattern.match(filePath).captured(2); QString type = vPattern.capturedTexts().size() == 3 ? vPattern.capturedTexts()[1] : "";
if (!type.isEmpty()) { if (!type.isEmpty()) {
return { return {
{"isVideo", true}, {"isVideo", true},
{"html", html.arg("video", "100%", filePath, "video/" + type)}, {"html", html.arg("video", "100%", filePath, mime.name())},
}; };
} else { } else {
QRegularExpression aPattern("[^\\s]+(.*?)\\.(ogg|flac|wav|mpeg|mp3)$", static const QRegExp aPattern("(audio/)(ogg|flac|wav|mpeg|mp3)$", Qt::CaseInsensitive);
QRegularExpression::CaseInsensitiveOption); match = aPattern.indexIn(mime.name());
type = aPattern.match(filePath).captured(2); type = aPattern.capturedTexts().size() == 3 ? aPattern.capturedTexts()[1] : "";
if (!type.isEmpty()) { if (!type.isEmpty()) {
return { return {
{"isVideo", false}, {"isVideo", false},
{"html", html.arg("audio", "54px", filePath, "audio/" + type)}, {"html", html.arg("audio", "54px", filePath, mime.name())},
}; };
} }
} }
......
...@@ -101,7 +101,7 @@ protected: ...@@ -101,7 +101,7 @@ protected:
Q_INVOKABLE void deleteInteraction(const QString& interactionId); Q_INVOKABLE void deleteInteraction(const QString& interactionId);
Q_INVOKABLE void copyToDownloads(const QString& interactionId, const QString& displayName); Q_INVOKABLE void copyToDownloads(const QString& interactionId, const QString& displayName);
Q_INVOKABLE void userIsComposing(bool isComposing); Q_INVOKABLE void userIsComposing(bool isComposing);
Q_INVOKABLE QVariantMap isLocalImage(const QString& msg); Q_INVOKABLE QVariantMap isLocalImage(const QString& mimeName);
Q_INVOKABLE QVariantMap getMediaInfo(const QString& msg); Q_INVOKABLE QVariantMap getMediaInfo(const QString& msg);
Q_INVOKABLE bool isRemoteImage(const QString& msg); Q_INVOKABLE bool isRemoteImage(const QString& msg);
Q_INVOKABLE QString getFormattedTime(const quint64 timestamp); Q_INVOKABLE QString getFormattedTime(const quint64 timestamp);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment