198{
199 boost::filesystem::path dest_path;
200 size_t last_gui_progress = 0;
201 size_t expected_size =
data.size;
202 dest_path =
data.target_path;
203 assert(!dest_path.empty());
204 if (dest_path.empty())
205 {
207 std::string line2 =
_u8L(
"Destination path is empty.");
208 std::string message =
GUI::format(
"%1%\n%2%", line1, line2);
209 BOOST_LOG_TRIVIAL(
error) << message;
210 wxCommandEvent* evt = new wxCommandEvent(EVT_SLIC3R_APP_DOWNLOAD_FAILED);
211 evt->SetString(message);
212 GUI::wxGetApp().QueueEvent(evt);
213 return boost::filesystem::path();
214 }
215
216 boost::filesystem::path tmp_path = dest_path;
218 FILE* file;
219 wxString temp_path_wstring(tmp_path.wstring());
220 file = fopen(temp_path_wstring.c_str(), "wb");
221 assert(file != NULL);
222 if (file == NULL) {
224 std::string line2 =
GUI::format(
_u8L(
"Can't create file at %1%"), tmp_path.string());
225 std::string message =
GUI::format(
"%1%\n%2%", line1, line2);
226 BOOST_LOG_TRIVIAL(
error) << message;
227 wxCommandEvent* evt = new wxCommandEvent(EVT_SLIC3R_APP_DOWNLOAD_FAILED);
228 evt->SetString(message);
229 GUI::wxGetApp().QueueEvent(evt);
230 return boost::filesystem::path();
231 }
232
233 std::string error_message;
235
236 , [&last_gui_progress, expected_size](Http::Progress progress) {
237
238 if (progress.dltotal > 0 && progress.dltotal > expected_size) {
239 std::string message = GUI::format("Downloading new %1% has failed. The file has incorrect file size. Aborting download.\nExpected size: %2%\nDownload size: %3%", SLIC3R_APP_NAME, expected_size, progress.dltotal);
240 BOOST_LOG_TRIVIAL(error) << message;
241 wxCommandEvent* evt = new wxCommandEvent(EVT_SLIC3R_APP_DOWNLOAD_FAILED);
242 evt->SetString(message);
243 GUI::wxGetApp().QueueEvent(evt);
244 return false;
245 }
else if (progress.dltotal > 0 && progress.dltotal < expected_size) {
246
247 BOOST_LOG_TRIVIAL(info) << GUI::format("Downloading new %1% has incorrect size. The download will continue. \nExpected size: %2%\nDownload size: %3%", SLIC3R_APP_NAME, expected_size, progress.dltotal);
248 }
249
250 size_t gui_progress = progress.dltotal > 0 ? 100 * progress.dlnow / progress.dltotal : 0;
251 BOOST_LOG_TRIVIAL(debug) << "App download " << gui_progress << "% " << progress.dlnow << " of " << progress.dltotal;
252 if (last_gui_progress < gui_progress && (last_gui_progress != 0 || gui_progress != 100)) {
253 last_gui_progress = gui_progress;
254 wxCommandEvent* evt = new wxCommandEvent(EVT_SLIC3R_APP_DOWNLOAD_PROGRESS);
255 evt->SetString(
GUI::from_u8(std::to_string(gui_progress)));
256 GUI::wxGetApp().QueueEvent(evt);
257 }
258 return true;
259 }
260
261 , [&file, dest_path, tmp_path, expected_size](std::string body, std::string& error_message){
262
263 size_t body_size = body.size();
264 if (body_size != expected_size) {
265 error_message =
GUI::format(
_u8L(
"Downloaded file has wrong size. Expected size: %1% Downloaded size: %2%"), expected_size, body_size);
266 return false;
267 }
268 if (file == NULL) {
269 error_message =
GUI::format(
_u8L(
"Can't create file at %1%"), tmp_path.string());
270 return false;
271 }
272 try
273 {
274 fwrite(body.c_str(), 1, body.size(), file);
275 fclose(file);
276 boost::filesystem::rename(tmp_path, dest_path);
277 }
278 catch (const std::exception& e)
279 {
280 error_message =
GUI::format(
_u8L(
"Failed to write to file or to move %1% to %2%:\n%3%"), tmp_path, dest_path, e.what());
281 return false;
282 }
283 return true;
284 }
285 , error_message
286 );
287 if (!res)
288 {
290 BOOST_LOG_TRIVIAL(info) << error_message;
291 wxCommandEvent* evt = new wxCommandEvent(EVT_SLIC3R_APP_DOWNLOAD_FAILED);
292 GUI::wxGetApp().QueueEvent(evt);
293 } else {
294 std::string message = (error_message.empty()
295 ? std::string()
296 : GUI::
format(
_u8L(
"Downloading new %1% has failed:\n%2%"), SLIC3R_APP_NAME, error_message));
297 wxCommandEvent* evt = new wxCommandEvent(EVT_SLIC3R_APP_DOWNLOAD_FAILED);
298 if (!message.empty()) {
299 BOOST_LOG_TRIVIAL(
error) << message;
300 evt->SetString(message);
301 }
302 GUI::wxGetApp().QueueEvent(evt);
303 }
304 return boost::filesystem::path();
305 }
306
307 return dest_path;
308}
#define _u8L(s)
macro used to mark string used at localization, return same string
Definition SLAPrint.cpp:29
static int64_t timestamp_now()
Definition GLCanvas3D.hpp:967
if(!(yy_init))
Definition lexer.c:1190
std::string format(const char *fmt, TArgs &&... args)
Definition format.hpp:56
wxString from_u8(const std::string &str)
Definition GUI.cpp:437
std::string format(const char *fmt, TArgs &&... args)
Definition format.hpp:44
constexpr auto data(C &c) -> decltype(c.data())
Definition span.hpp:195
bool http_get_file(const std::string &url, size_t size_limit, std::function< bool(Http::Progress)> progress_fn, std::function< bool(std::string, std::string &error_message)> completefn, std::string &error_message) const
Definition AppUpdater.cpp:167
static char error[256]
Definition tga.cpp:50