Move cloud transfer helpers to legacy cloud services
This commit is contained in:
121
src/app.cpp
121
src/app.cpp
@@ -226,77 +226,6 @@ void App::initLog()
|
||||
LOG("load preferences failed");
|
||||
}
|
||||
|
||||
#if WITH_CURL
|
||||
int progress_callback_download(void *clientp, curl_off_t dltotal,
|
||||
curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow)
|
||||
{
|
||||
(void)ultotal;
|
||||
(void)ulnow;
|
||||
auto* progress = static_cast<std::function<void(float)>*>(clientp);
|
||||
const auto plan = pp::app::plan_cloud_transfer_progress(
|
||||
static_cast<std::int64_t>(dltotal),
|
||||
static_cast<std::int64_t>(dlnow));
|
||||
if (progress != nullptr && *progress && plan.notify)
|
||||
(*progress)(plan.fraction);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int progress_callback_upload(void *clientp, curl_off_t dltotal,
|
||||
curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow)
|
||||
{
|
||||
(void)dltotal;
|
||||
(void)dlnow;
|
||||
auto* progress = static_cast<std::function<void(float)>*>(clientp);
|
||||
const auto plan = pp::app::plan_cloud_transfer_progress(
|
||||
static_cast<std::int64_t>(ultotal),
|
||||
static_cast<std::int64_t>(ulnow));
|
||||
if (progress != nullptr && *progress && plan.notify)
|
||||
(*progress)(plan.fraction);
|
||||
return 0;
|
||||
}
|
||||
#endif //CURL
|
||||
|
||||
void App::download(std::string url, std::string dest_filepath, std::function<void(float)> progress)
|
||||
{
|
||||
#if WITH_CURL
|
||||
const auto plan = pp::app::plan_cloud_download_transfer(
|
||||
url,
|
||||
dest_filepath,
|
||||
progress != nullptr,
|
||||
disables_network_tls_verification());
|
||||
if (plan.action != pp::app::CloudTransferAction::start_transfer) {
|
||||
LOG("download skipped: invalid transfer request");
|
||||
return;
|
||||
}
|
||||
|
||||
CURL *curl = curl_easy_init();
|
||||
if (curl)
|
||||
{
|
||||
FILE* fp = fopen(dest_filepath.c_str(), "wb");
|
||||
if (fp == nullptr) {
|
||||
LOG("download failed to open destination %s", dest_filepath.c_str());
|
||||
curl_easy_cleanup(curl);
|
||||
return;
|
||||
}
|
||||
LOG("download %s to %s", url.c_str(), dest_filepath.c_str());
|
||||
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);
|
||||
if (plan.disable_tls_verification)
|
||||
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
|
||||
if (plan.enable_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);
|
||||
}
|
||||
#endif //CURL
|
||||
}
|
||||
|
||||
bool App::check_license()
|
||||
{
|
||||
return true; // TODO: for distribuiton only
|
||||
@@ -331,56 +260,6 @@ bool App::check_license()
|
||||
return false;
|
||||
}
|
||||
|
||||
void App::upload(std::string filename, std::string name, std::function<void(float)> progress)
|
||||
{
|
||||
#if WITH_CURL
|
||||
const auto plan = pp::app::plan_cloud_upload_transfer(
|
||||
filename,
|
||||
progress != nullptr,
|
||||
disables_network_tls_verification());
|
||||
if (plan.action != pp::app::CloudTransferAction::start_transfer) {
|
||||
LOG("upload skipped: invalid transfer request");
|
||||
return;
|
||||
}
|
||||
|
||||
CURL *curl;
|
||||
|
||||
struct curl_httppost *formpost = NULL;
|
||||
struct curl_httppost *lastptr = NULL;
|
||||
|
||||
//curl_global_init(CURL_GLOBAL_ALL);
|
||||
|
||||
curl_formadd(&formpost,
|
||||
&lastptr,
|
||||
CURLFORM_COPYNAME, "fileToUpload",
|
||||
CURLFORM_FILE, filename.c_str(),
|
||||
CURLFORM_END);
|
||||
|
||||
curl = curl_easy_init();
|
||||
std::string res;
|
||||
|
||||
if (curl)
|
||||
{
|
||||
std::string url = "https://panopainter.com/cloud/cloud-upl.php?name=" + name;
|
||||
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
||||
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &res);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_data_handler);
|
||||
if (plan.disable_tls_verification)
|
||||
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
|
||||
if (plan.enable_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);
|
||||
}
|
||||
#endif //CURL
|
||||
}
|
||||
|
||||
void App::init()
|
||||
{
|
||||
LOG("Screen Resolution: %dx%d", (int)width, (int)height);
|
||||
|
||||
@@ -319,10 +319,6 @@ public:
|
||||
void cloud_upload();
|
||||
void cloud_upload_all();
|
||||
void cloud_browse();
|
||||
void upload(std::string filename, std::string name = "",
|
||||
std::function<void(float)> progress = nullptr);
|
||||
void download(std::string url, std::string dest_filepath,
|
||||
std::function<void(float)> progress = nullptr);
|
||||
bool check_license();
|
||||
|
||||
std::shared_ptr<NodeProgressBar> show_progress(const std::string& title, int total = 0);
|
||||
|
||||
@@ -14,6 +14,131 @@
|
||||
namespace pp::panopainter {
|
||||
namespace {
|
||||
|
||||
#if WITH_CURL
|
||||
int progress_callback_download(void* clientp, curl_off_t dltotal,
|
||||
curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow)
|
||||
{
|
||||
(void)ultotal;
|
||||
(void)ulnow;
|
||||
auto* progress = static_cast<std::function<void(float)>*>(clientp);
|
||||
const auto plan = pp::app::plan_cloud_transfer_progress(
|
||||
static_cast<std::int64_t>(dltotal),
|
||||
static_cast<std::int64_t>(dlnow));
|
||||
if (progress != nullptr && *progress && plan.notify)
|
||||
(*progress)(plan.fraction);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int progress_callback_upload(void* clientp, curl_off_t dltotal,
|
||||
curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow)
|
||||
{
|
||||
(void)dltotal;
|
||||
(void)dlnow;
|
||||
auto* progress = static_cast<std::function<void(float)>*>(clientp);
|
||||
const auto plan = pp::app::plan_cloud_transfer_progress(
|
||||
static_cast<std::int64_t>(ultotal),
|
||||
static_cast<std::int64_t>(ulnow));
|
||||
if (progress != nullptr && *progress && plan.notify)
|
||||
(*progress)(plan.fraction);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void execute_cloud_download_transfer(
|
||||
App& app,
|
||||
std::string url,
|
||||
std::string dest_filepath,
|
||||
std::function<void(float)> progress)
|
||||
{
|
||||
const auto plan = pp::app::plan_cloud_download_transfer(
|
||||
url,
|
||||
dest_filepath,
|
||||
progress != nullptr,
|
||||
app.disables_network_tls_verification());
|
||||
if (plan.action != pp::app::CloudTransferAction::start_transfer) {
|
||||
LOG("download skipped: invalid transfer request");
|
||||
return;
|
||||
}
|
||||
|
||||
CURL* curl = curl_easy_init();
|
||||
if (curl)
|
||||
{
|
||||
FILE* fp = fopen(dest_filepath.c_str(), "wb");
|
||||
if (fp == nullptr) {
|
||||
LOG("download failed to open destination %s", dest_filepath.c_str());
|
||||
curl_easy_cleanup(curl);
|
||||
return;
|
||||
}
|
||||
LOG("download %s to %s", url.c_str(), dest_filepath.c_str());
|
||||
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);
|
||||
if (plan.disable_tls_verification)
|
||||
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
|
||||
if (plan.enable_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 execute_cloud_upload_transfer(
|
||||
App& app,
|
||||
std::string filename,
|
||||
std::string name,
|
||||
std::function<void(float)> progress)
|
||||
{
|
||||
const auto plan = pp::app::plan_cloud_upload_transfer(
|
||||
filename,
|
||||
progress != nullptr,
|
||||
app.disables_network_tls_verification());
|
||||
if (plan.action != pp::app::CloudTransferAction::start_transfer) {
|
||||
LOG("upload skipped: invalid transfer request");
|
||||
return;
|
||||
}
|
||||
|
||||
CURL* curl;
|
||||
|
||||
struct curl_httppost* formpost = NULL;
|
||||
struct curl_httppost* lastptr = NULL;
|
||||
|
||||
//curl_global_init(CURL_GLOBAL_ALL);
|
||||
|
||||
curl_formadd(&formpost,
|
||||
&lastptr,
|
||||
CURLFORM_COPYNAME, "fileToUpload",
|
||||
CURLFORM_FILE, filename.c_str(),
|
||||
CURLFORM_END);
|
||||
|
||||
curl = curl_easy_init();
|
||||
std::string res;
|
||||
|
||||
if (curl)
|
||||
{
|
||||
std::string url = "https://panopainter.com/cloud/cloud-upl.php?name=" + name;
|
||||
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
||||
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &res);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_data_handler);
|
||||
if (plan.disable_tls_verification)
|
||||
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
|
||||
if (plan.enable_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);
|
||||
}
|
||||
}
|
||||
#endif //WITH_CURL
|
||||
|
||||
class LegacyCloudServices final : public pp::app::CloudServices {
|
||||
public:
|
||||
explicit LegacyCloudServices(App& app) noexcept
|
||||
@@ -41,7 +166,7 @@ public:
|
||||
const auto progress_plan = pp::app::plan_cloud_upload_progress_dialog();
|
||||
auto pb = app->show_progress(progress_plan.title, progress_plan.total);
|
||||
|
||||
app->upload(app->doc_path, app->doc_filename, [pb](float p) {
|
||||
execute_cloud_upload_transfer(*app, app->doc_path, app->doc_filename, [pb](float p) {
|
||||
pb->set_progress(p);
|
||||
});
|
||||
|
||||
@@ -77,7 +202,7 @@ public:
|
||||
for (const auto& n : names)
|
||||
{
|
||||
std::string path = app_.data_path + "/" + n;
|
||||
app_.upload(path);
|
||||
execute_cloud_upload_transfer(app_, path, std::string(), std::function<void(float)> {});
|
||||
|
||||
if (bulk_progress_) {
|
||||
bulk_progress_->increment();
|
||||
@@ -116,7 +241,7 @@ public:
|
||||
*app,
|
||||
pp::app::plan_cloud_download_progress_prompt());
|
||||
std::string url = "https://panopainter.com/cloud/cloud-dwl.php?file=" + request.selected_file;
|
||||
app->download(url, request.selected_path, [m](float p) {
|
||||
execute_cloud_download_transfer(*app, url, request.selected_path, [m](float p) {
|
||||
const auto progress = pp::app::format_cloud_download_progress_message(p);
|
||||
m->m_message->set_text(progress.c_str());
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user