12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463 |
- #include "CurlFtp.h"
- #include <regex>
- #include <iosfwd>
- #include <fstream>
- #include "stdlog.h"
- #if (__cplusplus >= 201703L)
- #include <filesystem>
- #else
- #include <QDir>
- #include <QFileInfo>
- #endif
- #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)
- {
-
-
-
-
-
-
-
-
-
-
-
-
- userStr->append((char*)contents, size * nmemb);
- 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
- const std::regex parseRegSpace(R"(( )+)");
- const std::regex parseReg1(R"(^([^ ]+) (\d*) (\w*) (\w*) (\d*) (.*) ([^ ]+)$)");
- const std::regex parseReg2(R"(\n)");
- const std::regex parseReg3(R"(^d.*)");
- const std::regex parseReg4(R"(^-.*$)");
- 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::sregex_token_iterator it2(str2.begin(), str2.end(), parseReg2, -1);
-
- for(; it2 != std::sregex_token_iterator(); ++it2)
- {
-
- auto line = std::regex_replace(it2->str(), parseRegSpace, " ");
-
- CF_FileInfo fi;
-
- std::string strFileType = std::regex_replace(line, parseReg1, "$1");
- if(std::regex_match(strFileType, parseReg3))
- {
- fi.type = CF_FileType::DIR;
- }else if (std::regex_match(strFileType, parseReg4))
- {
- fi.type = CF_FileType::FILE;
- }
-
- std::string strFileSize = std::regex_replace(line, parseReg1, "$5");
- fi.size = std::stoull(strFileSize);
-
- std::string strFileName = std::regex_replace(line, parseReg1, "$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);
- LOG_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);
- LOG_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())
- {
- LOG_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())
- {
- LOG_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)
- {
- LOG_DEBUG("Check remote dir:" << dir);
- if(m_ftpUrl.empty())
- {
- LOG_ERROR("ftpUrl is empty");
- return false;
- }
- if(dir == "/")
- {
- return true;
- }
- bool result = false;
- if(m_isSftp)
- {
- result = checkSftpDirExist(dir);
- }else {
- result = checkFtpDirExist(dir);
- }
-
- return result;
- }
- bool CurlFtp::createDirectory(const std::string& ftpDir)
- {
- if(m_ftpUrl.empty())
- {
- LOG_ERROR("ftpUrl is empty");
- return false;
- }
-
- if(isDirExist(ftpDir))
- {
- return true;
- }
-
- auto dirTmp = checkDirPath(ftpDir);
-
-
-
-
-
-
- resetCurl(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;
- if(m_isSftp)
- {
- mkdir = "MKDIR " + dirTmp;
- }else {
- 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);
- }
-
-
- LOG_DEBUG("Create remote dir: " << dirTmp);
-
- bool ret = performCurl(m_curl);
- if(!ret)
- {
- LOG_ERROR("Failed to create remote 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))
- {
- LOG_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())
- {
- LOG_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))
- {
- LOG_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_TIMEOUT, timeout);
- 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)
- {
- std::stringstream ss;
- ss << "Failed to download file: " << ftpUrl;
- LOG_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())
- {
- LOG_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_TIMEOUT, timeout);
- 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)
- {
- LOG_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())
- {
- LOG_ERROR("Url is empty");
- return false;
- }
-
-
- if(!checkLocalFileExist(localFile))
- {
- LOG_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 {
- LOG_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())
- {
- LOG_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);
- LOG_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_TIMEOUT, timeout);
- 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)
- {
- LOG_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())
- {
- LOG_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 {
- LOG_ERROR("Remote dir is not exist: " << remoteDirTmp);
- return false;
- }
- }
-
- std::string ftpUrl = m_ftpUrl + remoteFileTmp;
- LOG_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_TIMEOUT, timeout);
- 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)
- {
- LOG_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())
- {
- LOG_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)
- {
- LOG_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))
- {
- LOG_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 (__cplusplus >= 201703L)
- if(std::filesystem::exists(localDirTmp))
- #else
- if(QDir(localDirTmp.c_str()).exists())
- #endif
- {
- return true;
- }
-
- #if (__cplusplus >= 201703L)
- if(!std::filesystem::create_directories(localDirTmp))
- #else
- if(!QDir().mkpath(localDirTmp.c_str()))
- #endif
- {
- LOG_ERROR("Failed to create local dir: " << localDirTmp);
- return false;
- }
- return true;
- }
- bool CurlFtp::checkLocalDirExist(const std::string& localDir)
- {
-
- std::regex reg(R"([.]*/$)");
- std::string localDirTmp = std::regex_replace(localDir, reg, "");
-
- bool result = false;
- #if (__cplusplus >= 201703L)
- result = std::filesystem::exists(localDirTmp);
- #else
- result = QDir(localDirTmp.c_str()).exists();
- #endif
- return result;
- }
- bool CurlFtp::checkLocalFileExist(const std::string& localFile)
- {
-
- std::regex reg(R"([.]*/$)");
- std::string localDirTmp = std::regex_replace(localFile, reg, "");
-
- bool result = false;
- #if (__cplusplus >= 201703L)
- result = std::filesystem::exists(localFile);
- #else
- result = QFile(localFile.c_str()).exists();
- #endif
- return result;
- }
- 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)
- {
- LOG_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
- {
- LOG_ERROR("Perform curl failed, error code: " << (int)res << ", " << curl_easy_strerror(res));
- LOG_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)
- {
- LOG_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;
- }
- bool CurlFtp::checkFtpDirExist(const std::string& dir)
- {
-
- 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_REMOTE_FILE_NOT_FOUND)
- {
- result = false;
- }
- else if(res == CURLE_OK)
- {
- result = true;
- } else
- {
- LOG_ERROR("Check remote dir error, error code: " << (int)res << ", " << curl_easy_strerror(res));
- result = false;
- }
-
- return result;
- }
- bool CurlFtp::checkSftpDirExist(const std::string& dir)
- {
-
- #if (__cplusplus >= 201703L)
- std::string parentDir = std::filesystem::path(dir).parent_path().string();
- #else
- std::string parentDir = QFileInfo(QString::fromStdString(dir)).path().toStdString();
- #endif
-
- std::vector<std::string> vecDir;
- bool ret = getDirList(parentDir, vecDir);
- if(!ret)
- {
- LOG_ERROR("Failed to check sftp dir: " << dir);
- return false;
- }
-
- #if (__cplusplus >= 201703L)
- std::string dirName = std::filesystem::path(dir).filename().string();
- #else
- std::string dirName = QFileInfo(QString::fromStdString(dir)).fileName().toStdString();
- #endif
-
-
- bool result = false;
- for(const std::string& str : vecDir)
- {
- if(str == dirName)
- {
- result = true;
- break;
- }
- }
- return result;
- }
|