From 38bb619133f71ef48326d3f98d2b23e142121b7e Mon Sep 17 00:00:00 2001 From: wangyingjun01 Date: Wed, 19 Nov 2025 12:01:47 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20createParentPath=20?= =?UTF-8?q?=E5=87=BD=E6=95=B0=E7=9A=84=E5=BC=82=E5=B8=B8=E5=A4=84=E7=90=86?= =?UTF-8?q?=E5=92=8C=E6=94=B9=E8=BF=9B=20saveImage=20=E5=87=BD=E6=95=B0?= =?UTF-8?q?=E7=9A=84=E9=94=99=E8=AF=AF=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wangyingjun01 --- .../cpp/downloadUtils/HttpTaskProcessor.cpp | 79 ++++++++++++++++--- .../cpp/turboModules/RNSVGImageModule.cpp | 29 +++++++ 2 files changed, 98 insertions(+), 10 deletions(-) diff --git a/tester/harmony/svg/src/main/cpp/downloadUtils/HttpTaskProcessor.cpp b/tester/harmony/svg/src/main/cpp/downloadUtils/HttpTaskProcessor.cpp index 7b1f174..05a3793 100644 --- a/tester/harmony/svg/src/main/cpp/downloadUtils/HttpTaskProcessor.cpp +++ b/tester/harmony/svg/src/main/cpp/downloadUtils/HttpTaskProcessor.cpp @@ -44,9 +44,30 @@ void ResponseCallback(void *usrCtx, Rcp_Response *response, uint32_t errCode) { } void HttpTaskProcessor::createParentPath(const fs::path &filePath) { - fs::path parentPath = filePath.parent_path(); - if (!parentPath.empty() && !fs::exists(parentPath)) { - fs::create_directories(parentPath); + try { + fs::path parentPath = filePath.parent_path(); + if (!parentPath.empty() && !fs::exists(parentPath)) { + // 检查路径的有效性 + if (parentPath.is_relative()) { + LOG(ERROR) << "[SVGImage] Invalid parent path (relative path): " << parentPath; + return; + } + // 尝试创建目录,并捕获可能的异常 + auto result = fs::create_directories(parentPath); + if (result) { + LOG(INFO) << "[SVGImage] Successfully created parent directories: " << parentPath; + } else { + LOG(ERROR) << "[SVGImage] Failed to create parent directories: " << parentPath; + } + } + } catch (const fs::filesystem_error& e) { + LOG(ERROR) << "[SVGImage] Filesystem error in createParentPath: " << e.what() + << " for path: " << filePath; + } catch (const std::exception& e) { + LOG(ERROR) << "[SVGImage] Standard exception in createParentPath: " << e.what() + << " for path: " << filePath; + } catch (...) { + LOG(ERROR) << "[SVGImage] Unknown exception in createParentPath for path: " << filePath; } } @@ -54,14 +75,52 @@ size_t HttpTaskProcessor::saveImage(const char *buffer, uint32_t length) { size_t status = 0; if (filePath_ == "") return status; - createParentPath(filePath_); - fp_ = fopen(filePath_.c_str(), "w"); - if (fp_) { - status = fwrite(buffer, sizeof(char), length, fp_); - fclose(fp_); - } else { - LOG(ERROR) << "[SVGImage] write to file failed: " << filePath_; + + // 验证输入参数 + if (buffer == nullptr || length == 0) { + LOG(ERROR) << "[SVGImage] Invalid buffer or length in saveImage"; + return status; + } + + try { + // 先创建父目录 + createParentPath(filePath_); + + // 验证文件路径的合理性 + fs::path filePath(filePath_); + if (filePath.has_filename()) { + LOG(INFO) << "[SVGImage] Attempting to save image to: " << filePath_; + } else { + LOG(ERROR) << "[SVGImage] Invalid file path (no filename): " << filePath_; + return status; + } + + // 打开文件进行写入 + fp_ = fopen(filePath_.c_str(), "w"); + if (fp_) { + status = fwrite(buffer, sizeof(char), length, fp_); + fclose(fp_); + + if (status == length) { + LOG(INFO) << "[SVGImage] Successfully saved image: " << filePath_ + << " (size: " << length << " bytes)"; + } else { + LOG(ERROR) << "[SVGImage] Partial write error. Expected: " << length + << ", wrote: " << status; + status = 0; // 返回0表示失败 + } + } else { + LOG(ERROR) << "[SVGImage] Failed to open file for writing: " << filePath_ + << " (errno: " << errno << ")"; + } + } catch (const std::exception& e) { + LOG(ERROR) << "[SVGImage] Exception in saveImage: " << e.what(); + status = 0; + } catch (...) { + LOG(ERROR) << "[SVGImage] Unknown exception in saveImage"; + status = 0; } + return status; } diff --git a/tester/harmony/svg/src/main/cpp/turboModules/RNSVGImageModule.cpp b/tester/harmony/svg/src/main/cpp/turboModules/RNSVGImageModule.cpp index c46d415..6b19725 100644 --- a/tester/harmony/svg/src/main/cpp/turboModules/RNSVGImageModule.cpp +++ b/tester/harmony/svg/src/main/cpp/turboModules/RNSVGImageModule.cpp @@ -20,6 +20,35 @@ RNSVGImageModule::RNSVGImageModule(const ArkTSTurboModule::Context ctx, const st // 获取缓存路径前缀 auto cache = this->callSync("getCacheDir", {}); m_fileCacheDir = cache.asString() + "/"; + + // 确保缓存目录存在 + try { + fs::path cachePath(m_fileCacheDir); + if (!fs::exists(cachePath)) { + if (auto result = fs::create_directories(cachePath)) { + LOG(INFO) << "[SVGImage] Created cache directory: " << m_fileCacheDir; + } else { + LOG(WARNING) << "[SVGImage] Failed to create cache directory: " << m_fileCacheDir; + } + } else if (!fs::is_directory(cachePath)) { + LOG(ERROR) << "[SVGImage] Cache path exists but is not a directory: " << m_fileCacheDir; + // 尝试创建备份目录 + std::string backupDir = m_fileCacheDir + "_backup"; + fs::create_directories(backupDir); + m_fileCacheDir = backupDir + "/"; + LOG(INFO) << "[SVGImage] Using backup cache directory: " << m_fileCacheDir; + } + } catch (const std::exception& e) { + LOG(ERROR) << "[SVGImage] Error creating/validating cache directory: " << e.what(); + // 使用临时目录作为后备方案 + m_fileCacheDir = "/data/local/tmp/rnsvg_cache/"; + try { + fs::create_directories(m_fileCacheDir); + } catch (...) { + LOG(ERROR) << "[SVGImage] Failed to create temp cache directory"; + } + } + m_imageSourceResolver = std::make_shared(m_fileCacheDir); fs::path directoryPath = m_fileCacheDir;