mirror of
https://gitee.com/spark-store-project/spark-store
synced 2025-09-02 09:22:26 +08:00
!54 download: 在有cdn工作前提下,不使用主域名
* download: 在有cdn工作前提下,不使用主域名 * merge: upstream * README: 英文修正 ubuntu22 的依赖问题 * Merge remote-tracking branch 'upstream/master' * widget: 检查cdn状况在下载开始前检测,不堵塞ui线程 * Merge remote-tracking branch 'upstream/master' * readme: 修正 ubuntu 的编译说明 * download: 检查软件源的有效性
This commit is contained in:
parent
5f9599c47d
commit
958988d93c
@ -19,8 +19,6 @@ DownloadController::DownloadController(QObject *parent)
|
||||
domains.clear();
|
||||
domains.append("d.store.deepinos.org.cn");
|
||||
|
||||
|
||||
|
||||
/*
|
||||
domains = {
|
||||
"d1.store.deepinos.org.cn",
|
||||
@ -33,14 +31,11 @@ DownloadController::DownloadController(QObject *parent)
|
||||
this->threadNum = domains.size();
|
||||
}
|
||||
|
||||
|
||||
|
||||
void DownloadController::setFilename(QString filename)
|
||||
{
|
||||
this->filename = filename;
|
||||
}
|
||||
|
||||
|
||||
void timeSleeper(int time)
|
||||
{
|
||||
QElapsedTimer t1;
|
||||
@ -49,18 +44,6 @@ void timeSleeper(int time)
|
||||
return;
|
||||
}
|
||||
|
||||
//int checkPID(QString pidCommand){
|
||||
// system(pidCommand.toUtf8());
|
||||
// timeSleeper(10);
|
||||
// QFile downloadStatus("/tmp/spark-store/downloadStatus.txt");
|
||||
// downloadStatus.open(QFile::ReadOnly);
|
||||
// auto temp = QString(downloadStatus.readAll()).toUtf8();
|
||||
// downloadStatus.close();
|
||||
// if (temp!=""){
|
||||
// return 1;
|
||||
// }
|
||||
// return 0;
|
||||
//}
|
||||
|
||||
/**
|
||||
* @brief 开始下载
|
||||
@ -69,27 +52,31 @@ void DownloadController::startDownload(const QString &url)
|
||||
{
|
||||
// 获取下载任务信息
|
||||
fileSize = getFileSize(url);
|
||||
if(fileSize == 0)
|
||||
if (fileSize == 0)
|
||||
{
|
||||
emit errorOccur("文件大小获取失败");
|
||||
return;
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
QtConcurrent::run([=](){
|
||||
QtConcurrent::run([=]()
|
||||
{
|
||||
QFile serverList(QDir::homePath().toUtf8() + "/.config/spark-store/server.list");
|
||||
if(serverList.open(QFile::ReadOnly))
|
||||
if (serverList.open(QFile::ReadOnly))
|
||||
{
|
||||
QStringList list = QString(serverList.readAll()).trimmed().split("\n");
|
||||
qDebug() << list << list.size();
|
||||
domains.clear();
|
||||
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
if (list.at(i).contains("镜像源 Download only") && i + 1 < list.size()) {
|
||||
for (int j = i + 1; j < list.size(); j++) {
|
||||
system("curl -I -s --connect-timeout 5 https://" + list.at(j).toUtf8()
|
||||
+ "/dcs-repo.gpg-key.asc -w %{http_code} |tail -n1 > /tmp/spark-store/cdnStatus.txt");
|
||||
for (int i = 0; i < list.size(); i++)
|
||||
{
|
||||
if (list.at(i).contains("镜像源 Download only") && i + 1 < list.size())
|
||||
{
|
||||
for (int j = i + 1; j < list.size(); j++)
|
||||
{
|
||||
system("curl -I -s --connect-timeout 5 https://" + list.at(j).toUtf8() + "/dcs-repo.gpg-key.asc -w %{http_code} |tail -n1 > /tmp/spark-store/cdnStatus.txt");
|
||||
QFile cdnStatus("/tmp/spark-store/cdnStatus.txt");
|
||||
if(cdnStatus.open(QFile::ReadOnly) && QString(cdnStatus.readAll()).toUtf8()=="200"){
|
||||
if (cdnStatus.open(QFile::ReadOnly) && QString(cdnStatus.readAll()).toUtf8() == "200")
|
||||
{
|
||||
qDebug() << list.at(j);
|
||||
domains.append(list.at(j));
|
||||
}
|
||||
@ -97,8 +84,13 @@ void DownloadController::startDownload(const QString &url)
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (domains.size() == 0)
|
||||
{
|
||||
domains.append("d.store.deepinos.org.cn");
|
||||
}
|
||||
qDebug() << domains << domains.size();
|
||||
}
|
||||
qDebug() << domains << domains.size();
|
||||
|
||||
QDir tmpdir("/tmp/spark-store/");
|
||||
QString aria2Command = "-d";
|
||||
QString aria2Urls = "";
|
||||
@ -106,7 +98,9 @@ void DownloadController::startDownload(const QString &url)
|
||||
QString aria2Threads = "-s " + QString::number(domains.size());
|
||||
QStringList command;
|
||||
QString downloadDir = "/tmp/spark-store/";
|
||||
for(int i = 0; i < domains.size(); i ++)
|
||||
|
||||
|
||||
for (int i = 0; i < domains.size(); i++)
|
||||
{
|
||||
command.append(replaceDomain(url, domains.at(i)).toUtf8());
|
||||
aria2Urls += replaceDomain(url, domains.at(i));
|
||||
@ -126,69 +120,70 @@ void DownloadController::startDownload(const QString &url)
|
||||
cmd->setProgram("aria2c");
|
||||
cmd->setArguments(command);
|
||||
cmd->start();
|
||||
cmd->waitForStarted(); //等待启动完成
|
||||
cmd->waitForStarted(); //等待启动完成
|
||||
|
||||
QObject::connect(cmd,&QProcess::readyReadStandardOutput,
|
||||
[&](){
|
||||
QObject::connect(cmd, &QProcess::readyReadStandardOutput,
|
||||
[&]()
|
||||
{
|
||||
//通过读取输出计算下载速度
|
||||
QFileInfo info(tmpdir.absoluteFilePath(filename));
|
||||
QString message = cmd->readAllStandardOutput().data();
|
||||
message = message.replace(" ","").replace("\n","").replace("-","");
|
||||
message = message.replace("*","").replace("=","");
|
||||
message = message.replace(" ", "").replace("\n", "").replace("-", "");
|
||||
message = message.replace("*", "").replace("=", "");
|
||||
QStringList list;
|
||||
qint64 downloadSize = 0;
|
||||
int downloadSizePlace1 = message.indexOf("(");
|
||||
int downloadSizePlace2 = message.indexOf(")");
|
||||
int speedPlace1 = message.indexOf("DL:");
|
||||
int speedPlace2 = message.indexOf("ETA");
|
||||
if (downloadSizePlace1 != -1 && downloadSizePlace2 != -1){
|
||||
percentInfo = message.mid(downloadSizePlace1+1, downloadSizePlace2-downloadSizePlace1-1).replace("%","");
|
||||
if (percentInfo != "s"){
|
||||
if (downloadSizePlace1 != -1 && downloadSizePlace2 != -1)
|
||||
{
|
||||
percentInfo = message.mid(downloadSizePlace1 + 1, downloadSizePlace2 - downloadSizePlace1 - 1).replace("%", "");
|
||||
if (percentInfo != "s")
|
||||
{
|
||||
int percentInfoNumber = percentInfo.toUInt();
|
||||
|
||||
downloadSize = (percentInfoNumber+1) * fileSize / 100;
|
||||
|
||||
downloadSize = (percentInfoNumber + 1) * fileSize / 100;
|
||||
}
|
||||
}
|
||||
if (speedPlace1 != -1 && speedPlace2 != -1){
|
||||
speedInfo = message.mid(speedPlace1+3, speedPlace2-speedPlace1-3);
|
||||
if (speedPlace1 != -1 && speedPlace2 != -1)
|
||||
{
|
||||
speedInfo = message.mid(speedPlace1 + 3, speedPlace2 - speedPlace1 - 3);
|
||||
speedInfo += "/s";
|
||||
|
||||
}
|
||||
qDebug() << percentInfo << speedInfo;
|
||||
if(downloadSize >= downloadSizeRecord)
|
||||
if (downloadSize >= downloadSizeRecord)
|
||||
{
|
||||
downloadSizeRecord = downloadSize;
|
||||
}
|
||||
if(percentInfo=="OK"){
|
||||
if (percentInfo == "OK")
|
||||
{
|
||||
finished = true;
|
||||
emit downloadProcess("", fileSize, fileSize);
|
||||
qDebug() <<"finished:"<< finished;
|
||||
qDebug() << "finished:" << finished;
|
||||
}
|
||||
else{
|
||||
else
|
||||
{
|
||||
emit downloadProcess(speedInfo, downloadSizeRecord, fileSize);
|
||||
}
|
||||
|
||||
|
||||
|
||||
});
|
||||
QObject::connect(cmd,&QProcess::readyReadStandardError,
|
||||
[&](){
|
||||
});
|
||||
QObject::connect(cmd, &QProcess::readyReadStandardError,
|
||||
[&]()
|
||||
{
|
||||
emit errorOccur(cmd->readAllStandardError().data());
|
||||
return;
|
||||
});
|
||||
});
|
||||
|
||||
auto pidNumber = cmd->processId();
|
||||
this->pidNumber = pidNumber;
|
||||
int statusSum = 0;
|
||||
while(statusSum > -20)
|
||||
while (statusSum > -20)
|
||||
{
|
||||
auto status = cmd->waitForFinished() - 1;
|
||||
statusSum += status;
|
||||
}
|
||||
emit downloadFinished();
|
||||
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@ -199,12 +194,10 @@ void DownloadController::stopDownload()
|
||||
// 实现下载进程退出
|
||||
QString killCmd = QString("kill -9 %1").arg(pidNumber);
|
||||
system(killCmd.toUtf8());
|
||||
qDebug()<<"kill aria2!";
|
||||
|
||||
qDebug() << "kill aria2!";
|
||||
}
|
||||
|
||||
|
||||
qint64 DownloadController::getFileSize(const QString& url)
|
||||
qint64 DownloadController::getFileSize(const QString &url)
|
||||
{
|
||||
QEventLoop event;
|
||||
QNetworkAccessManager requestManager;
|
||||
@ -212,20 +205,19 @@ qint64 DownloadController::getFileSize(const QString& url)
|
||||
request.setUrl(QUrl(url));
|
||||
request.setAttribute(QNetworkRequest::FollowRedirectsAttribute, true);
|
||||
QNetworkReply *reply = requestManager.head(request);
|
||||
connect(reply, static_cast<void(QNetworkReply::*)(QNetworkReply::NetworkError) > (&QNetworkReply::error),
|
||||
connect(reply, static_cast<void (QNetworkReply::*)(QNetworkReply::NetworkError)>(&QNetworkReply::error),
|
||||
[this, reply](QNetworkReply::NetworkError error)
|
||||
{
|
||||
if(error != QNetworkReply::NoError)
|
||||
{
|
||||
emit errorOccur(reply->errorString());
|
||||
}
|
||||
});
|
||||
{
|
||||
if (error != QNetworkReply::NoError)
|
||||
{
|
||||
emit errorOccur(reply->errorString());
|
||||
}
|
||||
});
|
||||
connect(reply, &QNetworkReply::finished, &event, &QEventLoop::quit);
|
||||
event.exec();
|
||||
|
||||
qint64 fileSize = 0;
|
||||
if(reply->rawHeader("Accept-Ranges") == QByteArrayLiteral("bytes")
|
||||
&& reply->hasRawHeader(QString("Content-Length").toLocal8Bit()))
|
||||
if (reply->rawHeader("Accept-Ranges") == QByteArrayLiteral("bytes") && reply->hasRawHeader(QString("Content-Length").toLocal8Bit()))
|
||||
{
|
||||
fileSize = reply->header(QNetworkRequest::ContentLengthHeader).toUInt();
|
||||
}
|
||||
@ -234,10 +226,10 @@ qint64 DownloadController::getFileSize(const QString& url)
|
||||
return fileSize;
|
||||
}
|
||||
|
||||
QString DownloadController::replaceDomain(const QString& url, const QString domain)
|
||||
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())
|
||||
if (regex.match(url).hasMatch())
|
||||
{
|
||||
return QString(url).replace(regex.match(url).captured(), domain);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user