add breakPointDownload property

This commit is contained in:
朱子楚\zhuzi 2023-09-06 00:22:37 +08:00
parent d6bbe3a5ec
commit ddee70cdca
4 changed files with 38 additions and 20 deletions

View File

@ -233,6 +233,7 @@ FluContentPage{
HttpCallable{ HttpCallable{
id:callable_download id:callable_download
onStart: { onStart: {
btn_download.progress = 0
btn_download.disabled = true btn_download.disabled = true
} }
onFinish: { onFinish: {

View File

@ -235,6 +235,7 @@ FluContentPage{
HttpCallable{ HttpCallable{
id:callable_download id:callable_download
onStart: { onStart: {
btn_download.progress = 0
btn_download.disabled = true btn_download.disabled = true
} }
onFinish: { onFinish: {

View File

@ -25,6 +25,7 @@ FluHttp::FluHttp(QObject *parent)
timeout(15000); timeout(15000);
cacheMode(FluHttpType::CacheMode::NoCache); cacheMode(FluHttpType::CacheMode::NoCache);
cacheDir(QStandardPaths::writableLocation(QStandardPaths::CacheLocation)+"/httpcache"); cacheDir(QStandardPaths::writableLocation(QStandardPaths::CacheLocation)+"/httpcache");
breakPointDownload(true);
} }
FluHttp::~FluHttp(){ FluHttp::~FluHttp(){
@ -267,42 +268,56 @@ void FluHttp::download(QString url,HttpCallable* callable,QString savePath,QMap<
QNetworkRequest request(_url); QNetworkRequest request(_url);
addHeaders(&request,data["headers"].toMap()); addHeaders(&request,data["headers"].toMap());
QSharedPointer<QFile> file(new QFile(savePath)); QSharedPointer<QFile> file(new QFile(savePath));
QIODevice::OpenMode mode = QIODevice::WriteOnly|QIODevice::Truncate;
if (!file->open(mode))
{
Q_EMIT callable->error(-1,QString("Url: %1 %2 Non-Writable").arg(request.url().toString(),file->fileName()),"");
Q_EMIT callable->finish();
return;
}
QEventLoop loop; QEventLoop loop;
connect(&manager,&QNetworkAccessManager::finished,&manager,[&loop](QNetworkReply *reply){ connect(&manager,&QNetworkAccessManager::finished,&manager,[&loop](QNetworkReply *reply){
loop.quit(); loop.quit();
}); });
auto filePath = getCacheFilePath(data);
qint64 seek = 0;
QSharedPointer<QFile> fileCache(new QFile(filePath));
if(fileCache->exists() && file->exists() && _breakPointDownload){
QJsonObject cacheInfo = QJsonDocument::fromJson(readCache(data).toUtf8()).object();
auto fileSize = cacheInfo.value("fileSize").toInteger();
auto contentLength = cacheInfo.value("contentLength").toInteger();
if(fileSize == contentLength && file->size() == contentLength){
Q_EMIT callable->downloadProgress(fileSize,contentLength);
Q_EMIT callable->success(savePath);
Q_EMIT callable->finish();
return;
}
if(fileSize==file->size()){
request.setRawHeader("Range", QString("bytes=%1-").arg(fileSize).toUtf8());
seek = fileSize;
file->open(QIODevice::WriteOnly|QIODevice::Append);
}else{
file->open(QIODevice::WriteOnly|QIODevice::Truncate);
}
}else{
file->open(QIODevice::WriteOnly|QIODevice::Truncate);
}
QNetworkReply* reply = manager.get(request); QNetworkReply* reply = manager.get(request);
_cacheReply.append(reply); _cacheReply.append(reply);
auto filePath = getCacheFilePath(data); if (!fileCache->open(QIODevice::WriteOnly|QIODevice::Truncate))
QSharedPointer<QFile> fileCache(new QFile(filePath));
if (!fileCache->open(mode))
{ {
qDebug()<<"FileCache Error"; qDebug()<<"FileCache Error";
} }
connect(reply,&QNetworkReply::readyRead,reply,[reply,file,fileCache,data]{ connect(reply,&QNetworkReply::readyRead,reply,[reply,file,fileCache,data,callable,seek]{
if (!reply || !file || reply->error() != QNetworkReply::NoError) if (!reply || !file || reply->error() != QNetworkReply::NoError)
{ {
return; return;
} }
file->write(reply->readAll());
fileCache->resize(0);
QMap<QString, QVariant> downMap = data; QMap<QString, QVariant> downMap = data;
QVariant etagHeader = reply->header(QNetworkRequest::ETagHeader); qint64 contentLength = reply->header(QNetworkRequest::ContentLengthHeader).toLongLong()+seek;
if (etagHeader.isValid()) { downMap.insert("contentLength",contentLength);
downMap.insert("ETag",etagHeader.toString()); QString eTag = reply->header(QNetworkRequest::ETagHeader).toString();
} downMap.insert("eTag",eTag);
file->write(reply->readAll());
file->flush();
downMap.insert("fileSize",file->size()); downMap.insert("fileSize",file->size());
fileCache->resize(0);
fileCache->write(FluTools::getInstance()->toBase64(QJsonDocument::fromVariant(QVariant(downMap)).toJson()).toUtf8()); fileCache->write(FluTools::getInstance()->toBase64(QJsonDocument::fromVariant(QVariant(downMap)).toJson()).toUtf8());
}); fileCache->flush();
connect(reply,&QNetworkReply::downloadProgress,reply,[=](qint64 bytesReceived, qint64 bytesTotal){ Q_EMIT callable->downloadProgress(file->size(),contentLength);
Q_EMIT callable->downloadProgress(bytesReceived,bytesTotal);
}); });
loop.exec(); loop.exec();
if (reply->error() == QNetworkReply::NoError) { if (reply->error() == QNetworkReply::NoError) {

View File

@ -29,6 +29,7 @@ class FluHttp : public QObject
Q_PROPERTY_AUTO(int,timeout) Q_PROPERTY_AUTO(int,timeout)
Q_PROPERTY_AUTO(int,cacheMode); Q_PROPERTY_AUTO(int,cacheMode);
Q_PROPERTY_AUTO(QString,cacheDir); Q_PROPERTY_AUTO(QString,cacheDir);
Q_PROPERTY_AUTO(bool,breakPointDownload);
QML_NAMED_ELEMENT(FluHttp) QML_NAMED_ELEMENT(FluHttp)
private: private:
QVariant invokeIntercept(QMap<QString, QVariant> request); QVariant invokeIntercept(QMap<QString, QVariant> request);