mirror of
https://github.com/PrismLauncher/PrismLauncher.git
synced 2026-07-08 13:08:29 +02:00
add special modrinth header
Signed-off-by: Trial97 <alexandru.tripon97@gmail.com>
This commit is contained in:
parent
18924e43da
commit
ca721f9d67
18 changed files with 127 additions and 27 deletions
|
|
@ -36,10 +36,10 @@ std::pair<Download::Ptr, QByteArray*> ApiDownload::makeByteArray(QUrl url, Downl
|
|||
return { dl, response };
|
||||
}
|
||||
|
||||
Download::Ptr ApiDownload::makeFile(QUrl url, QString path, Download::Options options)
|
||||
Download::Ptr ApiDownload::makeFile(QUrl url, QString path, Download::Options options, ModrinthDownloadMeta meta)
|
||||
{
|
||||
auto dl = Download::makeFile(url, path, options);
|
||||
dl->addHeaderProxy(std::make_unique<ApiHeaderProxy>());
|
||||
dl->addHeaderProxy(std::make_unique<ApiHeaderProxy>(std::move(meta)));
|
||||
return dl;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -20,13 +20,15 @@
|
|||
#pragma once
|
||||
|
||||
#include "Download.h"
|
||||
#include "net/ApiHeaderProxy.h"
|
||||
|
||||
namespace Net {
|
||||
|
||||
namespace ApiDownload {
|
||||
Download::Ptr makeCached(QUrl url, MetaEntryPtr entry, Download::Options options = Download::Option::NoOptions);
|
||||
std::pair<Download::Ptr, QByteArray*> makeByteArray(QUrl url, Download::Options options = Download::Option::NoOptions);
|
||||
Download::Ptr makeFile(QUrl url, QString path, Download::Options options = Download::Option::NoOptions);
|
||||
Download::Ptr makeFile(QUrl url, QString path, Download::Options options = Download::Option::NoOptions,
|
||||
ModrinthDownloadMeta meta = ModrinthDownloadMeta());
|
||||
}; // namespace ApiDownload
|
||||
|
||||
} // namespace Net
|
||||
|
|
|
|||
|
|
@ -23,11 +23,35 @@
|
|||
#include "BuildConfig.h"
|
||||
#include "net/HeaderProxy.h"
|
||||
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
|
||||
namespace Net {
|
||||
|
||||
struct ModrinthDownloadMeta {
|
||||
QString reason;
|
||||
QString gameVersion;
|
||||
QString loader;
|
||||
|
||||
bool isEmpty() const { return reason.isEmpty(); }
|
||||
|
||||
QByteArray toJson() const
|
||||
{
|
||||
QJsonObject obj;
|
||||
if (!reason.isEmpty())
|
||||
obj["reason"] = reason;
|
||||
if (!gameVersion.isEmpty())
|
||||
obj["game_version"] = gameVersion;
|
||||
if (!loader.isEmpty())
|
||||
obj["loader"] = loader;
|
||||
return QJsonDocument(obj).toJson(QJsonDocument::Compact);
|
||||
}
|
||||
};
|
||||
|
||||
class ApiHeaderProxy : public HeaderProxy {
|
||||
public:
|
||||
ApiHeaderProxy() : HeaderProxy() {}
|
||||
explicit ApiHeaderProxy(ModrinthDownloadMeta meta) : m_meta(std::move(meta)) {}
|
||||
virtual ~ApiHeaderProxy() = default;
|
||||
|
||||
public:
|
||||
|
|
@ -39,11 +63,19 @@ class ApiHeaderProxy : public HeaderProxy {
|
|||
} 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() });
|
||||
if (!token.isNull()) {
|
||||
hdrs.append({ .headerName = "Authorization", .headerValue = token.toUtf8() });
|
||||
}
|
||||
}
|
||||
|
||||
if (request.url().host() == "cdn.modrinth.com" && !m_meta.isEmpty()) {
|
||||
hdrs.append({ .headerName = "modrinth-download-meta", .headerValue = m_meta.toJson() });
|
||||
}
|
||||
return hdrs;
|
||||
};
|
||||
|
||||
private:
|
||||
ModrinthDownloadMeta m_meta;
|
||||
};
|
||||
|
||||
} // namespace Net
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue