!646 新增FileUri.JS接口

Merge pull request !646 from 崔瑞斌/sandbox_manager
This commit is contained in:
openharmony_ci 2024-02-23 11:14:30 +00:00 committed by Gitee
commit 87a5d8df4b
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
5 changed files with 57 additions and 3 deletions

View File

@ -89,6 +89,7 @@ ohos_shared_library("fileuri_native") {
"bundle_framework:appexecfwk_core",
"c_utils:utils",
"hilog:libhilog",
"init:libbegetutil",
"ipc:ipc_core",
"samgr:samgr_proxy",
]

View File

@ -29,6 +29,7 @@ public:
std::string GetRealPath();
std::string ToString();
std::string GetFullDirectoryUri();
bool IsRemoteUri();
explicit FileUri(const std::string &uriOrPath);
~FileUri() = default;

View File

@ -24,6 +24,7 @@
#include "common_func.h"
#include "log.h"
#include "sandbox_helper.h"
#include "parameter.h"
using namespace std;
namespace OHOS {
@ -36,6 +37,20 @@ const std::string FILE_SCHEME_PREFIX = "file://";
const std::string FILE_MANAGER_AUTHORITY = "docs";
const std::string MEDIA_AUTHORITY = "media";
const std::string NETWORK_PARA = "?networkid=";
const std::string BACKFLASH = "/";
const char *g_fullMountEnableParameter = "const.filemanager.full_mount.enable";
static bool CheckFileManagerFullMountEnable()
{
char value[] = "false";
int retSystem = GetParameter(g_fullMountEnableParameter, "false", value, sizeof(value));
if (retSystem > 0 && !strcmp(value, "true")) {
LOGD("The full mount enable parameter is true");
return true;
}
LOGD("The full mount enable parameter is false");
return false;
}
string FileUri::GetName()
{
string sandboxPath = SandboxHelper::Decode(uri_.GetPath());
@ -70,7 +85,7 @@ string FileUri::GetRealPath()
string bundleName = uri_.GetAuthority();
if (bundleName == FILE_MANAGER_AUTHORITY &&
uri_.ToString().find(NETWORK_PARA) == string::npos &&
access(realPath.c_str(), F_OK) == 0) {
(access(realPath.c_str(), F_OK) == 0 || CheckFileManagerFullMountEnable())) {
return realPath;
}
@ -109,6 +124,17 @@ string FileUri::GetFullDirectoryUri()
return "";
}
bool FileUri::IsRemoteUri()
{
size_t pos = uri_.ToString().find(NETWORK_PARA);
if (pos != string::npos && pos > 0 && pos < uri_.ToString().size() - NETWORK_PARA.size()) {
if (uri_.ToString().substr(pos + NETWORK_PARA.size()).find(BACKFLASH) == string::npos) {
return true;
}
}
return false;
}
FileUri::FileUri(const string &uriOrPath): uri_(
(uriOrPath.find(FILE_SCHEME_PREFIX) == 0) ? uriOrPath : CommonFunc::GetUriFromPath(uriOrPath)
)

View File

@ -21,6 +21,7 @@
#include "file_utils.h"
#include "log.h"
#include "uri.h"
#include "sandbox_helper.h"
using namespace std;
namespace OHOS {
@ -28,7 +29,7 @@ namespace AppFileService {
namespace ModuleFileUri {
using namespace FileManagement;
using namespace FileManagement::LibN;
const std::string MEDIA_AUTHORITY = "media";
napi_value FileUriNExporter::Constructor(napi_env env, napi_callback_info info)
{
NFuncArg funcArg(env, info);
@ -113,6 +114,24 @@ napi_value FileUriNExporter::GetFullDirectoryUri(napi_env env, napi_callback_inf
return NVal::CreateUTF8String(env, uri).val_;
}
napi_value FileUriNExporter::IsRemoteUri(napi_env env, napi_callback_info info)
{
NFuncArg funcArg(env, info);
if (!funcArg.InitArgs(NARG_CNT::ZERO)) {
LOGE("Number of arguments unmatched");
NError(E_PARAMS).ThrowErr(env);
return nullptr;
}
auto fileuriEntity = NClass::GetEntityOf<FileUriEntity>(env, funcArg.GetThisVar());
if (!fileuriEntity) {
LOGE("Failed to get file entity");
NError(EINVAL).ThrowErr(env);
return nullptr;
}
bool isRemoteUri = fileuriEntity->fileUri_.IsRemoteUri();
return NVal::CreateBool(env, isRemoteUri).val_;
}
napi_value FileUriNExporter::GetFileUriPath(napi_env env, napi_callback_info info)
{
NFuncArg funcArg(env, info);
@ -127,7 +146,12 @@ napi_value FileUriNExporter::GetFileUriPath(napi_env env, napi_callback_info inf
NError(EINVAL).ThrowErr(env);
return nullptr;
}
return NVal::CreateUTF8String(env, fileuriEntity->fileUri_.GetPath()).val_;
string sandboxPath = SandboxHelper::Decode(fileuriEntity->fileUri_.uri_.GetPath());
string bundleName = fileuriEntity->fileUri_.uri_.GetAuthority();
if (bundleName == MEDIA_AUTHORITY) {
return NVal::CreateUTF8String(env, fileuriEntity->fileUri_.GetPath()).val_;
}
return NVal::CreateUTF8String(env, fileuriEntity->fileUri_.GetRealPath()).val_;
}
static std::string Split(const std::string &path, Uri &uri)
@ -447,6 +471,7 @@ bool FileUriNExporter::Export()
NVal::DeclareNapiGetter("name", GetFileUriName),
NVal::DeclareNapiGetter("path", GetFileUriPath),
NVal::DeclareNapiFunction("getFullDirectoryUri", GetFullDirectoryUri),
NVal::DeclareNapiFunction("isRemoteUri", IsRemoteUri),
NVal::DeclareNapiFunction("normalize", Normalize),
NVal::DeclareNapiFunction("equals", Equals),
NVal::DeclareNapiFunction("equalsTo", EqualsTo),

View File

@ -32,6 +32,7 @@ public:
static napi_value GetFileUriName(napi_env env, napi_callback_info info);
static napi_value GetFileUriPath(napi_env env, napi_callback_info info);
static napi_value GetFullDirectoryUri(napi_env env, napi_callback_info info);
static napi_value IsRemoteUri(napi_env env, napi_callback_info info);
static napi_value Normalize(napi_env env, napi_callback_info info);
static napi_value Equals(napi_env env, napi_callback_info info);
static napi_value EqualsTo(napi_env env, napi_callback_info info);