-
Notifications
You must be signed in to change notification settings - Fork 4
/
qdownloaderthread.cpp
68 lines (55 loc) · 1.66 KB
/
qdownloaderthread.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "qdownloaderthread.h"
#include <QByteArray>
#include <QFile>
#include <curl/curl.h>
#include <sstream>
#include <regex>
std::size_t write_data(char *ptr, std::size_t size, std::size_t nmemb, void *userdata)
{
for(std::size_t i = 0U; i < size * nmemb; ++i)
{
((std::vector<char>*) userdata)->push_back(*(ptr + i));
}
return size * nmemb;
}
qdownloaderthread::qdownloaderthread(QUrl url, bool saveToFile, QByteArray &data, QObject* parent)
: QThread(parent),
_url(url),
_data(&data),
_saveToFile(saveToFile)
{
}
void qdownloaderthread::run()
{
std::vector<char> stream;
CURL* curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
std::string url = _url.toString().toStdString();
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &stream);
CURLcode res = curl_easy_perform(curl);
if (res != CURLE_OK)
{
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
}
curl_easy_cleanup(curl);
if(_saveToFile)
{
QFile file(fileName());
file.open(QIODevice::WriteOnly);
file.write(&stream[0], stream.size());
file.close();
}
else
(*_data) = &stream[0];
}
QString qdownloaderthread::fileName() const
{
std::regex r("[0-9]+\\.(png|jpg|jpeg|gif|webm)", std::regex::ECMAScript);
const std::string url = _url.toString().toStdString();
auto fileName = std::sregex_iterator(url.begin(), url.end(), r);
auto str = fileName->str();
return QString::fromStdString(str);
}