Implement sending encrypted files

This commit is contained in:
Nicolas Werner 2019-12-05 15:31:53 +01:00
parent 5bfdaff778
commit 43d7fe0d35
6 changed files with 79 additions and 212 deletions

View file

@ -54,6 +54,8 @@ constexpr int CHECK_CONNECTIVITY_INTERVAL = 15'000;
constexpr int RETRY_TIMEOUT = 5'000;
constexpr size_t MAX_ONETIME_KEYS = 50;
Q_DECLARE_METATYPE(boost::optional<mtx::crypto::EncryptedFile>)
ChatPage::ChatPage(QSharedPointer<UserSettings> userSettings, QWidget *parent)
: QWidget(parent)
, isConnected_(true)
@ -62,6 +64,9 @@ ChatPage::ChatPage(QSharedPointer<UserSettings> userSettings, QWidget *parent)
{
setObjectName("chatPage");
qRegisterMetaType<boost::optional<mtx::crypto::EncryptedFile>>(
"boost::optional<mtx::crypto::EncryptedFile>");
topLayout_ = new QHBoxLayout(this);
topLayout_->setSpacing(0);
topLayout_->setMargin(0);
@ -299,9 +304,9 @@ ChatPage::ChatPage(QSharedPointer<UserSettings> userSettings, QWidget *parent)
connect(
text_input_,
&TextInputWidget::uploadImage,
&TextInputWidget::uploadMedia,
this,
[this](QSharedPointer<QIODevice> dev, const QString &fn) {
[this](QSharedPointer<QIODevice> dev, QString mimeClass, const QString &fn) {
QMimeDatabase db;
QMimeType mime = db.mimeTypeForData(dev.data());
@ -313,7 +318,16 @@ ChatPage::ChatPage(QSharedPointer<UserSettings> userSettings, QWidget *parent)
auto bin = dev->peek(dev->size());
auto payload = std::string(bin.data(), bin.size());
auto dimensions = QImageReader(dev.data()).size();
boost::optional<mtx::crypto::EncryptedFile> encryptedFile;
if (cache::client()->isRoomEncrypted(current_room_.toStdString())) {
mtx::crypto::BinaryBuf buf;
std::tie(buf, encryptedFile) = mtx::crypto::encrypt_file(payload);
payload = mtx::crypto::to_string(buf);
}
QSize dimensions;
if (mimeClass == "image")
dimensions = QImageReader(dev.data()).size();
http::client()->upload(
payload,
@ -322,193 +336,61 @@ ChatPage::ChatPage(QSharedPointer<UserSettings> userSettings, QWidget *parent)
[this,
room_id = current_room_,
filename = fn,
encryptedFile,
mimeClass,
mime = mime.name(),
size = payload.size(),
dimensions](const mtx::responses::ContentURI &res, mtx::http::RequestErr err) {
if (err) {
emit uploadFailed(
tr("Failed to upload image. Please try again."));
nhlog::net()->warn("failed to upload image: {} {} ({})",
tr("Failed to upload media. Please try again."));
nhlog::net()->warn("failed to upload media: {} {} ({})",
err->matrix_error.error,
to_string(err->matrix_error.errcode),
static_cast<int>(err->status_code));
return;
}
emit imageUploaded(room_id,
emit mediaUploaded(room_id,
filename,
encryptedFile,
QString::fromStdString(res.content_uri),
mimeClass,
mime,
size,
dimensions);
});
});
connect(text_input_,
&TextInputWidget::uploadFile,
this,
[this](QSharedPointer<QIODevice> dev, const QString &fn) {
QMimeDatabase db;
QMimeType mime = db.mimeTypeForData(dev.data());
if (!dev->open(QIODevice::ReadOnly)) {
emit uploadFailed(
QString("Error while reading media: %1").arg(dev->errorString()));
return;
}
auto bin = dev->readAll();
auto payload = std::string(bin.data(), bin.size());
http::client()->upload(
payload,
mime.name().toStdString(),
QFileInfo(fn).fileName().toStdString(),
[this,
room_id = current_room_,
filename = fn,
mime = mime.name(),
size = payload.size()](const mtx::responses::ContentURI &res,
mtx::http::RequestErr err) {
if (err) {
emit uploadFailed(
tr("Failed to upload file. Please try again."));
nhlog::net()->warn("failed to upload file: {} ({})",
err->matrix_error.error,
static_cast<int>(err->status_code));
return;
}
emit fileUploaded(room_id,
filename,
QString::fromStdString(res.content_uri),
mime,
size);
});
});
connect(text_input_,
&TextInputWidget::uploadAudio,
this,
[this](QSharedPointer<QIODevice> dev, const QString &fn) {
QMimeDatabase db;
QMimeType mime = db.mimeTypeForData(dev.data());
if (!dev->open(QIODevice::ReadOnly)) {
emit uploadFailed(
QString("Error while reading media: %1").arg(dev->errorString()));
return;
}
auto bin = dev->readAll();
auto payload = std::string(bin.data(), bin.size());
http::client()->upload(
payload,
mime.name().toStdString(),
QFileInfo(fn).fileName().toStdString(),
[this,
room_id = current_room_,
filename = fn,
mime = mime.name(),
size = payload.size()](const mtx::responses::ContentURI &res,
mtx::http::RequestErr err) {
if (err) {
emit uploadFailed(
tr("Failed to upload audio. Please try again."));
nhlog::net()->warn("failed to upload audio: {} ({})",
err->matrix_error.error,
static_cast<int>(err->status_code));
return;
}
emit audioUploaded(room_id,
filename,
QString::fromStdString(res.content_uri),
mime,
size);
});
});
connect(text_input_,
&TextInputWidget::uploadVideo,
this,
[this](QSharedPointer<QIODevice> dev, const QString &fn) {
QMimeDatabase db;
QMimeType mime = db.mimeTypeForData(dev.data());
if (!dev->open(QIODevice::ReadOnly)) {
emit uploadFailed(
QString("Error while reading media: %1").arg(dev->errorString()));
return;
}
auto bin = dev->readAll();
auto payload = std::string(bin.data(), bin.size());
http::client()->upload(
payload,
mime.name().toStdString(),
QFileInfo(fn).fileName().toStdString(),
[this,
room_id = current_room_,
filename = fn,
mime = mime.name(),
size = payload.size()](const mtx::responses::ContentURI &res,
mtx::http::RequestErr err) {
if (err) {
emit uploadFailed(
tr("Failed to upload video. Please try again."));
nhlog::net()->warn("failed to upload video: {} ({})",
err->matrix_error.error,
static_cast<int>(err->status_code));
return;
}
emit videoUploaded(room_id,
filename,
QString::fromStdString(res.content_uri),
mime,
size);
});
});
connect(this, &ChatPage::uploadFailed, this, [this](const QString &msg) {
text_input_->hideUploadSpinner();
emit showNotification(msg);
});
connect(this,
&ChatPage::imageUploaded,
&ChatPage::mediaUploaded,
this,
[this](QString roomid,
QString filename,
boost::optional<mtx::crypto::EncryptedFile> encryptedFile,
QString url,
QString mimeClass,
QString mime,
qint64 dsize,
QSize dimensions) {
text_input_->hideUploadSpinner();
if (mimeClass == "image")
view_manager_->queueImageMessage(
roomid, filename, url, mime, dsize, dimensions);
});
connect(this,
&ChatPage::fileUploaded,
this,
[this](QString roomid, QString filename, QString url, QString mime, qint64 dsize) {
text_input_->hideUploadSpinner();
view_manager_->queueFileMessage(roomid, filename, url, mime, dsize);
});
connect(this,
&ChatPage::audioUploaded,
this,
[this](QString roomid, QString filename, QString url, QString mime, qint64 dsize) {
text_input_->hideUploadSpinner();
view_manager_->queueAudioMessage(roomid, filename, url, mime, dsize);
});
connect(this,
&ChatPage::videoUploaded,
this,
[this](QString roomid, QString filename, QString url, QString mime, qint64 dsize) {
text_input_->hideUploadSpinner();
view_manager_->queueVideoMessage(roomid, filename, url, mime, dsize);
roomid, filename, encryptedFile, url, mime, dsize, dimensions);
else if (mimeClass == "audio")
view_manager_->queueAudioMessage(
roomid, filename, encryptedFile, url, mime, dsize);
else if (mimeClass == "video")
view_manager_->queueVideoMessage(
roomid, filename, encryptedFile, url, mime, dsize);
else
view_manager_->queueFileMessage(
roomid, filename, encryptedFile, url, mime, dsize);
});
connect(room_list_, &RoomList::roomAvatarChanged, this, &ChatPage::updateTopBarAvatar);

View file

@ -18,7 +18,9 @@
#pragma once
#include <atomic>
#include <boost/optional.hpp>
#include <boost/variant.hpp>
#include <mtx/common.hpp>
#include <mtx/responses.hpp>
#include <QFrame>
@ -94,27 +96,14 @@ signals:
const QPoint widgetPos);
void uploadFailed(const QString &msg);
void imageUploaded(const QString &roomid,
void mediaUploaded(const QString &roomid,
const QString &filename,
const boost::optional<mtx::crypto::EncryptedFile> &file,
const QString &url,
const QString &mimeClass,
const QString &mime,
qint64 dsize,
const QSize &dimensions);
void fileUploaded(const QString &roomid,
const QString &filename,
const QString &url,
const QString &mime,
qint64 dsize);
void audioUploaded(const QString &roomid,
const QString &filename,
const QString &url,
const QString &mime,
qint64 dsize);
void videoUploaded(const QString &roomid,
const QString &filename,
const QString &url,
const QString &mime,
qint64 dsize);
void contentLoaded();
void closing();

View file

@ -458,21 +458,16 @@ FilteredTextEdit::textChanged()
}
void
FilteredTextEdit::uploadData(const QByteArray data, const QString &media, const QString &filename)
FilteredTextEdit::uploadData(const QByteArray data,
const QString &mediaType,
const QString &filename)
{
QSharedPointer<QBuffer> buffer{new QBuffer{this}};
buffer->setData(data);
emit startedUpload();
if (media == "image")
emit image(buffer, filename);
else if (media == "audio")
emit audio(buffer, filename);
else if (media == "video")
emit video(buffer, filename);
else
emit file(buffer, filename);
emit media(buffer, mediaType, filename);
}
void
@ -580,10 +575,7 @@ TextInputWidget::TextInputWidget(QWidget *parent)
connect(input_, &FilteredTextEdit::message, this, &TextInputWidget::sendTextMessage);
connect(input_, &FilteredTextEdit::reply, this, &TextInputWidget::sendReplyMessage);
connect(input_, &FilteredTextEdit::command, this, &TextInputWidget::command);
connect(input_, &FilteredTextEdit::image, this, &TextInputWidget::uploadImage);
connect(input_, &FilteredTextEdit::audio, this, &TextInputWidget::uploadAudio);
connect(input_, &FilteredTextEdit::video, this, &TextInputWidget::uploadVideo);
connect(input_, &FilteredTextEdit::file, this, &TextInputWidget::uploadFile);
connect(input_, &FilteredTextEdit::media, this, &TextInputWidget::uploadMedia);
connect(emojiBtn_,
SIGNAL(emojiSelected(const QString &)),
this,
@ -642,14 +634,8 @@ TextInputWidget::openFileSelection()
const auto format = mime.name().split("/")[0];
QSharedPointer<QFile> file{new QFile{fileName, this}};
if (format == "image")
emit uploadImage(file, fileName);
else if (format == "audio")
emit uploadAudio(file, fileName);
else if (format == "video")
emit uploadVideo(file, fileName);
else
emit uploadFile(file, fileName);
emit uploadMedia(file, format, fileName);
showUploadSpinner();
}

View file

@ -63,10 +63,7 @@ signals:
void message(QString);
void reply(QString, const RelatedInfo &);
void command(QString name, QString args);
void image(QSharedPointer<QIODevice> data, const QString &filename);
void audio(QSharedPointer<QIODevice> data, const QString &filename);
void video(QSharedPointer<QIODevice> data, const QString &filename);
void file(QSharedPointer<QIODevice> data, const QString &filename);
void media(QSharedPointer<QIODevice> data, QString mimeClass, const QString &filename);
//! Trigger the suggestion popup.
void showSuggestions(const QString &query);
@ -179,10 +176,9 @@ signals:
void sendEmoteMessage(QString msg);
void heightChanged(int height);
void uploadImage(const QSharedPointer<QIODevice> data, const QString &filename);
void uploadFile(const QSharedPointer<QIODevice> data, const QString &filename);
void uploadAudio(const QSharedPointer<QIODevice> data, const QString &filename);
void uploadVideo(const QSharedPointer<QIODevice> data, const QString &filename);
void uploadMedia(const QSharedPointer<QIODevice> data,
QString mimeClass,
const QString &filename);
void sendJoinRoomRequest(const QString &room);

View file

@ -222,6 +222,7 @@ TimelineViewManager::queueEmoteMessage(const QString &msg)
void
TimelineViewManager::queueImageMessage(const QString &roomid,
const QString &filename,
const boost::optional<mtx::crypto::EncryptedFile> &file,
const QString &url,
const QString &mime,
uint64_t dsize,
@ -234,12 +235,15 @@ TimelineViewManager::queueImageMessage(const QString &roomid,
image.url = url.toStdString();
image.info.h = dimensions.height();
image.info.w = dimensions.width();
image.file = file;
models.value(roomid)->sendMessage(image);
}
void
TimelineViewManager::queueFileMessage(const QString &roomid,
TimelineViewManager::queueFileMessage(
const QString &roomid,
const QString &filename,
const boost::optional<mtx::crypto::EncryptedFile> &encryptedFile,
const QString &url,
const QString &mime,
uint64_t dsize)
@ -249,12 +253,14 @@ TimelineViewManager::queueFileMessage(const QString &roomid,
file.info.size = dsize;
file.body = filename.toStdString();
file.url = url.toStdString();
file.file = encryptedFile;
models.value(roomid)->sendMessage(file);
}
void
TimelineViewManager::queueAudioMessage(const QString &roomid,
const QString &filename,
const boost::optional<mtx::crypto::EncryptedFile> &file,
const QString &url,
const QString &mime,
uint64_t dsize)
@ -264,12 +270,14 @@ TimelineViewManager::queueAudioMessage(const QString &roomid,
audio.info.size = dsize;
audio.body = filename.toStdString();
audio.url = url.toStdString();
audio.file = file;
models.value(roomid)->sendMessage(audio);
}
void
TimelineViewManager::queueVideoMessage(const QString &roomid,
const QString &filename,
const boost::optional<mtx::crypto::EncryptedFile> &file,
const QString &url,
const QString &mime,
uint64_t dsize)
@ -279,5 +287,6 @@ TimelineViewManager::queueVideoMessage(const QString &roomid,
video.info.size = dsize;
video.body = filename.toStdString();
video.url = url.toStdString();
video.file = file;
models.value(roomid)->sendMessage(video);
}

View file

@ -5,6 +5,7 @@
#include <QSharedPointer>
#include <QWidget>
#include <mtx/common.hpp>
#include <mtx/responses.hpp>
#include "Cache.h"
@ -55,22 +56,26 @@ public slots:
void queueEmoteMessage(const QString &msg);
void queueImageMessage(const QString &roomid,
const QString &filename,
const boost::optional<mtx::crypto::EncryptedFile> &file,
const QString &url,
const QString &mime,
uint64_t dsize,
const QSize &dimensions);
void queueFileMessage(const QString &roomid,
const QString &filename,
const boost::optional<mtx::crypto::EncryptedFile> &file,
const QString &url,
const QString &mime,
uint64_t dsize);
void queueAudioMessage(const QString &roomid,
const QString &filename,
const boost::optional<mtx::crypto::EncryptedFile> &file,
const QString &url,
const QString &mime,
uint64_t dsize);
void queueVideoMessage(const QString &roomid,
const QString &filename,
const boost::optional<mtx::crypto::EncryptedFile> &file,
const QString &url,
const QString &mime,
uint64_t dsize);