From 9adddd0d896a806a5b8eadedabffd1a14f39af74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Blin?= <sebastien.blin@savoirfairelinux.com> Date: Fri, 13 May 2022 10:20:46 -0400 Subject: [PATCH] messagesadapter: better file's detection use QMimeDatabase to detect file's extension. GitLab: #738 Change-Id: I47c74ef3011881d9694c9c54ab4de7f8a2af86e3 --- src/app/messagesadapter.cpp | 54 ++++++++++++++++++------------------- src/app/messagesadapter.h | 2 +- 2 files changed, 28 insertions(+), 28 deletions(-) diff --git a/src/app/messagesadapter.cpp b/src/app/messagesadapter.cpp index 3ba15c1c7..5ea91cdda 100644 --- a/src/app/messagesadapter.cpp +++ b/src/app/messagesadapter.cpp @@ -29,16 +29,18 @@ #include <api/datatransfermodel.h> #include <QApplication> +#include <QBuffer> #include <QClipboard> #include <QDesktopServices> +#include <QDir> #include <QFileInfo> #include <QImageReader> #include <QList> -#include <QUrl> #include <QMimeData> -#include <QBuffer> +#include <QMimeDatabase> +#include <QUrl> #include <QtMath> -#include <QDir> +#include <QRegExp> MessagesAdapter::MessagesAdapter(AppSettingsManager* settingsManager, PreviewEngine* previewEngine, @@ -476,21 +478,17 @@ MessagesAdapter::conversationTypersUrlToName(const QSet<QString>& typersSet) } QVariantMap -MessagesAdapter::isLocalImage(const QString& msg) -{ - QImageReader reader; - reader.setDecideFormatFromContent(true); - reader.setFileName(msg); - QByteArray fileFormat = reader.format(); - if (fileFormat == "gif") { - return {{"isAnimatedImage", true}}; - } - QList<QByteArray> supportedFormats = reader.supportedImageFormats(); - auto iterator = std::find_if(supportedFormats.begin(), - supportedFormats.end(), - [fileFormat](QByteArray format) { return format == fileFormat; }); - if (iterator != supportedFormats.end()) { - return {{"isImage", true}}; +MessagesAdapter::isLocalImage(const QString& mimename) +{ + if (mimename.startsWith("image/")) { + QString fileFormat = mimename; + fileFormat.replace("image/", ""); + QImageReader reader; + QList<QByteArray> supportedFormats = reader.supportedImageFormats(); + auto iterator = std::find_if(supportedFormats.begin(), + supportedFormats.end(), + [fileFormat](QByteArray format) { return format == fileFormat; }); + return {{"isImage", iterator != supportedFormats.end()}}; } return {{"isImage", false}}; } @@ -504,26 +502,28 @@ MessagesAdapter::getMediaInfo(const QString& msg) "<%1 style='width:100%;height:%2;outline:none;background-color:#f1f3f4;" "object-fit:cover;' " "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()) { return fileInfo; } - QRegularExpression vPattern("[^\\s]+(.*?)\\.(avi|mov|webm|webp|rmvb)$", - QRegularExpression::CaseInsensitiveOption); - QString type = vPattern.match(filePath).captured(2); + static const QRegExp vPattern("(video/)(avi|mov|webm|webp|rmvb)$", Qt::CaseInsensitive); + auto match = vPattern.indexIn(mime.name()); + QString type = vPattern.capturedTexts().size() == 3 ? vPattern.capturedTexts()[1] : ""; if (!type.isEmpty()) { return { {"isVideo", true}, - {"html", html.arg("video", "100%", filePath, "video/" + type)}, + {"html", html.arg("video", "100%", filePath, mime.name())}, }; } else { - QRegularExpression aPattern("[^\\s]+(.*?)\\.(ogg|flac|wav|mpeg|mp3)$", - QRegularExpression::CaseInsensitiveOption); - type = aPattern.match(filePath).captured(2); + static const QRegExp aPattern("(audio/)(ogg|flac|wav|mpeg|mp3)$", Qt::CaseInsensitive); + match = aPattern.indexIn(mime.name()); + type = aPattern.capturedTexts().size() == 3 ? aPattern.capturedTexts()[1] : ""; if (!type.isEmpty()) { return { {"isVideo", false}, - {"html", html.arg("audio", "54px", filePath, "audio/" + type)}, + {"html", html.arg("audio", "54px", filePath, mime.name())}, }; } } diff --git a/src/app/messagesadapter.h b/src/app/messagesadapter.h index 3d28af316..2473fcbb2 100644 --- a/src/app/messagesadapter.h +++ b/src/app/messagesadapter.h @@ -101,7 +101,7 @@ protected: Q_INVOKABLE void deleteInteraction(const QString& interactionId); Q_INVOKABLE void copyToDownloads(const QString& interactionId, const QString& displayName); 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 bool isRemoteImage(const QString& msg); Q_INVOKABLE QString getFormattedTime(const quint64 timestamp); -- GitLab