Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the issue of not closing after opening a raw file in OHOS OH_ResourceManager. #2577

Merged
merged 1 commit into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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