Continue support for download
This commit is contained in:
@@ -17,6 +17,24 @@ CSiaCurl::~CSiaCurl()
|
||||
{
|
||||
}
|
||||
|
||||
std::string CSiaCurl::UrlEncode(const String& data, const bool& allowSlash)
|
||||
{
|
||||
CURL* curlHandle = curl_easy_init();
|
||||
curl_easy_reset(curlHandle);
|
||||
|
||||
char* value = curl_easy_escape(curlHandle, CW2A(data.c_str()).m_psz, 0);
|
||||
std::string ret = value;
|
||||
curl_free(value);
|
||||
if (allowSlash)
|
||||
{
|
||||
ReplaceStringInPlace(ret, "%2F", "/");
|
||||
}
|
||||
|
||||
curl_easy_cleanup(curlHandle);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
SiaCurlError CSiaCurl::CheckApiError(const json& result)
|
||||
{
|
||||
SiaCurlError ret = SiaCurlError::Success;
|
||||
@@ -39,7 +57,7 @@ SiaCurlError CSiaCurl::CheckApiError(const json& result)
|
||||
|
||||
std::string CSiaCurl::ConstructPath(const String& relativePath) const
|
||||
{
|
||||
const std::string ret = "http://" + std::string(CW2A(GetHostConfig().HostName.c_str())) + ":" + std::to_string(GetHostConfig().HostPort) + std::string(CW2A(relativePath.c_str()));
|
||||
const std::string ret = "http://" + std::string(CW2A(GetHostConfig().HostName.c_str()).m_psz) + ":" + std::to_string(GetHostConfig().HostPort) + UrlEncode(relativePath, true);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -83,13 +101,25 @@ SiaCurlError CSiaCurl::ProcessResponse(const int& res, const int& httpCode, cons
|
||||
return ret;
|
||||
}
|
||||
|
||||
SiaCurlError CSiaCurl::_Get(const String& path, json& response) const
|
||||
SiaCurlError CSiaCurl::_Get(const String& path, const HttpParameters& parameters, json& response) const
|
||||
{
|
||||
CURL* curlHandle = curl_easy_init();
|
||||
curl_easy_reset(curlHandle);
|
||||
|
||||
std::string url = ConstructPath(path);
|
||||
if (parameters.size())
|
||||
{
|
||||
url += "?";
|
||||
for (const auto& param : parameters)
|
||||
{
|
||||
if (url[url.length() - 1] != '?')
|
||||
{
|
||||
url += "&";
|
||||
}
|
||||
url += (CW2A(param.first.c_str()).m_psz + std::string("=") + UrlEncode(param.second));
|
||||
}
|
||||
}
|
||||
curl_easy_setopt(curlHandle, CURLOPT_USERAGENT, "Sia-Agent");
|
||||
curl_easy_setopt(curlHandle, CURLOPT_URL, ConstructPath(path).c_str());
|
||||
curl_easy_setopt(curlHandle, CURLOPT_URL, url.c_str());
|
||||
curl_easy_setopt(curlHandle, CURLOPT_WRITEFUNCTION, static_cast<size_t(*)(char*, size_t, size_t, void *)>([](char *buffer, size_t size, size_t nitems, void *outstream) -> size_t
|
||||
{
|
||||
(*reinterpret_cast<std::string*>(outstream)) += std::string(reinterpret_cast<LPCSTR>(buffer), size * nitems);
|
||||
@@ -127,7 +157,7 @@ bool CSiaCurl::CheckVersion(SiaCurlError& error) const
|
||||
String CSiaCurl::GetServerVersion() const
|
||||
{
|
||||
json response;
|
||||
if (ApiSuccess(_Get(L"/daemon/version", response)))
|
||||
if (ApiSuccess(_Get(L"/daemon/version", {}, response)))
|
||||
{
|
||||
return String(CA2W(response["version"].get<std::string>().c_str()));
|
||||
}
|
||||
@@ -140,13 +170,24 @@ SiaCurlError CSiaCurl::Get(const String& path, json& response) const
|
||||
SiaCurlError ret;
|
||||
if (CheckVersion(ret))
|
||||
{
|
||||
ret = _Get(path, response);
|
||||
ret = _Get(path, {}, response);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
SiaCurlError CSiaCurl::Post(const String& path, const PostParameters& parameters, json& response) const
|
||||
SiaCurlError CSiaCurl::Get(const String& path, const HttpParameters& parameters, json& response) const
|
||||
{
|
||||
SiaCurlError ret;
|
||||
if (CheckVersion(ret))
|
||||
{
|
||||
ret = _Get(path, parameters, response);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
SiaCurlError CSiaCurl::Post(const String& path, const HttpParameters& parameters, json& response) const
|
||||
{
|
||||
SiaCurlError ret;
|
||||
if (CheckVersion(ret))
|
||||
|
@@ -18,7 +18,7 @@ public:
|
||||
UnknownFailure
|
||||
};
|
||||
|
||||
typedef std::unordered_map<String, String> _PostParameters;
|
||||
typedef std::unordered_map<String, String> _HttpParameters;
|
||||
|
||||
public:
|
||||
CSiaCurl();
|
||||
@@ -31,24 +31,28 @@ public:
|
||||
private:
|
||||
Property(SiaHostConfig, HostConfig, public, public)
|
||||
|
||||
private:
|
||||
static _SiaCurlError CheckApiError(const json& result);
|
||||
public:
|
||||
static std::string UrlEncode(const String& data, const bool& allowSlash = false);
|
||||
|
||||
private:
|
||||
static _SiaCurlError CheckApiError(const json& result);
|
||||
static _SiaCurlError CheckHttpError(const std::string& result);
|
||||
|
||||
private:
|
||||
std::string ConstructPath(const String& relativePath) const;
|
||||
_SiaCurlError _Get(const String& path, json& response) const;
|
||||
_SiaCurlError _Get(const String& path, const _HttpParameters& parameters, json& response) const;
|
||||
bool CheckVersion(_SiaCurlError& error) const;
|
||||
_SiaCurlError ProcessResponse(const int& res, const int& httpCode, const std::string& result, json& response) const;
|
||||
|
||||
public:
|
||||
String GetServerVersion() const;
|
||||
_SiaCurlError Get(const String& path, json& result) const;
|
||||
_SiaCurlError Post(const String& path, const _PostParameters& parameters, json& response) const;
|
||||
_SiaCurlError Get(const String& path, const _HttpParameters& parameters, json& result) const;
|
||||
_SiaCurlError Post(const String& path, const _HttpParameters& parameters, json& response) const;
|
||||
};
|
||||
|
||||
typedef CSiaCurl::_SiaCurlError SiaCurlError;
|
||||
typedef CSiaCurl::_PostParameters PostParameters;
|
||||
typedef CSiaCurl::_HttpParameters HttpParameters;
|
||||
|
||||
NS_END(2)
|
||||
|
||||
|
@@ -131,7 +131,14 @@ SiaApiError CSiaApi::_CSiaRenter::FileExists(const String& siaPath, bool& exists
|
||||
|
||||
SiaApiError CSiaApi::_CSiaRenter::DownloadFile(const String& siaPath, const String& location) const
|
||||
{
|
||||
return SiaApiError::NotImplemented;
|
||||
SiaApiError ret = SiaApiError::RequestError;
|
||||
json result;
|
||||
if (ApiSuccess(GetSiaCurl().Get(L"/renter/download/" + siaPath, { { L"destination", location } }, result)))
|
||||
{
|
||||
ret = SiaApiError::Success;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
SiaApiError CSiaApi::_CSiaRenter::GetFileTree(CSiaFileTreePtr& siaFileTree) const
|
||||
|
@@ -63,7 +63,9 @@ private:
|
||||
ret = ApiSuccess(_siaApi->GetRenter()->DownloadFile(siaPath, tempPath));
|
||||
if (ret)
|
||||
{
|
||||
|
||||
String src = StdConstructPath(tempPath, L"");
|
||||
String dest = StdConstructPath(GetCacheLocation(), siaPath);
|
||||
ret = ::MoveFile(src.c_str(), dest.c_str()) ? true : false;
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user