ENH:importing to studio will retry three times

jira:[STUDIO-4223]

Change-Id: If66faa4a21a414d43860ef8aa657562f5ee617b8
(cherry picked from commit 59ca516ba5b8ada15cbb087a814f59f8e3683a51)
This commit is contained in:
tao wang 2023-09-05 12:22:20 +08:00 committed by Lane.Wei
parent fab4045723
commit 7aba8e5e88

View file

@ -8011,7 +8011,8 @@ void Plater::import_model_id(wxString download_info)
} }
bool download_ok = false; bool download_ok = false;
/* save to a file */ int retry_count = 0;
const int max_retries = 3;
/* jump to 3D eidtor */ /* jump to 3D eidtor */
wxGetApp().mainframe->select_tab((size_t)MainFrame::TabPosition::tp3DEditor); wxGetApp().mainframe->select_tab((size_t)MainFrame::TabPosition::tp3DEditor);
@ -8039,7 +8040,7 @@ void Plater::import_model_id(wxString download_info)
p->project.reset(); p->project.reset();
/* prepare project and profile */ /* prepare project and profile */
boost::thread import_thread = Slic3r::create_thread([&percent, &cont, &cancel, &msg, &target_path, &download_ok, download_url, &filename] { boost::thread import_thread = Slic3r::create_thread([&percent, &cont, &cancel, &retry_count, max_retries, &msg, &target_path, &download_ok, download_url, &filename] {
NetworkAgent* m_agent = Slic3r::GUI::wxGetApp().getAgent(); NetworkAgent* m_agent = Slic3r::GUI::wxGetApp().getAgent();
if (!m_agent) return; if (!m_agent) return;
@ -8110,36 +8111,42 @@ void Plater::import_model_id(wxString download_info)
auto url = download_url; auto url = download_url;
auto http = Http::get(url); auto http = Http::get(url);
http.on_progress([&percent, &cont, &msg](Http::Progress progress, bool& cancel) {
if (!cont) cancel = true; while (cont && retry_count < max_retries) {
if (progress.dltotal != 0) { retry_count++;
percent = progress.dlnow * 100 / progress.dltotal; http.on_progress([&percent, &cont, &msg](Http::Progress progress, bool& cancel) {
} if (!cont) cancel = true;
msg = wxString::Format(_L("Project downloaded %d%%"), percent); if (progress.dltotal != 0) {
}) percent = progress.dlnow * 100 / progress.dltotal;
.on_error([&msg, &cont](std::string body, std::string error, unsigned http_status) { }
(void)body; msg = wxString::Format(_L("Project downloaded %d%%"), percent);
BOOST_LOG_TRIVIAL(error) << format("Error getting: `%1%`: HTTP %2%, %3%", })
body, .on_error([&msg, &cont, &retry_count, max_retries](std::string body, std::string error, unsigned http_status) {
http_status, (void)body;
error); BOOST_LOG_TRIVIAL(error) << format("Error getting: `%1%`: HTTP %2%, %3%",
msg = wxString::Format("Download Failed! body=%s, error=%s, status=%d", body, error, http_status); body,
cont = false; http_status,
return; error);
if (retry_count == max_retries) {
msg = _L("Importing to Bambu Studio failed. Please download the file and manually import it.");
cont = false;
}
}) })
.on_complete([&cont, &download_ok, tmp_path, target_path](std::string body, unsigned /* http_status */) { .on_complete([&cont, &download_ok, tmp_path, target_path](std::string body, unsigned /* http_status */) {
fs::fstream file(tmp_path, std::ios::out | std::ios::binary | std::ios::trunc); fs::fstream file(tmp_path, std::ios::out | std::ios::binary | std::ios::trunc);
file.write(body.c_str(), body.size()); file.write(body.c_str(), body.size());
file.close(); file.close();
fs::rename(tmp_path, target_path); fs::rename(tmp_path, target_path);
cont = false; cont = false;
download_ok = true; download_ok = true;
}) }).perform_sync();
.perform_sync();
// for break while // for break while
cont = false; //cont = false;
}); }
});
while (cont && cont_dlg) { while (cont && cont_dlg) {
wxMilliSleep(50); wxMilliSleep(50);
@ -8176,7 +8183,11 @@ void Plater::import_model_id(wxString download_info)
p->notification_manager->push_import_finished_notification(target_path.string(), target_path.parent_path().string(), false); p->notification_manager->push_import_finished_notification(target_path.string(), target_path.parent_path().string(), false);
} }
else { else {
if (!msg.empty()) wxMessageBox(msg); if (!msg.empty()) {
MessageDialog msg_wingow(nullptr, msg, wxEmptyString, wxICON_WARNING | wxOK);
msg_wingow.SetSize(wxSize(FromDIP(480), -1));
msg_wingow.ShowModal();
}
return; return;
} }
} }