123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323 |
- #include "CurlFtp.h"
- #include <regex>
- #include <iosfwd>
- #include <filesystem>
- #include <fstream>
- #include "spdlog/spdlog.h"
- #if defined(_WIN32)
- #endif
- static bool hasInstace = false;
- static thread_local std::chrono::system_clock::time_point lastTime;
- static thread_local uint64_t dCount;
- static thread_local uint64_t uCount;
- static thread_local uint64_t dSpeed;
- static thread_local uint64_t uSpeed;
- static size_t writeStringCallback(void *contents, size_t size, size_t nmemb, std::string *userStr)
- {
- size_t newLength = size * nmemb;
- size_t oldLength = userStr->size();
- try
- {
- userStr->resize(oldLength + newLength);
- }
- catch(std::bad_alloc &e)
- {
-
- return 0;
- }
- std::copy_n((char*)contents, newLength, userStr->begin() + oldLength);
- return size * nmemb;
- }
- static int writeStringListCallback(void* buffer, size_t size, size_t nmemb, void* userp)
- {
- std::vector<std::string>* fileList = static_cast<std::vector<std::string>*>(userp);
- std::string line(static_cast<char*>(buffer), size * nmemb);
-
- fileList->push_back(line);
- return size * nmemb;
- }
- static size_t writeFileCallBack(void* contents, size_t size, size_t nmemb, std::ostream* pFile)
- {
- pFile->write(reinterpret_cast<char*>(contents), size * nmemb);
- return size * nmemb;
- }
- static size_t writeDataCallBack(void* contents, size_t size, size_t nmemb, std::vector<char>* vecData)
- {
- size_t copySize = size * nmemb;
- vecData->insert(vecData->end(), (char*)contents, (char*)contents + copySize);
- return copySize;
- }
- static size_t readFileCallBack(void* contents, size_t size, size_t nmemb, std::istream* pFile)
- {
- pFile->read(reinterpret_cast<char*>(contents), size * nmemb);
-
- size_t readSize = pFile->gcount();
- return readSize;
-
- }
- static size_t readDataCallBack(void* contents, size_t size, size_t nmemb, CF_ArrayInfo* pData)
- {
- if(pData == nullptr)
- {
- return 0;
- }
-
- size_t copySize = size * nmemb;
- if(pData->size - pData->pos < copySize)
- {
- copySize = pData->size - pData->pos;
- }
- memcpy(contents, pData->data + pData->pos, copySize);
- pData->pos += copySize;
- return copySize;
- }
- void computeSpeed(uint64_t speed, double& retSpeed, std::string& unit)
- {
- double KB = speed / 1024.0;
- double MB = KB / 1024.0;
- double GB = MB / 1024.0;
- if(GB > 1)
- {
- unit = "GB/S";
- retSpeed = GB;
- }
- else if(MB > 1)
- {
- unit = "MB/S";
- retSpeed = MB;
- }
- else if(KB > 1)
- {
- unit = "KB/S";
- retSpeed = KB;
- }
- else {
- unit = "B/S";
- retSpeed = speed;
- }
- }
- void printProgress(CF_TransType type, curl_off_t total, curl_off_t now)
- {
- std::string transType;
- uint64_t count = 0;
- if(type == CF_TransType::DOWNLOAD)
- {
- transType = "Download";
- count = now - dCount;
- dCount = now;
- }
- else if (type == CF_TransType::UPLOAD)
- {
- transType = "Upload";
- count = now - uCount;
- uCount = now;
- }
-
- double percent = (double)now / (double)total * 100;
-
- double tKB = total / 1024.0;
- double tMB = tKB / 1024.0;
- double tGB = tMB / 1024.0;
-
- double speed = 0.0;
- std::string unit;
- computeSpeed(count, speed, unit);
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- if(tGB > 1)
- {
- double dGB = now / 1024.0 / 1024.0 / 1024.0;
- double speed = count / 1024.0 / 1024.0;
- printf("%s Total / now : %.2fGB / %.2fGB, %.2f%%, Speed:%.2f %s\r", transType.c_str(), tGB, dGB, percent, speed, unit.c_str());
-
- }
- else if(tMB > 1)
- {
- double dMB = now / 1024.0 / 1024.0;
- printf("%s Total / now : %.2fMB / %.2fMB, %.2f%%, Speed:%.2f %s\r", transType.c_str(), tMB, dMB, percent, speed, unit.c_str());
-
- }
- else if(tKB > 1)
- {
- double dKB = now / 1024.0;
- printf("%s Total / now : %.2fKB / %.2fKB, %.2f%%, Speed:%.2f %s\r", transType.c_str(), tKB, dKB, percent, speed, unit.c_str());
- }
- else
- {
- printf("%s Total / now : %ldB / %ldB, %.2f%%, Speed:%.2f %s\r", transType.c_str(), total, now, percent, speed, unit.c_str());
- }
- fflush(stdout);
- }
- static int progress_callback(void *clientp,
- curl_off_t dltotal,
- curl_off_t dlnow,
- curl_off_t ultotal,
- curl_off_t ulnow)
- {
-
- if(dltotal == 0 && ultotal == 0)
- {
- dCount = 0;
- uCount = 0;
- dSpeed = 0;
- uSpeed = 0;
- return 0;
- }
- std::chrono::system_clock::time_point nowTime = std::chrono::system_clock::now();
- auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(nowTime - lastTime).count();
-
- if((duration < 1000) && ((dltotal != dlnow) || (ultotal != ulnow)))
- {
- return 0;
- }
- lastTime = nowTime;
-
- if(dltotal > 0)
- {
- printProgress(CF_TransType::DOWNLOAD, dltotal, dlnow);
-
- }
-
- else if(ultotal > 0)
- {
- printProgress(CF_TransType::UPLOAD, ultotal, ulnow);
-
-
- }
- return 0;
- }
- #if defined(_WIN32)
- static char* GBToUTF8(const char* gb2312)
- {
- int len = MultiByteToWideChar(CP_ACP, 0, gb2312, -1, NULL, 0);
- wchar_t* wstr = new wchar_t[len+1];
- memset(wstr, 0, len+1);
- MultiByteToWideChar(CP_ACP, 0, gb2312, -1, wstr, len);
- len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
- char* str = new char[len+1];
- memset(str, 0, len+1);
- WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL);
- if(wstr) delete[] wstr;
- return str;
- }
- #endif
- static bool parseFileInfo(const std::string& strSrc, std::vector<CF_FileInfo>& fileInfo)
- {
- #if defined(_WIN32)
-
- std::string str2 = strSrc;
- #else
- std::string str2 = strSrc;
- #endif
-
-
-
- std::regex reg(R"(( )+)");
-
- std::regex reg1(R"(^([^ ]+) (\d*) (\w*) (\w*) (\d*) (.*) ([^ ]+)$)");
-
-
- std::regex reg2(R"(\n)");
-
- std::regex reg3(R"(^d.*)");
-
- std::regex reg4(R"(^-.*$)");
-
- std::sregex_token_iterator it2(str2.begin(), str2.end(), reg2, -1);
-
- for(; it2 != std::sregex_token_iterator(); ++it2)
- {
-
- auto line = std::regex_replace(it2->str(), reg, " ");
-
- CF_FileInfo fi;
-
- std::string strFileType = std::regex_replace(line, reg1, "$1");
- if(std::regex_match(strFileType, reg3))
- {
- fi.type = CF_FileType::DIR;
- }else if (std::regex_match(strFileType, reg4))
- {
- fi.type = CF_FileType::FILE;
- }
-
- std::string strFileSize = std::regex_replace(line, reg1, "$5");
- fi.size = std::stoull(strFileSize);
-
- std::string strFileName = std::regex_replace(line, reg1, "$7");
- fi.name = strFileName;
-
- fileInfo.push_back(fi);
- }
- return true;
- }
- CurlFtp::CurlFtp()
- {
-
- curl_global_init(CURL_GLOBAL_DEFAULT);
-
- hasInstace = true;
- }
- CurlFtp::~CurlFtp()
- {
-
- curl_easy_cleanup(m_curl);
- curl_global_cleanup();
- hasInstace = false;
- }
- bool CurlFtp::setUsernameAndPassword(const std::string& username, const std::string& password)
- {
- m_username = username;
- m_password = password;
- return true;
- }
- bool CurlFtp::setFtpIPAndPort(const std::string& IP, const int port)
- {
-
- m_IP = IP;
- m_port = port;
- m_ftpUrl = "ftp://" + m_IP + ":" + std::to_string(m_port);
- SPDLOG_INFO("Set ftpUrl = {}", m_ftpUrl);
- m_isSftp = false;
- return true;
- }
- bool CurlFtp::setSftpIPAndPort(const std::string& IP, const int port)
- {
- m_IP = IP;
- m_port = port;
- m_ftpUrl = "sftp://" + m_IP + ":" + std::to_string(m_port);
- SPDLOG_INFO("Set sftpUrl = {}", m_ftpUrl);
- m_isSftp = true;
- return true;
- }
- void CurlFtp::setIgnoreSSLCert(bool isIgnore)
- {
- m_isIgnoreSSLCert = isIgnore;
- }
- void CurlFtp::setCaCertFile(const std::string& caCertFile)
- {
- m_caCertFile = caCertFile;
- }
- void CurlFtp::enableCurlDebug(bool isPrint)
- {
- m_enableCurlDebug = isPrint;
- }
- bool CurlFtp::getFileList(std::string dir, std::vector<std::string>& fileList)
- {
- if(m_IP.empty())
- {
- SPDLOG_WARN("IP or port is empty");
- return false;
- }
-
- auto dirTmp = checkDirPath(dir);
-
-
-
-
-
-
-
- resetCurl(m_curl);
- std::vector<CF_FileInfo> listInfo;
-
- listAll(m_curl, dirTmp, listInfo);
- for(const CF_FileInfo& fi : listInfo)
- {
-
- if(fi.type == CF_FileType::FILE)
- {
- fileList.push_back(fi.name);
- }
- }
-
-
- return true;
- }
- bool CurlFtp::getDirList(std::string dir, std::vector<std::string>& dirList)
- {
- if(m_IP.empty())
- {
- SPDLOG_WARN("IP or port is empty");
- return false;
- }
-
- auto dirTmp = checkDirPath(dir);
-
-
-
-
-
-
-
- resetCurl(m_curl);
- std::vector<CF_FileInfo> listInfo;
-
- listAll(m_curl, dirTmp, listInfo);
- for(const CF_FileInfo& fi : listInfo)
- {
-
- if(fi.type == CF_FileType::DIR)
- {
- dirList.push_back(fi.name);
- }
- }
-
- return true;
- }
- bool CurlFtp::isDirExist(const std::string& dir)
- {
- SPDLOG_DEBUG("Check dir: {}", dir);
- if(m_ftpUrl.empty())
- {
- SPDLOG_ERROR("ftpUrl is empty");
- return false;
- }
-
- auto dirTmp = checkDirPath(dir);
-
- resetCurl(m_curl);
- std::string ftpUrl = m_ftpUrl + dirTmp;
- curl_easy_setopt(m_curl, CURLOPT_URL, ftpUrl.c_str());
- curl_easy_setopt(m_curl, CURLOPT_USERNAME, m_username.c_str());
- curl_easy_setopt(m_curl, CURLOPT_PASSWORD, m_password.c_str());
-
- curl_easy_setopt(m_curl, CURLOPT_NOBODY, 1L);
-
- setSftp(m_curl);
-
- if(m_enableCurlDebug)
- {
- curl_easy_setopt(m_curl, CURLOPT_VERBOSE, 1L);
- }
-
-
-
- CURLcode res = curl_easy_perform(m_curl);
- bool result = true;
- if(res != CURLE_OK)
- {
-
- result = false;
- }
-
-
-
-
-
- return result;
- }
- bool CurlFtp::createDirectory(const std::string& ftpDir)
- {
- if(m_ftpUrl.empty())
- {
- SPDLOG_ERROR("ftpUrl is empty");
- return false;
- }
-
- if(isDirExist(ftpDir))
- {
- return true;
- }
-
- auto dirTmp = checkDirPath(ftpDir);
-
-
-
-
-
-
- if(m_curl == nullptr)
- {
- m_curl = curl_easy_init();
- if(m_curl == nullptr)
- {
- SPDLOG_ERROR("curl init failed !");
- return false;
- }
- }else {
- curl_easy_reset(m_curl);
- }
- curl_easy_setopt(m_curl, CURLOPT_URL, m_ftpUrl.c_str());
- curl_easy_setopt(m_curl, CURLOPT_USERNAME, m_username.c_str());
- curl_easy_setopt(m_curl, CURLOPT_PASSWORD, m_password.c_str());
-
- struct curl_slist *headerlist = NULL;
- std::string mkdir = "MKD " + dirTmp;
- headerlist = curl_slist_append(headerlist, mkdir.c_str());
-
- curl_easy_setopt(m_curl, CURLOPT_QUOTE, headerlist);
-
- curl_easy_setopt(m_curl, CURLOPT_NOBODY, 1L);
-
- curl_easy_setopt(m_curl, CURLOPT_FOLLOWLOCATION, 1L);
-
- curl_easy_setopt(m_curl, CURLOPT_TIMEOUT, 30L);
- curl_easy_setopt(m_curl, CURLOPT_CONNECTTIMEOUT, 30L);
-
- setSftp(m_curl);
- if(m_enableCurlDebug)
- {
-
- curl_easy_setopt(m_curl, CURLOPT_VERBOSE, 1L);
- }
-
-
- SPDLOG_DEBUG("Create dir: {}", dirTmp);
-
- bool ret = performCurl(m_curl);
- if(!ret)
- {
- SPDLOG_ERROR("Failed to create FTP Dir");
- }
-
- curl_slist_free_all(headerlist);
- return ret;
- }
- bool CurlFtp::createDirectories(const std::string& ftpDir)
- {
-
- std::string ftpDirTmp = checkDirPath(ftpDir);
- std::string ftpDir2 = ftpDirTmp.substr(1, ftpDirTmp.size() -1);
-
- std::vector<std::string> strList;
- std::regex reg(R"(/)");
- std::sregex_token_iterator it(ftpDir2.begin(), ftpDir2.end(), reg, -1);
- for( ; it != std::sregex_token_iterator(); ++it)
- {
- strList.push_back(it->str());
- }
-
- std::vector<std::string> dirList;
- for(const std::string& dir : strList)
- {
- std::string dirTmp = "/";
- if(!dirList.empty())
- {
- dirTmp = dirList.back();
- dirTmp += "/";
- }
- dirTmp += dir;
- dirList.push_back(dirTmp);
- }
-
- for(const std::string& dir : dirList)
- {
-
- if(!createDirectory(dir))
- {
- SPDLOG_ERROR("Failed to create dir: {}", dir);
- return false;
- }
- }
-
- return true;
- }
- bool CurlFtp::downloadFile(const std::string& remoteFile, const std::string& localFile, size_t timeout)
- {
- if(m_ftpUrl.empty())
- {
- SPDLOG_ERROR("ftpUrl is empty");
- return false;
- }
-
-
- std::string remoteFileTmp = checkFilePath(remoteFile);
- std::string ftpUrl = m_ftpUrl + remoteFileTmp;
-
-
- std::string localDirTmp = localFile.substr(0, localFile.find_last_of("/"));
- if(!checkLocalDir(localDirTmp))
- {
- SPDLOG_ERROR("Failed to create local dir: {}", localDirTmp);
- return false;
- }
-
-
-
-
-
-
-
- resetCurl(m_curl);
-
- std::ofstream ofs;
- ofs.open(localFile, std::ios::out | std::ios::binary | std::ios::trunc);
-
- curl_easy_setopt(m_curl, CURLOPT_URL, ftpUrl.c_str());
- curl_easy_setopt(m_curl, CURLOPT_PORT, m_port);
-
- curl_easy_setopt(m_curl, CURLOPT_USERNAME, m_username.c_str());
- curl_easy_setopt(m_curl, CURLOPT_PASSWORD, m_password.c_str());
- curl_easy_setopt(m_curl, CURLOPT_USERPWD, (m_username + ":" + m_password).c_str());
-
- curl_easy_setopt(m_curl, CURLOPT_FOLLOWLOCATION, 1L);
-
- curl_easy_setopt(m_curl, CURLOPT_WRITEFUNCTION, writeFileCallBack);
- curl_easy_setopt(m_curl, CURLOPT_WRITEDATA, &ofs);
-
-
- curl_easy_setopt(m_curl, CURLOPT_CONNECTTIMEOUT, timeout);
-
- curl_easy_setopt(m_curl, CURLOPT_XFERINFOFUNCTION, progress_callback);
- curl_easy_setopt(m_curl, CURLOPT_XFERINFODATA, nullptr);
-
- curl_easy_setopt(m_curl, CURLOPT_NOPROGRESS, 0L);
-
- setSftp(m_curl);
- if(m_enableCurlDebug)
- {
-
- curl_easy_setopt(m_curl, CURLOPT_VERBOSE, 1L);
- }
-
- curl_easy_setopt(m_curl, CURLOPT_TCP_KEEPALIVE, 1L);
-
- bool ret = performCurl(m_curl);
- if(!ret)
- {
- SPDLOG_ERROR("Failed to get file list, Url = {}", ftpUrl);
-
- ofs.close();
- std::remove(localFile.c_str());
- return false;
- }
-
- ofs.close();
-
-
-
- printf("\n");
- return true;
- }
- bool CurlFtp::downloadToArray(const std::string& remoteFile, std::vector<char>& arrayInfo, size_t timeout)
- {
- if(m_ftpUrl.empty())
- {
- SPDLOG_ERROR("ftpUrl is empty");
- return false;
- }
-
-
- std::string remoteFileTmp = checkFilePath(remoteFile);
- std::string ftpUrl = m_ftpUrl + remoteFileTmp;
-
-
-
-
-
-
-
- resetCurl(m_curl);
-
- curl_easy_setopt(m_curl, CURLOPT_URL, ftpUrl.c_str());
- curl_easy_setopt(m_curl, CURLOPT_PORT, m_port);
-
- curl_easy_setopt(m_curl, CURLOPT_USERNAME, m_username.c_str());
- curl_easy_setopt(m_curl, CURLOPT_PASSWORD, m_password.c_str());
- curl_easy_setopt(m_curl, CURLOPT_USERPWD, (m_username + ":" + m_password).c_str());
-
- curl_easy_setopt(m_curl, CURLOPT_FOLLOWLOCATION, 1L);
-
- curl_easy_setopt(m_curl, CURLOPT_WRITEFUNCTION, writeDataCallBack);
- curl_easy_setopt(m_curl, CURLOPT_WRITEDATA, &arrayInfo);
-
-
- curl_easy_setopt(m_curl, CURLOPT_CONNECTTIMEOUT, timeout);
-
- curl_easy_setopt(m_curl, CURLOPT_XFERINFOFUNCTION, progress_callback);
- curl_easy_setopt(m_curl, CURLOPT_XFERINFODATA, nullptr);
-
- curl_easy_setopt(m_curl, CURLOPT_NOPROGRESS, 0L);
-
- setSftp(m_curl);
- if(m_enableCurlDebug)
- {
-
- curl_easy_setopt(m_curl, CURLOPT_VERBOSE, 1L);
- }
-
- curl_easy_setopt(m_curl, CURLOPT_TCP_KEEPALIVE, 1L);
-
- bool ret = performCurl(m_curl);
- if(!ret)
- {
- SPDLOG_ERROR("Failed to get file list, Url = {}", ftpUrl);
- }
-
-
-
-
- printf("\n");
- return ret;
- }
- bool CurlFtp::uploadFile(const std::string& localFile, const std::string& remoteFile, size_t timeout, bool isCreateDir)
- {
- if(m_ftpUrl.empty())
- {
- SPDLOG_ERROR("Url is empty");
- return false;
- }
-
- if(!std::filesystem::exists(localFile))
- {
- SPDLOG_ERROR("Local file is not exist: {}", localFile);
- return false;
- }
-
- std::string remoteFileTmp = checkFilePath(remoteFile);
- std::string remoteDirTmp = remoteFileTmp.substr(0, remoteFileTmp.find_last_of("/"));
-
- if(!isDirExist(remoteDirTmp))
- {
- if(isCreateDir)
- {
- if(!createDirectories(remoteDirTmp))
- {
-
- return false;
- }
- }else {
- SPDLOG_ERROR("Remote dir is not exist: {}", remoteDirTmp);
- return false;
- }
- }
-
-
- std::string ftpUrl = m_ftpUrl + remoteFileTmp;
-
-
- std::ifstream ifs;
- ifs.open(localFile, std::ios::in | std::ios::binary);
- if(!ifs.is_open())
- {
- SPDLOG_ERROR("Failed to open local file: {}", localFile);
- return false;
- }
-
-
- ifs.seekg(0, std::ios::end);
- auto fileSize = ifs.tellg();
-
- ifs.seekg(0, std::ios::beg);
- SPDLOG_DEBUG("File size: {}", (long)fileSize);
-
-
-
-
-
-
-
-
- if(!resetCurl(m_curl))
- {
- ifs.close();
- return false;
- }
-
- curl_easy_setopt(m_curl, CURLOPT_URL, ftpUrl.c_str());
- curl_easy_setopt(m_curl, CURLOPT_PORT, m_port);
-
- curl_easy_setopt(m_curl, CURLOPT_USERNAME, m_username.c_str());
- curl_easy_setopt(m_curl, CURLOPT_PASSWORD, m_password.c_str());
-
- curl_easy_setopt(m_curl, CURLOPT_FOLLOWLOCATION, 1L);
-
- curl_easy_setopt(m_curl, CURLOPT_UPLOAD, 1L);
-
- curl_easy_setopt(m_curl, CURLOPT_READFUNCTION, readFileCallBack);
- curl_easy_setopt(m_curl, CURLOPT_READDATA, &ifs);
-
- curl_easy_setopt(m_curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)fileSize);
-
-
- curl_easy_setopt(m_curl, CURLOPT_CONNECTTIMEOUT, timeout);
-
- curl_easy_setopt(m_curl, CURLOPT_XFERINFOFUNCTION, progress_callback);
- curl_easy_setopt(m_curl, CURLOPT_XFERINFODATA, nullptr);
-
- curl_easy_setopt(m_curl, CURLOPT_NOPROGRESS, 0L);
-
- setSftp(m_curl);
-
- if(m_enableCurlDebug)
- {
- curl_easy_setopt(m_curl, CURLOPT_VERBOSE, 1L);
- }
-
- curl_easy_setopt(m_curl, CURLOPT_TCP_KEEPALIVE, 1L);
-
- bool ret = performCurl(m_curl);
- if(!ret)
- {
- SPDLOG_ERROR("Upload file failed, Url = {}", ftpUrl);
- }
-
- ifs.close();
-
-
- printf("\n");
- return ret;
- }
- bool CurlFtp::uploadData(char* srcData, size_t size, const std::string& remoteFile, size_t timeout, bool isCreateDir)
- {
- if(m_ftpUrl.empty())
- {
- SPDLOG_ERROR("Url is empty");
- return false;
- }
-
- CF_ArrayInfo arrayInfo;
- arrayInfo.data = srcData;
- arrayInfo.size = size;
- arrayInfo.pos = 0;
-
- std::string remoteFileTmp = checkFilePath(remoteFile);
- std::string remoteDirTmp = remoteFileTmp.substr(0, remoteFileTmp.find_last_of("/"));
-
- if(!isDirExist(remoteDirTmp))
- {
- if(isCreateDir)
- {
- if(!createDirectories(remoteDirTmp))
- {
-
- return false;
- }
- }else {
- SPDLOG_ERROR("Remote dir is not exist: {}", remoteDirTmp);
- return false;
- }
- }
-
- std::string ftpUrl = m_ftpUrl + remoteFileTmp;
- SPDLOG_DEBUG("Data size: {}", arrayInfo.size);
-
-
-
-
-
-
-
- if(!resetCurl(m_curl))
- {
- return false;
- }
-
- curl_easy_setopt(m_curl, CURLOPT_URL, ftpUrl.c_str());
- curl_easy_setopt(m_curl, CURLOPT_PORT, m_port);
-
-
-
- curl_easy_setopt(m_curl, CURLOPT_USERPWD, (m_username + ":" + m_password).c_str());
-
- curl_easy_setopt(m_curl, CURLOPT_FOLLOWLOCATION, 1L);
-
- curl_easy_setopt(m_curl, CURLOPT_UPLOAD, 1L);
-
- curl_easy_setopt(m_curl, CURLOPT_READFUNCTION, readDataCallBack);
- curl_easy_setopt(m_curl, CURLOPT_READDATA, &arrayInfo);
-
- curl_easy_setopt(m_curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)arrayInfo.size);
-
-
- curl_easy_setopt(m_curl, CURLOPT_CONNECTTIMEOUT, timeout);
-
- curl_easy_setopt(m_curl, CURLOPT_XFERINFOFUNCTION, progress_callback);
- curl_easy_setopt(m_curl, CURLOPT_XFERINFODATA, nullptr);
-
- curl_easy_setopt(m_curl, CURLOPT_NOPROGRESS, 0L);
-
- curl_easy_setopt(m_curl, CURLOPT_FTP_USE_EPSV, 1L);
- if(m_enableCurlDebug)
- {
-
- curl_easy_setopt(m_curl, CURLOPT_VERBOSE, 1L);
- }
-
- curl_easy_setopt(m_curl, CURLOPT_TCP_KEEPALIVE, 1L);
-
- bool ret = performCurl(m_curl);
- if(!ret)
- {
- SPDLOG_ERROR("Upload file failed, Url = {}", ftpUrl);
- }
-
-
-
- printf("\n");
- return ret;
- }
- bool CurlFtp::listAll(CURL* curl, std::string dir, std::vector<CF_FileInfo>& fileInfoList)
- {
- if(m_IP.empty())
- {
- SPDLOG_ERROR("IP is empty");
- return false;
- }
- bool result = false;
- std::string strSrc;
-
- std::string ftpUrl = m_ftpUrl + dir;
- curl_easy_setopt(curl, CURLOPT_URL, ftpUrl.c_str());
- curl_easy_setopt(curl, CURLOPT_PORT, m_port);
-
- curl_easy_setopt(curl, CURLOPT_USERNAME, m_username.c_str());
- curl_easy_setopt(curl, CURLOPT_PASSWORD, m_password.c_str());
-
-
-
- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeStringCallback);
-
- curl_easy_setopt(curl, CURLOPT_WRITEDATA, &strSrc);
-
- curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10L);
- curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10L);
-
-
- if(m_enableCurlDebug)
- {
-
- curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
- }
-
-
- bool ret = performCurl(curl);
- if(!ret)
- {
- SPDLOG_ERROR("Failed to get file listUrl = {}", ftpUrl);
- return false;
- }
-
- parseFileInfo(strSrc, fileInfoList);
- return true;
- }
- std::string CurlFtp::checkDirPath(const std::string& dir)
- {
- std::string dirTmp = dir;
-
- std::regex reg(R"(^/[.]*)");
- if(!std::regex_match(dirTmp, reg))
- {
- dirTmp = "/" + dirTmp;
- }
-
- std::regex reg1(R"(//)");
- dirTmp = std::regex_replace(dirTmp, reg1, "/");
-
- std::regex reg2(R"([.]*/$)");
- if(!std::regex_match(dirTmp, reg2))
- {
- dirTmp = dirTmp + "/";
- }
- return dirTmp;
- }
- std::string CurlFtp::checkFilePath(const std::string& file)
- {
- std::string dirTmp = file;
-
- std::regex reg2(R"([.]*/$)");
- if(std::regex_match(dirTmp, reg2))
- {
- SPDLOG_ERROR("File path is not correct, end with '/'");
- return std::string();
- }
-
-
- std::regex reg(R"(^/[.]*)");
- if(!std::regex_match(dirTmp, reg))
- {
- dirTmp = "/" + dirTmp;
- }
-
- std::regex reg1(R"(//)");
- dirTmp = std::regex_replace(dirTmp, reg1, "/");
-
- return dirTmp;
- }
- bool CurlFtp::checkLocalDir(const std::string& localDir)
- {
-
- std::regex reg(R"([.]*/$)");
- std::string localDirTmp = std::regex_replace(localDir, reg, "");
-
- if(std::filesystem::exists(localDirTmp))
- {
- return true;
- }
-
- if(!std::filesystem::create_directories(localDirTmp))
- {
- SPDLOG_ERROR("Failed to create local dir: {}", localDirTmp);
- return false;
- }
- return true;
- }
- bool CurlFtp::performCurl(CURL* curl)
- {
- int retry = 3;
- while(retry > 0)
- {
- CURLcode res = curl_easy_perform(curl);
- if(res == CURLE_OK)
- {
- return true;
- }
- else if (res == CURLE_LOGIN_DENIED)
- {
- SPDLOG_ERROR("Login failed, error code: {} ,{}", (int)res, curl_easy_strerror(res));
-
- curl_easy_setopt(curl, CURLOPT_USERNAME, m_username.c_str());
- curl_easy_setopt(curl, CURLOPT_PASSWORD, m_password.c_str());
- }
- else
- {
- SPDLOG_ERROR("Perform curl failed, error code: {} ,{}", (int)res, curl_easy_strerror(res));
- SPDLOG_ERROR("Retry times: {}", 4 - retry);
- }
- retry--;
- }
- return false;
- }
- bool CurlFtp::resetCurl(CURL* curl)
- {
- if(m_curl == nullptr)
- {
- m_curl = curl_easy_init();
- if(m_curl == nullptr)
- {
- SPDLOG_ERROR("curl init failed !");
- return false;
- }
- }else {
- curl_easy_reset(m_curl);
- }
- return true;
- }
- bool CurlFtp::setSftp(CURL* curl)
- {
-
- if(m_isSftp)
- {
-
- if(m_isIgnoreSSLCert)
- {
-
- curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
-
- curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
- } else
- {
-
- curl_easy_setopt(curl, CURLOPT_CAINFO, m_caCertFile.c_str());
- }
- }
- return true;
- }
|