From cd9d1a2ec69bf4c459f9e845293d5fd7dc1398d4 Mon Sep 17 00:00:00 2001 From: christarazi Date: Sun, 18 Feb 2018 12:52:31 -0800 Subject: Support audio, video, generic file for pasting (#220) * Refactor widget items to use same interface * Support audio, video, generic file for pasting * Add utils function for human readable file sizes * Set correct MIME type for media messages This change also determines the size of the upload once from the ContentLengthHeader, rather than seeking the QIODevice and asking for its size. This prevents any future trouble in case the QIODevice is sequential (cannot be seeked). The MIME type is also determined at upload once, rather than using the QIODevice and the underlying data inside. * Allow for file urls to be used as fall-back This fixes an issue on macOS which uses `text/uri-list` for copying files to the clipboard. fixes #228 --- src/MatrixClient.cc | 183 +++++++++++++++++++++++++++------------------------- 1 file changed, 95 insertions(+), 88 deletions(-) (limited to 'src/MatrixClient.cc') diff --git a/src/MatrixClient.cc b/src/MatrixClient.cc index 1d42e36c..c915c74a 100644 --- a/src/MatrixClient.cc +++ b/src/MatrixClient.cc @@ -280,7 +280,8 @@ MatrixClient::sendRoomMessage(mtx::events::MessageType ty, int txnId, const QString &roomid, const QString &msg, - const QFileInfo &fileinfo, + const QString &mime, + const int64_t media_size, const QString &url) noexcept { QUrlQuery query; @@ -291,14 +292,8 @@ MatrixClient::sendRoomMessage(mtx::events::MessageType ty, QString("/rooms/%1/send/m.room.message/%2").arg(roomid).arg(txnId)); endpoint.setQuery(query); - QString msgType(""); - - QMimeDatabase db; - QMimeType mime = - db.mimeTypeForFile(fileinfo.absoluteFilePath(), QMimeDatabase::MatchContent); - QJsonObject body; - QJsonObject info = {{"size", fileinfo.size()}, {"mimetype", mime.name()}}; + QJsonObject info = {{"size", static_cast(media_size)}, {"mimetype", mime}}; switch (ty) { case mtx::events::MessageType::Text: @@ -316,6 +311,9 @@ MatrixClient::sendRoomMessage(mtx::events::MessageType ty, case mtx::events::MessageType::Audio: body = {{"msgtype", "m.audio"}, {"body", msg}, {"url", url}, {"info", info}}; break; + case mtx::events::MessageType::Video: + body = {{"msgtype", "m.video"}, {"body", msg}, {"url", url}, {"info", info}}; + break; default: qDebug() << "SendRoomMessage: Unknown message type for" << msg; return; @@ -812,124 +810,97 @@ MatrixClient::messages(const QString &roomid, const QString &from_token, int lim void MatrixClient::uploadImage(const QString &roomid, - const QSharedPointer data, - const QString &filename) + const QString &filename, + const QSharedPointer data) { auto reply = makeUploadRequest(data); if (reply == nullptr) return; - connect(reply, &QNetworkReply::finished, this, [this, reply, roomid, data, filename]() { - reply->deleteLater(); - - int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); - - if (status == 0 || status >= 400) { - emit syncFailed(reply->errorString()); - return; - } - - auto res_data = reply->readAll(); - - if (res_data.isEmpty()) + connect(reply, &QNetworkReply::finished, this, [this, reply, roomid, filename, data]() { + auto json = getUploadReply(reply); + if (json.isEmpty()) return; - auto json = QJsonDocument::fromJson(res_data); - - if (!json.isObject()) { - qDebug() << "Media upload: Response is not a json object."; - return; - } + auto mime = reply->request().header(QNetworkRequest::ContentTypeHeader).toString(); + auto size = + reply->request().header(QNetworkRequest::ContentLengthHeader).toLongLong(); - QJsonObject object = json.object(); - if (!object.contains("content_uri")) { - qDebug() << "Media upload: Missing content_uri key"; - qDebug() << object; - return; - } - - emit imageUploaded(roomid, data, filename, object.value("content_uri").toString()); + emit imageUploaded( + roomid, filename, json.value("content_uri").toString(), mime, size); }); } void MatrixClient::uploadFile(const QString &roomid, - const QSharedPointer data, - const QString &filename) + const QString &filename, + const QSharedPointer data) { auto reply = makeUploadRequest(data); - connect(reply, &QNetworkReply::finished, this, [this, reply, roomid, data, filename]() { - reply->deleteLater(); - - int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); - - if (status == 0 || status >= 400) { - emit syncFailed(reply->errorString()); - return; - } - - auto data = reply->readAll(); + if (reply == nullptr) + return; - if (data.isEmpty()) + connect(reply, &QNetworkReply::finished, this, [this, reply, roomid, filename, data]() { + auto json = getUploadReply(reply); + if (json.isEmpty()) return; - auto json = QJsonDocument::fromJson(data); + auto mime = reply->request().header(QNetworkRequest::ContentTypeHeader).toString(); + auto size = + reply->request().header(QNetworkRequest::ContentLengthHeader).toLongLong(); - if (!json.isObject()) { - qDebug() << "Media upload: Response is not a json object."; - return; - } - - QJsonObject object = json.object(); - if (!object.contains("content_uri")) { - qDebug() << "Media upload: Missing content_uri key"; - qDebug() << object; - return; - } - - emit fileUploaded(roomid, filename, object.value("content_uri").toString()); + emit fileUploaded( + roomid, filename, json.value("content_uri").toString(), mime, size); }); } void MatrixClient::uploadAudio(const QString &roomid, - const QSharedPointer data, - const QString &filename) + const QString &filename, + const QSharedPointer data) { auto reply = makeUploadRequest(data); - connect(reply, &QNetworkReply::finished, this, [this, reply, roomid, data, filename]() { - reply->deleteLater(); - - int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); + if (reply == nullptr) + return; - if (status == 0 || status >= 400) { - emit syncFailed(reply->errorString()); + connect(reply, &QNetworkReply::finished, this, [this, reply, roomid, filename, data]() { + auto json = getUploadReply(reply); + if (json.isEmpty()) return; - } - auto data = reply->readAll(); + auto mime = reply->request().header(QNetworkRequest::ContentTypeHeader).toString(); + auto size = + reply->request().header(QNetworkRequest::ContentLengthHeader).toLongLong(); - if (data.isEmpty()) - return; + emit audioUploaded( + roomid, filename, json.value("content_uri").toString(), mime, size); + }); +} - auto json = QJsonDocument::fromJson(data); +void +MatrixClient::uploadVideo(const QString &roomid, + const QString &filename, + const QSharedPointer data) +{ + auto reply = makeUploadRequest(data); - if (!json.isObject()) { - qDebug() << "Media upload: Response is not a json object."; - return; - } + if (reply == nullptr) + return; - QJsonObject object = json.object(); - if (!object.contains("content_uri")) { - qDebug() << "Media upload: Missing content_uri key"; - qDebug() << object; + connect(reply, &QNetworkReply::finished, this, [this, reply, roomid, filename, data]() { + auto json = getUploadReply(reply); + if (json.isEmpty()) return; - } - emit audioUploaded(roomid, filename, object.value("content_uri").toString()); + auto mime = reply->request().header(QNetworkRequest::ContentTypeHeader).toString(); + auto size = + reply->request().header(QNetworkRequest::ContentLengthHeader).toLongLong(); + + emit videoUploaded( + roomid, filename, json.value("content_uri").toString(), mime, size); }); } @@ -1227,3 +1198,39 @@ MatrixClient::makeUploadRequest(QSharedPointer iodev) return reply; } + +QJsonObject +MatrixClient::getUploadReply(QNetworkReply *reply) +{ + QJsonObject object; + + reply->deleteLater(); + + int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); + + if (status == 0 || status >= 400) { + emit syncFailed(reply->errorString()); + return object; + } + + auto res_data = reply->readAll(); + + if (res_data.isEmpty()) + return object; + + auto json = QJsonDocument::fromJson(res_data); + + if (!json.isObject()) { + qDebug() << "Media upload: Response is not a json object."; + return object; + } + + object = json.object(); + if (!object.contains("content_uri")) { + qDebug() << "Media upload: Missing content_uri key"; + qDebug() << object; + return QJsonObject{}; + } + + return object; +} -- cgit 1.5.1