Skip to content
This repository has been archived by the owner on Oct 17, 2019. It is now read-only.

Commit

Permalink
Use the http link for video messages
Browse files Browse the repository at this point in the history
Add method to convert matrix content URIs to http links
  • Loading branch information
mujx committed Jul 15, 2018
1 parent 33490ed commit a9f0212
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 21 deletions.
4 changes: 4 additions & 0 deletions include/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
12 changes: 12 additions & 0 deletions src/Utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
8 changes: 2 additions & 6 deletions src/timeline/widgets/FileItem.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
8 changes: 2 additions & 6 deletions src/timeline/widgets/ImageItem.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
11 changes: 2 additions & 9 deletions src/timeline/widgets/VideoItem.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,8 @@
void
VideoItem::init()
{
// QList<QString> 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<mtx::events::msg::Video> &event, QWidget *parent)
Expand Down

0 comments on commit a9f0212

Please sign in to comment.