瀏覽代碼

V1.6.9
1、完成了Linux下的CurlFTP功能,包括SFTP

Apple 4 天之前
父節點
當前提交
2f76bf3357
共有 1 個文件被更改,包括 35 次插入22 次删除
  1. 35 22
      module/CurlFtp/CurlFtp.cpp

+ 35 - 22
module/CurlFtp/CurlFtp.cpp

@@ -758,44 +758,41 @@ bool CurlFtp::deleteFile(const std::string& remoteFile)
         return false;
     }
     
-    struct curl_slist *headerlist = nullptr;
     std::string deleteCmd;
     std::string ftpUrl;
     if(m_isSftp == false)
     {
         /* 切到父目录后再删除基名(兼容更多服务器) */
-        std::string parentDir, fileName;
-        splitParentAndBasePath(filePath, parentDir, fileName);
-        if(fileName.empty())
-        {
-            LOG_ERROR("Delete file base name is empty: " << filePath);
-            return false;
-        }
-        ftpUrl = m_ftpUrl + parentDir; // parentDir 以'/'结尾
-        deleteCmd = "DELE " + fileName; // ftp 使用 DELE
-    }else 
+        // std::string parentDir, fileName;
+        // splitParentAndBasePath(filePath, parentDir, fileName);
+        // if(fileName.empty())
+        // {
+        //     LOG_ERROR("Delete file base name is empty: " << filePath);
+        //     return false;
+        // }
+        // ftpUrl = m_ftpUrl + parentDir; // parentDir 以'/'结尾
+        // deleteCmd = "DELE " + fileName; // ftp 使用 DELE
+        ftpUrl = m_ftpUrl + "/";
+        deleteCmd = "DELE " + filePath;
+    }else
     {
         /* SFTP需要使用绝对路径删除 */
         ftpUrl = m_ftpUrl + "/";
-        headerlist = nullptr;
         deleteCmd = "rm " + filePath;
     }
     if(!setCommonCurlOptions(ftpUrl, 10))
     {
         return false;
     }
+    struct curl_slist *headerlist = nullptr;
     headerlist = curl_slist_append(headerlist, deleteCmd.c_str());
     curl_easy_setopt(m_curl, CURLOPT_QUOTE, headerlist);
     curl_easy_setopt(m_curl, CURLOPT_NOBODY, 1L);
     LOG_DEBUG("Delete remote file: " << filePath);
     bool ret = performCurl();
     curl_slist_free_all(headerlist);
-    if(ret)
-    {
-        return true;
-    }
 
-    
+    return ret;
 }
 
 /* 删除文件夹,递归删除 */
@@ -958,7 +955,7 @@ bool CurlFtp::downloadFile(const std::string& remoteFile, const std::string& loc
         LOG_ERROR("Failed to create local dir: " << localDirTmp);
         return false;
     }
-    
+    SPDLOG_DEBUG("远程文件: {}, 本地路径: {}", ftpUrl, localFile);
 
     /* 打开文件,Windows需要转换成 std::wstring 和 std::wofstream */
 // #if defined(_WIN32) && defined(_MSC_VER)
@@ -993,6 +990,12 @@ bool CurlFtp::downloadFile(const std::string& remoteFile, const std::string& loc
     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, 0L);
+    /* 使用二进制传输 */
+    curl_easy_setopt(m_curl, CURLOPT_TRANSFERTEXT, 0L);
+    // 忽略 PASV 返回的地址,使用控制连接的地址,解决 NAT 场景
+    // curl_easy_setopt(m_curl, CURLOPT_FTP_SKIP_PASV_IP, 1L);
     
     /* 发送请求 */
     bool ret = performCurl();
@@ -1042,6 +1045,10 @@ bool CurlFtp::downloadToArray(const std::string& remoteFile, std::vector<char>&
     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, 0L);
+    /* 使用二进制传输 */
+    curl_easy_setopt(m_curl, CURLOPT_TRANSFERTEXT, 0L);
 
     /* 发送请求 */
     bool ret = performCurl();
@@ -1143,6 +1150,10 @@ bool CurlFtp::uploadFile(const std::string& localFile, const std::string& remote
     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, 0L);
+    /* 使用二进制传输 */
+    curl_easy_setopt(m_curl, CURLOPT_TRANSFERTEXT, 0L);
 
     // printf("uploadFile: %d\n", __LINE__);
     /* 发送请求 */
@@ -1229,9 +1240,11 @@ bool CurlFtp::uploadData(char* srcData, size_t size, const std::string& remoteFi
     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);
+    /* 关闭被动模式,使用主动模式 */
+    curl_easy_setopt(m_curl, CURLOPT_FTP_USE_EPSV, 0L);
+    /* 使用二进制传输 */
+    curl_easy_setopt(m_curl, CURLOPT_TRANSFERTEXT, 0L);
+    
     if(m_enableCurlDebug)
     {
         /* 启用调试信息 */
@@ -1554,7 +1567,7 @@ bool CurlFtp::performCurl()
         else if(res == CURLE_REMOTE_FILE_NOT_FOUND)
         {
             LOG_WARN("Remote file or directory not found, error code: " << (int)res << ", " << curl_easy_strerror(res));
-            return true;
+            return false;
         } else
         {
             LOG_ERROR("Perform curl failed, error code: " << (int)res << ", " << curl_easy_strerror(res));