|
@@ -2,12 +2,19 @@
|
|
#include "CurlFtp.h"
|
|
#include "CurlFtp.h"
|
|
#include <regex>
|
|
#include <regex>
|
|
#include <iosfwd>
|
|
#include <iosfwd>
|
|
-#include <filesystem>
|
|
|
|
#include <fstream>
|
|
#include <fstream>
|
|
|
|
|
|
#include "stdlog.h"
|
|
#include "stdlog.h"
|
|
|
|
|
|
|
|
|
|
|
|
+#if (__cplusplus >= 201703L)
|
|
|
|
+ #include <filesystem>
|
|
|
|
+#else
|
|
|
|
+ #include <QDir>
|
|
|
|
+ #include <QFileInfo>
|
|
|
|
+#endif /* (__cplusplus >= 201703L) */
|
|
|
|
+
|
|
|
|
+
|
|
#if defined(_WIN32)
|
|
#if defined(_WIN32)
|
|
|
|
|
|
#endif /* _WIN32 */
|
|
#endif /* _WIN32 */
|
|
@@ -896,15 +903,18 @@ bool CurlFtp::uploadFile(const std::string& localFile, const std::string& remote
|
|
LOG_ERROR("Url is empty");
|
|
LOG_ERROR("Url is empty");
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
+ // printf("uploadFile: %d\n", __LINE__);
|
|
/* 检查本地文件是否存在 */
|
|
/* 检查本地文件是否存在 */
|
|
- if(!std::filesystem::exists(localFile))
|
|
|
|
|
|
+ if(!checkLocalFileExist(localFile))
|
|
{
|
|
{
|
|
LOG_ERROR("Local file is not exist: " << localFile);
|
|
LOG_ERROR("Local file is not exist: " << localFile);
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
+ // printf("uploadFile: %d\n", __LINE__);
|
|
/* 检查FTP文件名是否符合规范 */
|
|
/* 检查FTP文件名是否符合规范 */
|
|
std::string remoteFileTmp = checkFilePath(remoteFile);
|
|
std::string remoteFileTmp = checkFilePath(remoteFile);
|
|
std::string remoteDirTmp = remoteFileTmp.substr(0, remoteFileTmp.find_last_of("/"));
|
|
std::string remoteDirTmp = remoteFileTmp.substr(0, remoteFileTmp.find_last_of("/"));
|
|
|
|
+ // printf("uploadFile: %d\n", __LINE__);
|
|
/* 检查远程FTP上的文件夹是否存在,不存在则创建 */
|
|
/* 检查远程FTP上的文件夹是否存在,不存在则创建 */
|
|
if(!isDirExist(remoteDirTmp))
|
|
if(!isDirExist(remoteDirTmp))
|
|
{
|
|
{
|
|
@@ -920,6 +930,7 @@ bool CurlFtp::uploadFile(const std::string& localFile, const std::string& remote
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+ // printf("uploadFile: %d\n", __LINE__);
|
|
|
|
|
|
/* 拼接远程文件的url */
|
|
/* 拼接远程文件的url */
|
|
std::string ftpUrl = m_ftpUrl + remoteFileTmp;
|
|
std::string ftpUrl = m_ftpUrl + remoteFileTmp;
|
|
@@ -932,6 +943,7 @@ bool CurlFtp::uploadFile(const std::string& localFile, const std::string& remote
|
|
LOG_ERROR("Failed to open local file: " << localFile);
|
|
LOG_ERROR("Failed to open local file: " << localFile);
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
+ // printf("uploadFile: %d\n", __LINE__);
|
|
/* 获取文件大小 */
|
|
/* 获取文件大小 */
|
|
// auto startPos = ifs.tellg();
|
|
// auto startPos = ifs.tellg();
|
|
ifs.seekg(0, std::ios::end);
|
|
ifs.seekg(0, std::ios::end);
|
|
@@ -953,6 +965,7 @@ bool CurlFtp::uploadFile(const std::string& localFile, const std::string& remote
|
|
ifs.close();
|
|
ifs.close();
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
+ // printf("uploadFile: %d\n", __LINE__);
|
|
/* 设置FTP地址 */
|
|
/* 设置FTP地址 */
|
|
curl_easy_setopt(m_curl, CURLOPT_URL, ftpUrl.c_str());
|
|
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_PORT, m_port);
|
|
@@ -986,14 +999,17 @@ bool CurlFtp::uploadFile(const std::string& localFile, const std::string& remote
|
|
}
|
|
}
|
|
/* 启用持久连接 */
|
|
/* 启用持久连接 */
|
|
curl_easy_setopt(m_curl, CURLOPT_TCP_KEEPALIVE, 1L);
|
|
curl_easy_setopt(m_curl, CURLOPT_TCP_KEEPALIVE, 1L);
|
|
|
|
+ // printf("uploadFile: %d\n", __LINE__);
|
|
/* 发送请求 */
|
|
/* 发送请求 */
|
|
bool ret = performCurl(m_curl);
|
|
bool ret = performCurl(m_curl);
|
|
if(!ret)
|
|
if(!ret)
|
|
{
|
|
{
|
|
LOG_ERROR("Upload file failed, Url = " << ftpUrl);
|
|
LOG_ERROR("Upload file failed, Url = " << ftpUrl);
|
|
}
|
|
}
|
|
|
|
+ // printf("uploadFile: %d\n", __LINE__);
|
|
/* 关闭文件,清理curl */
|
|
/* 关闭文件,清理curl */
|
|
ifs.close();
|
|
ifs.close();
|
|
|
|
+ // printf("uploadFile: %d\n", __LINE__);
|
|
// curl_easy_cleanup(curl);
|
|
// curl_easy_cleanup(curl);
|
|
/* 打印一个换行符 */
|
|
/* 打印一个换行符 */
|
|
printf("\n");
|
|
printf("\n");
|
|
@@ -1232,12 +1248,20 @@ bool CurlFtp::checkLocalDir(const std::string& localDir)
|
|
std::regex reg(R"([.]*/$)");
|
|
std::regex reg(R"([.]*/$)");
|
|
std::string localDirTmp = std::regex_replace(localDir, reg, "");
|
|
std::string localDirTmp = std::regex_replace(localDir, reg, "");
|
|
/* 检查文件夹是否存在 */
|
|
/* 检查文件夹是否存在 */
|
|
|
|
+#if (__cplusplus >= 201703L)
|
|
if(std::filesystem::exists(localDirTmp))
|
|
if(std::filesystem::exists(localDirTmp))
|
|
|
|
+#else
|
|
|
|
+ if(QDir(localDirTmp.c_str()).exists())
|
|
|
|
+#endif /* (__cplusplus >= 201703L) */
|
|
{
|
|
{
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
/* 创建文件夹 */
|
|
/* 创建文件夹 */
|
|
|
|
+#if (__cplusplus >= 201703L)
|
|
if(!std::filesystem::create_directories(localDirTmp))
|
|
if(!std::filesystem::create_directories(localDirTmp))
|
|
|
|
+#else
|
|
|
|
+ if(!QDir().mkpath(localDirTmp.c_str()))
|
|
|
|
+#endif /* (__cplusplus >= 201703L) */
|
|
{
|
|
{
|
|
LOG_ERROR("Failed to create local dir: " << localDirTmp);
|
|
LOG_ERROR("Failed to create local dir: " << localDirTmp);
|
|
return false;
|
|
return false;
|
|
@@ -1246,6 +1270,40 @@ bool CurlFtp::checkLocalDir(const std::string& localDir)
|
|
return true;
|
|
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 /* (__cplusplus >= 201703L) */
|
|
|
|
+
|
|
|
|
+ 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 /* (__cplusplus >= 201703L) */
|
|
|
|
+
|
|
|
|
+ return result;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
|
|
/* 执行curl,添加重试机制 */
|
|
/* 执行curl,添加重试机制 */
|
|
bool CurlFtp::performCurl(CURL* curl)
|
|
bool CurlFtp::performCurl(CURL* curl)
|
|
@@ -1368,7 +1426,11 @@ bool CurlFtp::checkFtpDirExist(const std::string& dir)
|
|
bool CurlFtp::checkSftpDirExist(const std::string& dir)
|
|
bool CurlFtp::checkSftpDirExist(const std::string& dir)
|
|
{
|
|
{
|
|
/* 取出上一层文件夹路径 */
|
|
/* 取出上一层文件夹路径 */
|
|
|
|
+#if (__cplusplus >= 201703L)
|
|
std::string parentDir = std::filesystem::path(dir).parent_path().string();
|
|
std::string parentDir = std::filesystem::path(dir).parent_path().string();
|
|
|
|
+#else
|
|
|
|
+ std::string parentDir = QFileInfo(QString::fromStdString(dir)).path().toStdString();
|
|
|
|
+#endif /* (__cplusplus >= 201703L) */
|
|
// LOG_DEBUG("Parent dir: {}", parentDir);
|
|
// LOG_DEBUG("Parent dir: {}", parentDir);
|
|
std::vector<std::string> vecDir;
|
|
std::vector<std::string> vecDir;
|
|
bool ret = getDirList(parentDir, vecDir);
|
|
bool ret = getDirList(parentDir, vecDir);
|
|
@@ -1378,7 +1440,11 @@ bool CurlFtp::checkSftpDirExist(const std::string& dir)
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
/* 取出本层文件夹名称 */
|
|
/* 取出本层文件夹名称 */
|
|
|
|
+#if (__cplusplus >= 201703L)
|
|
std::string dirName = std::filesystem::path(dir).filename().string();
|
|
std::string dirName = std::filesystem::path(dir).filename().string();
|
|
|
|
+#else
|
|
|
|
+ std::string dirName = QFileInfo(QString::fromStdString(dir)).fileName().toStdString();
|
|
|
|
+#endif /* (__cplusplus >= 201703L) */
|
|
// LOG_DEBUG("Dir name: {}", dirName);
|
|
// LOG_DEBUG("Dir name: {}", dirName);
|
|
/* 判断是否存在 */
|
|
/* 判断是否存在 */
|
|
bool result = false;
|
|
bool result = false;
|