app_curlFunc.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #ifndef APP_CURLFUNC_H
  2. #define APP_CURLFUNC_H
  3. #include <QObject>
  4. #include <QString>
  5. #include <curl/curl.h>
  6. #include <QFuture>
  7. #include <QQueue>
  8. #include <QAtomicInt>
  9. class CurlFtpUploader : public QObject
  10. {
  11. Q_OBJECT
  12. public:
  13. explicit CurlFtpUploader(QObject *parent = nullptr);
  14. ~CurlFtpUploader();
  15. // 设置 FTP 参数
  16. void setFtpParams(const QString &username,
  17. const QString &password,
  18. const QString &host,
  19. int port,
  20. const QString &remotePath);
  21. // 异步上传文件(非阻塞)
  22. //void uploadFileAsync(const QString &localFilePath);
  23. public slots:
  24. void uploadFilesAsync(const QStringList &filePaths);
  25. signals:
  26. void progressChanged(double uploadedPercent); // 进度百分比 (0.0 ~ 100.0)
  27. void progressChanged_file(const QString &currentFile, double percent);
  28. void finished(bool success, const QString &error); // 完成信号
  29. void allUploadsFinished();
  30. void fileUploaded(const QString &filePath);
  31. private:
  32. QMutex m_queueMutex;
  33. QQueue<QString> m_uploadQueue;
  34. QString m_currentUploadingFile;
  35. void processNextFile();
  36. // 静态回调函数(供 CURL 调用)
  37. static size_t readCallback(char *buffer, size_t size, size_t nitems, void *userdata);
  38. static int progressCallback(void *clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow);
  39. // 内部上传逻辑
  40. void performUpload(const QString &localFilePath);
  41. QFuture<void> ftp_Thread;
  42. // FTP 参数
  43. QString m_username;
  44. QString m_password;
  45. QString m_host;
  46. int m_port;
  47. QString m_remotePath;
  48. };
  49. #endif // APP_CURLFUNC_H