PasteUpload task changed to use 0x0.st's protocol

- Modified PasteUpload task to upload the log file to 0x0.st and other
  services with the same protocol.

- Modified Paste settings UI to allow the user to select a custom paste
  URL, simplified the settings page code.
This commit is contained in:
Lenny McLennington 2022-01-19 07:44:29 +00:00
parent a606b47a22
commit b50e584369
No known key found for this signature in database
GPG Key ID: F0467078ECA45FCB
9 changed files with 51 additions and 125 deletions

View File

@ -715,7 +715,7 @@ Application::Application(int &argc, char **argv) : QApplication(argc, argv)
m_settings->registerSetting("UpdateDialogGeometry", ""); m_settings->registerSetting("UpdateDialogGeometry", "");
// pastebin URL // pastebin URL
m_settings->registerSetting("PastebinURL", "0x0.st"); m_settings->registerSetting("PastebinURL", "https://0x0.st");
// Init page provider // Init page provider
{ {

View File

@ -8,44 +8,34 @@
#include <QJsonDocument> #include <QJsonDocument>
#include <QFile> #include <QFile>
PasteUpload::PasteUpload(QWidget *window, QString text, QString key) : m_window(window) PasteUpload::PasteUpload(QWidget *window, QString text, QString url) : m_window(window), m_uploadUrl(url), m_text(text.toUtf8())
{ {
m_key = key;
QByteArray temp;
QJsonObject topLevelObj;
QJsonObject sectionObject;
sectionObject.insert("contents", text);
QJsonArray sectionArray;
sectionArray.append(sectionObject);
topLevelObj.insert("description", "Log Upload");
topLevelObj.insert("sections", sectionArray);
QJsonDocument docOut;
docOut.setObject(topLevelObj);
m_jsonContent = docOut.toJson();
} }
PasteUpload::~PasteUpload() PasteUpload::~PasteUpload()
{ {
} }
bool PasteUpload::validateText()
{
return m_jsonContent.size() <= maxSize();
}
void PasteUpload::executeTask() void PasteUpload::executeTask()
{ {
QNetworkRequest request(QUrl("https://api.paste.ee/v1/pastes")); QNetworkRequest request{QUrl(m_uploadUrl)};
request.setHeader(QNetworkRequest::UserAgentHeader, BuildConfig.USER_AGENT_UNCACHED); request.setHeader(QNetworkRequest::UserAgentHeader, BuildConfig.USER_AGENT_UNCACHED);
request.setRawHeader("Content-Type", "application/json"); QHttpMultiPart *multiPart = new QHttpMultiPart{QHttpMultiPart::FormDataType};
request.setRawHeader("Content-Length", QByteArray::number(m_jsonContent.size()));
request.setRawHeader("X-Auth-Token", m_key.toStdString().c_str());
QNetworkReply *rep = APPLICATION->network()->post(request, m_jsonContent); QHttpPart filePart;
filePart.setBody(m_text);
filePart.setHeader(QNetworkRequest::ContentTypeHeader, "text/plain");
filePart.setHeader(QNetworkRequest::ContentDispositionHeader, "form-data; name=\"file\"; filename=\"log.txt\"");
multiPart->append(filePart);
QNetworkReply *rep = APPLICATION->network()->post(request, multiPart);
multiPart->setParent(rep);
m_reply = std::shared_ptr<QNetworkReply>(rep); m_reply = std::shared_ptr<QNetworkReply>(rep);
setStatus(tr("Uploading to paste.ee")); setStatus(tr("Uploading to %1").arg(m_uploadUrl));
connect(rep, &QNetworkReply::uploadProgress, this, &Task::setProgress); connect(rep, &QNetworkReply::uploadProgress, this, &Task::setProgress);
connect(rep, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(downloadError(QNetworkReply::NetworkError))); connect(rep, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(downloadError(QNetworkReply::NetworkError)));
connect(rep, SIGNAL(finished()), this, SLOT(downloadFinished())); connect(rep, SIGNAL(finished()), this, SLOT(downloadFinished()));
@ -61,45 +51,23 @@ void PasteUpload::downloadError(QNetworkReply::NetworkError error)
void PasteUpload::downloadFinished() void PasteUpload::downloadFinished()
{ {
QByteArray data = m_reply->readAll(); QByteArray data = m_reply->readAll();
// if the download succeeded int statusCode = m_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
if (m_reply->error() == QNetworkReply::NetworkError::NoError)
if (m_reply->error() != QNetworkReply::NetworkError::NoError)
{ {
m_reply.reset(); emitFailed(tr("Network error: %1").arg(m_reply->errorString()));
QJsonParseError jsonError;
QJsonDocument doc = QJsonDocument::fromJson(data, &jsonError);
if (jsonError.error != QJsonParseError::NoError)
{
emitFailed(jsonError.errorString());
return;
}
if (!parseResult(doc))
{
emitFailed(tr("paste.ee returned an error. Please consult the logs for more information"));
return;
}
}
// else the download failed
else
{
emitFailed(QString("Network error: %1").arg(m_reply->errorString()));
m_reply.reset(); m_reply.reset();
return; return;
} }
else if (statusCode != 200)
{
QString reasonPhrase = m_reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString();
emitFailed(tr("Error: %1 returned unexpected status code %2 %3").arg(m_uploadUrl).arg(statusCode).arg(reasonPhrase));
qCritical() << m_uploadUrl << " returned unexpected status code " << statusCode << " with body: " << data;
m_reply.reset();
return;
}
m_pasteLink = QString::fromUtf8(data);
emitSucceeded(); emitSucceeded();
} }
bool PasteUpload::parseResult(QJsonDocument doc)
{
auto object = doc.object();
auto status = object.value("success").toBool();
if (!status)
{
qCritical() << "paste.ee reported error:" << QString(object.value("error").toString());
return false;
}
m_pasteLink = object.value("link").toString();
m_pasteID = object.value("id").toString();
qDebug() << m_pasteLink;
return true;
}

View File

@ -8,37 +8,21 @@ class PasteUpload : public Task
{ {
Q_OBJECT Q_OBJECT
public: public:
PasteUpload(QWidget *window, QString text, QString key = "public"); PasteUpload(QWidget *window, QString text, QString url);
virtual ~PasteUpload(); virtual ~PasteUpload();
QString pasteLink() QString pasteLink()
{ {
return m_pasteLink; return m_pasteLink;
} }
QString pasteID()
{
return m_pasteID;
}
int maxSize()
{
// 2MB for paste.ee - public
if(m_key == "public")
return 1024*1024*2;
// 12MB for paste.ee - with actual key
return 1024*1024*12;
}
bool validateText();
protected: protected:
virtual void executeTask(); virtual void executeTask();
private: private:
bool parseResult(QJsonDocument doc);
QString m_error;
QWidget *m_window; QWidget *m_window;
QString m_pasteID;
QString m_pasteLink; QString m_pasteLink;
QString m_key; QString m_uploadUrl;
QByteArray m_jsonContent; QByteArray m_text;
std::shared_ptr<QNetworkReply> m_reply; std::shared_ptr<QNetworkReply> m_reply;
public public
slots: slots:

View File

@ -16,21 +16,8 @@
QString GuiUtil::uploadPaste(const QString &text, QWidget *parentWidget) QString GuiUtil::uploadPaste(const QString &text, QWidget *parentWidget)
{ {
ProgressDialog dialog(parentWidget); ProgressDialog dialog(parentWidget);
auto APIKeySetting = APPLICATION->settings()->get("PasteEEAPIKey").toString(); auto pasteUrlSetting = APPLICATION->settings()->get("PastebinURL").toString();
if(APIKeySetting == "multimc") std::unique_ptr<PasteUpload> paste(new PasteUpload(parentWidget, text, pasteUrlSetting));
{
APIKeySetting = BuildConfig.PASTE_EE_KEY;
}
std::unique_ptr<PasteUpload> paste(new PasteUpload(parentWidget, text, APIKeySetting));
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 QString();
}
dialog.execWithTask(paste.get()); dialog.execWithTask(paste.get());
if (!paste->wasSuccessful()) if (!paste->wasSuccessful())

View File

@ -20,6 +20,7 @@
#include <QFileDialog> #include <QFileDialog>
#include <QStandardPaths> #include <QStandardPaths>
#include <QTabBar> #include <QTabBar>
#include <QVariant>
#include "settings/SettingsObject.h" #include "settings/SettingsObject.h"
#include "tools/BaseProfiler.h" #include "tools/BaseProfiler.h"
@ -31,7 +32,6 @@ PastePage::PastePage(QWidget *parent) :
{ {
ui->setupUi(this); ui->setupUi(this);
ui->tabWidget->tabBar()->hide();\ ui->tabWidget->tabBar()->hide();\
connect(ui->customAPIkeyEdit, &QLineEdit::textEdited, this, &PastePage::textEdited);
loadSettings(); loadSettings();
} }
@ -43,23 +43,15 @@ PastePage::~PastePage()
void PastePage::loadSettings() void PastePage::loadSettings()
{ {
auto s = APPLICATION->settings(); auto s = APPLICATION->settings();
QString pastebin = s->get("PastebinURL"); QString pastebinURL = s->get("PastebinURL").toString();
int index = ui->urlChoices->findText(pastebin); ui->urlChoices->setCurrentText(pastebinURL);
ui->urlChoices->setCurrentIndex(index);
} }
void PastePage::applySettings() void PastePage::applySettings()
{ {
auto s = APPLICATION->settings(); auto s = APPLICATION->settings();
QString pastebinURL = ui->urlChoices->currentText();
QString pasteKeyToUse; s->set("PastebinURL", pastebinURL);
if (ui->customButton->isChecked())
pasteKeyToUse = ui->customAPIkeyEdit->text();
else
{
pasteKeyToUse = "multimc";
}
s->set("PasteEEAPIKey", pasteKeyToUse);
} }
bool PastePage::apply() bool PastePage::apply()
@ -67,8 +59,3 @@ bool PastePage::apply()
applySettings(); applySettings();
return true; return true;
} }
void PastePage::textEdited(const QString& text)
{
ui->customButton->setChecked(true);
}

View File

@ -54,9 +54,6 @@ private:
void loadSettings(); void loadSettings();
void applySettings(); void applySettings();
private slots:
void textEdited(const QString &text);
private: private:
Ui::PastePage *ui; Ui::PastePage *ui;
}; };

View File

@ -36,7 +36,7 @@
<item> <item>
<widget class="QGroupBox" name="groupBox_2"> <widget class="QGroupBox" name="groupBox_2">
<property name="title"> <property name="title">
<string>Pastebin Site</string> <string>Pastebin URL</string>
</property> </property>
<layout class="QVBoxLayout" name="verticalLayout_10"> <layout class="QVBoxLayout" name="verticalLayout_10">
<item> <item>
@ -48,14 +48,20 @@
</item> </item>
<item> <item>
<widget class="QComboBox" name="urlChoices"> <widget class="QComboBox" name="urlChoices">
<property name="editable">
<bool>true</bool>
</property>
<property name="insertPolicy">
<enum>QComboBox::NoInsert</enum>
</property>
<item> <item>
<property name="text"> <property name="text">
<string>0x0.st</string> <string>https://0x0.st</string>
</property> </property>
</item> </item>
<item> <item>
<property name="text"> <property name="text">
<string>paste.polymc.org</string> <string>https://paste.polymc.org</string>
</property> </property>
</item> </item>
</widget> </widget>
@ -63,7 +69,7 @@
<item> <item>
<widget class="QLabel" name="label"> <widget class="QLabel" name="label">
<property name="text"> <property name="text">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;paste.polymc.org is a pastebin managed by PolyMC's lead maintainer. Something something trust&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string> <string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Here you can choose from a predefined list, or input the URL of a different paste service, provided that it supports the same protocol as 0x0.st, that is POST a file to the URL and return a link in the response body.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property> </property>
<property name="textFormat"> <property name="textFormat">
<enum>Qt::RichText</enum> <enum>Qt::RichText</enum>
@ -103,7 +109,4 @@
</tabstops> </tabstops>
<resources/> <resources/>
<connections/> <connections/>
<buttongroups>
<buttongroup name="pasteButtonGroup"/>
</buttongroups>
</ui> </ui>

View File

@ -100,7 +100,7 @@
<item> <item>
<widget class="QPushButton" name="btnPaste"> <widget class="QPushButton" name="btnPaste">
<property name="toolTip"> <property name="toolTip">
<string>Upload the log to paste.ee - it will stay online for a month</string> <string>Upload the log to the paste service configured in preferences</string>
</property> </property>
<property name="text"> <property name="text">
<string>Upload</string> <string>Upload</string>

View File

@ -84,7 +84,7 @@
<item row="3" column="2"> <item row="3" column="2">
<widget class="QPushButton" name="btnPaste"> <widget class="QPushButton" name="btnPaste">
<property name="toolTip"> <property name="toolTip">
<string>Upload the log to paste.ee - it will stay online for a month</string> <string>Upload the log to the paste service configured in preferences.</string>
</property> </property>
<property name="text"> <property name="text">
<string>Upload</string> <string>Upload</string>