Skip to content

Commit e494122

Browse files
committed
use QFile instead of FILE* with curl to save to downloads
1 parent 6902b46 commit e494122

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

src/Network.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,12 @@ void Downloader::run() {
138138
return;
139139
}
140140

141-
#pragma cppcheck-suppress fopen_s
142-
FILE* file = fopen(filePath.toUtf8(), "wb"); // flawfinder: ignore
143-
if (!file) {
141+
QFile file(filePath);
142+
if (!file.open(QIODevice::WriteOnly)) { // flawfinder: ignore
144143
qDebug() << "Error opening file for writing:" << filePath;
145144
return;
146145
}
147146

148-
149147
CURL* curl = curl_easy_init();
150148
if (curl) {
151149
std::string cert_buffer = get_ssl_certificate("www.trle.net");
@@ -164,11 +162,17 @@ void Downloader::run() {
164162
"sha256//7WRPcNY2QpOjWiQSLbiBu/9Og69JmzccPAdfj2RT5Vw=");
165163

166164
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,
167-
+[](const void* buf, size_t size, size_t nmemb, void* data) -> size_t {
168-
return fwrite(buf, size, nmemb, static_cast<FILE*>(data));
165+
+[](const void* buf, size_t size, size_t nmemb, void* data)
166+
-> size_t {
167+
QFile* file = static_cast<QFile*>(data);
168+
if (file->isOpen()) {
169+
return file->write(static_cast<const char*>(buf),
170+
size * nmemb);
171+
}
172+
return 0;
169173
});
170174

171-
curl_easy_setopt(curl, CURLOPT_WRITEDATA, file);
175+
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &file);
172176

173177
// Follow redirects
174178
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
@@ -227,5 +231,5 @@ void Downloader::run() {
227231
}
228232
curl_easy_cleanup(curl);
229233
}
230-
fclose(file);
234+
file.close();
231235
}

0 commit comments

Comments
 (0)