From 1151037f968628cd659f06457f9ca7403f77f071 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Sun, 11 Jan 2015 03:08:41 +0100 Subject: [PATCH] GH-719 Fix paste upload encoding and do not try to upload over limit --- gui/GuiUtil.cpp | 24 +++++++++++++++++------- logic/net/PasteUpload.cpp | 11 +++++++++-- logic/net/PasteUpload.h | 8 +++++++- 3 files changed, 33 insertions(+), 10 deletions(-) diff --git a/gui/GuiUtil.cpp b/gui/GuiUtil.cpp index 72c09ffe..fcbc8acd 100644 --- a/gui/GuiUtil.cpp +++ b/gui/GuiUtil.cpp @@ -11,12 +11,22 @@ void GuiUtil::uploadPaste(const QString &text, QWidget *parentWidget) { ProgressDialog dialog(parentWidget); - PasteUpload *paste = new PasteUpload(parentWidget, text); - dialog.exec(paste); + std::unique_ptr paste(new PasteUpload(parentWidget, text)); + + if (!paste->validateText()) + { + CustomMessageBox::selectable( + parentWidget, QObject::tr("Upload failed"), + QObject::tr("The log file is too big. You'll have to upload it manually."), + QMessageBox::Warning)->exec(); + return; + } + + dialog.exec(paste.get()); if (!paste->successful()) { - CustomMessageBox::selectable(parentWidget, "Upload failed", paste->failReason(), - QMessageBox::Critical)->exec(); + CustomMessageBox::selectable(parentWidget, QObject::tr("Upload failed"), + paste->failReason(), QMessageBox::Critical)->exec(); } else { @@ -25,11 +35,11 @@ void GuiUtil::uploadPaste(const QString &text, QWidget *parentWidget) QDesktopServices::openUrl(link); CustomMessageBox::selectable( parentWidget, QObject::tr("Upload finished"), - QObject::tr("The link to the uploaded log has been opened in the default " - "browser and placed in your clipboard.").arg(link), + QObject::tr("The link to the uploaded log has been opened in " + "the default " + "browser and placed in your clipboard.").arg(link), QMessageBox::Information)->exec(); } - delete paste; } void GuiUtil::setClipboardText(const QString &text) diff --git a/logic/net/PasteUpload.cpp b/logic/net/PasteUpload.cpp index 2b953c5c..5fd8d679 100644 --- a/logic/net/PasteUpload.cpp +++ b/logic/net/PasteUpload.cpp @@ -6,8 +6,15 @@ #include "gui/dialogs/CustomMessageBox.h" #include -PasteUpload::PasteUpload(QWidget *window, QString text) : m_text(text), m_window(window) +PasteUpload::PasteUpload(QWidget *window, QString text) : m_window(window) { + m_text = text.toUtf8(); + m_text.replace('\n', "\r\n"); +} + +bool PasteUpload::validateText() +{ + return m_text.size() <= maxSize(); } void PasteUpload::executeTask() @@ -16,7 +23,7 @@ void PasteUpload::executeTask() request.setHeader(QNetworkRequest::UserAgentHeader, "MultiMC/5.0 (Uncached)"); QByteArray content( "key=public&description=MultiMC5+Log+File&language=plain&format=json&expire=2592000&paste=" + - m_text.toUtf8()); + m_text.toPercentEncoding()); request.setRawHeader("Content-Type", "application/x-www-form-urlencoded"); request.setRawHeader("Content-Length", QByteArray::number(content.size())); diff --git a/logic/net/PasteUpload.h b/logic/net/PasteUpload.h index 0ddc8cef..307d592b 100644 --- a/logic/net/PasteUpload.h +++ b/logic/net/PasteUpload.h @@ -18,12 +18,18 @@ public: { return m_pasteID; } + uint32_t maxSize() + { + // 2MB for paste.ee + return 1024*1024*2; + } + bool validateText(); protected: virtual void executeTask(); private: bool parseResult(QJsonDocument doc); - QString m_text; + QByteArray m_text; QString m_error; QWidget *m_window; QString m_pasteID;