CurlFtp.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990
  1. #include "CurlFtp.h"
  2. #include <regex>
  3. #include <iosfwd>
  4. #include <filesystem>
  5. #include <fstream>
  6. #include "fmtlog.h"
  7. #if defined(_WIN32)
  8. #endif /* _WIN32 */
  9. /* ==================================================================================
  10. * *********************************** 全局变量 *************************************
  11. * ==================================================================================
  12. */
  13. /* 是否有初始化实例,主要用于静态函数调用curl_global_cleanup()函数前判断,有实例就不调用 */
  14. static bool hasInstace = false;
  15. /* ==================================================================================
  16. * *********************************** 全局函数 *************************************
  17. * ==================================================================================
  18. */
  19. /**
  20. * @brief 写入回调函数
  21. *
  22. * @param contents curl的数据缓冲区
  23. * @param size 数据大小,单位是size_t
  24. * @param nmemb size_t的单位字节数
  25. * @param userStr 用户提供的字符串,格式可以是任意的
  26. * @return size_t 总共获取到的数据大小,单位是字节
  27. */
  28. static size_t writeStringCallback(void *contents, size_t size, size_t nmemb, std::string *userStr)
  29. {
  30. size_t newLength = size * nmemb;
  31. size_t oldLength = userStr->size();
  32. try
  33. {
  34. userStr->resize(oldLength + newLength);
  35. }
  36. catch(std::bad_alloc &e)
  37. {
  38. //handle memory problem
  39. return 0;
  40. }
  41. std::copy_n((char*)contents, newLength, userStr->begin() + oldLength);
  42. return size * nmemb;
  43. }
  44. /**
  45. * @brief 写入回调函数,listFiles需要调用
  46. *
  47. * @param buffer curl下载回来的数据
  48. * @param size 数据大小
  49. * @param nmemb 数据单位字节数
  50. * @param userp 用户传进来的容器
  51. * @return int 返回拷贝的字节数
  52. */
  53. static int writeStringListCallback(void* buffer, size_t size, size_t nmemb, void* userp)
  54. {
  55. std::vector<std::string>* fileList = static_cast<std::vector<std::string>*>(userp);
  56. std::string line(static_cast<char*>(buffer), size * nmemb);
  57. // printf("line = %s\n", line.c_str());
  58. fileList->push_back(line);
  59. return size * nmemb;
  60. }
  61. /**
  62. * @brief 写入文件回调函数
  63. *
  64. * @param contents 读取到的数据内容
  65. * @param size 数据大小
  66. * @param nmemb 数据单位
  67. * @param pFile 文件指针
  68. * @return size_t 实际读取的大小
  69. */
  70. static size_t writeDataCallBack(void* contents, size_t size, size_t nmemb, std::ostream* pFile)
  71. {
  72. pFile->write(reinterpret_cast<char*>(contents), size * nmemb);
  73. return size * nmemb;
  74. }
  75. /**
  76. * @brief 读取文件回调函数
  77. *
  78. * @param contents 下载到的数据内容
  79. * @param size 数据大小
  80. * @param nmemb 数据单位
  81. * @param pFile 文件指针
  82. * @return size_t 写入的大小
  83. */
  84. static size_t readDataCallBack(void* contents, size_t size, size_t nmemb, std::istream* pFile)
  85. {
  86. pFile->read(reinterpret_cast<char*>(contents), size * nmemb);
  87. /* 获取读取到的字节数,可能读取到文件末尾,所以不能直接使用传入的字节数 */
  88. size_t readSize = pFile->gcount();
  89. return readSize;
  90. }
  91. /**
  92. * @brief 上传和下载进度回调函数,这个函数kennel会被多次调用,即使是没有在下载的时候,因此需要判断传入的数据是否是0
  93. * 必须将 CURLOPT_NOPROGRESS 设为 0 才能真正调用该函数。
  94. *
  95. * @param clientp 通过CURLOPT_XFERINFODATA 设置的指针,libcurl 不会使用它,只会将其从应用程序传递给回调函数。
  96. * 可以通过这个指针将下载的进度传递给应用程序
  97. * @param dltotal 下载的总字节数,上传的时候这个为0
  98. * @param dlnow 已经下载的总字节数
  99. * @param ultotal 需要上传的总字节数,下载的时候这个为0
  100. * @param ulnow 已经上传的总字节数
  101. * @return int
  102. */
  103. static int progress_callback(void *clientp,
  104. curl_off_t dltotal,
  105. curl_off_t dlnow,
  106. curl_off_t ultotal,
  107. curl_off_t ulnow)
  108. {
  109. // FMTLOG_DEBUG("dTotal: {}, dNow: {}, uTotal: {}, uNow: {}", dltotal, dlnow, ultotal, ulnow);
  110. if(dltotal == 0 && ultotal == 0)
  111. {
  112. return 0;
  113. }
  114. /* 正在下载 */
  115. if(dltotal > 0)
  116. {
  117. /* 计算进度,百分比 */
  118. double downloadPercent = (double)dlnow / (double)dltotal * 100;
  119. /* 使用回车刷新这一行 */
  120. FMTLOG_DEBUG_NON("Download Total / now : {} / {}, {:.2f}%\r", dltotal, dlnow, downloadPercent);
  121. }
  122. /* 正在上传 */
  123. else if(ultotal > 0)
  124. {
  125. double uploadPercent = (double)ulnow / (double)ultotal * 100;
  126. FMTLOG_DEBUG_NON("Upload Total / now : {} / {}, {:.2f}%\r", ultotal, ulnow, uploadPercent);
  127. }
  128. return 0;
  129. }
  130. /* 使用Windows API进行编码转换 */
  131. #if defined(_WIN32)
  132. static char* GBToUTF8(const char* gb2312)
  133. {
  134. int len = MultiByteToWideChar(CP_ACP, 0, gb2312, -1, NULL, 0);
  135. wchar_t* wstr = new wchar_t[len+1];
  136. memset(wstr, 0, len+1);
  137. MultiByteToWideChar(CP_ACP, 0, gb2312, -1, wstr, len);
  138. len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
  139. char* str = new char[len+1];
  140. memset(str, 0, len+1);
  141. WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL);
  142. if(wstr) delete[] wstr;
  143. return str;
  144. }
  145. #endif /* _WIN32 */
  146. /**
  147. * @brief 解析字符串文件信息,如果FTP服务器规定好个编码,不需要进行转换
  148. *
  149. * @param strSrc 读取到的字符串
  150. * @param fileList 文件信息列表
  151. * @return true
  152. * @return false
  153. */
  154. static bool parseFileInfo(const std::string& strSrc, std::vector<CF_FileInfo>& fileInfo)
  155. {
  156. #if defined(_WIN32)
  157. // auto str1 = GBToUTF8(strSrc.c_str());
  158. std::string str2 = strSrc;
  159. #else
  160. std::string str2 = strSrc;
  161. #endif /* _WIN32 */
  162. // FMTLOG_DEBUG("\n{}", str2);
  163. /* 正则表达式,匹配多个空格 */
  164. std::regex reg(R"(( )+)");
  165. /* 匹配以非空格开头的字符,空格隔开,中间任意字符,空格隔开,非空格组成的结尾
  166. * (文件类型) ... (文件大小,$5) ... (文件名)
  167. */
  168. std::regex reg1(R"(^([^ ]+) (\d*) (\w*) (\w*) (\d*) (.*) ([^ ]+)$)");
  169. // FMTLOG_INFO("\n-------------------\n");
  170. /* 匹配换行符,分割每一行 */
  171. std::regex reg2(R"(\n)");
  172. /* 匹配文件夹 */
  173. std::regex reg3(R"(^d.*)");
  174. /* 匹配文件 */
  175. std::regex reg4(R"(^-.*$)");
  176. /* -1 表示对正则表达式之前的子序列感兴趣
  177. * 0 表示对正则表达式本身感兴趣,输出的就是换行符 */
  178. std::sregex_token_iterator it2(str2.begin(), str2.end(), reg2, -1);
  179. /* 这里取出每一行 */
  180. for(; it2 != std::sregex_token_iterator(); ++it2)
  181. {
  182. /* 去掉多余的空格 */
  183. auto line = std::regex_replace(it2->str(), reg, " ");
  184. // FMTLOG_INFO("{}", line);
  185. CF_FileInfo fi;
  186. /* 取出文件类型 */
  187. std::string strFileType = std::regex_replace(line, reg1, "$1");
  188. if(std::regex_match(strFileType, reg3))
  189. {
  190. fi.type = CF_FileType::DIR;
  191. }else if (std::regex_match(strFileType, reg4))
  192. {
  193. fi.type = CF_FileType::FILE;
  194. }
  195. /* 取出文件大小 */
  196. std::string strFileSize = std::regex_replace(line, reg1, "$5");
  197. fi.size = std::stoull(strFileSize);
  198. /* 取出文件名 */
  199. std::string strFileName = std::regex_replace(line, reg1, "$7");
  200. fi.name = strFileName;
  201. /* 加入队列 */
  202. fileInfo.push_back(fi);
  203. }
  204. return true;
  205. }
  206. /* ==================================================================================
  207. * *********************************** 成员函数 *************************************
  208. * ================================================================================== */
  209. CurlFtp::CurlFtp()
  210. {
  211. /* 调用初始化函数,这个函数可以重复调用 */
  212. curl_global_init(CURL_GLOBAL_DEFAULT);
  213. /* 初始化curl */
  214. hasInstace = true;
  215. }
  216. CurlFtp::~CurlFtp()
  217. {
  218. /* 清理curl */
  219. curl_global_cleanup();
  220. hasInstace = false;
  221. }
  222. /**
  223. * @brief 列出FTP文件夹
  224. *
  225. * @param ftpUrl 需要列出文件夹的FTP地址
  226. * @param username
  227. * @param password
  228. * @return std::string
  229. */
  230. std::string CurlFtp::listDir(const std::string &ftpUrl, const std::string &username, const std::string &password)
  231. {
  232. CURL *curl;
  233. CURLcode res;
  234. bool result = false;
  235. std::string retList;
  236. /* 1. 初始化curl,这个函数需要在调用任何curl函数之前 */
  237. curl_global_init(CURL_GLOBAL_DEFAULT);
  238. /* 2. 获取一个curl句柄 */
  239. curl = curl_easy_init();
  240. if(curl)
  241. {
  242. /* 3. 设置curl选项 */
  243. curl_easy_setopt(curl, CURLOPT_URL, ftpUrl.c_str());
  244. curl_easy_setopt(curl, CURLOPT_USERNAME, username.c_str());
  245. curl_easy_setopt(curl, CURLOPT_PASSWORD, password.c_str());
  246. // curl_easy_setopt(curl, CURLOPT_DIRLISTONLY, 1L); // We only want the directory listing
  247. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeStringCallback);
  248. curl_easy_setopt(curl, CURLOPT_WRITEDATA, &retList);
  249. /* 4. 发送信息,一直等待知道返回 */
  250. res = curl_easy_perform(curl);
  251. // printf("res = %d\n", res);
  252. if(res != CURLE_OK)
  253. {
  254. fprintf(stderr, "getDirList() failed, error code %d, :%s\n",res, curl_easy_strerror(res));
  255. } else
  256. {
  257. // std::cout << "Directory list: \n" << retList << std::endl;
  258. }
  259. /* 5. 清理curl */
  260. curl_easy_cleanup(curl);
  261. }
  262. if(hasInstace == false)
  263. {
  264. curl_global_cleanup();
  265. }
  266. return retList;
  267. }
  268. /* 列出文件夹中的所有文件 */
  269. bool CurlFtp::listFiles(const std::string &ftpUrl, const std::string &username, const std::string &password, std::vector<std::string>& fileList)
  270. {
  271. CURL *curl;
  272. CURLcode res;
  273. bool result = false;
  274. curl_global_init(CURL_GLOBAL_DEFAULT);
  275. curl = curl_easy_init();
  276. if(curl)
  277. {
  278. curl_easy_setopt(curl, CURLOPT_URL, (ftpUrl).c_str());
  279. curl_easy_setopt(curl, CURLOPT_USERNAME, username.c_str());
  280. curl_easy_setopt(curl, CURLOPT_PASSWORD, password.c_str());
  281. curl_easy_setopt(curl, CURLOPT_DIRLISTONLY, 1L);
  282. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeStringListCallback);
  283. curl_easy_setopt(curl, CURLOPT_WRITEDATA, &fileList);
  284. res = curl_easy_perform(curl);
  285. if(res != CURLE_OK)
  286. {
  287. fprintf(stderr, "Failed to get file list, error code :%d ,%s\n", res, curl_easy_strerror(res));
  288. }
  289. else
  290. {
  291. // for(const std::string& filename : fileList)
  292. // {
  293. // printf("%s\n", filename.c_str());
  294. // }
  295. result = true;
  296. }
  297. curl_easy_cleanup(curl);
  298. }
  299. if(hasInstace == false)
  300. {
  301. curl_global_cleanup();
  302. }
  303. return result;
  304. }
  305. /* 创建文件夹 */
  306. bool CurlFtp::createDir(const std::string &ftpUrl, const std::string &username, const std::string &password, const std::string &dirName)
  307. {
  308. CURL *curl;
  309. CURLcode res;
  310. bool result = false;
  311. curl_global_init(CURL_GLOBAL_DEFAULT);
  312. curl = curl_easy_init();
  313. if(curl)
  314. {
  315. curl_easy_setopt(curl, CURLOPT_URL, ftpUrl.c_str());
  316. curl_easy_setopt(curl, CURLOPT_USERNAME, username.c_str());
  317. curl_easy_setopt(curl, CURLOPT_PASSWORD, password.c_str());
  318. // Create a list of FTP commands to be executed on the server
  319. struct curl_slist *headerlist = NULL;
  320. std::string mkdir = "MKD " + dirName;
  321. headerlist = curl_slist_append(headerlist, mkdir.c_str());
  322. // Set the list of FTP commands to be executed on the server before the transfer
  323. curl_easy_setopt(curl, CURLOPT_QUOTE, headerlist);
  324. // Tell libcurl to not include the body in the output
  325. curl_easy_setopt(curl, CURLOPT_NOBODY, 1L);
  326. res = curl_easy_perform(curl);
  327. if(res != CURLE_OK)
  328. {
  329. fprintf(stderr, "createDir() failed, error code :%d ,%s\n", res, curl_easy_strerror(res));
  330. result = false;
  331. }else
  332. {
  333. result = true;
  334. }
  335. curl_easy_cleanup(curl);
  336. curl_slist_free_all(headerlist);
  337. }
  338. if(hasInstace == false)
  339. {
  340. curl_global_cleanup();
  341. }
  342. return result;
  343. }
  344. /* 设置IP和端口 */
  345. bool CurlFtp::setFtpIPAndPort(const std::string& IP, const int port)
  346. {
  347. /*先判断是否有ftp器前缀,通过正则表达式,取出IP地址和端口号,去掉前缀和后面的'/ */
  348. m_IP = IP;
  349. m_port = port;
  350. m_ftpUrl = "ftp://" + m_IP + ":" + std::to_string(m_port);
  351. FMTLOG_INFO("Set ftpUrl = {}", m_ftpUrl);
  352. m_isSftp = false;
  353. return true;
  354. }
  355. /* 设置SFTP的IP和端口 */
  356. bool CurlFtp::setSftpIPAndPort(const std::string& IP, const int port)
  357. {
  358. m_IP = IP;
  359. m_port = port;
  360. m_ftpUrl = "sftp://" + m_IP + ":" + std::to_string(m_port);
  361. FMTLOG_INFO("Set sftpUrl = {}", m_ftpUrl);
  362. m_isSftp = true;
  363. return true;
  364. }
  365. /* 设置是否忽略SSL证书,使用SFTP不建议忽略 */
  366. void CurlFtp::setIgnoreSSLCert(bool isIgnore)
  367. {
  368. m_isIgnoreSSLCert = isIgnore;
  369. }
  370. /* 设置CA证书文件 */
  371. void CurlFtp::setCaCertFile(const std::string& caCertFile)
  372. {
  373. m_caCertFile = caCertFile;
  374. }
  375. /* 设置用户名和密码 */
  376. bool CurlFtp::setFtpUsernameAndPassword(const std::string& username, const std::string& password)
  377. {
  378. m_username = username;
  379. m_password = password;
  380. return true;
  381. }
  382. /* 列出文件列表 */
  383. bool CurlFtp::getFileList(std::string dir, std::vector<std::string>& fileList)
  384. {
  385. if(m_IP.empty())
  386. {
  387. FMTLOG_WARN("IP or port is empty");
  388. return false;
  389. }
  390. /* 检查dir,添加前缀“/” */
  391. auto dirTmp = checkDirPath(dir);
  392. CURL *curl = nullptr;
  393. curl = curl_easy_init();
  394. if(curl == nullptr)
  395. {
  396. FMTLOG_ERROR("curl init failed !");
  397. return false;
  398. }
  399. std::vector<CF_FileInfo> listInfo;
  400. /* 获取文件信息 */
  401. listAll(curl, dirTmp, listInfo);
  402. for(const CF_FileInfo& fi : listInfo)
  403. {
  404. // FMTLOG_INFO("type = {}, size = {}, name = {}", (int)fi.type, fi.size, fi.name);
  405. if(fi.type == CF_FileType::FILE)
  406. {
  407. fileList.push_back(fi.name);
  408. }
  409. }
  410. curl_easy_cleanup(curl);
  411. return true;
  412. }
  413. /* 获取文件夹列表 */
  414. bool CurlFtp::getDirList(std::string dir, std::vector<std::string>& dirList)
  415. {
  416. if(m_IP.empty())
  417. {
  418. FMTLOG_WARN("IP or port is empty");
  419. return false;
  420. }
  421. /* 检查dir,添加前缀“/” */
  422. auto dirTmp = checkDirPath(dir);
  423. CURL *curl = nullptr;
  424. curl = curl_easy_init();
  425. if(curl == nullptr)
  426. {
  427. FMTLOG_ERROR("curl init failed !");
  428. return false;
  429. }
  430. std::vector<CF_FileInfo> listInfo;
  431. /* 获取文件信息 */
  432. listAll(curl, dirTmp, listInfo);
  433. for(const CF_FileInfo& fi : listInfo)
  434. {
  435. // FMTLOG_INFO("type = {}, size = {}, name = {}", (int)fi.type, fi.size, fi.name);
  436. if(fi.type == CF_FileType::DIR)
  437. {
  438. dirList.push_back(fi.name);
  439. }
  440. }
  441. curl_easy_cleanup(curl);
  442. return true;
  443. }
  444. /* 判断文件夹是否存在 */
  445. bool CurlFtp::isDirExist(const std::string& dir)
  446. {
  447. if(m_ftpUrl.empty())
  448. {
  449. FMTLOG_ERROR("ftpUrl is empty");
  450. return false;
  451. }
  452. /* 检查传入的文件夹 */
  453. auto dirTmp = checkDirPath(dir);
  454. CURL* curl = nullptr;
  455. curl = curl_easy_init();
  456. if(curl == nullptr)
  457. {
  458. FMTLOG_ERROR("curl init failed !");
  459. return false;
  460. }
  461. std::string ftpUrl = m_ftpUrl + dirTmp;
  462. curl_easy_setopt(curl, CURLOPT_URL, ftpUrl.c_str());
  463. curl_easy_setopt(curl, CURLOPT_USERNAME, m_username.c_str());
  464. curl_easy_setopt(curl, CURLOPT_PASSWORD, m_password.c_str());
  465. /* 获取文件夹是否存在,不需要接收文件 */
  466. curl_easy_setopt(curl, CURLOPT_NOBODY, 1L);
  467. CURLcode res = curl_easy_perform(curl);
  468. bool result = true;
  469. if(res != CURLE_OK)
  470. {
  471. // FMTLOG_ERROR("Failed to get file list, error code: {} ,{}", (int)res, curl_easy_strerror(res));
  472. result = false;
  473. }
  474. return result;
  475. }
  476. /**
  477. * @brief 创建FTP文件夹,只能创建一层文件夹
  478. *
  479. * @param ftpDir 文件夹路径
  480. * @return true 文件夹创建成功或者文件夹存在
  481. * @return false
  482. */
  483. bool CurlFtp::createDirectory(const std::string& ftpDir)
  484. {
  485. if(m_ftpUrl.empty())
  486. {
  487. FMTLOG_ERROR("ftpUrl is empty");
  488. return false;
  489. }
  490. /* 先检查FTP文件夹是否存在,如果存在直接返回true */
  491. if(isDirExist(ftpDir))
  492. {
  493. return true;
  494. }
  495. /* 检查传入的文件夹格式 */
  496. auto dirTmp = checkDirPath(ftpDir);
  497. CURL *curl = curl_easy_init();
  498. if(curl == nullptr)
  499. {
  500. FMTLOG_ERROR("Create FTP DIR, curl init failed !");
  501. return false;
  502. }
  503. curl_easy_setopt(curl, CURLOPT_URL, m_ftpUrl.c_str());
  504. curl_easy_setopt(curl, CURLOPT_USERNAME, m_username.c_str());
  505. curl_easy_setopt(curl, CURLOPT_PASSWORD, m_password.c_str());
  506. /* 创建FTP头信息 */
  507. struct curl_slist *headerlist = NULL;
  508. std::string mkdir = "MKD " + dirTmp;
  509. headerlist = curl_slist_append(headerlist, mkdir.c_str());
  510. /* 设置FTP命令行选项 */
  511. curl_easy_setopt(curl, CURLOPT_QUOTE, headerlist);
  512. /* 不包含实体 */
  513. curl_easy_setopt(curl, CURLOPT_NOBODY, 1L);
  514. /* 启用跟随重定向 */
  515. curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
  516. /* 设置超时时间 */
  517. curl_easy_setopt(curl, CURLOPT_TIMEOUT, 30L);
  518. curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 30L);
  519. CURLcode res = curl_easy_perform(curl);
  520. bool result = true;
  521. if(res != CURLE_OK)
  522. {
  523. FMTLOG_ERROR("Failed to create FTP Dir, error code: {} ,{}", (int)res, curl_easy_strerror(res));
  524. result = false;
  525. }else
  526. {
  527. result = true;
  528. }
  529. curl_easy_cleanup(curl);
  530. curl_slist_free_all(headerlist);
  531. return result;
  532. }
  533. /* 创建FTP文件夹,递归创建 */
  534. bool CurlFtp::createDirectories(const std::string& ftpDir)
  535. {
  536. /* 检查路径格式,并去掉第一个 / */
  537. std::string ftpDirTmp = checkDirPath(ftpDir);
  538. std::string ftpDir2 = ftpDirTmp.substr(1, ftpDirTmp.size() -1);
  539. /* 将文件夹分开,取出每一个文件夹名称 */
  540. std::vector<std::string> strList;
  541. std::regex reg(R"(/)");
  542. std::sregex_token_iterator it(ftpDir2.begin(), ftpDir2.end(), reg, -1);
  543. for( ; it != std::sregex_token_iterator(); ++it)
  544. {
  545. strList.push_back(it->str());
  546. }
  547. /* 将每一层拼接起来,逐层递归 */
  548. std::vector<std::string> dirList;
  549. for(const std::string& dir : strList)
  550. {
  551. std::string dirTmp = "/";
  552. if(!dirList.empty())
  553. {
  554. dirTmp = dirList.back();
  555. dirTmp += "/";
  556. }
  557. dirTmp += dir;
  558. dirList.push_back(dirTmp);
  559. }
  560. /* 逐层创建 */
  561. for(const std::string& dir : dirList)
  562. {
  563. // FMTLOG_DEBUG("Create dir: {}", dir);
  564. if(!createDirectory(dir))
  565. {
  566. FMTLOG_ERROR("Failed to create dir: {}", dir);
  567. return false;
  568. }
  569. }
  570. return true;
  571. }
  572. /* 下载文件 */
  573. bool CurlFtp::downloadFile(const std::string& remoteFile, const std::string& localFile)
  574. {
  575. if(m_ftpUrl.empty())
  576. {
  577. FMTLOG_ERROR("ftpUrl is empty");
  578. return false;
  579. }
  580. /* 检查传入的文件是否符合规范 */
  581. std::string remoteFileTmp = checkFilePath(remoteFile);
  582. std::string ftpUrl = m_ftpUrl + remoteFileTmp;
  583. /* 检查本地文件夹是否存在,不存在则创建 */
  584. // std::string localDir = localFile.substr(0, localFile.find_last_of("/")); /* 去掉最后的 / */
  585. std::string localDirTmp = localFile.substr(0, localFile.find_last_of("/"));
  586. if(!checkLocalDir(localDirTmp))
  587. {
  588. FMTLOG_ERROR("Failed to create local dir: {}", localDirTmp);
  589. return false;
  590. }
  591. CURL* curl = nullptr;
  592. curl = curl_easy_init();
  593. if(curl == nullptr)
  594. {
  595. FMTLOG_ERROR("curl init failed !");
  596. return false;
  597. }
  598. /* 打开文件 */
  599. std::ofstream ofs;
  600. ofs.open(localFile, std::ios::out | std::ios::binary | std::ios::trunc);
  601. /* 设置FTP地址 */
  602. curl_easy_setopt(curl, CURLOPT_URL, ftpUrl.c_str());
  603. curl_easy_setopt(curl, CURLOPT_PORT, m_port);
  604. /* 设置用户名和密码 */
  605. curl_easy_setopt(curl, CURLOPT_USERNAME, m_username.c_str());
  606. curl_easy_setopt(curl, CURLOPT_PASSWORD, m_password.c_str());
  607. /* 启用跟随重定向 */
  608. curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
  609. /* 设置回调函数 */
  610. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeDataCallBack);
  611. curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ofs);
  612. /* 设置超时时间 */
  613. // curl_easy_setopt(curl, CURLOPT_TIMEOUT, 30L);
  614. curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 30L);
  615. /* 设置进度回调函数 */
  616. curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, progress_callback);
  617. curl_easy_setopt(curl, CURLOPT_XFERINFODATA, nullptr);
  618. /* 启用下载进度回调函数 */
  619. curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
  620. /* 判断是否是SFTP */
  621. if(m_isSftp)
  622. {
  623. /* 判断是否需要设置CA证书 */
  624. if(m_isIgnoreSSLCert)
  625. {
  626. /* 忽略证书验证 */
  627. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
  628. /* 忽略主机名验证 */
  629. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
  630. } else
  631. {
  632. /* 设置CA证书 */
  633. curl_easy_setopt(curl, CURLOPT_CAINFO, m_caCertFile.c_str());
  634. }
  635. }
  636. /* 发送请求 */
  637. CURLcode res = curl_easy_perform(curl);
  638. if(res != CURLE_OK)
  639. {
  640. FMTLOG_ERROR("Failed to get file list, error code: {} ,{}", (int)res, curl_easy_strerror(res));
  641. FMTLOG_ERROR("ftpUrl = {}", ftpUrl);
  642. /* 清理下载失败的文件 */
  643. ofs.close();
  644. std::remove(localFile.c_str());
  645. return false;
  646. }
  647. /* 关闭文件,清理curl */
  648. ofs.close();
  649. curl_easy_cleanup(curl);
  650. /* 打印一个换行符 */
  651. FMTLOG_DEBUG_NON("\n");
  652. return true;
  653. }
  654. /**
  655. * @brief 上传文件
  656. *
  657. * @param localFile 本地文件
  658. * @param remoteFile 远程文件
  659. * @return true
  660. * @return false
  661. */
  662. bool CurlFtp::uploadFile(const std::string& localFile, const std::string& remoteFile)
  663. {
  664. if(m_ftpUrl.empty())
  665. {
  666. FMTLOG_ERROR("ftpUrl is empty");
  667. return false;
  668. }
  669. /* 检查本地文件是否存在 */
  670. if(!std::filesystem::exists(localFile))
  671. {
  672. FMTLOG_ERROR("Local file is not exist: {}", localFile);
  673. return false;
  674. }
  675. /* 检查FTP文件名是否符合规范 */
  676. std::string remoteFileTmp = checkFilePath(remoteFile);
  677. std::string remoteDirTmp = remoteFileTmp.substr(0, remoteFileTmp.find_last_of("/"));
  678. /* 检查远程FTP上的文件夹是否存在,不存在则创建 */
  679. if(!isDirExist(remoteDirTmp))
  680. {
  681. if(!createDirectories(remoteDirTmp))
  682. {
  683. // FMTLOG_ERROR("Failed to create remote dir: {}", remoteFileTmp);
  684. return false;
  685. }
  686. }
  687. /* 拼接远程文件的url */
  688. std::string ftpUrl = m_ftpUrl + remoteFileTmp;
  689. /* 打开文件 */
  690. std::ifstream ifs;
  691. ifs.open(localFile, std::ios::in | std::ios::binary);
  692. if(!ifs.is_open())
  693. {
  694. FMTLOG_ERROR("Failed to open local file: {}", localFile);
  695. return false;
  696. }
  697. /* 获取文件大小 */
  698. // auto startPos = ifs.tellg();
  699. ifs.seekg(0, std::ios::end);
  700. auto fileSize = ifs.tellg();
  701. /* 恢复指针到文件头 */
  702. ifs.seekg(0, std::ios::beg);
  703. FMTLOG_DEBUG("File size: {}", (long)fileSize);
  704. CURL* curl = nullptr;
  705. curl = curl_easy_init();
  706. if(curl == nullptr)
  707. {
  708. FMTLOG_ERROR("curl init failed !");
  709. ifs.close();
  710. return false;
  711. }
  712. /* 设置FTP地址 */
  713. curl_easy_setopt(curl, CURLOPT_URL, ftpUrl.c_str());
  714. curl_easy_setopt(curl, CURLOPT_PORT, m_port);
  715. /* 设置用户名和密码 */
  716. curl_easy_setopt(curl, CURLOPT_USERNAME, m_username.c_str());
  717. curl_easy_setopt(curl, CURLOPT_PASSWORD, m_password.c_str());
  718. /* 启用跟随重定向 */
  719. curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
  720. /* 启用上传 */
  721. curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
  722. /* 设置回调函数 */
  723. curl_easy_setopt(curl, CURLOPT_READFUNCTION, readDataCallBack);
  724. curl_easy_setopt(curl, CURLOPT_READDATA, &ifs);
  725. /* 设置上传文件的大小 */
  726. curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)fileSize);
  727. /* 设置超时时间 */
  728. // curl_easy_setopt(curl, CURLOPT_TIMEOUT, 30L);
  729. curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 30L);
  730. /* 设置进度回调函数 */
  731. curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, progress_callback);
  732. curl_easy_setopt(curl, CURLOPT_XFERINFODATA, nullptr);
  733. /* 启用下载进度回调函数 */
  734. curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
  735. /* 发送请求 */
  736. CURLcode res = curl_easy_perform(curl);
  737. bool result = true;
  738. if(res != CURLE_OK)
  739. {
  740. FMTLOG_ERROR("Upload file failed, error code: {} ,{}", (int)res, curl_easy_strerror(res));
  741. FMTLOG_ERROR("ftpUrl = {}", ftpUrl);
  742. result = false;
  743. }
  744. /* 关闭文件,清理curl */
  745. ifs.close();
  746. curl_easy_cleanup(curl);
  747. /* 打印一个换行符 */
  748. FMTLOG_DEBUG_NON("\n");
  749. return result;
  750. }
  751. /**
  752. * @brief 列出文件列表,这个需要的参数很多,无法设置成静态函数
  753. *
  754. * @param curl CURL句柄
  755. * @param dir 文件夹,相对路径,不带有IP和端口号
  756. * @param fileList 返回值,文件列表
  757. * @return true
  758. * @return false
  759. */
  760. bool CurlFtp::listAll(CURL* curl, std::string dir, std::vector<CF_FileInfo>& fileInfoList)
  761. {
  762. if(m_IP.empty())
  763. {
  764. FMTLOG_ERROR("IP is empty");
  765. return false;
  766. }
  767. if(curl == nullptr)
  768. {
  769. FMTLOG_ERROR("curl is nullptr");
  770. return false;
  771. }
  772. bool result = false;
  773. std::string strSrc;
  774. /* 先设置FTP地址 */
  775. std::string ftpUrl = m_ftpUrl + dir;
  776. curl_easy_setopt(curl, CURLOPT_URL, ftpUrl.c_str());
  777. curl_easy_setopt(curl, CURLOPT_PORT, m_port);
  778. /* 设置用户名和密码 */
  779. curl_easy_setopt(curl, CURLOPT_USERNAME, m_username.c_str());
  780. curl_easy_setopt(curl, CURLOPT_PASSWORD, m_password.c_str());
  781. /* 设置列出文件命令,只列出文件名称,不携带信息 */
  782. // curl_easy_setopt(m_curl, CURLOPT_DIRLISTONLY, 1L);
  783. /* 设置回调函数 */
  784. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeStringCallback);
  785. /* 设置需要写入的容器,回调函数的第四个参数 */
  786. curl_easy_setopt(curl, CURLOPT_WRITEDATA, &strSrc);
  787. /* 设置超时时间 */
  788. curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10L);
  789. curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10L);
  790. /* 发送请求 */
  791. CURLcode res = curl_easy_perform(curl);
  792. if(res != CURLE_OK)
  793. {
  794. FMTLOG_ERROR("Failed to get file list, error code: {} ,{}", (int)res, curl_easy_strerror(res));
  795. FMTLOG_ERROR("ftpUrl = {}", ftpUrl);
  796. return false;
  797. }
  798. /* 解析字符串 */
  799. parseFileInfo(strSrc, fileInfoList);
  800. return true;
  801. }
  802. /* 检查文件夹路径是否合规,不合规就修改 */
  803. std::string CurlFtp::checkDirPath(const std::string& dir)
  804. {
  805. std::string dirTmp = dir;
  806. /* 检查是否以“/”开头 */
  807. std::regex reg(R"(^/[.]*)");
  808. if(!std::regex_match(dirTmp, reg))
  809. {
  810. dirTmp = "/" + dirTmp;
  811. }
  812. /* 如果有重复的“/”,替换成一个 “/” */
  813. std::regex reg1(R"(//)");
  814. dirTmp = std::regex_replace(dirTmp, reg1, "/");
  815. /* 检查是否以“/”结束 */
  816. std::regex reg2(R"([.]*/$)");
  817. if(!std::regex_match(dirTmp, reg2))
  818. {
  819. dirTmp = dirTmp + "/";
  820. }
  821. return dirTmp;
  822. }
  823. /* 检查文件路径是否合规 */
  824. std::string CurlFtp::checkFilePath(const std::string& file)
  825. {
  826. std::string dirTmp = file;
  827. /* 检查是否以“/”结束,如果是以“/”结尾,返回空字符串,并报错 */
  828. std::regex reg2(R"([.]*/$)");
  829. if(std::regex_match(dirTmp, reg2))
  830. {
  831. FMTLOG_ERROR("File path is not correct, end with '/'");
  832. return std::string();
  833. }
  834. /* 检查是否以“/”开头 */
  835. std::regex reg(R"(^/[.]*)");
  836. if(!std::regex_match(dirTmp, reg))
  837. {
  838. dirTmp = "/" + dirTmp;
  839. }
  840. /* 如果有重复的“/”,替换成一个 “/” */
  841. std::regex reg1(R"(//)");
  842. dirTmp = std::regex_replace(dirTmp, reg1, "/");
  843. return dirTmp;
  844. }
  845. /**
  846. * @brief 检查本地文件夹是否存在,不存在则创建
  847. * 这里默认传入的是文件夹路径,不是文件路径,如果最后有‘/’,会自动去掉
  848. *
  849. * @param localDir 文件夹路径
  850. * @return true
  851. * @return false
  852. */
  853. bool CurlFtp::checkLocalDir(const std::string& localDir)
  854. {
  855. /* 去掉最后的‘/’ */
  856. std::regex reg(R"([.]*/$)");
  857. std::string localDirTmp = std::regex_replace(localDir, reg, "");
  858. /* 检查文件夹是否存在 */
  859. if(std::filesystem::exists(localDirTmp))
  860. {
  861. return true;
  862. }
  863. /* 创建文件夹 */
  864. if(!std::filesystem::create_directories(localDirTmp))
  865. {
  866. FMTLOG_ERROR("Failed to create local dir: {}", localDirTmp);
  867. return false;
  868. }
  869. return true;
  870. }