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

View file

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

View file

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

View file

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

View file

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

View file

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