GH-719 Fix paste upload encoding and do not try to upload over limit

This commit is contained in:
Petr Mrázek 2015-01-11 03:08:41 +01:00
parent acb3346409
commit 1151037f96
3 changed files with 33 additions and 10 deletions

View File

@ -11,12 +11,22 @@
void GuiUtil::uploadPaste(const QString &text, QWidget *parentWidget) void GuiUtil::uploadPaste(const QString &text, QWidget *parentWidget)
{ {
ProgressDialog dialog(parentWidget); ProgressDialog dialog(parentWidget);
PasteUpload *paste = new PasteUpload(parentWidget, text); std::unique_ptr<PasteUpload> paste(new PasteUpload(parentWidget, text));
dialog.exec(paste);
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()) if (!paste->successful())
{ {
CustomMessageBox::selectable(parentWidget, "Upload failed", paste->failReason(), CustomMessageBox::selectable(parentWidget, QObject::tr("Upload failed"),
QMessageBox::Critical)->exec(); paste->failReason(), QMessageBox::Critical)->exec();
} }
else else
{ {
@ -25,11 +35,11 @@ void GuiUtil::uploadPaste(const QString &text, QWidget *parentWidget)
QDesktopServices::openUrl(link); QDesktopServices::openUrl(link);
CustomMessageBox::selectable( CustomMessageBox::selectable(
parentWidget, QObject::tr("Upload finished"), parentWidget, QObject::tr("Upload finished"),
QObject::tr("The <a href=\"%1\">link to the uploaded log</a> has been opened in the default " QObject::tr("The <a href=\"%1\">link to the uploaded log</a> has been opened in "
"the default "
"browser and placed in your clipboard.").arg(link), "browser and placed in your clipboard.").arg(link),
QMessageBox::Information)->exec(); QMessageBox::Information)->exec();
} }
delete paste;
} }
void GuiUtil::setClipboardText(const QString &text) void GuiUtil::setClipboardText(const QString &text)

View File

@ -6,8 +6,15 @@
#include "gui/dialogs/CustomMessageBox.h" #include "gui/dialogs/CustomMessageBox.h"
#include <QDesktopServices> #include <QDesktopServices>
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() void PasteUpload::executeTask()
@ -16,7 +23,7 @@ void PasteUpload::executeTask()
request.setHeader(QNetworkRequest::UserAgentHeader, "MultiMC/5.0 (Uncached)"); request.setHeader(QNetworkRequest::UserAgentHeader, "MultiMC/5.0 (Uncached)");
QByteArray content( QByteArray content(
"key=public&description=MultiMC5+Log+File&language=plain&format=json&expire=2592000&paste=" + "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-Type", "application/x-www-form-urlencoded");
request.setRawHeader("Content-Length", QByteArray::number(content.size())); request.setRawHeader("Content-Length", QByteArray::number(content.size()));

View File

@ -18,12 +18,18 @@ public:
{ {
return m_pasteID; return m_pasteID;
} }
uint32_t maxSize()
{
// 2MB for paste.ee
return 1024*1024*2;
}
bool validateText();
protected: protected:
virtual void executeTask(); virtual void executeTask();
private: private:
bool parseResult(QJsonDocument doc); bool parseResult(QJsonDocument doc);
QString m_text; QByteArray m_text;
QString m_error; QString m_error;
QWidget *m_window; QWidget *m_window;
QString m_pasteID; QString m_pasteID;