|
@@ -3,24 +3,10 @@
|
|
|
#include <QFileInfo> // 添加此行
|
|
#include <QFileInfo> // 添加此行
|
|
|
#include <QThread>
|
|
#include <QThread>
|
|
|
#include <QDebug>
|
|
#include <QDebug>
|
|
|
-#include <QtConcurrent/QtConcurrent>
|
|
|
|
|
-#include <m_loger.h>
|
|
|
|
|
-CurlFtpUploader::CurlFtpUploader(QObject *parent) : QObject(parent), m_port(21) {
|
|
|
|
|
- static std::once_flag curl_init_flag;
|
|
|
|
|
- std::call_once(curl_init_flag, [](){
|
|
|
|
|
- curl_global_init(CURL_GLOBAL_ALL);
|
|
|
|
|
- });
|
|
|
|
|
- connect(this, &CurlFtpUploader::fileUploaded, [this](const QString &filePath) {
|
|
|
|
|
- m_uploadQueue.removeAll(filePath); // 线程安全移除
|
|
|
|
|
- processNextFile();
|
|
|
|
|
- });
|
|
|
|
|
-}
|
|
|
|
|
|
|
|
|
|
-CurlFtpUploader::~CurlFtpUploader() {
|
|
|
|
|
- if (ftp_Thread.isRunning()) {
|
|
|
|
|
- ftp_Thread.waitForFinished(); // wait for finish thread
|
|
|
|
|
- }
|
|
|
|
|
-}
|
|
|
|
|
|
|
+CurlFtpUploader::CurlFtpUploader(QObject *parent) : QObject(parent), m_port(21) {}
|
|
|
|
|
+
|
|
|
|
|
+CurlFtpUploader::~CurlFtpUploader() {}
|
|
|
|
|
|
|
|
void CurlFtpUploader::setFtpParams(const QString &username,
|
|
void CurlFtpUploader::setFtpParams(const QString &username,
|
|
|
const QString &password,
|
|
const QString &password,
|
|
@@ -34,23 +20,17 @@ void CurlFtpUploader::setFtpParams(const QString &username,
|
|
|
m_remotePath = remotePath;
|
|
m_remotePath = remotePath;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void CurlFtpUploader::uploadFilesAsync(const QStringList &filePaths) {
|
|
|
|
|
- LOG("uploadFilesAsync...");
|
|
|
|
|
- {
|
|
|
|
|
- // 遍历并添加每个文件路径
|
|
|
|
|
- for (const QString &path : filePaths) {
|
|
|
|
|
- m_uploadQueue.enqueue(path);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if (m_currentUploadingFile.isEmpty()) {
|
|
|
|
|
- processNextFile();
|
|
|
|
|
- }
|
|
|
|
|
|
|
+void CurlFtpUploader::uploadFileAsync(const QString &localFilePath) {
|
|
|
|
|
+ // 在子线程中执行上传(避免阻塞主线程)
|
|
|
|
|
+ QThread *thread = QThread::create([this, localFilePath]() {
|
|
|
|
|
+ performUpload(localFilePath);
|
|
|
|
|
+ });
|
|
|
|
|
+ connect(thread, &QThread::finished, thread, &QThread::deleteLater);
|
|
|
|
|
+ thread->start();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-
|
|
|
|
|
void CurlFtpUploader::performUpload(const QString &localFilePath) {
|
|
void CurlFtpUploader::performUpload(const QString &localFilePath) {
|
|
|
-
|
|
|
|
|
|
|
+ curl_global_init(CURL_GLOBAL_ALL);
|
|
|
CURL *curl = curl_easy_init();
|
|
CURL *curl = curl_easy_init();
|
|
|
if (!curl) {
|
|
if (!curl) {
|
|
|
emit finished(false, "Failed to initialize CURL");
|
|
emit finished(false, "Failed to initialize CURL");
|
|
@@ -82,60 +62,25 @@ void CurlFtpUploader::performUpload(const QString &localFilePath) {
|
|
|
curl_easy_setopt(curl, CURLOPT_READFUNCTION, &CurlFtpUploader::readCallback);
|
|
curl_easy_setopt(curl, CURLOPT_READFUNCTION, &CurlFtpUploader::readCallback);
|
|
|
curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)file.size());
|
|
curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)file.size());
|
|
|
curl_easy_setopt(curl, CURLOPT_FTP_CREATE_MISSING_DIRS, 1L); // 自动创建目录
|
|
curl_easy_setopt(curl, CURLOPT_FTP_CREATE_MISSING_DIRS, 1L); // 自动创建目录
|
|
|
- curl_easy_setopt(curl, CURLOPT_TIMEOUT, 180L); // 传输超时 180 秒
|
|
|
|
|
- curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10L); // 连接超时 10 秒
|
|
|
|
|
|
|
+ curl_easy_setopt(curl, CURLOPT_TIMEOUT, 180L); // 传输超时 120 秒
|
|
|
|
|
+ curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 20L); // 连接超时 10 秒
|
|
|
// 进度回调
|
|
// 进度回调
|
|
|
- #if 0
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, &CurlFtpUploader::progressCallback);
|
|
curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, &CurlFtpUploader::progressCallback);
|
|
|
curl_easy_setopt(curl, CURLOPT_XFERINFODATA, this);
|
|
curl_easy_setopt(curl, CURLOPT_XFERINFODATA, this);
|
|
|
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
|
|
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
|
|
|
- #endif
|
|
|
|
|
|
|
+
|
|
|
// 执行上传
|
|
// 执行上传
|
|
|
CURLcode res = curl_easy_perform(curl);
|
|
CURLcode res = curl_easy_perform(curl);
|
|
|
file.close();
|
|
file.close();
|
|
|
|
|
|
|
|
// 处理结果
|
|
// 处理结果
|
|
|
- // 切换到主线程触发完成信号
|
|
|
|
|
- QMetaObject::invokeMethod(this, [this, res, localFilePath]() {
|
|
|
|
|
- if (res != CURLE_OK) {
|
|
|
|
|
- emit finished(false, curl_easy_strerror(res));
|
|
|
|
|
- ERROR("FTP上传失败:{}",localFilePath.toStdString());
|
|
|
|
|
- } else {
|
|
|
|
|
- emit finished(true, "");
|
|
|
|
|
- LOG("上传成功:{}",localFilePath.toStdString());
|
|
|
|
|
- //emit fileUploaded(localFilePath);
|
|
|
|
|
- // 不使用信号和槽,直接调用
|
|
|
|
|
- QMetaObject::invokeMethod(this, [this](){
|
|
|
|
|
- this->processNextFile();
|
|
|
|
|
- }, Qt::QueuedConnection);
|
|
|
|
|
- }
|
|
|
|
|
- }, Qt::QueuedConnection);
|
|
|
|
|
-
|
|
|
|
|
- curl_easy_cleanup(curl);
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-void CurlFtpUploader::processNextFile() {
|
|
|
|
|
- LOG("处理下一个文件,队列剩余:{}", m_uploadQueue.size());
|
|
|
|
|
-
|
|
|
|
|
- QString nextFile;
|
|
|
|
|
- {
|
|
|
|
|
- if (!m_uploadQueue.isEmpty()) {
|
|
|
|
|
- nextFile = m_uploadQueue.dequeue();
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if (!nextFile.isEmpty()) {
|
|
|
|
|
- QtConcurrent::run(QThreadPool::globalInstance(), [this, nextFile]() {
|
|
|
|
|
- performUpload(nextFile);
|
|
|
|
|
-
|
|
|
|
|
- // 切换回主线程清理并处理下个任务
|
|
|
|
|
- QMetaObject::invokeMethod(this, [this]() {
|
|
|
|
|
- processNextFile();
|
|
|
|
|
- }, Qt::QueuedConnection);
|
|
|
|
|
- });
|
|
|
|
|
|
|
+ if (res != CURLE_OK) {
|
|
|
|
|
+ emit finished(false, curl_easy_strerror(res));
|
|
|
} else {
|
|
} else {
|
|
|
- emit allUploadsFinished();
|
|
|
|
|
|
|
+ emit finished(true, "");
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ curl_easy_cleanup(curl);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// 静态回调:读取文件内容
|
|
// 静态回调:读取文件内容
|
|
@@ -152,11 +97,8 @@ int CurlFtpUploader::progressCallback(void *clientp, curl_off_t dltotal, curl_of
|
|
|
|
|
|
|
|
CurlFtpUploader *uploader = static_cast<CurlFtpUploader*>(clientp);
|
|
CurlFtpUploader *uploader = static_cast<CurlFtpUploader*>(clientp);
|
|
|
if (ultotal > 0) {
|
|
if (ultotal > 0) {
|
|
|
- double percent = qMin(100.0, (static_cast<double>(ulnow) / ultotal) * 100.0);
|
|
|
|
|
- // 切换到主线程触发信号
|
|
|
|
|
- QMetaObject::invokeMethod(uploader, [uploader, percent]() {
|
|
|
|
|
- emit uploader->progressChanged(percent);
|
|
|
|
|
- }, Qt::QueuedConnection);
|
|
|
|
|
|
|
+ double percent = (static_cast<double>(ulnow) / ultotal) * 100.0;
|
|
|
|
|
+ emit uploader->progressChanged(percent);
|
|
|
}
|
|
}
|
|
|
return 0;
|
|
return 0;
|
|
|
}
|
|
}
|