summary refs log tree commit diff
path: root/src/timeline/InputBar.cpp
diff options
context:
space:
mode:
authortastytea <tastytea@tastytea.de>2022-03-24 19:34:20 +0100
committertastytea <tastytea@tastytea.de>2022-03-24 20:13:12 +0100
commit179ae53b409f6f987e726e67991a77c57f022601 (patch)
tree5946d8395357c8e12de236434ed62a9f3da6ffc0 /src/timeline/InputBar.cpp
parentTranslated using Weblate (Indonesian) (diff)
downloadnheko-179ae53b409f6f987e726e67991a77c57f022601.tar.xz
Don't send thumbnails that are larger than the original
- Set PNG compression to maximum
- Don't upload thumbnail if it is not at least 10% smaller than  the
  original
Diffstat (limited to 'src/timeline/InputBar.cpp')
-rw-r--r--src/timeline/InputBar.cpp70
1 files changed, 39 insertions, 31 deletions
diff --git a/src/timeline/InputBar.cpp b/src/timeline/InputBar.cpp
index a82796a8..d5c2d428 100644
--- a/src/timeline/InputBar.cpp
+++ b/src/timeline/InputBar.cpp
@@ -896,39 +896,47 @@ MediaUpload::startUpload()
         QByteArray ba;
         QBuffer buffer(&ba);
         buffer.open(QIODevice::WriteOnly);
-        thumbnail_.save(&buffer, "PNG");
-        auto payload = std::string(ba.data(), ba.size());
-        if (encrypt_) {
-            mtx::crypto::BinaryBuf buf;
-            std::tie(buf, thumbnailEncryptedFile) = mtx::crypto::encrypt_file(std::move(payload));
-            payload                               = mtx::crypto::to_string(buf);
-        }
-        thumbnailSize_ = payload.size();
-
-        http::client()->upload(
-          payload,
-          encryptedFile ? "application/octet-stream" : "image/png",
-          "",
-          [this](const mtx::responses::ContentURI &res, mtx::http::RequestErr err) mutable {
-              if (err) {
-                  emit ChatPage::instance()->showNotification(
-                    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;
-              }
+        thumbnail_.save(&buffer, "PNG", 0);
+        if (ba.size() >= (data.size() - data.size() / 10)) {
+            nhlog::ui()->info(
+              "Thumbnail is not a lot smaller than original image, not uploading it");
+            nhlog::ui()->debug(
+              "\n    Image size: {:9d}\nThumbnail size: {:9d}", data.size(), ba.size());
+        } else {
+            auto payload = std::string(ba.data(), ba.size());
+            if (encrypt_) {
+                mtx::crypto::BinaryBuf buf;
+                std::tie(buf, thumbnailEncryptedFile) =
+                  mtx::crypto::encrypt_file(std::move(payload));
+                payload = mtx::crypto::to_string(buf);
+            }
+            thumbnailSize_ = payload.size();
+
+            http::client()->upload(
+              payload,
+              encryptedFile ? "application/octet-stream" : "image/png",
+              "",
+              [this](const mtx::responses::ContentURI &res, mtx::http::RequestErr err) mutable {
+                  if (err) {
+                      emit ChatPage::instance()->showNotification(
+                        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);
-              if (thumbnailEncryptedFile)
-                  thumbnailEncryptedFile->url = res.content_uri;
+                  thumbnailUrl_ = QString::fromStdString(res.content_uri);
+                  if (thumbnailEncryptedFile)
+                      thumbnailEncryptedFile->url = res.content_uri;
 
-              startUpload();
-          });
-        return;
+                  startUpload();
+              });
+            return;
+        }
     }
 
     auto payload = std::string(data.data(), data.size());