CurlFtp.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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.默认端口是21
  13. * 3.设置好IP和端口号后,后续输入的文件路径都是想对你路径,无需添加IP和端口号
  14. * 4.使用ftp,用setFtpIPAndPort()设置IP和端口,使用sftp,用setSftpIPAndPort()设置IP和端口
  15. */
  16. class CurlFtp
  17. {
  18. public:
  19. CurlFtp();
  20. ~CurlFtp();
  21. /*********************** 成员函数 *********************/
  22. /* 设置用户名和密码 */
  23. bool setUsernameAndPassword(const std::string& username, const std::string& password);
  24. /* 设置IP和端口 */
  25. bool setFtpIPAndPort(const std::string& IP, const int port = 21);
  26. /* 设置SFTP的IP和端口 */
  27. bool setSftpIPAndPort(const std::string& IP, const int port = 22);
  28. /* 设置是否忽略SSL证书,使用SFTP不建议忽略 */
  29. void setIgnoreSSLCert(bool isIgnore);
  30. /* 设置CA证书文件 */
  31. void setCaCertFile(const std::string& caCertFile);
  32. /* 设置是否启用CURL的调试信息 */
  33. void enableCurlDebug(bool isPrint = false);
  34. /* 列出文件列表 */
  35. bool getFileList(std::string dir, std::vector<std::string>& fileList);
  36. /* 获取文件夹列表 */
  37. bool getDirList(std::string dir, std::vector<std::string>& dirList);
  38. /* 判断文件夹是否存在 */
  39. bool isDirExist(const std::string& dir);
  40. /* 创建FTP文件夹 */
  41. bool createDirectory(const std::string& ftpDir);
  42. /* 创建FTP文件夹,递归创建 */
  43. bool createDirectories(const std::string& ftpDir);
  44. /* 下载文件 */
  45. bool downloadFile(const std::string& remoteFile, const std::string& localFile, size_t timeout = 30);
  46. /* 下载文件到数组 */
  47. bool downloadToArray(const std::string& remoteFile,std::vector<char>& arrayInfo, size_t timeout = 30);
  48. /* 上传文件 */
  49. bool uploadFile(const std::string& localFile, const std::string& remoteFile, size_t timeout = 30, bool isCreateDir = false);
  50. /* 上传文件,上传数据 */
  51. bool uploadData(char* srcData, size_t size, const std::string& remoteFile, size_t timeout = 30, bool isCreateDir = false);
  52. private:
  53. /* 列出所有内容 */
  54. bool listAll(CURL* curl, std::string dir, std::vector<CF_FileInfo>& fileInfoList);
  55. /* 检查FTP文件夹路径是否合规,不合规就修改 */
  56. std::string checkDirPath(const std::string& dir);
  57. /* 检查FTP文件路径是否合规 */
  58. std::string checkFilePath(const std::string& file);
  59. /* 检查本地文件夹是否存在,不存在则创建 */
  60. bool checkLocalDir(const std::string& localDir);
  61. /* 执行curl,添加重试机制 */
  62. bool performCurl(CURL* curl);
  63. /* 重置curl设置 */
  64. inline bool resetCurl(CURL* curl);
  65. /* 设置sftp,如果是sftp就设置 */
  66. inline bool setSftp(CURL* curl);
  67. private:
  68. std::mutex m_mutexCurl; /* curl互斥锁 */
  69. std::string m_currentDir; /* 当前目录 */
  70. CURL* m_curl = nullptr; /* curl句柄 */
  71. std::string m_IP; /* IP */
  72. int m_port = 21; /* 端口 */
  73. std::string m_ftpUrl; /* ftpUrl */
  74. std::string m_username;
  75. std::string m_password;
  76. bool m_isSftp = false; /* 是否是SFTP */
  77. bool m_isIgnoreSSLCert = false; /* 是否忽略SSL证书 */
  78. bool m_enableCurlDebug = false; /* 是否打印调试信息 */
  79. std::string m_caCertFile; /* CA证书文件 */
  80. };
  81. #endif /* CURLFTP_H */