#include <rawfile/raw_file.h>
#include <rawfile/raw_dir.h>
#include <rawfile/raw_file_manager.h>
// 示例一:获取rawfile文件列表 GetFileList
static napi_value GetFileList(napi_env env, napi_callback_info info)
{
OH_LOG_Print(LOG_APP, LOG_ERROR, GLOBAL_RESMGR, tag, "NDKTest Begin");
size_t requireArgc = 3;
size_t argc = 2;
napi_value argv[2] = { nullptr };
// 获取参数信息
napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
// argv[0]即为函数第一个参数Js资源对象,OH_ResourceManager_InitNativeResourceManager转为Native对象。
NativeResourceManager *mNativeResMgr = OH_ResourceManager_InitNativeResourceManager(env, argv[0]);
// 获取函数argv[1],此为为rawfile相对路径
size_t strSize;
char strBuf[256];
napi_get_value_string_utf8(env, argv[1], strBuf, sizeof(strBuf), &strSize);
std::string dirName(strBuf, strSize);
// 获取对应的rawDir指针对象
RawDir* rawDir = OH_ResourceManager_OpenRawDir(mNativeResMgr, dirName.c_str());
// 获取rawDir下文件及文件夹数量
int count = OH_ResourceManager_GetRawFileCount(rawDir);
// 遍历获取文件名称,并保存
std::vector<std::string> tempArray;
for(int i = 0; i < count; i++) {
std::string filename = OH_ResourceManager_GetRawFileName(rawDir, i);
tempArray.emplace_back(filename);
}
napi_value fileList;
napi_create_array(env, &fileList);
for (size_t i = 0; i < tempArray.size(); i++) {
napi_value jsString;
napi_create_string_utf8(env, tempArray[i].c_str(), NAPI_AUTO_LENGTH, &jsString);
napi_set_element(env, fileList, i, jsString);
}
// 关闭打开的指针对象
OH_ResourceManager_CloseRawDir(rawDir);
OH_ResourceManager_ReleaseNativeResourceManager(mNativeResMgr);
return fileList;
}
// 示例二:获取rawfile文件内容 GetRawFileContent
napi_value CreateJsArrayValue(napi_env env, std::unique_ptr<uint8_t[]> &data, long length)
{
napi_value buffer;
napi_status status = napi_create_external_arraybuffer(env, data.get(), length,
[](napi_env env, void *data, void *hint) {
delete[] static_cast<char*>(data);
}, nullptr, &buffer);
if (status != napi_ok) {
OH_LOG_Print(LOG_APP, LOG_ERROR, GLOBAL_RESMGR, tag, "Failed to create external array buffer");
return nullptr;
}
napi_value result = nullptr;
status = napi_create_typedarray(env, napi_uint8_array, length, buffer, 0, &result);
if (status != napi_ok) {
OH_LOG_Print(LOG_APP, LOG_ERROR, GLOBAL_RESMGR, tag, "Failed to create media typed array");
return nullptr;
}
data.release();
return result;
}
static napi_value GetRawFileContent(napi_env env, napi_callback_info info)
{
OH_LOG_Print(LOG_APP, LOG_ERROR, GLOBAL_RESMGR, tag, "GetFileContent Begin");
size_t requireArgc = 3;
size_t argc = 2;
napi_value argv[2] = { nullptr };
// 获取参数信息
napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
// argv[0]即为函数第一个参数Js资源对象,OH_ResourceManager_InitNativeResourceManager转为Native对象。
NativeResourceManager *mNativeResMgr = OH_ResourceManager_InitNativeResourceManager(env, argv[0]);
size_t strSize;
char strBuf[256];
napi_get_value_string_utf8(env, argv[1], strBuf, sizeof(strBuf), &strSize);
std::string filename(strBuf, strSize);
// 获取rawfile指针对象
RawFile *rawFile = OH_ResourceManager_OpenRawFile(mNativeResMgr, filename.c_str());
if (rawFile != nullptr) {
OH_LOG_Print(LOG_APP, LOG_ERROR, GLOBAL_RESMGR, tag, "OH_ResourceManager_OpenRawFile success");
}
// 获取rawfile大小并申请内存
long len = OH_ResourceManager_GetRawFileSize(rawFile);
std::unique_ptr<uint8_t[]> data= std::make_unique<uint8_t[]>(len);
// 读取rawfile
int res = OH_ResourceManager_ReadRawFile(rawFile, data.get(), len);
// 关闭打开的指针对象
OH_ResourceManager_CloseRawFile(rawFile);
OH_ResourceManager_ReleaseNativeResourceManager(mNativeResMgr);
// 转为js对象
return CreateJsArrayValue(env, data, len);
}
// 示例三:获取rawfile文件描述符 GetRawFileDescriptor
napi_value createJsFileDescriptor(napi_env env, RawFileDescriptor &descriptor)
{
napi_value result;
napi_status status = napi_create_object(env, &result);
if (status != napi_ok) {
return result;
}
napi_value fd;
status = napi_create_int32(env, descriptor.fd, &fd);
if (status != napi_ok) {
return result;
}
status = napi_set_named_property(env, result, "fd", fd);
if (status != napi_ok) {
return result;
}
napi_value offset;
status = napi_create_int64(env, descriptor.start, &offset);
if (status != napi_ok) {
return result;
}
status = napi_set_named_property(env, result, "offset", offset);
if (status != napi_ok) {
return result;
}
napi_value length;
status = napi_create_int64(env, descriptor.length, &length);
if (status != napi_ok) {
return result;
}
status = napi_set_named_property(env, result, "length", length);
if (status != napi_ok) {
return result;
}
return result;
}
static napi_value GetRawFileDescriptor(napi_env env, napi_callback_info info)
{
OH_LOG_Print(LOG_APP, LOG_ERROR, GLOBAL_RESMGR, tag, "NDKTest GetRawFileDescriptor Begin");
size_t requireArgc = 3;
size_t argc = 2;
napi_value argv[2] = { nullptr };
// 获取参数信息
napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
napi_valuetype valueType;
napi_typeof(env, argv[0], &valueType);
// 获取native的resourceManager对象
NativeResourceManager *mNativeResMgr = OH_ResourceManager_InitNativeResourceManager(env, argv[0]);
size_t strSize;
char strBuf[256];
napi_get_value_string_utf8(env, argv[1], strBuf, sizeof(strBuf), &strSize);
std::string filename(strBuf, strSize);
// 获取rawfile指针对象
RawFile *rawFile = OH_ResourceManager_OpenRawFile(mNativeResMgr, filename.c_str());
if (rawFile != nullptr) {
OH_LOG_Print(LOG_APP, LOG_ERROR, GLOBAL_RESMGR, tag, "OH_ResourceManager_OpenRawFile success");
}
// 获取rawfile的描述符RawFileDescriptor {fd, offset, length}
RawFileDescriptor descriptor;
OH_ResourceManager_GetRawFileDescriptor(rawFile, descriptor);
// 关闭打开的指针对象
OH_ResourceManager_CloseRawFile(rawFile);
OH_ResourceManager_ReleaseNativeResourceManager(mNativeResMgr);
// 转为js对象
return createJsFileDescriptor(env,descriptor);
}
评论