refactor: override / mask static make functions for ApiDownload

Signed-off-by: Rachel Powers <508861+Ryex@users.noreply.github.com>
This commit is contained in:
Rachel Powers 2023-06-01 20:15:39 -07:00
parent 9c10965997
commit 418677ef31
No known key found for this signature in database
GPG Key ID: E10E321EB160949B
11 changed files with 101 additions and 26 deletions

View File

@ -140,6 +140,7 @@ set(NET_SOURCES
net/RawHeaderProxy.h
net/ApiHeaderProxy.h
net/ApiDownload.h
net/ApiDownload.cpp
)
# Game launch logic

View File

@ -0,0 +1,67 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* Prism Launcher - Minecraft Launcher
* Copyright (C) 2023 Rachel Powers <508861+Ryex@users.noreply.github.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
#include "ByteArraySink.h"
#include "ChecksumValidator.h"
#include "MetaCacheSink.h"
#include "net/ApiDownload.h"
#include "net/NetAction.h"
namespace Net {
auto ApiDownload::makeCached(QUrl url, MetaEntryPtr entry, Options options) -> Download::Ptr
{
auto dl = makeShared<ApiDownload>();
dl->m_url = url;
dl->setObjectName(QString("CACHE:") + url.toString());
dl->m_options = options;
auto md5Node = new ChecksumValidator(QCryptographicHash::Md5);
auto cachedNode = new MetaCacheSink(entry, md5Node, options.testFlag(Option::MakeEternal));
dl->m_sink.reset(cachedNode);
return dl;
}
auto ApiDownload::makeByteArray(QUrl url, QByteArray* output, Options options) -> Download::Ptr
{
auto dl = makeShared<ApiDownload>();
dl->m_url = url;
dl->setObjectName(QString("BYTES:") + url.toString());
dl->m_options = options;
dl->m_sink.reset(new ByteArraySink(output));
return dl;
}
auto ApiDownload::makeFile(QUrl url, QString path, Options options) -> Download::Ptr
{
auto dl = makeShared<ApiDownload>();
dl->m_url = url;
dl->setObjectName(QString("FILE:") + url.toString());
dl->m_options = options;
dl->m_sink.reset(new FileSink(path));
return dl;
}
void ApiDownload::init()
{
qDebug() << "Setting up api download";
auto api_headers = new ApiHeaderProxy();
addHeaderProxy(api_headers);
}
} // namespace Net

View File

@ -1,8 +1,6 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* Prism Launcher - Minecraft Launcher
* Copyright (c) 2022 flowln <flowlnlnln@gmail.com>
* Copyright (C) 2022 Sefa Eyeoglu <contact@scrumplex.net>
* Copyright (C) 2023 Rachel Powers <508861+Ryex@users.noreply.github.com>
*
* This program is free software: you can redistribute it and/or modify
@ -28,12 +26,14 @@ namespace Net {
class ApiDownload : public Download {
public:
ApiDownload() : Download()
{
auto api_headers = new ApiHeaderProxy();
addHeaderProxy(api_headers);
}
virtual ~ApiDownload() = default;
static auto makeCached(QUrl url, MetaEntryPtr entry, Options options = Option::NoOptions) -> Download::Ptr;
static auto makeByteArray(QUrl url, QByteArray* output, Options options = Option::NoOptions) -> Download::Ptr;
static auto makeFile(QUrl url, QString path, Options options = Option::NoOptions) -> Download::Ptr;
void init() override;
};
} // namespace Net

View File

@ -1,8 +1,6 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* Prism Launcher - Minecraft Launcher
* Copyright (c) 2022 flowln <flowlnlnln@gmail.com>
* Copyright (C) 2022 Sefa Eyeoglu <contact@scrumplex.net>
* Copyright (C) 2023 Rachel Powers <508861+Ryex@users.noreply.github.com>
*
* This program is free software: you can redistribute it and/or modify
@ -21,9 +19,9 @@
#pragma once
#include "net/HeaderProxy.h"
#include "Application.h"
#include "BuildConfig.h"
#include "net/HeaderProxy.h"
namespace Net {
@ -33,19 +31,19 @@ class ApiHeaderProxy : public HeaderProxy {
virtual ~ApiHeaderProxy() = default;
public:
virtual QList<HeaderPair> headers(const QNetworkRequest& request) const override
{
QList<HeaderPair> hdrs;
if (APPLICATION->capabilities() & Application::SupportsFlame && request.url().host() == QUrl(BuildConfig.FLAME_BASE_URL).host()) {
hdrs.append({ "x-api-key", APPLICATION->getFlameAPIKey().toUtf8() });
} else if (request.url().host() == QUrl(BuildConfig.MODRINTH_PROD_URL).host() ||
request.url().host() == QUrl(BuildConfig.MODRINTH_STAGING_URL).host()) {
QString token = APPLICATION->getModrinthAPIToken();
if (!token.isNull())
hdrs.append({ "Authorization", token.toUtf8() });
}
return hdrs;
virtual QList<HeaderPair> headers(const QNetworkRequest& request) const override {
QList<HeaderPair> hdrs;
if (APPLICATION->capabilities() & Application::SupportsFlame && request.url().host() == QUrl(BuildConfig.FLAME_BASE_URL).host()) {
hdrs.append({ "x-api-key", APPLICATION->getFlameAPIKey().toUtf8() });
} else if (request.url().host() == QUrl(BuildConfig.MODRINTH_PROD_URL).host() ||
request.url().host() == QUrl(BuildConfig.MODRINTH_STAGING_URL).host()) {
QString token = APPLICATION->getModrinthAPIToken();
if (!token.isNull())
hdrs.append({ "Authorization", token.toUtf8() });
}
return hdrs;
};
};
} // namespace Net

View File

@ -96,6 +96,8 @@ void Download::addValidator(Validator* v)
void Download::executeTask()
{
init();
setStatus(tr("Downloading %1").arg(StringUtils::truncateUrlHumanFriendly(m_url, 80)));
if (getState() == Task::State::AbortedByUser) {
@ -124,7 +126,8 @@ void Download::executeTask()
}
request.setHeader(QNetworkRequest::UserAgentHeader, APPLICATION->getUserAgent().toUtf8());
for ( auto header_proxy : m_headerProxies ) {
for ( auto& header_proxy : m_headerProxies ) {
header_proxy->writeHeaders(request);
}
// TODO remove duplication

View File

@ -63,6 +63,8 @@ class Download : public NetAction {
static auto makeByteArray(QUrl url, QByteArray* output, Options options = Option::NoOptions) -> Download::Ptr;
static auto makeFile(QUrl url, QString path, Options options = Option::NoOptions) -> Download::Ptr;
void init() override {};
public:
void addValidator(Validator* v);
auto abort() -> bool override;
@ -81,7 +83,7 @@ class Download : public NetAction {
public slots:
void executeTask() override;
private:
protected:
std::unique_ptr<Sink> m_sink;
Options m_options;

View File

@ -1,8 +1,6 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* Prism Launcher - Minecraft Launcher
* Copyright (c) 2022 flowln <flowlnlnln@gmail.com>
* Copyright (C) 2022 Sefa Eyeoglu <contact@scrumplex.net>
* Copyright (C) 2023 Rachel Powers <508861+Ryex@users.noreply.github.com>
*
* This program is free software: you can redistribute it and/or modify
@ -22,6 +20,7 @@
#pragma once
#include <QNetworkRequest>
#include <QDebug>
namespace Net {

View File

@ -59,6 +59,7 @@ class NetAction : public Task {
void setNetwork(shared_qobject_ptr<QNetworkAccessManager> network) { m_network = network; }
void addHeaderProxy(Net::HeaderProxy* proxy) { m_headerProxies.push_back(std::shared_ptr<Net::HeaderProxy>(proxy)); }
virtual void init() = 0;
protected slots:
virtual void downloadProgress(qint64 bytesReceived, qint64 bytesTotal) = 0;

View File

@ -51,6 +51,7 @@ namespace Net {
static Upload::Ptr makeByteArray(QUrl url, QByteArray *output, QByteArray m_post_data);
auto abort() -> bool override;
auto canAbort() const -> bool override { return true; };
virtual void init() override {};
protected slots:
void downloadProgress(qint64 bytesReceived, qint64 bytesTotal) override;

View File

@ -57,6 +57,8 @@ public:
return m_id;
}
void init() override {};
protected
slots:
void downloadProgress(qint64 bytesReceived, qint64 bytesTotal) override;

View File

@ -46,6 +46,7 @@ public:
static Ptr make(ScreenShot::Ptr shot) {
return Ptr(new ImgurUpload(shot));
}
void init() override {};
protected
slots: