app_curlFunc.h 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #ifndef APP_CURLFUNC_H
  2. #define APP_CURLFUNC_H
  3. #include <QObject>
  4. #include <QString>
  5. #include <QMutex>
  6. #include <QWaitCondition>
  7. #include <QThread>
  8. #include <QSet>
  9. #include <atomic>
  10. #include <deque>
  11. #include <curl/curl.h>
  12. class CurlFtpUploader : public QObject
  13. {
  14. Q_OBJECT
  15. public:
  16. explicit CurlFtpUploader(QObject *parent = nullptr);
  17. ~CurlFtpUploader();
  18. // 设置 FTP 参数
  19. void setFtpParams(const QString &username,
  20. const QString &password,
  21. const QString &host,
  22. int port,
  23. const QString &remotePath);
  24. // 提交上传文件(非阻塞):文件进入内部串行队列,由后台线程一张一张按提交顺序上传。
  25. // 无论来自图片队列、补录重传还是日志上传,都不会并发传输(并发会争抢FTP连接导致失败)
  26. void uploadFileAsync(const QString &localFilePath);
  27. // 补录/重传重载:携带入库时记录的父目录,据此对齐远端归档目录
  28. void uploadFileAsync(const QString &localFilePath, const QString &parentPath);
  29. signals:
  30. void downloadStart(const QString &tips);
  31. void progressChanged(double uploadedPercent, const QString &filePath); // 进度百分比 (0.0 ~ 100.0) 与上传文件路径
  32. // 完成信号: success=是否成功; filePath=对应文件(调用方据此判断是哪一张传完);
  33. // error=失败原因(成功时为空)
  34. void finished(bool success, const QString &filePath, const QString &error);
  35. void sglProgress(double val,double timeLeft,double speed); //进度信号
  36. private:
  37. // 静态回调函数(供 CURL 调用)
  38. static size_t readCallback(char *buffer, size_t size, size_t nitems, void *userdata);
  39. static int progressCallback(void *clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow);
  40. // 上传任务(一张图一条)
  41. struct UploadJob {
  42. QString localFilePath;
  43. QString parentPath;
  44. };
  45. // 提交任务到串行队列并唤醒工作线程
  46. void enqueueJob(const QString &localFilePath, const QString &parentPath);
  47. // 工作线程主循环:按FIFO顺序逐个上传
  48. void runLoop();
  49. // 内部上传逻辑
  50. void performUpload(const QString &localFilePath);
  51. // 供补录/重传使用,parentPath 为入库时记录的父目录(为空时退回按文件自身父目录推导)
  52. void performUpload(const QString &localFilePath, const QString &parentPath);
  53. // 在 FTP 上逐级创建缺失目录(显式 MKD, 已存在的报错忽略)。
  54. // 只靠 CURLOPT_FTP_CREATE_MISSING_DIRS 时, 部分服务端对多级不存在路径不会 MKD,
  55. // 图片会被直接 STOR 到上一级, 现场表现为"FTP 上没有时间/日期文件夹"。
  56. void ensureRemoteDir(const QString &dirPath);
  57. // FTP 参数
  58. QString m_username;
  59. QString m_password;
  60. QString m_host;
  61. int m_port;
  62. QString m_remotePath;
  63. QString update_type;
  64. QString m_currentFilePath; // 当前正在上传的文件路径,用于进度信号携带
  65. // 已经确认创建过的远端目录:同一个归档目录(如 .../20261107103000)只 MKD 一次,
  66. // 之后同目录下的其它图片直接上传,不重复建目录。仅上传工作线程访问,无需加锁。
  67. QSet<QString> m_createdDirs;
  68. // 串行上传队列(工作线程消费)
  69. QMutex m_jobMutex;
  70. QWaitCondition m_jobCond;
  71. std::deque<UploadJob> m_jobs;
  72. std::atomic<bool> m_stopping{false};
  73. QThread *m_worker = nullptr;
  74. };
  75. #endif // APP_CURLFUNC_H