The Battle for Wesnoth  1.17.17+dev
network_download_file.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2023
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
16 
17 #include "filesystem.hpp"
18 #include "gettext.hpp"
19 #include "gui/dialogs/message.hpp"
20 #include "log.hpp"
21 
22 #include <curl/curl.h>
23 
24 static lg::log_domain log_network("network");
25 #define ERR_NW LOG_STREAM(err, log_network)
26 #define DBG_NW LOG_STREAM(debug, log_network)
27 
28 namespace network
29 {
30  static size_t write_callback(char* contents, size_t size, size_t nmemb, void* buffer)
31  {
32  size_t amount = size * nmemb;
33  static_cast<std::string*>(buffer)->append(contents, amount);
34  DBG_NW << "Downloaded " << amount << " bytes.";
35  return amount;
36  }
37 
38  void download(const std::string& url, const std::string& local_path)
39  {
40  CURL* curl = curl_easy_init();
41  std::string buffer;
42  // curl doesn't initialize the error buffer until version 7.60.0, which isn't currently available on all supported macOS versions
43  char error[CURL_ERROR_SIZE];
44  std::fill_n(error, CURL_ERROR_SIZE-1, ' ');
45  error[CURL_ERROR_SIZE-1] = '\0';
46 
47  if(curl) {
48  curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
49  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
50  curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
51  curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error);
52  curl_easy_setopt(curl, CURLOPT_FORBID_REUSE, 1L);
53  curl_easy_setopt(curl, CURLOPT_FRESH_CONNECT, 1L);
54  curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L);
55  curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, 5000L);
56  curl_easy_setopt(curl, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);
57 
58  CURLcode res = curl_easy_perform(curl);
59 
60  if(res != CURLE_OK) {
61  ERR_NW << "Error downloading file from url `" << url << "`.\n"
62  << "Short error: " << curl_easy_strerror(res) << "\n"
63  << "Long error: " << std::string(error);
64  gui2::show_message(_("Download error"), _("An error occurred when downloading the file. Check the game logs for more information."), gui2::dialogs::message::button_style::auto_close);
65  } else {
66  try {
67  if(filesystem::file_exists(local_path)) {
68  const int res = gui2::show_message(_("Confirm overwrite"), _("Overwrite existing file?"), gui2::dialogs::message::yes_no_buttons);
69  if(res == gui2::retval::OK) {
70  filesystem::write_file(local_path, buffer);
71  }
72  } else {
73  filesystem::write_file(local_path, buffer);
74  }
75  DBG_NW << "Wrote downloaded file to: " << local_path;
76  } catch(const filesystem::io_exception& e) {
77  ERR_NW << "io_exception writing downloaded data to file at: " << local_path
78  << "\n" << e.what() << " : " << e.message;
79  gui2::show_message(_("Download error"), _("An error occurred when downloading the file. Check the game logs for more information."), gui2::dialogs::message::button_style::auto_close);
80  }
81  gui2::show_message(_("Download complete"), _("The file has been downloaded."), gui2::dialogs::message::button_style::auto_close);
82  }
83 
84  curl_easy_cleanup(curl);
85  } else {
86  ERR_NW << "curl_easy_init failed initialization, unable to download file.";
87  gui2::show_message(_("Download error"), _("An error occurred when downloading the file. Check the game logs for more information."), gui2::dialogs::message::button_style::auto_close);
88  }
89  }
90 }
@ yes_no_buttons
Shows a yes and no button.
Definition: message.hpp:81
Declarations for File-IO.
static std::string _(const char *str)
Definition: gettext.hpp:93
Standard logging facilities (interface).
static bool file_exists(const bfs::path &fpath)
Definition: filesystem.cpp:320
void write_file(const std::string &fname, const std::string &data, std::ios_base::openmode mode)
Throws io_exception if an error occurs.
void show_message(const std::string &title, const std::string &msg, const std::string &button_caption, const bool auto_close, const bool message_use_markup, const bool title_use_markup)
Shows a message to the user.
Definition: message.cpp:151
@ OK
Dialog was closed with the OK button.
Definition: retval.hpp:35
High level network layer for config object transport.
static size_t write_callback(char *contents, size_t size, size_t nmemb, void *buffer)
void download(const std::string &url, const std::string &local_path)
Initiates a standalone download of a single file from an HTTPS URL.
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:87
#define ERR_NW
static lg::log_domain log_network("network")
#define DBG_NW
An exception object used when an IO error occurs.
Definition: filesystem.hpp:65
#define e