CurlFtp.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #ifndef CURLFTP_H
  2. #define CURLFTP_H
  3. #include "curl/curl.h"
  4. #include <string>
  5. #include <vector>
  6. #include <mutex>
  7. #include "CurlFtpInfo.h"
  8. /**
  9. * 使用CURL进行FTP操作,这里是单线程的,需要多线程同时操作,就创建多个实例
  10. *
  11. * 1.默认超时时间是30s
  12. * 2.FTP默认端口是21,SFTP默认端口是22
  13. * 3.设置好IP和端口号后,后续输入的文件路径都是相对路径,无需添加IP和端口号
  14. * 4.使用ftp,用setFtpIPAndPort()设置IP和端口,使用sftp,用setSftpIPAndPort()设置IP和端口
  15. * 5.这里使用了filesystem,需要C++17支持,uos默认的gcc版本是8.3,支持C++17,但是需要手动开启
  16. * 这个库的支持,在项目文件里添加“stdc++fs”这个库,如下:
  17. * target_link_libraries(${this_exe} PRIVATE stdc++fs)
  18. * 6. 如果C++版本低于C++17,会改为Qt的QDir
  19. */
  20. class CurlFtp
  21. {
  22. public:
  23. CurlFtp();
  24. ~CurlFtp();
  25. /*********************** 成员函数 *********************/
  26. /* 设置用户名和密码 */
  27. bool setUsernameAndPassword(const std::string& username, const std::string& password);
  28. /* 设置IP和端口 */
  29. bool setFtpIPAndPort(const std::string& IP, const int port = 21);
  30. /* 设置SFTP的IP和端口 */
  31. bool setSftpIPAndPort(const std::string& IP, const int port = 22);
  32. /* 设置是否忽略SSL证书,使用SFTP不建议忽略 */
  33. void setIgnoreSSLCert(bool isIgnore);
  34. /* 设置CA证书文件 */
  35. void setCaCertFile(const std::string& caCertFile);
  36. /* 设置是否启用CURL的调试信息 */
  37. void enableCurlDebug(bool isPrint = false);
  38. /* 列出文件列表 */
  39. bool getFileList(std::string dir, std::vector<std::string>& fileList);
  40. /* 获取文件夹列表 */
  41. bool getDirList(std::string dir, std::vector<std::string>& dirList);
  42. /* 判断文件夹是否存在 */
  43. bool isDirExist(const std::string& dir);
  44. /* 创建FTP文件夹 */
  45. bool createDirectory(const std::string& ftpDir);
  46. /* 创建FTP文件夹,递归创建 */
  47. bool createDirectories(const std::string& ftpDir);
  48. /* 下载文件 */
  49. bool downloadFile(const std::string& remoteFile, const std::string& localFile, size_t timeout = 30);
  50. /* 下载文件到数组 */
  51. bool downloadToArray(const std::string& remoteFile,std::vector<char>& arrayInfo, size_t timeout = 30);
  52. /* 上传文件 */
  53. bool uploadFile(const std::string& localFile, const std::string& remoteFile, size_t timeout = 30, bool isCreateDir = false);
  54. /* 上传文件,上传数据 */
  55. bool uploadData(char* srcData, size_t size, const std::string& remoteFile, size_t timeout = 30, bool isCreateDir = false);
  56. private:
  57. /* 列出所有内容 */
  58. bool listAll(CURL* curl, std::string dir, std::vector<CF_FileInfo>& fileInfoList);
  59. /* 检查FTP文件夹路径是否合规,不合规就修改 */
  60. std::string checkDirPath(const std::string& dir);
  61. /* 检查FTP文件路径是否合规 */
  62. std::string checkFilePath(const std::string& file);
  63. /* 检查本地文件夹是否存在,不存在则创建 */
  64. bool checkLocalDir(const std::string& localDir);
  65. /* 检查本地文件夹是否存在,不会创建 */
  66. bool checkLocalDirExist(const std::string& localDir);
  67. /* 检查本地文件是否存在 */
  68. bool checkLocalFileExist(const std::string& localFile);
  69. /* 执行curl,添加重试机制 */
  70. bool performCurl(CURL* curl);
  71. /* 重置curl设置 */
  72. inline bool resetCurl(CURL* curl);
  73. /* 设置sftp,如果是sftp就设置 */
  74. inline bool setSftp(CURL* curl);
  75. /* 检查FTP文件夹是否存在 */
  76. bool checkFtpDirExist(const std::string& dir);
  77. /* 检查SFTP文件夹是否存在 */
  78. bool checkSftpDirExist(const std::string& dir);
  79. private:
  80. std::mutex m_mutexCurl; /* curl互斥锁 */
  81. std::string m_currentDir; /* 当前目录 */
  82. CURL* m_curl = nullptr; /* curl句柄 */
  83. std::string m_IP; /* IP */
  84. int m_port = 21; /* 端口 */
  85. std::string m_ftpUrl; /* ftpUrl */
  86. std::string m_username;
  87. std::string m_password;
  88. bool m_isSftp = false; /* 是否是SFTP */
  89. bool m_isIgnoreSSLCert = false; /* 是否忽略SSL证书 */
  90. bool m_enableCurlDebug = false; /* 是否打印调试信息 */
  91. std::string m_caCertFile; /* CA证书文件 */
  92. };
  93. #endif /* CURLFTP_H */