feat: refactor logs upload to use the NetJob
Signed-off-by: Trial97 <alexandru.tripon97@gmail.com>
This commit is contained in:
parent
cb8f6f5e80
commit
cb01d5c46e
8 changed files with 215 additions and 212 deletions
|
@ -53,7 +53,6 @@ Config::Config()
|
||||||
LAUNCHER_SVGFILENAME = "@Launcher_SVGFileName@";
|
LAUNCHER_SVGFILENAME = "@Launcher_SVGFileName@";
|
||||||
|
|
||||||
USER_AGENT = "@Launcher_UserAgent@";
|
USER_AGENT = "@Launcher_UserAgent@";
|
||||||
USER_AGENT_UNCACHED = USER_AGENT + " (Uncached)";
|
|
||||||
|
|
||||||
// Version information
|
// Version information
|
||||||
VERSION_MAJOR = @Launcher_VERSION_MAJOR@;
|
VERSION_MAJOR = @Launcher_VERSION_MAJOR@;
|
||||||
|
|
|
@ -107,9 +107,6 @@ class Config {
|
||||||
/// User-Agent to use.
|
/// User-Agent to use.
|
||||||
QString USER_AGENT;
|
QString USER_AGENT;
|
||||||
|
|
||||||
/// User-Agent to use for uncached requests.
|
|
||||||
QString USER_AGENT_UNCACHED;
|
|
||||||
|
|
||||||
/// The git commit hash of this build
|
/// The git commit hash of this build
|
||||||
QString GIT_COMMIT;
|
QString GIT_COMMIT;
|
||||||
|
|
||||||
|
|
|
@ -1883,17 +1883,6 @@ QString Application::getUserAgent()
|
||||||
return BuildConfig.USER_AGENT;
|
return BuildConfig.USER_AGENT;
|
||||||
}
|
}
|
||||||
|
|
||||||
QString Application::getUserAgentUncached()
|
|
||||||
{
|
|
||||||
QString uaOverride = m_settings->get("UserAgentOverride").toString();
|
|
||||||
if (!uaOverride.isEmpty()) {
|
|
||||||
uaOverride += " (Uncached)";
|
|
||||||
return uaOverride.replace("$LAUNCHER_VER", BuildConfig.printableVersionString());
|
|
||||||
}
|
|
||||||
|
|
||||||
return BuildConfig.USER_AGENT_UNCACHED;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Application::handleDataMigration(const QString& currentData,
|
bool Application::handleDataMigration(const QString& currentData,
|
||||||
const QString& oldData,
|
const QString& oldData,
|
||||||
const QString& name,
|
const QString& name,
|
||||||
|
|
|
@ -160,7 +160,6 @@ class Application : public QApplication {
|
||||||
QString getFlameAPIKey();
|
QString getFlameAPIKey();
|
||||||
QString getModrinthAPIToken();
|
QString getModrinthAPIToken();
|
||||||
QString getUserAgent();
|
QString getUserAgent();
|
||||||
QString getUserAgentUncached();
|
|
||||||
|
|
||||||
/// this is the root of the 'installation'. Used for automatic updates
|
/// this is the root of the 'installation'. Used for automatic updates
|
||||||
const QString& root() { return m_rootPath; }
|
const QString& root() { return m_rootPath; }
|
||||||
|
|
|
@ -36,74 +36,42 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "PasteUpload.h"
|
#include "PasteUpload.h"
|
||||||
#include "Application.h"
|
|
||||||
#include "BuildConfig.h"
|
|
||||||
|
|
||||||
#include <QDebug>
|
|
||||||
#include <QFile>
|
|
||||||
#include <QHttpPart>
|
#include <QHttpPart>
|
||||||
#include <QJsonArray>
|
#include <QJsonArray>
|
||||||
#include <QJsonDocument>
|
#include <QJsonDocument>
|
||||||
#include <QJsonObject>
|
#include <QJsonObject>
|
||||||
#include <QUrlQuery>
|
#include <QUrlQuery>
|
||||||
|
|
||||||
#include "net/Logging.h"
|
const std::array<PasteUpload::PasteTypeInfo, 4> PasteUpload::PasteTypes = { { { "0x0.st", "https://0x0.st", "" },
|
||||||
|
{ "hastebin", "https://hst.sh", "/documents" },
|
||||||
|
{ "paste.gg", "https://paste.gg", "/api/v1/pastes" },
|
||||||
|
{ "mclo.gs", "https://api.mclo.gs", "/1/log" } } };
|
||||||
|
|
||||||
std::array<PasteUpload::PasteTypeInfo, 4> PasteUpload::PasteTypes = { { { "0x0.st", "https://0x0.st", "" },
|
QNetworkReply* PasteUpload::getReply(QNetworkRequest& request)
|
||||||
{ "hastebin", "https://hst.sh", "/documents" },
|
|
||||||
{ "paste.gg", "https://paste.gg", "/api/v1/pastes" },
|
|
||||||
{ "mclo.gs", "https://api.mclo.gs", "/1/log" } } };
|
|
||||||
|
|
||||||
PasteUpload::PasteUpload(QWidget* window, QString text, QString baseUrl, PasteType pasteType)
|
|
||||||
: m_window(window), m_baseUrl(baseUrl), m_pasteType(pasteType), m_text(text.toUtf8())
|
|
||||||
{
|
{
|
||||||
if (m_baseUrl == "")
|
switch (m_paste_type) {
|
||||||
m_baseUrl = PasteTypes.at(pasteType).defaultBase;
|
case PasteUpload::NullPointer: {
|
||||||
|
QHttpMultiPart* multiPart = new QHttpMultiPart{ QHttpMultiPart::FormDataType, this };
|
||||||
// HACK: Paste's docs say the standard API path is at /api/<version> but the official instance paste.gg doesn't follow that??
|
|
||||||
if (pasteType == PasteGG && m_baseUrl == PasteTypes.at(pasteType).defaultBase)
|
|
||||||
m_uploadUrl = "https://api.paste.gg/v1/pastes";
|
|
||||||
else
|
|
||||||
m_uploadUrl = m_baseUrl + PasteTypes.at(pasteType).endpointPath;
|
|
||||||
}
|
|
||||||
|
|
||||||
PasteUpload::~PasteUpload() {}
|
|
||||||
|
|
||||||
void PasteUpload::executeTask()
|
|
||||||
{
|
|
||||||
QNetworkRequest request{ QUrl(m_uploadUrl) };
|
|
||||||
QNetworkReply* rep{};
|
|
||||||
|
|
||||||
request.setHeader(QNetworkRequest::UserAgentHeader, APPLICATION->getUserAgentUncached().toUtf8());
|
|
||||||
|
|
||||||
switch (m_pasteType) {
|
|
||||||
case NullPointer: {
|
|
||||||
QHttpMultiPart* multiPart = new QHttpMultiPart{ QHttpMultiPart::FormDataType };
|
|
||||||
|
|
||||||
QHttpPart filePart;
|
QHttpPart filePart;
|
||||||
filePart.setBody(m_text);
|
filePart.setBody(m_log.toUtf8());
|
||||||
filePart.setHeader(QNetworkRequest::ContentTypeHeader, "text/plain");
|
filePart.setHeader(QNetworkRequest::ContentTypeHeader, "text/plain");
|
||||||
filePart.setHeader(QNetworkRequest::ContentDispositionHeader, "form-data; name=\"file\"; filename=\"log.txt\"");
|
filePart.setHeader(QNetworkRequest::ContentDispositionHeader, "form-data; name=\"file\"; filename=\"log.txt\"");
|
||||||
multiPart->append(filePart);
|
multiPart->append(filePart);
|
||||||
|
|
||||||
rep = APPLICATION->network()->post(request, multiPart);
|
return m_network->post(request, multiPart);
|
||||||
multiPart->setParent(rep);
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
case Hastebin: {
|
case PasteUpload::Hastebin: {
|
||||||
request.setHeader(QNetworkRequest::UserAgentHeader, APPLICATION->getUserAgentUncached().toUtf8());
|
return m_network->post(request, m_log.toUtf8());
|
||||||
rep = APPLICATION->network()->post(request, m_text);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
case Mclogs: {
|
case PasteUpload::Mclogs: {
|
||||||
QUrlQuery postData;
|
QUrlQuery postData;
|
||||||
postData.addQueryItem("content", m_text);
|
postData.addQueryItem("content", m_log);
|
||||||
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
|
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
|
||||||
rep = APPLICATION->network()->post(request, postData.toString().toUtf8());
|
return m_network->post(request, postData.toString().toUtf8());
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
case PasteGG: {
|
case PasteUpload::PasteGG: {
|
||||||
QJsonObject obj;
|
QJsonObject obj;
|
||||||
QJsonDocument doc;
|
QJsonDocument doc;
|
||||||
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
|
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
|
||||||
|
@ -114,7 +82,7 @@ void PasteUpload::executeTask()
|
||||||
QJsonObject logFileInfo;
|
QJsonObject logFileInfo;
|
||||||
QJsonObject logFileContentInfo;
|
QJsonObject logFileContentInfo;
|
||||||
logFileContentInfo.insert("format", "text");
|
logFileContentInfo.insert("format", "text");
|
||||||
logFileContentInfo.insert("value", QString::fromUtf8(m_text));
|
logFileContentInfo.insert("value", m_log);
|
||||||
logFileInfo.insert("name", "log.txt");
|
logFileInfo.insert("name", "log.txt");
|
||||||
logFileInfo.insert("content", logFileContentInfo);
|
logFileInfo.insert("content", logFileContentInfo);
|
||||||
files.append(logFileInfo);
|
files.append(logFileInfo);
|
||||||
|
@ -122,108 +90,115 @@ void PasteUpload::executeTask()
|
||||||
obj.insert("files", files);
|
obj.insert("files", files);
|
||||||
|
|
||||||
doc.setObject(obj);
|
doc.setObject(obj);
|
||||||
rep = APPLICATION->network()->post(request, doc.toJson());
|
return m_network->post(request, doc.toJson());
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
connect(rep, &QNetworkReply::uploadProgress, this, &Task::setProgress);
|
return nullptr;
|
||||||
connect(rep, &QNetworkReply::finished, this, &PasteUpload::downloadFinished);
|
};
|
||||||
|
|
||||||
connect(rep, &QNetworkReply::errorOccurred, this, &PasteUpload::downloadError);
|
auto PasteUpload::Sink::init(QNetworkRequest&) -> Task::State
|
||||||
|
{
|
||||||
|
m_output.clear();
|
||||||
|
return Task::State::Running;
|
||||||
|
};
|
||||||
|
|
||||||
m_reply = std::shared_ptr<QNetworkReply>(rep);
|
auto PasteUpload::Sink::write(QByteArray& data) -> Task::State
|
||||||
|
{
|
||||||
setStatus(tr("Uploading to %1").arg(m_uploadUrl));
|
m_output.append(data);
|
||||||
|
return Task::State::Running;
|
||||||
}
|
}
|
||||||
|
|
||||||
void PasteUpload::downloadError(QNetworkReply::NetworkError error)
|
auto PasteUpload::Sink::abort() -> Task::State
|
||||||
{
|
{
|
||||||
// error happened during download.
|
m_output.clear();
|
||||||
qCCritical(taskUploadLogC) << getUid().toString() << "Network error: " << error;
|
return Task::State::Failed;
|
||||||
emitFailed(m_reply->errorString());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void PasteUpload::downloadFinished()
|
auto PasteUpload::Sink::finalize(QNetworkReply&) -> Task::State
|
||||||
{
|
{
|
||||||
QByteArray data = m_reply->readAll();
|
switch (m_paste_type) {
|
||||||
int statusCode = m_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
case PasteUpload::NullPointer:
|
||||||
|
m_result->link = QString::fromUtf8(m_output).trimmed();
|
||||||
if (m_reply->error() != QNetworkReply::NetworkError::NoError) {
|
|
||||||
emitFailed(tr("Network error: %1").arg(m_reply->errorString()));
|
|
||||||
m_reply.reset();
|
|
||||||
return;
|
|
||||||
} else if (statusCode != 200 && statusCode != 201) {
|
|
||||||
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));
|
|
||||||
qCCritical(taskUploadLogC) << getUid().toString() << m_uploadUrl << " returned unexpected status code " << statusCode
|
|
||||||
<< " with body: " << data;
|
|
||||||
m_reply.reset();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (m_pasteType) {
|
|
||||||
case NullPointer:
|
|
||||||
m_pasteLink = QString::fromUtf8(data).trimmed();
|
|
||||||
break;
|
break;
|
||||||
case Hastebin: {
|
case PasteUpload::Hastebin: {
|
||||||
QJsonDocument jsonDoc{ QJsonDocument::fromJson(data) };
|
QJsonParseError jsonError;
|
||||||
QJsonObject jsonObj{ jsonDoc.object() };
|
auto doc = QJsonDocument::fromJson(m_output, &jsonError);
|
||||||
if (jsonObj.contains("key") && jsonObj["key"].isString()) {
|
if (jsonError.error != QJsonParseError::NoError) {
|
||||||
QString key = jsonDoc.object()["key"].toString();
|
qDebug() << "hastebin server did not reply with JSON" << jsonError.errorString();
|
||||||
m_pasteLink = m_baseUrl + "/" + key;
|
return Task::State::Failed;
|
||||||
|
}
|
||||||
|
auto obj = doc.object();
|
||||||
|
if (obj.contains("key") && obj["key"].isString()) {
|
||||||
|
QString key = doc.object()["key"].toString();
|
||||||
|
m_result->link = m_base_url + "/" + key;
|
||||||
} else {
|
} else {
|
||||||
emitFailed(tr("Error: %1 returned a malformed response body").arg(m_uploadUrl));
|
qDebug() << "Log upload failed:" << doc.toJson();
|
||||||
qCCritical(taskUploadLogC) << getUid().toString() << getUid().toString() << m_uploadUrl
|
return Task::State::Failed;
|
||||||
<< " returned malformed response body: " << data;
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case Mclogs: {
|
case PasteUpload::Mclogs: {
|
||||||
QJsonDocument jsonDoc{ QJsonDocument::fromJson(data) };
|
QJsonParseError jsonError;
|
||||||
QJsonObject jsonObj{ jsonDoc.object() };
|
auto doc = QJsonDocument::fromJson(m_output, &jsonError);
|
||||||
if (jsonObj.contains("success") && jsonObj["success"].isBool()) {
|
if (jsonError.error != QJsonParseError::NoError) {
|
||||||
bool success = jsonObj["success"].toBool();
|
qDebug() << "mclogs server did not reply with JSON" << jsonError.errorString();
|
||||||
|
return Task::State::Failed;
|
||||||
|
}
|
||||||
|
auto obj = doc.object();
|
||||||
|
if (obj.contains("success") && obj["success"].isBool()) {
|
||||||
|
bool success = obj["success"].toBool();
|
||||||
if (success) {
|
if (success) {
|
||||||
m_pasteLink = jsonObj["url"].toString();
|
m_result->link = obj["url"].toString();
|
||||||
} else {
|
} else {
|
||||||
QString error = jsonObj["error"].toString();
|
m_result->error = obj["error"].toString();
|
||||||
emitFailed(tr("Error: %1 returned an error: %2").arg(m_uploadUrl, error));
|
|
||||||
qCCritical(taskUploadLogC) << getUid().toString() << m_uploadUrl << " returned error: " << error;
|
|
||||||
qCCritical(taskUploadLogC) << getUid().toString() << "Response body: " << data;
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
emitFailed(tr("Error: %1 returned a malformed response body").arg(m_uploadUrl));
|
qDebug() << "Log upload failed:" << doc.toJson();
|
||||||
qCCritical(taskUploadLogC) << getUid().toString() << m_uploadUrl << " returned malformed response body: " << data;
|
return Task::State::Failed;
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case PasteGG:
|
case PasteUpload::PasteGG:
|
||||||
QJsonDocument jsonDoc{ QJsonDocument::fromJson(data) };
|
QJsonParseError jsonError;
|
||||||
QJsonObject jsonObj{ jsonDoc.object() };
|
auto doc = QJsonDocument::fromJson(m_output, &jsonError);
|
||||||
if (jsonObj.contains("status") && jsonObj["status"].isString()) {
|
if (jsonError.error != QJsonParseError::NoError) {
|
||||||
QString status = jsonObj["status"].toString();
|
qDebug() << "pastegg server did not reply with JSON" << jsonError.errorString();
|
||||||
|
return Task::State::Failed;
|
||||||
|
}
|
||||||
|
auto obj = doc.object();
|
||||||
|
if (obj.contains("status") && obj["status"].isString()) {
|
||||||
|
QString status = obj["status"].toString();
|
||||||
if (status == "success") {
|
if (status == "success") {
|
||||||
m_pasteLink = m_baseUrl + "/p/anonymous/" + jsonObj["result"].toObject()["id"].toString();
|
m_result->link = m_base_url + "/p/anonymous/" + obj["result"].toObject()["id"].toString();
|
||||||
} else {
|
} else {
|
||||||
QString error = jsonObj["error"].toString();
|
m_result->error = obj["error"].toString();
|
||||||
QString message =
|
m_result->extra_message = (obj.contains("message") && obj["message"].isString()) ? obj["message"].toString() : "none";
|
||||||
(jsonObj.contains("message") && jsonObj["message"].isString()) ? jsonObj["message"].toString() : "none";
|
|
||||||
emitFailed(tr("Error: %1 returned an error code: %2\nError message: %3").arg(m_uploadUrl, error, message));
|
|
||||||
qCCritical(taskUploadLogC) << getUid().toString() << m_uploadUrl << " returned error: " << error;
|
|
||||||
qCCritical(taskUploadLogC) << getUid().toString() << "Error message: " << message;
|
|
||||||
qCCritical(taskUploadLogC) << getUid().toString() << "Response body: " << data;
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
emitFailed(tr("Error: %1 returned a malformed response body").arg(m_uploadUrl));
|
qDebug() << "Log upload failed:" << doc.toJson();
|
||||||
qCCritical(taskUploadLogC) << getUid().toString() << m_uploadUrl << " returned malformed response body: " << data;
|
return Task::State::Failed;
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
emitSucceeded();
|
return Task::State::Succeeded;
|
||||||
|
}
|
||||||
|
|
||||||
|
Net::NetRequest::Ptr PasteUpload::make(const QString& log,
|
||||||
|
const PasteUpload::PasteType pasteType,
|
||||||
|
const QString customBaseURL,
|
||||||
|
ResultPtr result)
|
||||||
|
{
|
||||||
|
auto base = PasteUpload::PasteTypes.at(pasteType);
|
||||||
|
QString baseUrl = customBaseURL.isEmpty() ? base.defaultBase : customBaseURL;
|
||||||
|
auto up = makeShared<PasteUpload>(log, pasteType);
|
||||||
|
|
||||||
|
// HACK: Paste's docs say the standard API path is at /api/<version> but the official instance paste.gg doesn't follow that??
|
||||||
|
if (pasteType == PasteUpload::PasteGG && baseUrl == base.defaultBase)
|
||||||
|
up->m_url = "https://api.paste.gg/v1/pastes";
|
||||||
|
else
|
||||||
|
up->m_url = baseUrl + base.endpointPath;
|
||||||
|
|
||||||
|
up->m_sink.reset(new Sink(pasteType, baseUrl, result));
|
||||||
|
return up;
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,15 +35,16 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <QBuffer>
|
#include "net/NetRequest.h"
|
||||||
#include <QNetworkReply>
|
|
||||||
#include <QString>
|
|
||||||
#include <array>
|
|
||||||
#include <memory>
|
|
||||||
#include "tasks/Task.h"
|
#include "tasks/Task.h"
|
||||||
|
|
||||||
class PasteUpload : public Task {
|
#include <QNetworkReply>
|
||||||
Q_OBJECT
|
#include <QString>
|
||||||
|
|
||||||
|
#include <array>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
class PasteUpload : public Net::NetRequest {
|
||||||
public:
|
public:
|
||||||
enum PasteType : int {
|
enum PasteType : int {
|
||||||
// 0x0.st
|
// 0x0.st
|
||||||
|
@ -58,32 +59,47 @@ class PasteUpload : public Task {
|
||||||
First = NullPointer,
|
First = NullPointer,
|
||||||
Last = Mclogs
|
Last = Mclogs
|
||||||
};
|
};
|
||||||
|
|
||||||
struct PasteTypeInfo {
|
struct PasteTypeInfo {
|
||||||
const QString name;
|
const QString name;
|
||||||
const QString defaultBase;
|
const QString defaultBase;
|
||||||
const QString endpointPath;
|
const QString endpointPath;
|
||||||
};
|
};
|
||||||
|
|
||||||
static std::array<PasteTypeInfo, 4> PasteTypes;
|
static const std::array<PasteTypeInfo, 4> PasteTypes;
|
||||||
|
struct Result {
|
||||||
|
QString link;
|
||||||
|
QString error;
|
||||||
|
QString extra_message;
|
||||||
|
};
|
||||||
|
|
||||||
PasteUpload(QWidget* window, QString text, QString url, PasteType pasteType);
|
using ResultPtr = std::shared_ptr<Result>;
|
||||||
virtual ~PasteUpload();
|
|
||||||
|
|
||||||
QString pasteLink() { return m_pasteLink; }
|
class Sink : public Net::Sink {
|
||||||
|
public:
|
||||||
|
Sink(const PasteType pasteType, const QString base_url, ResultPtr result)
|
||||||
|
: m_paste_type(pasteType), m_base_url(base_url), m_result(result) {};
|
||||||
|
virtual ~Sink() = default;
|
||||||
|
|
||||||
protected:
|
public:
|
||||||
virtual void executeTask();
|
auto init(QNetworkRequest& request) -> Task::State override;
|
||||||
|
auto write(QByteArray& data) -> Task::State override;
|
||||||
|
auto abort() -> Task::State override;
|
||||||
|
auto finalize(QNetworkReply& reply) -> Task::State override;
|
||||||
|
auto hasLocalData() -> bool override { return false; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
const PasteType m_paste_type;
|
||||||
|
const QString m_base_url;
|
||||||
|
ResultPtr m_result;
|
||||||
|
QByteArray m_output;
|
||||||
|
};
|
||||||
|
PasteUpload(const QString& log, const PasteType pasteType) : m_log(log), m_paste_type(pasteType) {}
|
||||||
|
virtual ~PasteUpload() = default;
|
||||||
|
|
||||||
|
static NetRequest::Ptr make(const QString& log, const PasteType pasteType, const QString baseURL, ResultPtr result);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QWidget* m_window;
|
virtual QNetworkReply* getReply(QNetworkRequest&) override;
|
||||||
QString m_pasteLink;
|
QString m_log;
|
||||||
QString m_baseUrl;
|
const PasteType m_paste_type;
|
||||||
QString m_uploadUrl;
|
};
|
||||||
PasteType m_pasteType;
|
|
||||||
QByteArray m_text;
|
|
||||||
std::shared_ptr<QNetworkReply> m_reply;
|
|
||||||
public slots:
|
|
||||||
void downloadError(QNetworkReply::NetworkError);
|
|
||||||
void downloadFinished();
|
|
||||||
};
|
|
|
@ -38,10 +38,15 @@
|
||||||
#include "GuiUtil.h"
|
#include "GuiUtil.h"
|
||||||
|
|
||||||
#include <QApplication>
|
#include <QApplication>
|
||||||
|
#include <QBuffer>
|
||||||
#include <QClipboard>
|
#include <QClipboard>
|
||||||
#include <QFileDialog>
|
#include <QFileDialog>
|
||||||
#include <QStandardPaths>
|
#include <QStandardPaths>
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
#include "FileSystem.h"
|
||||||
|
#include "net/NetJob.h"
|
||||||
#include "net/PasteUpload.h"
|
#include "net/PasteUpload.h"
|
||||||
#include "ui/dialogs/CustomMessageBox.h"
|
#include "ui/dialogs/CustomMessageBox.h"
|
||||||
#include "ui/dialogs/ProgressDialog.h"
|
#include "ui/dialogs/ProgressDialog.h"
|
||||||
|
@ -74,52 +79,52 @@ QString truncateLogForMclogs(const QString& logContent)
|
||||||
return logContent;
|
return logContent;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::optional<QString> GuiUtil::uploadPaste(const QString& name, const QFileInfo& filePath, QWidget* parentWidget)
|
||||||
|
{
|
||||||
|
return uploadPaste(name, FS::read(filePath.absoluteFilePath()), parentWidget);
|
||||||
|
};
|
||||||
|
|
||||||
std::optional<QString> GuiUtil::uploadPaste(const QString& name, const QString& text, QWidget* parentWidget)
|
std::optional<QString> GuiUtil::uploadPaste(const QString& name, const QString& text, QWidget* parentWidget)
|
||||||
{
|
{
|
||||||
ProgressDialog dialog(parentWidget);
|
ProgressDialog dialog(parentWidget);
|
||||||
auto pasteTypeSetting = static_cast<PasteUpload::PasteType>(APPLICATION->settings()->get("PastebinType").toInt());
|
auto pasteType = static_cast<PasteUpload::PasteType>(APPLICATION->settings()->get("PastebinType").toInt());
|
||||||
auto pasteCustomAPIBaseSetting = APPLICATION->settings()->get("PastebinCustomAPIBase").toString();
|
auto baseURL = APPLICATION->settings()->get("PastebinCustomAPIBase").toString();
|
||||||
bool shouldTruncate = false;
|
bool shouldTruncate = false;
|
||||||
|
|
||||||
{
|
if (baseURL.isEmpty())
|
||||||
QUrl baseUrl;
|
baseURL = PasteUpload::PasteTypes[pasteType].defaultBase;
|
||||||
if (pasteCustomAPIBaseSetting.isEmpty())
|
|
||||||
baseUrl = PasteUpload::PasteTypes[pasteTypeSetting].defaultBase;
|
|
||||||
else
|
|
||||||
baseUrl = pasteCustomAPIBaseSetting;
|
|
||||||
|
|
||||||
if (baseUrl.isValid()) {
|
if (auto url = QUrl(baseURL); url.isValid()) {
|
||||||
auto response = CustomMessageBox::selectable(parentWidget, QObject::tr("Confirm Upload"),
|
auto response = CustomMessageBox::selectable(parentWidget, QObject::tr("Confirm Upload"),
|
||||||
QObject::tr("You are about to upload \"%1\" to %2.\n"
|
QObject::tr("You are about to upload \"%1\" to %2.\n"
|
||||||
"You should double-check for personal information.\n\n"
|
"You should double-check for personal information.\n\n"
|
||||||
"Are you sure?")
|
"Are you sure?")
|
||||||
.arg(name, baseUrl.host()),
|
.arg(name, url.host()),
|
||||||
QMessageBox::Warning, QMessageBox::Yes | QMessageBox::No, QMessageBox::No)
|
QMessageBox::Warning, QMessageBox::Yes | QMessageBox::No, QMessageBox::No)
|
||||||
->exec();
|
->exec();
|
||||||
|
|
||||||
if (response != QMessageBox::Yes)
|
if (response != QMessageBox::Yes)
|
||||||
|
return {};
|
||||||
|
|
||||||
|
if (baseURL == "https://api.mclo.gs" && text.count("\n") > MaxMclogsLines) {
|
||||||
|
auto truncateResponse = CustomMessageBox::selectable(
|
||||||
|
parentWidget, QObject::tr("Confirm Truncation"),
|
||||||
|
QObject::tr("The log has %1 lines, exceeding mclo.gs' limit of %2.\n"
|
||||||
|
"The launcher can keep the first %3 and last %4 lines, trimming the middle.\n\n"
|
||||||
|
"If you choose 'No', mclo.gs will only keep the first %2 lines, cutting off "
|
||||||
|
"potentially useful info like crashes at the end.\n\n"
|
||||||
|
"Proceed with truncation?")
|
||||||
|
.arg(text.count("\n"))
|
||||||
|
.arg(MaxMclogsLines)
|
||||||
|
.arg(InitialMclogsLines)
|
||||||
|
.arg(FinalMclogsLines),
|
||||||
|
QMessageBox::Warning, QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel, QMessageBox::No)
|
||||||
|
->exec();
|
||||||
|
|
||||||
|
if (truncateResponse == QMessageBox::Cancel) {
|
||||||
return {};
|
return {};
|
||||||
|
|
||||||
if (baseUrl.toString() == "https://api.mclo.gs" && text.count("\n") > MaxMclogsLines) {
|
|
||||||
auto truncateResponse = CustomMessageBox::selectable(
|
|
||||||
parentWidget, QObject::tr("Confirm Truncation"),
|
|
||||||
QObject::tr("The log has %1 lines, exceeding mclo.gs' limit of %2.\n"
|
|
||||||
"The launcher can keep the first %3 and last %4 lines, trimming the middle.\n\n"
|
|
||||||
"If you choose 'No', mclo.gs will only keep the first %2 lines, cutting off "
|
|
||||||
"potentially useful info like crashes at the end.\n\n"
|
|
||||||
"Proceed with truncation?")
|
|
||||||
.arg(text.count("\n"))
|
|
||||||
.arg(MaxMclogsLines)
|
|
||||||
.arg(InitialMclogsLines)
|
|
||||||
.arg(FinalMclogsLines),
|
|
||||||
QMessageBox::Warning, QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel, QMessageBox::No)
|
|
||||||
->exec();
|
|
||||||
|
|
||||||
if (truncateResponse == QMessageBox::Cancel) {
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
shouldTruncate = truncateResponse == QMessageBox::Yes;
|
|
||||||
}
|
}
|
||||||
|
shouldTruncate = truncateResponse == QMessageBox::Yes;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -128,22 +133,43 @@ std::optional<QString> GuiUtil::uploadPaste(const QString& name, const QString&
|
||||||
textToUpload = truncateLogForMclogs(text);
|
textToUpload = truncateLogForMclogs(text);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<PasteUpload> paste(new PasteUpload(parentWidget, textToUpload, pasteCustomAPIBaseSetting, pasteTypeSetting));
|
auto result = std::make_shared<PasteUpload::Result>();
|
||||||
|
auto job = NetJob::Ptr(new NetJob("Log Upload", APPLICATION->network()));
|
||||||
|
|
||||||
dialog.execWithTask(paste.get());
|
job->addNetAction(PasteUpload::make(textToUpload, pasteType, baseURL, result));
|
||||||
if (!paste->wasSuccessful()) {
|
QObject::connect(job.get(), &Task::failed, [parentWidget](QString reason) {
|
||||||
CustomMessageBox::selectable(parentWidget, QObject::tr("Upload failed"), paste->failReason(), QMessageBox::Critical)->exec();
|
CustomMessageBox::selectable(parentWidget, QObject::tr("Failed to upload logs!"), reason, QMessageBox::Critical)->show();
|
||||||
return QString();
|
});
|
||||||
} else {
|
QObject::connect(job.get(), &Task::aborted, [parentWidget] {
|
||||||
const QString link = paste->pasteLink();
|
CustomMessageBox::selectable(parentWidget, QObject::tr("Logs upload aborted"),
|
||||||
setClipboardText(link);
|
QObject::tr("The task has been aborted by the user."), QMessageBox::Information)
|
||||||
|
->show();
|
||||||
|
});
|
||||||
|
|
||||||
|
if (dialog.execWithTask(job.get()) == QDialog::Accepted) {
|
||||||
|
if (!result->error.isEmpty() || !result->extra_message.isEmpty()) {
|
||||||
|
QString message = QObject::tr("Error: %1").arg(result->error);
|
||||||
|
if (!result->extra_message.isEmpty()) {
|
||||||
|
message += QObject::tr("\nError message: %1").arg(result->extra_message);
|
||||||
|
}
|
||||||
|
CustomMessageBox::selectable(parentWidget, QObject::tr("Failed to upload logs!"), message, QMessageBox::Critical)->show();
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
if (result->link.isEmpty()) {
|
||||||
|
CustomMessageBox::selectable(parentWidget, QObject::tr("Failed to upload logs!"), "The upload link is empty",
|
||||||
|
QMessageBox::Critical)
|
||||||
|
->show();
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
setClipboardText(result->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 placed in your clipboard.").arg(link),
|
QObject::tr("The <a href=\"%1\">link to the uploaded log</a> has been placed in your clipboard.").arg(result->link),
|
||||||
QMessageBox::Information)
|
QMessageBox::Information)
|
||||||
->exec();
|
->exec();
|
||||||
return link;
|
return result->link;
|
||||||
}
|
}
|
||||||
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
void GuiUtil::setClipboardText(const QString& text)
|
void GuiUtil::setClipboardText(const QString& text)
|
||||||
|
|
|
@ -1,10 +1,12 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <QFileInfo>
|
||||||
#include <QWidget>
|
#include <QWidget>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
|
|
||||||
namespace GuiUtil {
|
namespace GuiUtil {
|
||||||
std::optional<QString> uploadPaste(const QString& name, const QString& text, QWidget* parentWidget);
|
std::optional<QString> uploadPaste(const QString& name, const QFileInfo& filePath, QWidget* parentWidget);
|
||||||
|
std::optional<QString> uploadPaste(const QString& name, const QString& data, QWidget* parentWidget);
|
||||||
void setClipboardText(const QString& text);
|
void setClipboardText(const QString& text);
|
||||||
QStringList BrowseForFiles(QString context, QString caption, QString filter, QString defaultPath, QWidget* parentWidget);
|
QStringList BrowseForFiles(QString context, QString caption, QString filter, QString defaultPath, QWidget* parentWidget);
|
||||||
QString BrowseForFile(QString context, QString caption, QString filter, QString defaultPath, QWidget* parentWidget);
|
QString BrowseForFile(QString context, QString caption, QString filter, QString defaultPath, QWidget* parentWidget);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue