| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- #include "app_curlFunc.h"
- #include <QFile>
- #include <QFileInfo> // 添加此行
- #include <QThread>
- #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
- }
- }
- void CurlFtpUploader::setFtpParams(const QString &username,
- const QString &password,
- const QString &host,
- int port,
- const QString &remotePath) {
- m_username = username;
- m_password = password;
- m_host = host;
- m_port = port;
- 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::performUpload(const QString &localFilePath) {
- CURL *curl = curl_easy_init();
- if (!curl) {
- emit finished(false, "Failed to initialize CURL");
- return;
- }
- // 打开本地文件
- QFile file(localFilePath);
- if (!file.open(QIODevice::ReadOnly)) {
- emit finished(false, "Cannot open local file: " + localFilePath);
- curl_easy_cleanup(curl);
- return;
- }
- // 构建 FTP URL(示例:ftp://user:pass@host:port/remote/path/filename)
- QString url = QString("ftp://%1/%2/%3")
- .arg(m_host)
- .arg(m_remotePath)
- .arg(QFileInfo(localFilePath).fileName());
- // 转义 URL 中的特殊字符
- //char *escapedUrl = curl_easy_escape(curl, url.toUtf8().constData(), 0);
- QString username = m_username + ":" + m_password;
- // 设置 CURL 选项
- curl_easy_setopt(curl, CURLOPT_URL, url.toUtf8().constData());
- curl_easy_setopt(curl, CURLOPT_USERPWD, username.toUtf8().constData());
- curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
- curl_easy_setopt(curl, CURLOPT_READDATA, &file);
- 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_FTP_CREATE_MISSING_DIRS, 1L); // 自动创建目录
- curl_easy_setopt(curl, CURLOPT_TIMEOUT, 180L); // 传输超时 180 秒
- curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10L); // 连接超时 10 秒
- // 进度回调
- #if 0
- curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, &CurlFtpUploader::progressCallback);
- curl_easy_setopt(curl, CURLOPT_XFERINFODATA, this);
- curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
- #endif
- // 执行上传
- CURLcode res = curl_easy_perform(curl);
- 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);
- });
- } else {
- emit allUploadsFinished();
- }
- }
- // 静态回调:读取文件内容
- size_t CurlFtpUploader::readCallback(char *buffer, size_t size, size_t nitems, void *userdata){
- QFile *file = static_cast<QFile*>(userdata);
- qint64 bytesRead = file->read(buffer, size * nitems);
- return static_cast<size_t>(bytesRead);
- }
- // 静态回调:上传进度
- int CurlFtpUploader::progressCallback(void *clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow) {
- Q_UNUSED(dltotal);
- Q_UNUSED(dlnow);
- CurlFtpUploader *uploader = static_cast<CurlFtpUploader*>(clientp);
- 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);
- }
- return 0;
- }
|