mirror of
https://gitee.com/spark-store-project/spark-store
synced 2025-09-23 03:22:20 +08:00
切换多域名下载,提高下载速度
This commit is contained in:
parent
2f8c11a30b
commit
9cc68fac86
@ -6,6 +6,7 @@
|
|||||||
#include <QNetworkReply>
|
#include <QNetworkReply>
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include <QThread>
|
#include <QThread>
|
||||||
|
#include <QRegularExpression>
|
||||||
|
|
||||||
DownloadWorker::DownloadWorker(QObject *parent)
|
DownloadWorker::DownloadWorker(QObject *parent)
|
||||||
{
|
{
|
||||||
@ -87,7 +88,14 @@ void DownloadWorker::handleProcess(qint64, qint64)
|
|||||||
|
|
||||||
DownloadController::DownloadController(QObject *parent)
|
DownloadController::DownloadController(QObject *parent)
|
||||||
{
|
{
|
||||||
this->threadNum = QThread::idealThreadCount() > 4 ? 4 : QThread::idealThreadCount();
|
domains = {
|
||||||
|
"sucdn1.jerrywang.top",
|
||||||
|
"sucdn2.jerrywang.top",
|
||||||
|
"sucdn3.jerrywang.top",
|
||||||
|
"sucdn4.jerrywang.top",
|
||||||
|
"sucdn5.jerrywang.top"
|
||||||
|
};
|
||||||
|
this->threadNum = domains.size() > 6 ? 6 : domains.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
DownloadController::~DownloadController()
|
DownloadController::~DownloadController()
|
||||||
@ -151,8 +159,9 @@ void DownloadController::startDownload(const QString &url)
|
|||||||
qDebug() << QString("第%1个下载请求:%2-%3").arg(i).arg(ranges.at(i).first).arg(ranges.at(i).second);
|
qDebug() << QString("第%1个下载请求:%2-%3").arg(i).arg(ranges.at(i).first).arg(ranges.at(i).second);
|
||||||
auto worker = new DownloadWorker(this);
|
auto worker = new DownloadWorker(this);
|
||||||
auto range = ranges.at(i);
|
auto range = ranges.at(i);
|
||||||
|
QString chunkUrl = replaceDomain(url, domains.at(i));
|
||||||
worker->setIdentifier(i);
|
worker->setIdentifier(i);
|
||||||
worker->setParamter(url, range, file);
|
worker->setParamter(chunkUrl, range, file);
|
||||||
workers.append(worker);
|
workers.append(worker);
|
||||||
connect(worker, &DownloadWorker::downloadProcess, this, &DownloadController::handleProcess);
|
connect(worker, &DownloadWorker::downloadProcess, this, &DownloadController::handleProcess);
|
||||||
connect(worker, &DownloadWorker::workFinished, this, &DownloadController::chunkDownloadFinish);
|
connect(worker, &DownloadWorker::workFinished, this, &DownloadController::chunkDownloadFinish);
|
||||||
@ -232,3 +241,12 @@ qint64 DownloadController::getFileSize(const QString& url)
|
|||||||
return fileSize;
|
return fileSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QString DownloadController::replaceDomain(const QString& url, const QString domain)
|
||||||
|
{
|
||||||
|
QRegularExpression regex(R"((?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9])");
|
||||||
|
if (regex.match(url).hasMatch()) {
|
||||||
|
return QString(url).replace(regex.match(url).captured(), domain);
|
||||||
|
}
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
|
||||||
|
@ -50,6 +50,7 @@ public:
|
|||||||
void startDownload(const QString &url);
|
void startDownload(const QString &url);
|
||||||
void stopDownload();
|
void stopDownload();
|
||||||
qint64 getFileSize(const QString& url);
|
qint64 getFileSize(const QString& url);
|
||||||
|
QString replaceDomain(const QString& url, const QString domain);
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void handleProcess();
|
void handleProcess();
|
||||||
@ -68,6 +69,7 @@ private:
|
|||||||
QFile *file;
|
QFile *file;
|
||||||
QList<DownloadWorker*> workers;
|
QList<DownloadWorker*> workers;
|
||||||
int finish = 0;
|
int finish = 0;
|
||||||
|
QVector<QString> domains;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // FILEDOWNLOADWORKER_H
|
#endif // FILEDOWNLOADWORKER_H
|
||||||
|
Loading…
x
Reference in New Issue
Block a user