Browse Source

V1.4.11
1、修复了ftp在Windows下中文路径下载失败的问题
2、修改ftp写入文件方式为QT

Apple 20 hours ago
parent
commit
1f4cabd660
1 changed files with 77 additions and 36 deletions
  1. 77 36
      module/CurlFtp/CurlFtp.cpp

+ 77 - 36
module/CurlFtp/CurlFtp.cpp

@@ -11,13 +11,15 @@
 #if (__cplusplus >= 201703L)
     #include <filesystem>
 #else
-    #include <QDir>
-    #include <QFileInfo>
+    
 #endif /* (__cplusplus >= 201703L) */
 
+#include <QDir>
+#include <QFileInfo>
 
 #if defined(_WIN32)
-
+    #include <QDir>
+    #include <QFileInfo>
 #endif /* _WIN32 */
 
 /* ==================================================================================
@@ -101,6 +103,26 @@ static size_t writeFileCallBack(void* contents, size_t size, size_t nmemb, std::
     return size * nmemb;
 }
 
+
+/**
+ * @brief 写入文件回调函数,使用Qt的QFile写文件,解决Windows下中文路径的问题
+ * 
+ * @param contents 
+ * @param size 
+ * @param nmemb 
+ * @param pFile 
+ * @return size_t 
+ */
+static size_t writeFileCallBack_QT(void* contents, size_t size, size_t nmemb, QFile* pFile)
+{
+    qint64 written = pFile->write(reinterpret_cast<char*>(contents), size * nmemb);
+    if (written == -1) {
+        // Handle write error
+        return 0;
+    }
+    return static_cast<size_t>(written);
+}
+
 /**
  * @brief 写入数据到vector中
  * 
@@ -267,7 +289,7 @@ void printProgress(CF_TransType type, curl_off_t total, curl_off_t now)
     }
     else
     {
-        printf("%s Total / now : %ldB / %ldB, %.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(), total, now, percent, speed, unit.c_str());
     }
     fflush(stdout);
 }
@@ -764,9 +786,28 @@ bool CurlFtp::downloadFile(const std::string& remoteFile, const std::string& loc
     //     return false;
     // }
     resetCurl(m_curl);
-    /* 打开文件 */
-    std::ofstream ofs;
-    ofs.open(localFile, std::ios::out | std::ios::binary | std::ios::trunc);
+
+    /* 打开文件,Windows需要转换成 std::wstring 和 std::wofstream */
+// #if defined(_WIN32) && defined(_MSC_VER)
+//     std::wstring wLocalFile(localFile.begin(), localFile.end());
+//     std::wofstream ofs;
+//     ofs.open(wLocalFile, std::ios::out | std::ios::binary | std::ios::trunc);
+// #else
+//     std::ofstream ofs;
+//     ofs.open(localFile, std::ios::out | std::ios::binary | std::ios::trunc);
+// #endif /* defined(_WIN32) || defined(_WIN64) */
+//     if(!ofs.is_open())
+//     {
+//         LOG_ERROR("Failed to open local file: " << localFile);
+//         LOG_ERROR("Error info: " << std::strerror(errno));
+//         return false;
+//     }
+    QFile file(QString::fromStdString(localFile));
+    if(!file.open(QIODevice::WriteOnly))
+    {
+        LOG_ERROR("Failed to open local file: " << localFile);
+        return false;
+    }
 
     /* 设置FTP地址 */
     curl_easy_setopt(m_curl, CURLOPT_URL, ftpUrl.c_str());
@@ -778,8 +819,8 @@ bool CurlFtp::downloadFile(const std::string& remoteFile, const std::string& loc
     /* 启用跟随重定向 */
     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_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);
@@ -806,12 +847,12 @@ bool CurlFtp::downloadFile(const std::string& remoteFile, const std::string& loc
         ss << "Failed to download file: " << ftpUrl;
         LOG_ERROR("Failed to get file list, Url = " << ftpUrl);
         /* 清理下载失败的文件 */
-        ofs.close();
+        file.close();
         std::remove(localFile.c_str());
         return false;
     }
     /* 关闭文件,清理curl */
-    ofs.close();
+    file.close();
     // curl_easy_cleanup(curl);
 
     /* 打印一个换行符 */
@@ -1145,7 +1186,7 @@ bool CurlFtp::listAll(CURL* curl, std::string dir, std::vector<CF_FileInfo>& fil
         return false;
     }
 
-    bool result = false;
+    // bool result = false;
     std::string strSrc;
     /* 先设置FTP地址 */
     std::string ftpUrl = m_ftpUrl + dir;
@@ -1250,20 +1291,20 @@ 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 (__cplusplus >= 201703L)
+//     if(std::filesystem::exists(localDirTmp))
+// #else
     if(QDir(localDirTmp.c_str()).exists())
-#endif /* (__cplusplus >= 201703L) */
+// #endif /* (__cplusplus >= 201703L) */
     {
         return true;
     }
     /* 创建文件夹 */
-#if (__cplusplus >= 201703L)
-    if(!std::filesystem::create_directories(localDirTmp))
-#else
+// #if (__cplusplus >= 201703L)
+//     if(!std::filesystem::create_directories(localDirTmp))
+// #else
     if(!QDir().mkpath(localDirTmp.c_str()))
-#endif /* (__cplusplus >= 201703L) */
+// #endif /* (__cplusplus >= 201703L) */
     {
         LOG_ERROR("Failed to create local dir: " << localDirTmp);
         return false;
@@ -1280,11 +1321,11 @@ bool CurlFtp::checkLocalDirExist(const std::string& localDir)
     std::string localDirTmp = std::regex_replace(localDir, reg, "");
     /* 检查文件夹是否存在 */
     bool result = false;
-#if (__cplusplus >= 201703L)
-    result = std::filesystem::exists(localDirTmp);
-#else
+// #if (__cplusplus >= 201703L)
+//     result = std::filesystem::exists(localDirTmp);
+// #else
     result = QDir(localDirTmp.c_str()).exists();
-#endif /* (__cplusplus >= 201703L) */
+// #endif /* (__cplusplus >= 201703L) */
 
     return result;
 }
@@ -1297,11 +1338,11 @@ bool CurlFtp::checkLocalFileExist(const std::string& localFile)
     std::string localDirTmp = std::regex_replace(localFile, reg, "");
     /* 检查文件是否存在 */
     bool result = false;
-#if (__cplusplus >= 201703L)
-    result = std::filesystem::exists(localFile);
-#else
+// #if (__cplusplus >= 201703L)
+//     result = std::filesystem::exists(localFile);
+// #else
     result = QFile(localFile.c_str()).exists();
-#endif /* (__cplusplus >= 201703L) */
+// #endif /* (__cplusplus >= 201703L) */
 
     return result;
 }
@@ -1428,11 +1469,11 @@ bool CurlFtp::checkFtpDirExist(const std::string& dir)
 bool CurlFtp::checkSftpDirExist(const std::string& dir)
 {
     /* 取出上一层文件夹路径 */
-#if (__cplusplus >= 201703L)
-    std::string parentDir = std::filesystem::path(dir).parent_path().string();
-#else
+// #if (__cplusplus >= 201703L)
+//     std::string parentDir = std::filesystem::path(dir).parent_path().string();
+// #else
     std::string parentDir = QFileInfo(QString::fromStdString(dir)).path().toStdString();
-#endif /* (__cplusplus >= 201703L) */
+// #endif /* (__cplusplus >= 201703L) */
     // LOG_DEBUG("Parent dir: {}", parentDir);
     std::vector<std::string> vecDir;
     bool ret = getDirList(parentDir, vecDir);
@@ -1442,11 +1483,11 @@ bool CurlFtp::checkSftpDirExist(const std::string& dir)
         return false;
     }
     /* 取出本层文件夹名称 */
-#if (__cplusplus >= 201703L)
-    std::string dirName = std::filesystem::path(dir).filename().string();
-#else
+// #if (__cplusplus >= 201703L)
+//     std::string dirName = std::filesystem::path(dir).filename().string();
+// #else
     std::string dirName = QFileInfo(QString::fromStdString(dir)).fileName().toStdString();
-#endif /* (__cplusplus >= 201703L) */
+// #endif /* (__cplusplus >= 201703L) */
     // LOG_DEBUG("Dir name: {}", dirName);
     /* 判断是否存在 */
     bool result = false;