瀏覽代碼

V1.6.6
1、修改了CurlFtp,新增删除远程文件功能,还未完全实现

Apple 1 周之前
父節點
當前提交
3994bbeca2
共有 3 個文件被更改,包括 114 次插入168 次删除
  1. 106 160
      module/CurlFtp/CurlFtp.cpp
  2. 8 0
      module/CurlFtp/CurlFtp.h
  3. 0 8
      module/CurlFtp/CurlFtpInfo.h

+ 106 - 160
module/CurlFtp/CurlFtp.cpp

@@ -289,7 +289,7 @@ void printProgress(CF_TransType type, curl_off_t total, curl_off_t now)
     }
     else
     {
-        printf("%s Total / now : %lldB / %lldB, %.2f%%, Speed:%.2f %s\r", transType.c_str(), total, now, percent, speed, unit.c_str());
+        printf("%s Total / now : %lldB / %lldB, %.2f%%, Speed:%.2f %s\r", transType.c_str(), static_cast<long long>(total), static_cast<long long>(now), percent, speed, unit.c_str());
     }
     fflush(stdout);
 }
@@ -531,15 +531,6 @@ bool CurlFtp::getFileList(std::string dir, std::vector<std::string>& fileList)
     /* 检查dir,添加前缀“/” */
     auto dirTmp = checkDirPath(dir);
 
-    // CURL *curl = nullptr;
-    // curl = curl_easy_init();
-    // if(curl == nullptr)
-    // {
-    //     LOG_ERROR("curl init failed !");
-    //     return false;
-    // }
-    resetCurl(m_curl);
-
     std::vector<CF_FileInfo> listInfo;
     /* 获取文件信息 */
     listAll(m_curl, dirTmp, listInfo);
@@ -656,11 +647,12 @@ bool CurlFtp::createDirectory(const std::string& ftpDir)
     //     LOG_ERROR("Create FTP DIR, curl init failed !");
     //     return false;
     // }
-    resetCurl(m_curl);
+    // resetCurl(m_curl);
+    setCommonCurlOptions(m_curl, m_ftpUrl, 30);
 
-    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());
+    // 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());
     /* 创建FTP头信息 */
     struct curl_slist *headerlist = NULL;
     std::string mkdir;
@@ -676,19 +668,19 @@ bool CurlFtp::createDirectory(const std::string& ftpDir)
     /* 不包含实体 */
     curl_easy_setopt(m_curl, CURLOPT_NOBODY, 1L);
     /* 启用跟随重定向 */
-    curl_easy_setopt(m_curl, CURLOPT_FOLLOWLOCATION, 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);
+    // curl_easy_setopt(m_curl, CURLOPT_TIMEOUT, 30L);
+    // curl_easy_setopt(m_curl, CURLOPT_CONNECTTIMEOUT, 30L);
 
     /* 设置SFTP */
-    setSftp(m_curl);
+    // setSftp(m_curl);
 
-    if(m_enableCurlDebug)
-    {
-        /* 启用调试信息 */
-        curl_easy_setopt(m_curl, CURLOPT_VERBOSE, 1L);
-    }
+    // if(m_enableCurlDebug)
+    // {
+    //     /* 启用调试信息 */
+    //     curl_easy_setopt(m_curl, CURLOPT_VERBOSE, 1L);
+    // }
     // 启用持久连接
     // curl_easy_setopt(m_curl, CURLOPT_TCP_KEEPALIVE, 1L);
 
@@ -749,6 +741,33 @@ bool CurlFtp::createDirectories(const std::string& ftpDir)
 
 
 
+/* 删除文件 */
+bool CurlFtp::deleteFile(const std::string& remoteFile)
+{
+    if(m_ftpUrl.empty())
+    {
+        LOG_ERROR("ftpUrl is empty");
+        return false;
+    }
+
+
+    return true;
+}
+
+/* 删除文件夹 */
+bool CurlFtp::deleteDirectory(const std::string& ftpDir)
+{
+    if(m_ftpUrl.empty())
+    {
+        LOG_ERROR("ftpUrl is empty");
+        return false;
+    }
+
+    return true;
+}
+
+
+
 /**
  * @brief 下载文件
  * 
@@ -777,15 +796,7 @@ bool CurlFtp::downloadFile(const std::string& remoteFile, const std::string& loc
         LOG_ERROR("Failed to create local dir: " << localDirTmp);
         return false;
     }
-
-    // CURL* curl = nullptr;
-    // curl = curl_easy_init();
-    // if(curl == nullptr)
-    // {
-    //     LOG_ERROR("curl init failed !");
-    //     return false;
-    // }
-    resetCurl(m_curl);
+    
 
     /* 打开文件,Windows需要转换成 std::wstring 和 std::wofstream */
 // #if defined(_WIN32) && defined(_MSC_VER)
@@ -810,35 +821,17 @@ bool CurlFtp::downloadFile(const std::string& remoteFile, const std::string& loc
     }
 
     /* 设置FTP地址 */
-    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);
+    setCommonCurlOptions(m_curl, ftpUrl, timeout);
+    
     /* 设置回调函数 */
     curl_easy_setopt(m_curl, CURLOPT_WRITEFUNCTION, writeFileCallBack_QT);
     curl_easy_setopt(m_curl, CURLOPT_WRITEDATA, &file);
-    /* 设置超时时间 */
-    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);
-    /* 设置SFTP */
-    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)
@@ -875,45 +868,19 @@ bool CurlFtp::downloadToArray(const std::string& remoteFile, std::vector<char>&
     std::string remoteFileTmp = checkFilePath(remoteFile);
     std::string ftpUrl = m_ftpUrl + remoteFileTmp;
 
-    // CURL* curl = nullptr;
-    // curl = curl_easy_init();
-    // if(curl == nullptr)
-    // {
-    //     LOG_ERROR("curl init failed !");
-    //     return false;
-    // }
-    resetCurl(m_curl);
 
     /* 设置FTP地址 */
-    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);
+    setCommonCurlOptions(m_curl, ftpUrl, timeout);
+
     /* 设置回调函数 */
     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);
-    /* 设置SFTP */
-    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)
@@ -995,28 +962,13 @@ bool CurlFtp::uploadFile(const std::string& localFile, const std::string& remote
     ifs.seekg(0, std::ios::beg);
     LOG_DEBUG("File size: " << (long)fileSize);
 
-    // CURL* curl = nullptr;
-    // curl = curl_easy_init();
-    // if(curl == nullptr)
-    // {
-    //     LOG_ERROR("curl init failed !");
-    //     ifs.close();
-    //     return false;
-    // }
-    if(!resetCurl(m_curl))
+
+    /* 设置FTP地址 */
+    if(!setCommonCurlOptions(m_curl, ftpUrl, timeout))
     {
         ifs.close();
         return false;
     }
-    // printf("uploadFile: %d\n", __LINE__);
-    /* 设置FTP地址 */
-    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);
     /* 设置回调函数 */
@@ -1024,24 +976,12 @@ bool CurlFtp::uploadFile(const std::string& localFile, const std::string& remote
     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);
 
-    /* 设置SFTP */
-    setSftp(m_curl);
-    /* 启用调试信息 */
-    if(m_enableCurlDebug)
-    {
-        curl_easy_setopt(m_curl, CURLOPT_VERBOSE, 1L);
-    }
-    /* 启用持久连接 */
-    curl_easy_setopt(m_curl, CURLOPT_TCP_KEEPALIVE, 1L);
     // printf("uploadFile: %d\n", __LINE__);
     /* 发送请求 */
     bool ret = performCurl(m_curl);
@@ -1108,26 +1048,12 @@ bool CurlFtp::uploadData(char* srcData, size_t size, const std::string& remoteFi
     std::string ftpUrl = m_ftpUrl + remoteFileTmp;
     LOG_DEBUG("Data size: " << arrayInfo.size);
 
-    // CURL* curl = nullptr;
-    // curl = curl_easy_init();
-    // if(curl == nullptr)
-    // {
-    //     LOG_ERROR("curl init failed !");
-    //     return false;
-    // }
-    if(!resetCurl(m_curl))
+
+    /* 设置FTP地址 */
+    if(!setCommonCurlOptions(m_curl, ftpUrl, timeout))
     {
         return false;
     }
-    /* 设置FTP地址 */
-    curl_easy_setopt(m_curl, CURLOPT_URL, ftpUrl.c_str());
-    curl_easy_setopt(m_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(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);
     /* 设置回调函数 */
@@ -1135,9 +1061,7 @@ bool CurlFtp::uploadData(char* srcData, size_t size, const std::string& remoteFi
     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);
@@ -1151,8 +1075,6 @@ bool CurlFtp::uploadData(char* srcData, size_t size, const std::string& remoteFi
         /* 启用调试信息 */
         curl_easy_setopt(m_curl, CURLOPT_VERBOSE, 1L);
     }
-    /* 启用持久连接 */
-    curl_easy_setopt(m_curl, CURLOPT_TCP_KEEPALIVE, 1L);
     /* 发送请求 */
     bool ret = performCurl(m_curl);
     if(!ret)
@@ -1169,6 +1091,7 @@ bool CurlFtp::uploadData(char* srcData, size_t size, const std::string& remoteFi
 
 
 
+
 /**
  * @brief 列出文件列表,这个需要的参数很多,无法设置成静态函数
  * 
@@ -1190,11 +1113,10 @@ bool CurlFtp::listAll(CURL* curl, std::string dir, std::vector<CF_FileInfo>& fil
     std::string strSrc;
     /* 先设置FTP地址 */
     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());
+    if(!setCommonCurlOptions(curl, m_ftpUrl + dir, 10))
+    {
+        return false;
+    }
 
     /* 设置列出文件命令,只列出文件名称,不携带信息 */
     // curl_easy_setopt(m_curl, CURLOPT_DIRLISTONLY, 1L);
@@ -1203,17 +1125,8 @@ bool CurlFtp::listAll(CURL* curl, std::string dir, std::vector<CF_FileInfo>& fil
     /* 设置需要写入的容器,回调函数的第四个参数 */
     curl_easy_setopt(curl, CURLOPT_WRITEDATA, &strSrc);
 
-    /* 设置超时时间 */
-    curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10L);
-    curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10L);
-
     // 禁用被动模式,设置为0是禁用(如果需要)
     // curl_easy_setopt(curl, CURLOPT_FTP_USE_EPSV, 1L);
-    if(m_enableCurlDebug)
-    {
-        /* 启用调试信息 */
-        curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
-    }
     /* 发送请求 */
     // CURLcode res = curl_easy_perform(curl);
     bool ret = performCurl(curl);
@@ -1228,6 +1141,43 @@ bool CurlFtp::listAll(CURL* curl, std::string dir, std::vector<CF_FileInfo>& fil
     return true;
 }
 
+
+/* 设置常规通用的curl设置 */
+bool CurlFtp::setCommonCurlOptions(CURL* curl, const std::string& ftpUrl, size_t timeout)
+{
+    if(!resetCurl(m_curl))
+    {
+        return false;
+    }
+    /* 设置FTP地址 */
+    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_TIMEOUT, timeout);
+    curl_easy_setopt(m_curl, CURLOPT_CONNECTTIMEOUT, timeout);
+    /* 启用持久连接 */
+    curl_easy_setopt(m_curl, CURLOPT_TCP_KEEPALIVE, 1L);
+
+    if(m_enableCurlDebug)
+    {
+        /* 启用调试信息 */
+        curl_easy_setopt(m_curl, CURLOPT_VERBOSE, 1L);
+    }
+    /* 设置SFTP */
+    if(!setSftp(m_curl))
+    {
+        return false;
+    }
+
+    return true;
+}
+
 /* 检查文件夹路径是否合规,不合规就修改 */
 std::string CurlFtp::checkDirPath(const std::string& dir)
 {
@@ -1427,23 +1377,19 @@ 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());
+    if(!setCommonCurlOptions(m_curl, m_ftpUrl + dir, 10))
+    {
+        return false;
+    }
+    // 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);
     // curl_easy_setopt(m_curl, CURLOPT_HEADER, 1L);
 
-    /* 设置SFTP */
-    setSftp(m_curl);
-    /* 启用调试信息 */
-    if(m_enableCurlDebug)
-    {
-        curl_easy_setopt(m_curl, CURLOPT_VERBOSE, 1L);
-    }
-
     /* 启用持久连接 */
     // curl_easy_setopt(m_curl, CURLOPT_TCP_KEEPALIVE, 1L);
     /* 这里只需要执行一次 */

+ 8 - 0
module/CurlFtp/CurlFtp.h

@@ -54,6 +54,10 @@ public:
     bool createDirectory(const std::string& ftpDir);
     /* 创建FTP文件夹,递归创建 */
     bool createDirectories(const std::string& ftpDir);
+    /* 删除文件 */
+    bool deleteFile(const std::string& remoteFile);
+    /* 删除文件夹 */
+    bool deleteDirectory(const std::string& ftpDir);
 
     /* 下载文件 */
     bool downloadFile(const std::string& remoteFile, const std::string& localFile, size_t timeout = 30);
@@ -64,7 +68,11 @@ public:
     /* 上传文件,上传数据 */
     bool uploadData(char* srcData, size_t size, const std::string& remoteFile, size_t timeout = 30, bool isCreateDir = false);
 
+    
+
 private:
+    /* 设置常规通用的curl设置 */
+    bool setCommonCurlOptions(CURL* curl, const std::string& ftpUrl, size_t timeout);
     /* 列出所有内容 */
     bool listAll(CURL* curl, std::string dir, std::vector<CF_FileInfo>& fileInfoList);
     /* 检查FTP文件夹路径是否合规,不合规就修改 */

+ 0 - 8
module/CurlFtp/CurlFtpInfo.h

@@ -35,14 +35,6 @@ struct CF_FileInfo
     CF_FileInfo() : type(CF_FileType::UNKNOWN), size(0), name(""){}
     CF_FileInfo(CF_FileType type, uint64_t size, const std::string& name)
         : type(type), size(size), name(name) {}
-    
-    CF_FileInfo& operator=(const CF_FileInfo& info)
-    {
-        type = info.type;
-        size = info.size;
-        name = info.name;
-        return *this;
-    }
 };
 
 /**