Skip to content
Snippets Groups Projects
Unverified Commit 83738a61 authored by Nicolas Werner's avatar Nicolas Werner Committed by GitHub
Browse files

Merge pull request #1013 from tastytea/smaller-thumbnails

Don't send thumbnails that are larger than the original
parents c59c8b6f 179ae53b
No related branches found
No related tags found
No related merge requests found
Pipeline #2878 passed
...@@ -896,39 +896,47 @@ MediaUpload::startUpload() ...@@ -896,39 +896,47 @@ MediaUpload::startUpload()
QByteArray ba; QByteArray ba;
QBuffer buffer(&ba); QBuffer buffer(&ba);
buffer.open(QIODevice::WriteOnly); buffer.open(QIODevice::WriteOnly);
thumbnail_.save(&buffer, "PNG"); thumbnail_.save(&buffer, "PNG", 0);
auto payload = std::string(ba.data(), ba.size()); if (ba.size() >= (data.size() - data.size() / 10)) {
if (encrypt_) { nhlog::ui()->info(
mtx::crypto::BinaryBuf buf; "Thumbnail is not a lot smaller than original image, not uploading it");
std::tie(buf, thumbnailEncryptedFile) = mtx::crypto::encrypt_file(std::move(payload)); nhlog::ui()->debug(
payload = mtx::crypto::to_string(buf); "\n Image size: {:9d}\nThumbnail size: {:9d}", data.size(), ba.size());
} } else {
thumbnailSize_ = payload.size(); auto payload = std::string(ba.data(), ba.size());
if (encrypt_) {
http::client()->upload( mtx::crypto::BinaryBuf buf;
payload, std::tie(buf, thumbnailEncryptedFile) =
encryptedFile ? "application/octet-stream" : "image/png", mtx::crypto::encrypt_file(std::move(payload));
"", payload = mtx::crypto::to_string(buf);
[this](const mtx::responses::ContentURI &res, mtx::http::RequestErr err) mutable { }
if (err) { thumbnailSize_ = payload.size();
emit ChatPage::instance()->showNotification(
tr("Failed to upload media. Please try again.")); http::client()->upload(
nhlog::net()->warn("failed to upload media: {} {} ({})", payload,
err->matrix_error.error, encryptedFile ? "application/octet-stream" : "image/png",
to_string(err->matrix_error.errcode), "",
static_cast<int>(err->status_code)); [this](const mtx::responses::ContentURI &res, mtx::http::RequestErr err) mutable {
thumbnail_ = QImage(); if (err) {
startUpload(); emit ChatPage::instance()->showNotification(
return; 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));
thumbnail_ = QImage();
startUpload();
return;
}
thumbnailUrl_ = QString::fromStdString(res.content_uri); thumbnailUrl_ = QString::fromStdString(res.content_uri);
if (thumbnailEncryptedFile) if (thumbnailEncryptedFile)
thumbnailEncryptedFile->url = res.content_uri; thumbnailEncryptedFile->url = res.content_uri;
startUpload(); startUpload();
}); });
return; return;
}
} }
auto payload = std::string(data.data(), data.size()); auto payload = std::string(data.data(), data.size());
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment