Modify getpkg/src/GetbinClient.cpp.bak

This commit is contained in:
Your Name
2025-06-29 19:02:48 +12:00
parent a1b12fe177
commit 91ba44bcf1

View File

@ -1,260 +0,0 @@
#include "GetbinClient.hpp"
#include <cpr/cpr.h>
#include <nlohmann/json.hpp>
#include <fstream>
#include <iostream>
#include <sstream>
#include <algorithm>
using json = nlohmann::json;
const std::string GetbinClient::SERVER_HOST = "getpkg.xyz";
GetbinClient::GetbinClient() {
// Initialize CPR (done automatically, but we could add global config here)
}
std::string GetbinClient::getUserAgent() const {
return "getpkg/1.0";
}
bool GetbinClient::download(const std::string& toolName, const std::string& arch, const std::string& outPath,
ProgressCallback progressCallback) {
try {
std::string url = "https://" + SERVER_HOST + "/object/" + toolName + ":" + arch;
cpr::Session session;
session.SetUrl(cpr::Url{url});
session.SetHeader(cpr::Header{{"User-Agent", getUserAgent()}});
session.SetTimeout(cpr::Timeout{30000}); // 30 seconds
session.SetVerifySsl(cpr::VerifySsl{true});
// Add progress callback if provided
if (progressCallback) {
session.SetProgressCallback(cpr::ProgressCallback{[progressCallback](cpr::cpr_off_t downloadTotal, cpr::cpr_off_t downloadNow,
cpr::cpr_off_t uploadTotal, cpr::cpr_off_t uploadNow,
intptr_t userdata) -> bool {
return progressCallback(static_cast<size_t>(downloadNow), static_cast<size_t>(downloadTotal));
}});
}
auto response = session.Get();
if (response.status_code == 200) {
std::ofstream ofs(outPath, std::ios::binary);
if (ofs) {
ofs.write(response.text.data(), response.text.size());
return ofs.good();
}
} else if (response.status_code == 404) {
// Not found - this is expected for arch fallback
return false;
} else {
std::cerr << "[GetbinClient::download] HTTP " << response.status_code << ": " << response.error.message << std::endl;
}
return false;
} catch (const std::exception& e) {
std::cerr << "[GetbinClient::download] Exception: " << e.what() << std::endl;
return false;
}
}
bool GetbinClient::upload(const std::string& archivePath, std::string& outUrl, std::string& outHash,
const std::string& token, ProgressCallback progressCallback) {
try {
std::string url = "https://" + SERVER_HOST + "/upload";
cpr::Session session;
session.SetUrl(cpr::Url{url});
session.SetHeader(cpr::Header{
{"User-Agent", getUserAgent()},
{"Authorization", "Bearer " + token}
});
session.SetTimeout(cpr::Timeout{300000}); // 5 minutes for uploads
session.SetVerifySsl(cpr::VerifySsl{true});
// Read file for upload
std::ifstream file(archivePath, std::ios::binary);
if (!file) {
std::cerr << "[GetbinClient::upload] Cannot open file: " << archivePath << std::endl;
return false;
}
// Get file size
file.seekg(0, std::ios::end);
size_t fileSize = file.tellg();
file.seekg(0, std::ios::beg);
// Read file content
std::string fileContent(fileSize, '\0');
file.read(&fileContent[0], fileSize);
file.close();
// Set up multipart form
session.SetMultipart(cpr::Multipart{
cpr::Part{"file", fileContent, "application/gzip"}
});
// Add progress callback if provided
if (progressCallback) {
session.SetProgressCallback(cpr::ProgressCallback{[progressCallback](cpr::cpr_off_t downloadTotal, cpr::cpr_off_t downloadNow,
cpr::cpr_off_t uploadTotal, cpr::cpr_off_t uploadNow,
intptr_t userdata) -> bool {
return progressCallback(static_cast<size_t>(uploadNow), static_cast<size_t>(uploadTotal));
}});
}
auto response = session.Post();
if (response.status_code == 200) {
try {
auto resp_json = json::parse(response.text);
if (resp_json.contains("url") && resp_json.contains("hash")) {
outUrl = resp_json["url"].get<std::string>();
outHash = resp_json["hash"].get<std::string>();
return true;
}
} catch (const json::exception& e) {
// Try to extract from plain text response
outUrl = "";
outHash = response.text;
// Remove trailing newline if present
if (!outHash.empty() && outHash.back() == '\n') {
outHash.pop_back();
}
return !outHash.empty();
}
} else {
std::cerr << "[GetbinClient::upload] HTTP " << response.status_code << ": " << response.error.message << std::endl;
if (!response.text.empty()) {
std::cerr << "[GetbinClient::upload] Response: " << response.text << std::endl;
}
}
return false;
} catch (const std::exception& e) {
std::cerr << "[GetbinClient::upload] Exception: " << e.what() << std::endl;
return false;
}
}
bool GetbinClient::getHash(const std::string& toolName, const std::string& arch, std::string& outHash) {
try {
std::string url = "https://" + SERVER_HOST + "/hash/" + toolName + ":" + arch;
auto response = cpr::Get(cpr::Url{url},
cpr::Header{{"User-Agent", getUserAgent()}},
cpr::Timeout{10000}, // 10 seconds
cpr::VerifySsl{true});
if (response.status_code == 200) {
try {
// Try JSON first
auto resp_json = json::parse(response.text);
if (resp_json.contains("hash")) {
outHash = resp_json["hash"].get<std::string>();
return true;
}
} catch (const json::exception&) {
// Not JSON, treat as plain text
outHash = response.text;
// Remove trailing newline if present
if (!outHash.empty() && outHash.back() == '\n') {
outHash.pop_back();
}
return !outHash.empty();
}
} else if (response.status_code == 404) {
// Not found - this is expected for non-existent tools/archs
return false;
} else {
std::cerr << "[GetbinClient::getHash] HTTP " << response.status_code << ": " << response.error.message << std::endl;
}
return false;
} catch (const std::exception& e) {
std::cerr << "[GetbinClient::getHash] Exception: " << e.what() << std::endl;
return false;
}
}
bool GetbinClient::deleteObject(const std::string& hash, const std::string& token) {
try {
std::string url = "https://" + SERVER_HOST + "/deleteobject?hash=" + hash;
auto response = cpr::Get(cpr::Url{url},
cpr::Header{
{"User-Agent", getUserAgent()},
{"Authorization", "Bearer " + token}
},
cpr::Timeout{30000}, // 30 seconds
cpr::VerifySsl{true});
if (response.status_code == 200) {
return true;
} else {
std::cerr << "[GetbinClient::deleteObject] HTTP " << response.status_code << ": " << response.error.message << std::endl;
if (!response.text.empty()) {
std::cerr << "[GetbinClient::deleteObject] Response: " << response.text << std::endl;
}
}
return false;
} catch (const std::exception& e) {
std::cerr << "[GetbinClient::deleteObject] Exception: " << e.what() << std::endl;
return false;
}
}
bool GetbinClient::listPackages(std::vector<std::string>& outPackages) {
try {
std::string url = "https://" + SERVER_HOST + "/packages";
auto response = cpr::Get(cpr::Url{url},
cpr::Header{{"User-Agent", getUserAgent()}},
cpr::Timeout{30000}, // 30 seconds
cpr::VerifySsl{true});
if (response.status_code == 200) {
try {
auto resp_json = json::parse(response.text);
if (resp_json.is_array()) {
outPackages.clear();
for (const auto& item : resp_json) {
if (item.is_string()) {
outPackages.push_back(item.get<std::string>());
}
}
return true;
} else if (resp_json.contains("packages") && resp_json["packages"].is_array()) {
outPackages.clear();
for (const auto& item : resp_json["packages"]) {
if (item.is_string()) {
outPackages.push_back(item.get<std::string>());
}
}
return true;
}
} catch (const json::exception&) {
// Try to parse as newline-separated list
outPackages.clear();
std::istringstream stream(response.text);
std::string line;
while (std::getline(stream, line)) {
if (!line.empty()) {
outPackages.push_back(line);
}
}
return !outPackages.empty();
}
} else {
std::cerr << "[GetbinClient::listPackages] HTTP " << response.status_code << ": " << response.error.message << std::endl;
}
return false;
} catch (const std::exception& e) {
std::cerr << "[GetbinClient::listPackages] Exception: " << e.what() << std::endl;
return false;
}
}