CurlFtp.cpp 33 KB

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