#ifndef CURLFTP_H #define CURLFTP_H #include "curl/curl.h" #include #include #include #include "CurlFtpInfo.h" /** * 使用CURL进行FTP操作,这里是单线程的,需要多线程同时操作,就创建多个实例 * * 1.默认超时时间是30s * 2.FTP默认端口是21,SFTP默认端口是22 * 3.设置好IP和端口号后,后续输入的文件路径都是相对路径,无需添加IP和端口号 * 4.使用ftp,用setFtpIPAndPort()设置IP和端口,使用sftp,用setSftpIPAndPort()设置IP和端口 * 5.这里使用了filesystem,需要C++17支持,uos默认的gcc版本是8.3,支持C++17,但是需要手动开启 * 这个库的支持,在项目文件里添加“stdc++fs”这个库,如下: * target_link_libraries(${this_exe} PRIVATE stdc++fs) * 6. 如果C++版本低于C++17,会改为Qt的QDir */ class CurlFtp { public: CurlFtp(); ~CurlFtp(); /*********************** 成员函数 *********************/ /* 设置用户名和密码 */ bool setUsernameAndPassword(const std::string& username, const std::string& password = ""); /* 设置IP和端口 */ bool setFtpIPAndPort(const std::string& IP, const int port = 21); /* 设置SFTP的IP和端口 */ bool setSftpIPAndPort(const std::string& IP, const int port = 22); /* 设置SFTP密钥,仅对SFTP生效 */ bool setSftpPrivateKey(const std::string& privateKeyFile, const std::string& publicKeyFile = "", const std::string& passphrase = ""); /* 设置启用SFTP密钥,仅对SFTP生效 */ void setSftpEnableKey(bool isEnable) { m_isSftpEnableKey = isEnable; } /* 设置是否启用CURL的调试信息 */ void enableCurlDebug(bool isPrint = false); /* 获取列表,包括文件和文件夹 */ bool getList(std::string dir, std::vector& fileInfoList); /* 列出文件列表 */ bool getFileList(std::string dir, std::vector& fileList); /* 获取文件夹列表 */ bool getDirList(std::string dir, std::vector& dirList); /* 判断文件夹是否存在 */ bool existsDir(const std::string& dir); /* 创建FTP文件夹,递归创建 */ bool createDirectory(const std::string& ftpDir); /* 删除文件,文件后面不能带有“/”否则会报错,这里是绝对路径 */ bool deleteFile(const std::string& remoteFile); /* 删除文件夹,递归删除 */ bool deleteDirectory(const std::string& ftpDir); /* 下载文件 */ bool downloadFile(const std::string& remoteFile, const std::string& localFile, size_t timeout = 30); /* 下载文件到数组 */ bool downloadToArray(const std::string& remoteFile,std::vector& arrayInfo, size_t timeout = 30); /* 上传文件 */ bool uploadFile(const std::string& localFile, const std::string& remoteFile, size_t timeout = 30, bool isCreateDir = false); /* 上传文件,上传数据 */ bool uploadData(char* srcData, size_t size, const std::string& remoteFile, size_t timeout = 30, bool isCreateDir = false); private: /* 设置常规通用的curl设置 */ bool setCommonCurlOptions(const std::string& ftpUrl, size_t timeout); /* 列出所有内容 */ // bool listAll(CURL* curl, std::string dir, std::vector& fileInfoList); /* 通过通配符列出文件信息,SFTP依旧是使用正则表达式解析字符串 */ bool listByWildcard(const std::string& dir, std::vector& out); /* 检查FTP文件夹路径是否合规,不合规就修改 */ std::string checkDirPath(const std::string& dir); /* 检查FTP文件路径是否合规 */ std::string checkFilePath(const std::string& file); /* 检查本地文件夹是否存在,不存在则创建 */ bool checkLocalDir(const std::string& localDir); /* 检查本地文件夹是否存在,不会创建 */ bool checkLocalDirExist(const std::string& localDir); /* 检查本地文件是否存在 */ bool checkLocalFileExist(const std::string& localFile); /* 执行curl,添加重试机制 */ bool performCurl(); bool performCurlStrict(); /* 重置curl设置 */ inline bool resetCurl(); /* 设置sftp,如果是sftp就设置 */ inline bool setSftp(); /* 检查FTP文件夹是否存在 */ bool checkFtpDirExist(const std::string& dir); /* 检查FTP/SFTP文件是否存在,使用通配符获取文件列表匹配 */ bool checkFtpFileExist(const std::string& file); /* 进入一个路径 */ bool changeToDir(const std::string& dir); /* 删除一个文件,相对路径,调用这个函数,需要先调用上面那个函数进入路径 */ bool deleteFileRelative(const std::string& remoteFile); /* 删除一个文件夹,相对路径,调用这个函数,需要先调用上面那个函数进入路径 */ bool deleteDirectoryRelative(const std::string& ftpDir); /* 创建一个FTP文件夹 */ bool createOneDirectory(const std::string& ftpDir); private: std::mutex m_mutexCurl; /* curl互斥锁 */ std::string m_currentDir; /* 当前目录 */ CURL* m_curl = nullptr; /* curl句柄 */ std::string m_IP; /* IP */ int m_port = 21; /* 端口 */ std::string m_ftpUrl; /* ftpUrl */ std::string m_username; std::string m_password; bool m_isSftp = false; /* 是否是SFTP */ bool m_enableCurlDebug = false; /* 是否打印调试信息 */ bool m_isSftpEnableKey = false; /* 是否启用SFTP密钥,默认不启用 */ std::string m_sftpPrivateKeyFile; /* SFTP私钥文件 */ std::string m_sftpPublicKeyFile; /* SFTP公钥文件 */ std::string m_sftpPassphrase; /* SFTP密钥密码 */ }; #endif /* CURLFTP_H */