@@ -74,62 +74,25 @@ std::string get_ssl_certificate(const std::string& host) {
7474 return cert_buffer;
7575}
7676
77- struct WriteData {
78- Downloader* downloader;
79- FILE* file;
80- };
81-
8277bool Downloader::setUpCamp (const QString& levelDir) {
8378 QFileInfo levelPathInfo (levelDir);
8479 if (levelPathInfo.isDir ()) {
85- levelDir_m .setPath (levelDir);
80+ m_levelDir .setPath (levelDir);
8681 return true ;
8782 }
8883 return false ;
8984}
9085
91- size_t Downloader::write_callback (
92- void * contents,
93- size_t size,
94- size_t nmemb,
95- void * userData) {
96- WriteData* data = reinterpret_cast <WriteData*>(userData);
97- return fwrite (contents, size, nmemb, data->file );
98- }
99-
100- int Downloader::progress_callback (
101- void * clientp,
102- curl_off_t dltotal,
103- curl_off_t dlnow,
104- curl_off_t ultotal,
105- curl_off_t ulnow) {
106- if (dltotal > 0 ) {
107- double progress = static_cast <double >(dlnow)
108- / static_cast <double >(dltotal) * 50.0 ;
109-
110- // Emit signal only if progress has increased by at least 1%
111- static int lastEmittedProgress = 0 ;
112- if (static_cast <int >(progress) == 0 ) lastEmittedProgress = 0 ;
113- if (static_cast <int >(progress) > lastEmittedProgress) {
114- static Downloader& instance = Downloader::getInstance ();
115- emit instance.networkWorkTickSignal ();
116- QCoreApplication::processEvents ();
117- lastEmittedProgress = static_cast <int >(progress);
118- }
119- }
120- return 0 ;
121- }
122-
12386void Downloader::setUrl (QUrl url) {
124- url_m = url;
87+ m_url = url;
12588}
12689
12790void Downloader::setSaveFile (const QString& file) {
128- file_m = file;
91+ m_file = file;
12992}
13093
13194int Downloader::getStatus () {
132- return status_m ;
95+ return m_status ;
13396}
13497
13598void Downloader::saveToFile (const QByteArray& data, const QString& filePath) {
@@ -152,15 +115,21 @@ void Downloader::saveToFile(const QByteArray& data, const QString& filePath) {
152115}
153116
154117void Downloader::run () {
155- if (url_m .isEmpty () || file_m .isEmpty () || levelDir_m .isEmpty ())
118+ if (m_url .isEmpty () || m_file .isEmpty () || m_levelDir .isEmpty ())
156119 return ;
157120
158- QString urlString = url_m.toString ();
121+ qDebug () << " m_url: " << m_url.toString ();
122+ qDebug () << " m_file: " << m_file;
123+ qDebug () << " m_levelDir: " << m_levelDir.absolutePath ();
124+
125+ QString urlString = m_url.toString ();
159126 QByteArray byteArray = urlString.toUtf8 ();
160127 const char * url_cstring = byteArray.constData ();
161128
162- const QString filePath = levelDir_m.absolutePath () +
163- QDir::separator () + file_m;
129+ const QString filePath = QString (" %1%2%3" )
130+ .arg (m_levelDir.absolutePath (), QDir::separator (), m_file);
131+
132+ qDebug () << " filePath: " << filePath;
164133
165134 QFileInfo fileInfo (filePath);
166135
@@ -169,13 +138,13 @@ void Downloader::run() {
169138 return ;
170139 }
171140
141+ #pragma cppcheck-suppress fopen_s
172142 FILE* file = fopen (filePath.toUtf8 (), " wb" ); // flawfinder: ignore
173143 if (!file) {
174144 qDebug () << " Error opening file for writing:" << filePath;
175145 return ;
176146 }
177147
178- WriteData writeData = { this , file };
179148
180149 CURL* curl = curl_easy_init ();
181150 if (curl) {
@@ -189,26 +158,55 @@ void Downloader::run() {
189158
190159 curl_easy_setopt (curl, CURLOPT_URL, url_cstring);
191160 curl_easy_setopt (curl, CURLOPT_CAINFO_BLOB, &blob);
161+
162+ curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, 1L );
163+ curl_easy_setopt (curl, CURLOPT_PINNEDPUBLICKEY,
164+ " sha256//7WRPcNY2QpOjWiQSLbiBu/9Og69JmzccPAdfj2RT5Vw=" );
165+
192166 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION,
193- Downloader::write_callback);
167+ +[](const void * buf, size_t size, size_t nmemb, void * data) -> size_t {
168+ return fwrite (buf, size, nmemb, static_cast <FILE*>(data));
169+ });
194170
195- curl_easy_setopt (curl, CURLOPT_WRITEDATA, &writeData );
171+ curl_easy_setopt (curl, CURLOPT_WRITEDATA, file );
196172
197173 // Follow redirects
198174 curl_easy_setopt (curl, CURLOPT_FOLLOWLOCATION, 1L );
199175
200176 // Enable progress meter
201177 curl_easy_setopt (curl, CURLOPT_NOPROGRESS, 0L );
178+
202179 curl_easy_setopt (curl, CURLOPT_XFERINFOFUNCTION,
203- Downloader::progress_callback);
180+ +[](void * clientp, curl_off_t dltotal, curl_off_t dlnow,
181+ curl_off_t ultotal, curl_off_t ulnow) -> int {
182+ Downloader& downloader = Downloader::getInstance ();
183+
184+ if (dltotal > 0 ) {
185+ int & lastEmittedProgress =
186+ downloader.m_lastEmittedProgress ;
187+
188+ double progress = static_cast <double >(dlnow)
189+ / static_cast <double >(dltotal) * 50.0 ;
190+
191+ if (static_cast <int >(progress) == 0 ) {
192+ lastEmittedProgress = 0 ;
193+ }
194+ if (static_cast <int >(progress) > lastEmittedProgress) {
195+ emit downloader.networkWorkTickSignal ();
196+ QCoreApplication::processEvents ();
197+ lastEmittedProgress = static_cast <int >(progress);
198+ }
199+ }
200+ return 0 ;
201+ });
204202
205203 curl_easy_setopt (curl, CURLOPT_XFERINFODATA, nullptr );
206204
207205 // Perform the download
208206 CURLcode res = curl_easy_perform (curl);
209207
210208 if (res != CURLE_OK) {
211- status_m = 1 ;
209+ m_status = 1 ;
212210 qDebug () << " CURL failed:" << curl_easy_strerror (res);
213211 // we need to catch any of those that seem inportant here to the GUI
214212 // and reset GUI state
@@ -224,7 +222,7 @@ void Downloader::run() {
224222 QCoreApplication::processEvents ();
225223 }
226224 } else {
227- status_m = 0 ;
225+ m_status = 0 ;
228226 qDebug () << " Downloaded successfully" ;
229227 }
230228 curl_easy_cleanup (curl);
0 commit comments