Add file uploading

This commit is contained in:
Nicolas Werner 2020-11-15 04:52:49 +01:00
parent 0bb4885632
commit a31d3d0816
17 changed files with 475 additions and 318 deletions

View file

@ -268,126 +268,6 @@ ChatPage::ChatPage(QSharedPointer<UserSettings> userSettings, QWidget *parent)
this,
SIGNAL(unreadMessages(int)));
connect(
text_input_,
&TextInputWidget::uploadMedia,
this,
[this](QSharedPointer<QIODevice> dev, QString mimeClass, const QString &fn) {
if (!dev->open(QIODevice::ReadOnly)) {
emit uploadFailed(
QString("Error while reading media: %1").arg(dev->errorString()));
return;
}
auto bin = dev->readAll();
QMimeDatabase db;
QMimeType mime = db.mimeTypeForData(bin);
auto payload = std::string(bin.data(), bin.size());
std::optional<mtx::crypto::EncryptedFile> encryptedFile;
if (cache::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;
QString blurhash;
if (mimeClass == "image") {
QImage img = utils::readImage(&bin);
dimensions = img.size();
if (img.height() > 200 && img.width() > 360)
img = img.scaled(360, 200, Qt::KeepAspectRatioByExpanding);
std::vector<unsigned char> data;
for (int y = 0; y < img.height(); y++) {
for (int x = 0; x < img.width(); x++) {
auto p = img.pixel(x, y);
data.push_back(static_cast<unsigned char>(qRed(p)));
data.push_back(static_cast<unsigned char>(qGreen(p)));
data.push_back(static_cast<unsigned char>(qBlue(p)));
}
}
blurhash = QString::fromStdString(
blurhash::encode(data.data(), img.width(), img.height(), 4, 3));
}
http::client()->upload(
payload,
encryptedFile ? "application/octet-stream" : mime.name().toStdString(),
QFileInfo(fn).fileName().toStdString(),
[this,
room_id = current_room_,
filename = fn,
encryptedFile,
mimeClass,
mime = mime.name(),
size = payload.size(),
dimensions,
blurhash](const mtx::responses::ContentURI &res, mtx::http::RequestErr err) {
if (err) {
emit uploadFailed(
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 mediaUploaded(room_id,
filename,
encryptedFile,
QString::fromStdString(res.content_uri),
mimeClass,
mime,
size,
dimensions,
blurhash);
});
});
connect(this, &ChatPage::uploadFailed, this, [this](const QString &msg) {
text_input_->hideUploadSpinner();
emit showNotification(msg);
});
connect(this,
&ChatPage::mediaUploaded,
this,
[this](QString roomid,
QString filename,
std::optional<mtx::crypto::EncryptedFile> encryptedFile,
QString url,
QString mimeClass,
QString mime,
qint64 dsize,
QSize dimensions,
QString blurhash) {
text_input_->hideUploadSpinner();
if (encryptedFile)
encryptedFile->url = url.toStdString();
if (mimeClass == "image")
view_manager_->queueImageMessage(roomid,
filename,
encryptedFile,
url,
mime,
dsize,
dimensions,
blurhash);
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(text_input_, &TextInputWidget::callButtonPress, this, [this]() {
if (callManager_->onActiveCall()) {
callManager_->hangUp();