app_curlFunc.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #include "app_curlFunc.h"
  2. #include <QFile>
  3. #include <QFileInfo> // 添加此行
  4. #include <QThread>
  5. #include <QDebug>
  6. #include <QtConcurrent/QtConcurrent>
  7. #include <m_loger.h>
  8. CurlFtpUploader::CurlFtpUploader(QObject *parent) : QObject(parent), m_port(21) {
  9. static std::once_flag curl_init_flag;
  10. std::call_once(curl_init_flag, [](){
  11. curl_global_init(CURL_GLOBAL_ALL);
  12. });
  13. connect(this, &CurlFtpUploader::fileUploaded, [this](const QString &filePath) {
  14. m_uploadQueue.removeAll(filePath); // 线程安全移除
  15. processNextFile();
  16. });
  17. }
  18. CurlFtpUploader::~CurlFtpUploader() {
  19. if (ftp_Thread.isRunning()) {
  20. ftp_Thread.waitForFinished(); // wait for finish thread
  21. }
  22. }
  23. void CurlFtpUploader::setFtpParams(const QString &username,
  24. const QString &password,
  25. const QString &host,
  26. int port,
  27. const QString &remotePath) {
  28. m_username = username;
  29. m_password = password;
  30. m_host = host;
  31. m_port = port;
  32. m_remotePath = remotePath;
  33. }
  34. void CurlFtpUploader::uploadFilesAsync(const QStringList &filePaths) {
  35. LOG("uploadFilesAsync...");
  36. {
  37. // 遍历并添加每个文件路径
  38. for (const QString &path : filePaths) {
  39. m_uploadQueue.enqueue(path);
  40. }
  41. }
  42. if (m_currentUploadingFile.isEmpty()) {
  43. processNextFile();
  44. }
  45. }
  46. void CurlFtpUploader::performUpload(const QString &localFilePath) {
  47. CURL *curl = curl_easy_init();
  48. if (!curl) {
  49. emit finished(false, "Failed to initialize CURL");
  50. return;
  51. }
  52. // 打开本地文件
  53. QFile file(localFilePath);
  54. if (!file.open(QIODevice::ReadOnly)) {
  55. emit finished(false, "Cannot open local file: " + localFilePath);
  56. curl_easy_cleanup(curl);
  57. return;
  58. }
  59. // 构建 FTP URL(示例:ftp://user:pass@host:port/remote/path/filename)
  60. QString url = QString("ftp://%1/%2/%3")
  61. .arg(m_host)
  62. .arg(m_remotePath)
  63. .arg(QFileInfo(localFilePath).fileName());
  64. // 转义 URL 中的特殊字符
  65. //char *escapedUrl = curl_easy_escape(curl, url.toUtf8().constData(), 0);
  66. QString username = m_username + ":" + m_password;
  67. // 设置 CURL 选项
  68. curl_easy_setopt(curl, CURLOPT_URL, url.toUtf8().constData());
  69. curl_easy_setopt(curl, CURLOPT_USERPWD, username.toUtf8().constData());
  70. curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
  71. curl_easy_setopt(curl, CURLOPT_READDATA, &file);
  72. curl_easy_setopt(curl, CURLOPT_READFUNCTION, &CurlFtpUploader::readCallback);
  73. curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)file.size());
  74. curl_easy_setopt(curl, CURLOPT_FTP_CREATE_MISSING_DIRS, 1L); // 自动创建目录
  75. curl_easy_setopt(curl, CURLOPT_TIMEOUT, 180L); // 传输超时 180 秒
  76. curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10L); // 连接超时 10 秒
  77. // 进度回调
  78. #if 0
  79. curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, &CurlFtpUploader::progressCallback);
  80. curl_easy_setopt(curl, CURLOPT_XFERINFODATA, this);
  81. curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
  82. #endif
  83. // 执行上传
  84. CURLcode res = curl_easy_perform(curl);
  85. file.close();
  86. // 处理结果
  87. // 切换到主线程触发完成信号
  88. QMetaObject::invokeMethod(this, [this, res, localFilePath]() {
  89. if (res != CURLE_OK) {
  90. emit finished(false, curl_easy_strerror(res));
  91. ERROR("FTP上传失败:{}",localFilePath.toStdString());
  92. } else {
  93. emit finished(true, "");
  94. LOG("上传成功:{}",localFilePath.toStdString());
  95. //emit fileUploaded(localFilePath);
  96. // 不使用信号和槽,直接调用
  97. QMetaObject::invokeMethod(this, [this](){
  98. this->processNextFile();
  99. }, Qt::QueuedConnection);
  100. }
  101. }, Qt::QueuedConnection);
  102. curl_easy_cleanup(curl);
  103. }
  104. void CurlFtpUploader::processNextFile() {
  105. LOG("处理下一个文件,队列剩余:{}", m_uploadQueue.size());
  106. QString nextFile;
  107. {
  108. if (!m_uploadQueue.isEmpty()) {
  109. nextFile = m_uploadQueue.dequeue();
  110. }
  111. }
  112. if (!nextFile.isEmpty()) {
  113. QtConcurrent::run(QThreadPool::globalInstance(), [this, nextFile]() {
  114. performUpload(nextFile);
  115. // 切换回主线程清理并处理下个任务
  116. QMetaObject::invokeMethod(this, [this]() {
  117. processNextFile();
  118. }, Qt::QueuedConnection);
  119. });
  120. } else {
  121. emit allUploadsFinished();
  122. }
  123. }
  124. // 静态回调:读取文件内容
  125. size_t CurlFtpUploader::readCallback(char *buffer, size_t size, size_t nitems, void *userdata){
  126. QFile *file = static_cast<QFile*>(userdata);
  127. qint64 bytesRead = file->read(buffer, size * nitems);
  128. return static_cast<size_t>(bytesRead);
  129. }
  130. // 静态回调:上传进度
  131. int CurlFtpUploader::progressCallback(void *clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow) {
  132. Q_UNUSED(dltotal);
  133. Q_UNUSED(dlnow);
  134. CurlFtpUploader *uploader = static_cast<CurlFtpUploader*>(clientp);
  135. if (ultotal > 0) {
  136. double percent = qMin(100.0, (static_cast<double>(ulnow) / ultotal) * 100.0);
  137. // 切换到主线程触发信号
  138. QMetaObject::invokeMethod(uploader, [uploader, percent]() {
  139. emit uploader->progressChanged(percent);
  140. }, Qt::QueuedConnection);
  141. }
  142. return 0;
  143. }