60 lines
1.7 KiB
C++
60 lines
1.7 KiB
C++
#pragma once
|
|||
|
|
|
||
|
|
#include <QNetworkProxy>
|
||
|
|
#include <QObject>
|
||
|
|
#include <QString>
|
||
|
|
|
||
|
|
#include "modelstore.h"
|
||
|
|
|
||
|
|
class QNetworkAccessManager;
|
||
|
|
class QNetworkReply;
|
||
|
|
class QFile;
|
||
|
|
|
||
|
|
namespace lc::models {
|
||
|
|
|
||
|
|
// 按注册表下载模型文件。支持:
|
||
|
|
// - 镜像基址替换(把注册表里的地址换成用户选的源)
|
||
|
|
// - HTTP 代理
|
||
|
|
// - 断点续传(已下过的部分用 Range 续)
|
||
|
|
class ModelDownloader : public QObject {
|
||
|
|
Q_OBJECT
|
||
|
|
|
||
|
|
public:
|
||
|
|
explicit ModelDownloader(QObject * parent = nullptr);
|
||
|
|
~ModelDownloader() override;
|
||
|
|
|
||
|
|
// mirrorBase 形如 https://hf-mirror.com;留空则用注册表里的原始地址
|
||
|
|
void setMirrorBase(const QString & mirrorBase) { mirrorBase_ = mirrorBase; }
|
||
|
|
void setProxy(const QNetworkProxy & proxy) { proxy_ = proxy; }
|
||
|
|
|
||
|
|
void start(const ModelInfo & model);
|
||
|
|
void cancel();
|
||
|
|
bool isRunning() const { return running_; }
|
||
|
|
|
||
|
|
Q_SIGNALS:
|
||
|
|
void progress(const QString & file, qint64 received, qint64 total);
|
||
|
|
void status(const QString & message);
|
||
|
|
void finished(bool ok, const QString & error);
|
||
|
|
|
||
|
|
private:
|
||
|
|
void startNextFile();
|
||
|
|
void onReadyRead();
|
||
|
|
void onFinished();
|
||
|
|
void cleanup();
|
||
|
|
|
||
|
|
QString rewriteUrl(const QString & url) const;
|
||
|
|
|
||
|
|
QNetworkAccessManager * net_ = nullptr;
|
||
|
|
QNetworkReply * reply_ = nullptr;
|
||
|
|
QFile * out_ = nullptr;
|
||
|
|
std::vector<ModelFile> queue_;
|
||
|
|
size_t index_ = 0;
|
||
|
|
QString dir_;
|
||
|
|
QString mirrorBase_;
|
||
|
|
QNetworkProxy proxy_;
|
||
|
|
bool running_ = false;
|
||
|
|
bool cancel_ = false;
|
||
|
|
qint64 baseOffset_ = 0; // 断点续传的起始偏移
|
||
|
|
};
|
||
|
|
|
||
|
|
} // namespace lc::models
|