parent
b21942a3e3
commit
fdb76bb5c1
13 changed files with 210 additions and 101 deletions
|
|
@ -21,6 +21,7 @@
|
|||
#include <QJsonArray>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QMimeDatabase>
|
||||
#include <QNetworkReply>
|
||||
#include <QNetworkRequest>
|
||||
#include <QPixmap>
|
||||
|
|
@ -287,6 +288,9 @@ MatrixClient::sendRoomMessage(matrix::events::MessageEventType ty,
|
|||
case matrix::events::MessageEventType::Image:
|
||||
body = {{"msgtype", "m.image"}, {"body", msg}, {"url", url}};
|
||||
break;
|
||||
case matrix::events::MessageEventType::File:
|
||||
body = {{"msgtype", "m.file"}, {"body", msg}, {"url", url}};
|
||||
break;
|
||||
default:
|
||||
qDebug() << "SendRoomMessage: Unknown message type for" << msg;
|
||||
return;
|
||||
|
|
@ -755,6 +759,62 @@ MatrixClient::uploadImage(const QString &roomid, const QString &filename)
|
|||
});
|
||||
}
|
||||
|
||||
void
|
||||
MatrixClient::uploadFile(const QString &roomid, const QString &filename)
|
||||
{
|
||||
QUrlQuery query;
|
||||
query.addQueryItem("access_token", token_);
|
||||
|
||||
QUrl endpoint(server_);
|
||||
endpoint.setPath(mediaApiUrl_ + "/upload");
|
||||
endpoint.setQuery(query);
|
||||
|
||||
QFile file(filename);
|
||||
if (!file.open(QIODevice::ReadWrite)) {
|
||||
qDebug() << "Error while reading" << filename;
|
||||
return;
|
||||
}
|
||||
|
||||
QMimeDatabase db;
|
||||
QMimeType mime = db.mimeTypeForFile(filename, QMimeDatabase::MatchContent);
|
||||
|
||||
QNetworkRequest request(QString(endpoint.toEncoded()));
|
||||
request.setHeader(QNetworkRequest::ContentLengthHeader, file.size());
|
||||
request.setHeader(QNetworkRequest::ContentTypeHeader, mime.name());
|
||||
|
||||
auto reply = post(request, file.readAll());
|
||||
connect(reply, &QNetworkReply::finished, this, [this, reply, roomid, 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 (data.isEmpty())
|
||||
return;
|
||||
|
||||
auto json = QJsonDocument::fromJson(data);
|
||||
|
||||
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());
|
||||
});
|
||||
}
|
||||
void
|
||||
MatrixClient::joinRoom(const QString &roomIdOrAlias)
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue