CurlFtp.cpp 25 KB

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