2019-10-31 17:45:17 +00:00
|
|
|
|
#include "downloader.h"
|
|
|
|
|
#include "record_format.h"
|
|
|
|
|
|
2019-10-31 19:33:18 +00:00
|
|
|
|
#include <QEventLoop>
|
2019-10-31 17:45:17 +00:00
|
|
|
|
#include <QNetworkReply>
|
|
|
|
|
#include <qelapsedtimer.h>
|
|
|
|
|
|
2019-11-01 18:12:48 +00:00
|
|
|
|
Downloader::Downloader() : QObject () {
|
|
|
|
|
m_data = nullptr;
|
2019-10-31 17:45:17 +00:00
|
|
|
|
m_reply = nullptr;
|
2019-11-01 18:12:48 +00:00
|
|
|
|
m_result = false;
|
2019-10-31 17:45:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Downloader::~Downloader() {
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-01 18:12:48 +00:00
|
|
|
|
bool Downloader::request(const QString& url, const QString& sid, const QString& filename) {
|
|
|
|
|
return _request(url, sid, filename, nullptr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Downloader::request(const QString& url, const QString& sid, QByteArray* data) {
|
|
|
|
|
QString fname;
|
|
|
|
|
return _request(url, sid, fname, data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Downloader::_request(const QString& url, const QString& sid, const QString& filename, QByteArray* data) {
|
|
|
|
|
if(filename.isEmpty() && data == nullptr)
|
|
|
|
|
return false;
|
|
|
|
|
if(!filename.isEmpty() && data != nullptr)
|
|
|
|
|
return false;
|
|
|
|
|
m_data = data;
|
2019-10-31 17:45:17 +00:00
|
|
|
|
|
2019-11-01 18:12:48 +00:00
|
|
|
|
if(!filename.isEmpty()) {
|
|
|
|
|
m_file.setFileName(filename);
|
2019-10-31 17:45:17 +00:00
|
|
|
|
if(!m_file.open(QIODevice::WriteOnly | QFile::Truncate)){
|
|
|
|
|
qDebug("open file for write failed.");
|
2019-11-01 18:12:48 +00:00
|
|
|
|
return false;
|
2019-10-31 17:45:17 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString cookie = QString("_sid=%1\r\n").arg(sid);
|
|
|
|
|
|
|
|
|
|
QNetworkRequest req;
|
|
|
|
|
req.setUrl(QUrl(url));
|
|
|
|
|
req.setRawHeader("Cookie", cookie.toLatin1());
|
|
|
|
|
|
2019-11-01 18:12:48 +00:00
|
|
|
|
QNetworkAccessManager* nam = new QNetworkAccessManager();
|
|
|
|
|
QEventLoop eloop;
|
2019-10-31 17:45:17 +00:00
|
|
|
|
m_reply = nam->get(req);
|
2019-11-01 18:12:48 +00:00
|
|
|
|
|
|
|
|
|
connect(m_reply, &QNetworkReply::finished, &eloop, &QEventLoop::quit);
|
2019-10-31 17:45:17 +00:00
|
|
|
|
connect(m_reply, &QNetworkReply::finished, this, &Downloader::_on_finished);
|
|
|
|
|
connect(m_reply, &QIODevice::readyRead, this, &Downloader::_on_data_ready);
|
2019-10-31 19:33:18 +00:00
|
|
|
|
|
2019-11-01 18:12:48 +00:00
|
|
|
|
// qDebug("before eventLoop.exec(%p)", &eloop);
|
|
|
|
|
eloop.exec();
|
|
|
|
|
// qDebug("after eventLoop.exec()");
|
2019-10-31 19:33:18 +00:00
|
|
|
|
|
2019-11-01 18:12:48 +00:00
|
|
|
|
disconnect(m_reply, &QNetworkReply::finished, &eloop, &QEventLoop::quit);
|
2019-10-31 19:33:18 +00:00
|
|
|
|
disconnect(m_reply, &QNetworkReply::finished, this, &Downloader::_on_finished);
|
|
|
|
|
disconnect(m_reply, &QIODevice::readyRead, this, &Downloader::_on_data_ready);
|
2019-11-01 18:12:48 +00:00
|
|
|
|
|
2019-10-31 19:33:18 +00:00
|
|
|
|
delete m_reply;
|
|
|
|
|
m_reply = nullptr;
|
2019-11-01 18:12:48 +00:00
|
|
|
|
delete nam;
|
|
|
|
|
|
|
|
|
|
qDebug("Downloader::_request() end.");
|
|
|
|
|
return m_result;
|
2019-10-31 17:45:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Downloader::_on_data_ready() {
|
2019-10-31 19:33:18 +00:00
|
|
|
|
// qDebug("Downloader::_on_data_ready(%p).", this);
|
2019-10-31 17:45:17 +00:00
|
|
|
|
QNetworkReply *reply = reinterpret_cast<QNetworkReply*>(sender());
|
|
|
|
|
|
2019-11-01 18:12:48 +00:00
|
|
|
|
if(m_data != nullptr) {
|
|
|
|
|
m_data->push_back(reply->readAll());
|
2019-10-31 17:45:17 +00:00
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
m_file.write(reply->readAll());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Downloader::abort() {
|
2019-10-31 19:33:18 +00:00
|
|
|
|
if(m_reply) {
|
2019-11-01 18:12:48 +00:00
|
|
|
|
qDebug("Downloader::abort().");
|
2019-10-31 17:45:17 +00:00
|
|
|
|
m_reply->abort();
|
2019-10-31 19:33:18 +00:00
|
|
|
|
}
|
2019-10-31 17:45:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Downloader::_on_finished() {
|
2019-10-31 19:33:18 +00:00
|
|
|
|
// qDebug("Downloader::_on_finished(%p).", this);
|
2019-10-31 17:45:17 +00:00
|
|
|
|
QNetworkReply *reply = reinterpret_cast<QNetworkReply*>(sender());
|
|
|
|
|
|
|
|
|
|
QVariant statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
|
|
|
|
|
|
|
|
|
|
if (reply->error() != QNetworkReply::NoError) {
|
|
|
|
|
// reply->abort() got "Operation canceled"
|
|
|
|
|
//QString strError = reply->errorString();
|
2019-11-01 18:12:48 +00:00
|
|
|
|
qDebug() << "ERROR:" << reply->errorString();
|
|
|
|
|
if(m_data == nullptr) {
|
|
|
|
|
m_file.flush();
|
|
|
|
|
m_file.close();
|
|
|
|
|
}
|
|
|
|
|
m_result = false;
|
2019-10-31 17:45:17 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-01 18:12:48 +00:00
|
|
|
|
if(m_data != nullptr) {
|
|
|
|
|
m_data->push_back(reply->readAll());
|
2019-10-31 17:45:17 +00:00
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
m_file.write(reply->readAll());
|
|
|
|
|
m_file.flush();
|
|
|
|
|
m_file.close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
reply->deleteLater();
|
|
|
|
|
|
2019-11-01 18:12:48 +00:00
|
|
|
|
m_result = true;
|
2019-10-31 17:45:17 +00:00
|
|
|
|
}
|