mirror of
https://github.com/f4exb/sdrangel.git
synced 2025-06-17 13:52:47 -04:00
HTTPDownloadManager support Google drive and HTTP redirects
This commit is contained in:
parent
cb1067946c
commit
f026733bae
@ -20,6 +20,7 @@
|
|||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include <QFile>
|
#include <QFile>
|
||||||
#include <QDateTime>
|
#include <QDateTime>
|
||||||
|
#include <QRegExp>
|
||||||
|
|
||||||
HttpDownloadManager::HttpDownloadManager()
|
HttpDownloadManager::HttpDownloadManager()
|
||||||
{
|
{
|
||||||
@ -29,16 +30,23 @@ HttpDownloadManager::HttpDownloadManager()
|
|||||||
QNetworkReply *HttpDownloadManager::download(const QUrl &url, const QString &filename)
|
QNetworkReply *HttpDownloadManager::download(const QUrl &url, const QString &filename)
|
||||||
{
|
{
|
||||||
QNetworkRequest request(url);
|
QNetworkRequest request(url);
|
||||||
|
request.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy);
|
||||||
QNetworkReply *reply = manager.get(request);
|
QNetworkReply *reply = manager.get(request);
|
||||||
|
|
||||||
connect(reply, &QNetworkReply::sslErrors, this, &HttpDownloadManager::sslErrors);
|
connect(reply, &QNetworkReply::sslErrors, this, &HttpDownloadManager::sslErrors);
|
||||||
|
|
||||||
qDebug() << "HttpDownloadManager: Downloading from " << url << " to " << filename;
|
qDebug() << "HttpDownloadManager: Downloading from " << url << " to " << filename;
|
||||||
downloads.append(reply);
|
m_downloads.append(reply);
|
||||||
filenames.append(filename);
|
m_filenames.append(filename);
|
||||||
return reply;
|
return reply;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Indicate if we have any downloads in progress
|
||||||
|
bool HttpDownloadManager::downloading() const
|
||||||
|
{
|
||||||
|
return m_filenames.size() > 0;
|
||||||
|
}
|
||||||
|
|
||||||
qint64 HttpDownloadManager::fileAgeInDays(const QString& filename)
|
qint64 HttpDownloadManager::fileAgeInDays(const QString& filename)
|
||||||
{
|
{
|
||||||
QFile file(filename);
|
QFile file(filename);
|
||||||
@ -75,7 +83,7 @@ bool HttpDownloadManager::isHttpRedirect(QNetworkReply *reply)
|
|||||||
return (status >= 301 && status <= 308);
|
return (status >= 301 && status <= 308);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool HttpDownloadManager::writeToFile(const QString &filename, QIODevice *data)
|
bool HttpDownloadManager::writeToFile(const QString &filename, const QByteArray &data)
|
||||||
{
|
{
|
||||||
QFile file(filename);
|
QFile file(filename);
|
||||||
|
|
||||||
@ -87,7 +95,7 @@ bool HttpDownloadManager::writeToFile(const QString &filename, QIODevice *data)
|
|||||||
|
|
||||||
if (file.open(QIODevice::WriteOnly))
|
if (file.open(QIODevice::WriteOnly))
|
||||||
{
|
{
|
||||||
file.write(data->readAll());
|
file.write(data);
|
||||||
file.close();
|
file.close();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -101,15 +109,48 @@ bool HttpDownloadManager::writeToFile(const QString &filename, QIODevice *data)
|
|||||||
void HttpDownloadManager::downloadFinished(QNetworkReply *reply)
|
void HttpDownloadManager::downloadFinished(QNetworkReply *reply)
|
||||||
{
|
{
|
||||||
QString url = reply->url().toEncoded().constData();
|
QString url = reply->url().toEncoded().constData();
|
||||||
int idx = downloads.indexOf(reply);
|
int idx = m_downloads.indexOf(reply);
|
||||||
QString filename = filenames[idx];
|
QString filename = m_filenames[idx];
|
||||||
bool success = false;
|
bool success = false;
|
||||||
|
bool retry = false;
|
||||||
|
|
||||||
if (!reply->error())
|
if (!reply->error())
|
||||||
{
|
{
|
||||||
if (!isHttpRedirect(reply))
|
if (!isHttpRedirect(reply))
|
||||||
{
|
{
|
||||||
if (writeToFile(filename, reply))
|
QByteArray data = reply->readAll();
|
||||||
|
QRegExp regexp("href=\\\"\\/uc\\?export\\=download\\&\\;confirm=([a-zA-Z0-9_\\-]*)\\&\\;id=([a-zA-Z0-9_\\-\\=\\;\\&]*)\\\"");
|
||||||
|
|
||||||
|
// Google drive can redirect downloads to a virus scan warning page
|
||||||
|
// We need to extract the confirm code and retry
|
||||||
|
if (url.startsWith("https://drive.google.com/uc?export=download")
|
||||||
|
&& data.startsWith("<!DOCTYPE html>")
|
||||||
|
&& !filename.endsWith(".html")
|
||||||
|
&& (regexp.indexIn(data) >= 0)
|
||||||
|
)
|
||||||
|
{
|
||||||
|
QString confirm = regexp.capturedTexts()[1];
|
||||||
|
QString id = regexp.capturedTexts()[2];
|
||||||
|
if (confirm.isEmpty())
|
||||||
|
{
|
||||||
|
qDebug() << "HttpDownloadManager::downloadFinished - Got HTML response but not confirmation code";
|
||||||
|
qDebug() << QString(data);
|
||||||
|
qDebug() << regexp.capturedTexts();
|
||||||
|
}
|
||||||
|
|
||||||
|
m_downloads.removeAll(reply);
|
||||||
|
m_filenames.remove(idx);
|
||||||
|
|
||||||
|
qDebug() << "HttpDownloadManager: Skipping Google drive warning: " << confirm << " " << id;
|
||||||
|
QUrl newUrl(QString("https://drive.google.com/uc?export=download&confirm=%1&id=%2").arg(confirm).arg(id));
|
||||||
|
QNetworkReply *newReply = download(newUrl, filename);
|
||||||
|
|
||||||
|
// Indicate that we are retrying, so progress dialogs can be updated
|
||||||
|
emit retryDownload(filename, reply, newReply);
|
||||||
|
|
||||||
|
retry = true;
|
||||||
|
}
|
||||||
|
else if (writeToFile(filename, data))
|
||||||
{
|
{
|
||||||
success = true;
|
success = true;
|
||||||
qDebug() << "HttpDownloadManager: Download from " << url << " to " << filename << " finshed.";
|
qDebug() << "HttpDownloadManager: Download from " << url << " to " << filename << " finshed.";
|
||||||
@ -125,8 +166,11 @@ void HttpDownloadManager::downloadFinished(QNetworkReply *reply)
|
|||||||
qCritical() << "HttpDownloadManager: Download of " << url << " failed: " << reply->errorString();
|
qCritical() << "HttpDownloadManager: Download of " << url << " failed: " << reply->errorString();
|
||||||
}
|
}
|
||||||
|
|
||||||
downloads.removeAll(reply);
|
if (!retry)
|
||||||
filenames.remove(idx);
|
{
|
||||||
reply->deleteLater();
|
m_downloads.removeAll(reply);
|
||||||
emit downloadComplete(filename, success);
|
m_filenames.remove(idx);
|
||||||
|
emit downloadComplete(filename, success, url, reply->errorString());
|
||||||
|
}
|
||||||
|
reply->deleteLater();
|
||||||
}
|
}
|
||||||
|
@ -32,6 +32,7 @@ class SDRBASE_API HttpDownloadManager : public QObject
|
|||||||
public:
|
public:
|
||||||
HttpDownloadManager();
|
HttpDownloadManager();
|
||||||
QNetworkReply *download(const QUrl &url, const QString &filename);
|
QNetworkReply *download(const QUrl &url, const QString &filename);
|
||||||
|
bool downloading() const;
|
||||||
|
|
||||||
static QString downloadDir();
|
static QString downloadDir();
|
||||||
|
|
||||||
@ -40,10 +41,10 @@ protected:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
QNetworkAccessManager manager;
|
QNetworkAccessManager manager;
|
||||||
QVector<QNetworkReply *> downloads;
|
QVector<QNetworkReply *> m_downloads;
|
||||||
QVector<QString> filenames;
|
QVector<QString> m_filenames;
|
||||||
|
|
||||||
static bool writeToFile(const QString &filename, QIODevice *data);
|
static bool writeToFile(const QString &filename, const QByteArray &data);
|
||||||
static bool isHttpRedirect(QNetworkReply *reply);
|
static bool isHttpRedirect(QNetworkReply *reply);
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
@ -51,7 +52,8 @@ public slots:
|
|||||||
void sslErrors(const QList<QSslError> &errors);
|
void sslErrors(const QList<QSslError> &errors);
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void downloadComplete(const QString &filename, bool success);
|
void downloadComplete(const QString &filename, bool success, const QString &url, const QString &errorMessage);
|
||||||
|
void retryDownload(const QString &filename, QNetworkReply *oldReply, QNetworkReply *newReply);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user