Skip to content

Commit

Permalink
Fix the issue of not closing after opening a raw file in OHOS OH_Reso…
Browse files Browse the repository at this point in the history
…urceManager. (#2577)
  • Loading branch information
Hparty authored Nov 8, 2024
1 parent 832d86c commit f9bef34
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 24 deletions.
15 changes: 5 additions & 10 deletions src/platform/ohos/JPAGFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 1 addition & 4 deletions src/platform/ohos/JPAGFont.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
14 changes: 4 additions & 10 deletions src/platform/ohos/JPAGImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
16 changes: 16 additions & 0 deletions src/platform/ohos/JsHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -437,4 +437,20 @@ napi_value CreateVideoRanges(napi_env env, const std::vector<PAGVideoRange>& vid
return result;
}

std::shared_ptr<ByteData> 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
3 changes: 3 additions & 0 deletions src/platform/ohos/JsHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#pragma once
#include <napi/native_api.h>
#include <rawfile/raw_file_manager.h>
#include <string>
#include "pag/pag.h"
#include "pag/types.h"
Expand Down Expand Up @@ -63,4 +64,6 @@ Color ToColor(int value);

napi_value CreateVideoRanges(napi_env env, const std::vector<PAGVideoRange>& videoRanges);

std::shared_ptr<ByteData> LoadDataFromAsset(NativeResourceManager* mNativeResMgr, char* srcBuf);

} // namespace pag

0 comments on commit f9bef34

Please sign in to comment.