diff --git a/src/platform/ohos/JPAGFile.cpp b/src/platform/ohos/JPAGFile.cpp index 221fb091f9..01913595b1 100644 --- a/src/platform/ohos/JPAGFile.cpp +++ b/src/platform/ohos/JPAGFile.cpp @@ -80,17 +80,12 @@ static napi_value LoadFromAssets(napi_env env, napi_callback_info info) { char srcBuf[1024]; napi_get_value_string_utf8(env, args[1], srcBuf, sizeof(srcBuf), &strSize); std::string fileName(srcBuf, strSize); - RawFile* rawFile = OH_ResourceManager_OpenRawFile(mNativeResMgr, srcBuf); - if (rawFile != NULL) { - long len = OH_ResourceManager_GetRawFileSize(rawFile); - auto data = ByteData::Make(len); - if (!data) { - return nullptr; - } - OH_ResourceManager_ReadRawFile(rawFile, data->data(), len); - return JPAGLayerHandle::ToJs(env, PAGFile::Load(data->data(), len, "asset://" + fileName)); + auto data = LoadDataFromAsset(mNativeResMgr, srcBuf); + if (data == NULL) { + return nullptr; } - return nullptr; + return JPAGLayerHandle::ToJs(env, + PAGFile::Load(data->data(), data->length(), "asset://" + fileName)); } static napi_value TagLevel(napi_env env, napi_callback_info info) { diff --git a/src/platform/ohos/JPAGFont.cpp b/src/platform/ohos/JPAGFont.cpp index 01573088bf..41abd075e8 100644 --- a/src/platform/ohos/JPAGFont.cpp +++ b/src/platform/ohos/JPAGFont.cpp @@ -71,13 +71,10 @@ static napi_value RegisterFontFromAsset(napi_env env, napi_callback_info info) { if (rawFile == NULL) { return nullptr; } - - long len = OH_ResourceManager_GetRawFileSize(rawFile); - auto data = ByteData::Make(len); + auto data = LoadDataFromAsset(mNativeResMgr, name); if (data == nullptr) { return nullptr; } - OH_ResourceManager_ReadRawFile(rawFile, data->data(), len); int index = 0; if (argc > 2) { diff --git a/src/platform/ohos/JPAGImage.cpp b/src/platform/ohos/JPAGImage.cpp index 6a1382089b..6257d5962a 100644 --- a/src/platform/ohos/JPAGImage.cpp +++ b/src/platform/ohos/JPAGImage.cpp @@ -70,17 +70,11 @@ static napi_value LoadFromAssets(napi_env env, napi_callback_info info) { size_t strSize; char srcBuf[1024]; napi_get_value_string_utf8(env, args[1], srcBuf, sizeof(srcBuf), &strSize); - RawFile* rawFile = OH_ResourceManager_OpenRawFile(mNativeResMgr, srcBuf); - if (rawFile != NULL) { - long len = OH_ResourceManager_GetRawFileSize(rawFile); - auto data = ByteData::Make(len); - if (!data) { - return nullptr; - } - OH_ResourceManager_ReadRawFile(rawFile, data->data(), len); - return JPAGImage::ToJs(env, PAGImage::FromBytes(data->data(), data->length())); + auto data = LoadDataFromAsset(mNativeResMgr, srcBuf); + if (data == NULL) { + return nullptr; } - return nullptr; + return JPAGImage::ToJs(env, PAGImage::FromBytes(data->data(), data->length())); } static napi_value Width(napi_env env, napi_callback_info info) { diff --git a/src/platform/ohos/JsHelper.cpp b/src/platform/ohos/JsHelper.cpp index da9f5b9e51..959629329d 100644 --- a/src/platform/ohos/JsHelper.cpp +++ b/src/platform/ohos/JsHelper.cpp @@ -437,4 +437,20 @@ napi_value CreateVideoRanges(napi_env env, const std::vector& vid return result; } +std::shared_ptr LoadDataFromAsset(NativeResourceManager* mNativeResMgr, char* srcBuf) { + RawFile* rawFile = OH_ResourceManager_OpenRawFile(mNativeResMgr, srcBuf); + if (rawFile == NULL) { + return nullptr; + } + long len = OH_ResourceManager_GetRawFileSize(rawFile); + auto data = ByteData::Make(len); + if (!data) { + OH_ResourceManager_CloseRawFile(rawFile); + return nullptr; + } + OH_ResourceManager_ReadRawFile(rawFile, data->data(), len); + OH_ResourceManager_CloseRawFile(rawFile); + return data; +} + } // namespace pag \ No newline at end of file diff --git a/src/platform/ohos/JsHelper.h b/src/platform/ohos/JsHelper.h index e72ff5498c..ac789cde89 100644 --- a/src/platform/ohos/JsHelper.h +++ b/src/platform/ohos/JsHelper.h @@ -18,6 +18,7 @@ #pragma once #include +#include #include #include "pag/pag.h" #include "pag/types.h" @@ -63,4 +64,6 @@ Color ToColor(int value); napi_value CreateVideoRanges(napi_env env, const std::vector& videoRanges); +std::shared_ptr LoadDataFromAsset(NativeResourceManager* mNativeResMgr, char* srcBuf); + } // namespace pag