From a9f02128a24d149579101e8dc2fc92b5069b8691 Mon Sep 17 00:00:00 2001 From: Konstantinos Sideris Date: Sun, 15 Jul 2018 20:05:31 +0300 Subject: [PATCH] Use the http link for video messages Add method to convert matrix content URIs to http links --- include/Utils.h | 4 ++++ src/Utils.cc | 12 ++++++++++++ src/timeline/widgets/FileItem.cc | 8 ++------ src/timeline/widgets/ImageItem.cc | 8 ++------ src/timeline/widgets/VideoItem.cc | 11 ++--------- 5 files changed, 22 insertions(+), 21 deletions(-) diff --git a/include/Utils.h b/include/Utils.h index 7db405b1..8f9b7cff 100644 --- a/include/Utils.h +++ b/include/Utils.h @@ -187,4 +187,8 @@ levenshtein_distance(const std::string &s1, const std::string &s2); QPixmap scaleImageToPixmap(const QImage &img, int size); + +//! Convert a Content Matrix URI to an HTTP link. +QString +mxcToHttp(const QUrl &url, const QString &server, int port); } diff --git a/src/Utils.cc b/src/Utils.cc index 705a9e21..2247c2b7 100644 --- a/src/Utils.cc +++ b/src/Utils.cc @@ -174,3 +174,15 @@ utils::scaleImageToPixmap(const QImage &img, int size) return QPixmap::fromImage( img.scaled(sz, sz, Qt::IgnoreAspectRatio, Qt::SmoothTransformation)); } + +QString +utils::mxcToHttp(const QUrl &url, const QString &server, int port) +{ + auto mxcParts = mtx::client::utils::parse_mxc_url(url.toString().toStdString()); + + return QString("https://%1:%2/_matrix/media/r0/download/%3/%4") + .arg(server) + .arg(port) + .arg(QString::fromStdString(mxcParts.server)) + .arg(QString::fromStdString(mxcParts.media_id)); +} diff --git a/src/timeline/widgets/FileItem.cc b/src/timeline/widgets/FileItem.cc index 7c2be9f2..b4555b2f 100644 --- a/src/timeline/widgets/FileItem.cc +++ b/src/timeline/widgets/FileItem.cc @@ -81,12 +81,8 @@ FileItem::openUrl() if (url_.toString().isEmpty()) return; - auto mxc_parts = mtx::client::utils::parse_mxc_url(url_.toString().toStdString()); - auto urlToOpen = QString("https://%1:%2/_matrix/media/r0/download/%3/%4") - .arg(QString::fromStdString(http::client()->server())) - .arg(http::client()->port()) - .arg(QString::fromStdString(mxc_parts.server)) - .arg(QString::fromStdString(mxc_parts.media_id)); + auto urlToOpen = utils::mxcToHttp( + url_, QString::fromStdString(http::client()->server()), http::client()->port()); if (!QDesktopServices::openUrl(urlToOpen)) nhlog::ui()->warn("Could not open url: {}", urlToOpen.toStdString()); diff --git a/src/timeline/widgets/ImageItem.cc b/src/timeline/widgets/ImageItem.cc index 8c8f50a1..b7adb0fa 100644 --- a/src/timeline/widgets/ImageItem.cc +++ b/src/timeline/widgets/ImageItem.cc @@ -106,12 +106,8 @@ ImageItem::openUrl() if (url_.toString().isEmpty()) return; - auto mxc_parts = mtx::client::utils::parse_mxc_url(url_.toString().toStdString()); - auto urlToOpen = QString("https://%1:%2/_matrix/media/r0/download/%3/%4") - .arg(QString::fromStdString(http::client()->server())) - .arg(http::client()->port()) - .arg(QString::fromStdString(mxc_parts.server)) - .arg(QString::fromStdString(mxc_parts.media_id)); + auto urlToOpen = utils::mxcToHttp( + url_, QString::fromStdString(http::client()->server()), http::client()->port()); if (!QDesktopServices::openUrl(urlToOpen)) nhlog::ui()->warn("could not open url: {}", urlToOpen.toStdString()); diff --git a/src/timeline/widgets/VideoItem.cc b/src/timeline/widgets/VideoItem.cc index 34d963a9..daf181b2 100644 --- a/src/timeline/widgets/VideoItem.cc +++ b/src/timeline/widgets/VideoItem.cc @@ -26,15 +26,8 @@ void VideoItem::init() { - // QList url_parts = url_.toString().split("mxc://"); - // if (url_parts.size() != 2) { - // qDebug() << "Invalid format for image" << url_.toString(); - // return; - // } - - // QString media_params = url_parts[1]; - // url_ = QString("%1/_matrix/media/r0/download/%2") - // .arg(http::client()->getHomeServer().toString(), media_params); + url_ = utils::mxcToHttp( + url_, QString::fromStdString(http::client()->server()), http::client()->port()); } VideoItem::VideoItem(const mtx::events::RoomEvent &event, QWidget *parent)