From 3dd09ea106572ba1b8950febd95a244715501aeb Mon Sep 17 00:00:00 2001 From: 18721213663 Date: Thu, 4 Jan 2024 09:47:46 +0800 Subject: [PATCH] fileuri_add Signed-off-by: 18721213663 --- .../kits/js/file_uri/file_uri_n_exporter.cpp | 18 +++++++++++++++ .../kits/js/file_uri/file_uri_n_exporter.h | 1 + .../kits/js/file_uri/prop_n_exporter.cpp | 22 +++++++++++++++++++ interfaces/kits/js/file_uri/prop_n_exporter.h | 3 +++ 4 files changed, 44 insertions(+) diff --git a/interfaces/kits/js/file_uri/file_uri_n_exporter.cpp b/interfaces/kits/js/file_uri/file_uri_n_exporter.cpp index 3c1d0f39e..03ee1759f 100644 --- a/interfaces/kits/js/file_uri/file_uri_n_exporter.cpp +++ b/interfaces/kits/js/file_uri/file_uri_n_exporter.cpp @@ -431,6 +431,23 @@ napi_value FileUriNExporter::GetFragment(napi_env env, napi_callback_info info) return NVal::CreateUTF8String(env, fileuriEntity->fileUri_.uri_.GetFragment()).val_; } +napi_value FileUriNExporter::GetFileUriRealPath(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ZERO)) { + LOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + auto fileuriEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); + if (!fileuriEntity) { + LOGE("Failed to get file entity"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + return NVal::CreateUTF8String(env, fileuriEntity->fileUri_.GetRealPath()).val_; +} + bool FileUriNExporter::Export() { vector props = { @@ -450,6 +467,7 @@ bool FileUriNExporter::Export() NVal::DeclareNapiGetter("port", GetPort), NVal::DeclareNapiGetter("query", GetQuery), NVal::DeclareNapiGetter("fragment", GetFragment), + NVal::DeclareNapiFunction("getRealPath", GetFileUriRealPath), }; auto [succ, classValue] = NClass::DefineClass(exports_.env_, className, Constructor, std::move(props)); diff --git a/interfaces/kits/js/file_uri/file_uri_n_exporter.h b/interfaces/kits/js/file_uri/file_uri_n_exporter.h index 59b450dbf..8f76c04e5 100644 --- a/interfaces/kits/js/file_uri/file_uri_n_exporter.h +++ b/interfaces/kits/js/file_uri/file_uri_n_exporter.h @@ -44,6 +44,7 @@ public: static napi_value GetPort(napi_env env, napi_callback_info info); static napi_value GetQuery(napi_env env, napi_callback_info info); static napi_value GetFragment(napi_env env, napi_callback_info info); + static napi_value GetFileUriRealPath(napi_env env, napi_callback_info info); FileUriNExporter(napi_env env, napi_value exports); ~FileUriNExporter() override; diff --git a/interfaces/kits/js/file_uri/prop_n_exporter.cpp b/interfaces/kits/js/file_uri/prop_n_exporter.cpp index ef5864e2c..f694faf66 100644 --- a/interfaces/kits/js/file_uri/prop_n_exporter.cpp +++ b/interfaces/kits/js/file_uri/prop_n_exporter.cpp @@ -14,16 +14,38 @@ */ #include "prop_n_exporter.h" +#include "file_uri.h" #include "get_uri_from_path.h" +#include "log.h" namespace OHOS::AppFileService::ModuleFileUri { using namespace std; using namespace FileManagement::LibN; +napi_value PropNExporter::IsUri(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE)) { + LOGE("Number of arguments unmatched"); + NError(E_PARAMS).ThrowErr(env); + return nullptr; + } + auto [succ, arg, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + if (!succ) { + LOGE("Failed to get uri parameter"); + NError(E_PARAMS).ThrowErr(env); + return nullptr; + } + string uri(arg.get()); + auto fileUri = new FileUri(uri); + return NVal::CreateBool(env, fileUri->uri_.GetHost() != "" && fileUri->uri_.GetScheme() == "file").val_; +} + bool PropNExporter::Export() { return exports_.AddProp({ NVal::DeclareNapiFunction("getUriFromPath", GetUriFromPath::Sync), + NVal::DeclareNapiFunction("isUri", IsUri), }); } diff --git a/interfaces/kits/js/file_uri/prop_n_exporter.h b/interfaces/kits/js/file_uri/prop_n_exporter.h index 886fb7329..24cc5fb03 100644 --- a/interfaces/kits/js/file_uri/prop_n_exporter.h +++ b/interfaces/kits/js/file_uri/prop_n_exporter.h @@ -24,11 +24,14 @@ class PropNExporter final : public FileManagement::LibN::NExporter { public: inline static const std::string className = "GetUriFromPath"; + static napi_value IsUri(napi_env env, napi_callback_info info); + bool Export() override; std::string GetClassName() override; PropNExporter(napi_env env, napi_value exports); ~PropNExporter() override; }; +const std::string FILEURI_PREFIX = "file://"; } // namespace OHOS::AppFileService::ModuleFileUri #endif // INTERFACES_KITS_JS_FILE_URI_PROP_N_EXPORTER_H \ No newline at end of file -- Gitee