upload progresso bar

This commit is contained in:
2018-01-09 03:27:15 +00:00
parent 5feb3acb1d
commit 0bdb879e76
4 changed files with 72 additions and 32 deletions

View File

@@ -63,7 +63,7 @@ void App::initLog()
LogRemote::I.file_init();
}
int progress_callback(void *clientp, curl_off_t dltotal,
int progress_callback_download(void *clientp, curl_off_t dltotal,
curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow)
{
std::function<void(float)> progress = *(std::function<void(float)>*)clientp;
@@ -71,6 +71,14 @@ int progress_callback(void *clientp, curl_off_t dltotal,
return 0;
}
int progress_callback_upload(void *clientp, curl_off_t dltotal,
curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow)
{
std::function<void(float)> progress = *(std::function<void(float)>*)clientp;
progress((float)ulnow / (float)ultotal);
return 0;
}
void App::download(std::string filename, std::function<void(float)> progress)
{
CURL *curl = curl_easy_init();
@@ -82,16 +90,19 @@ void App::download(std::string filename, std::function<void(float)> progress)
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_data_write);
curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, progress_callback);
curl_easy_setopt(curl, CURLOPT_XFERINFODATA, &progress);
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
if (progress)
{
curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, progress_callback_download);
curl_easy_setopt(curl, CURLOPT_XFERINFODATA, &progress);
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
}
auto err = curl_easy_perform(curl);
curl_easy_cleanup(curl);
fclose(fp);
}
}
void App::upload(std::string filename, std::string name)
void App::upload(std::string filename, std::string name, std::function<void(float)> progress)
{
CURL *curl;
@@ -116,6 +127,12 @@ void App::upload(std::string filename, std::string name)
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &res);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_data_handler);
if (progress)
{
curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, progress_callback_upload);
curl_easy_setopt(curl, CURLOPT_XFERINFODATA, &progress);
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
}
auto err = curl_easy_perform(curl);
std::cout << "\n\nUPLOAD RESULT\n" << res << "\n\n\n";
curl_easy_cleanup(curl);