From 5f553fa84f3e546f1ad78c38bb2f345155632ed1 Mon Sep 17 00:00:00 2001 From: lvyuanyuan Date: Wed, 3 Jan 2024 09:15:07 +0800 Subject: [PATCH] =?UTF-8?q?fileuri=E5=AE=8C=E5=96=84=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: lvyuanyuan Change-Id: I946385e265e51acfd054ca702c8d9cab9354567e --- .../native/file_uri/include/file_uri.h | 1 - .../kits/js/file_uri/file_uri_n_exporter.cpp | 319 +++++++++++++++++- .../kits/js/file_uri/file_uri_n_exporter.h | 12 + 3 files changed, 330 insertions(+), 2 deletions(-) diff --git a/interfaces/innerkits/native/file_uri/include/file_uri.h b/interfaces/innerkits/native/file_uri/include/file_uri.h index 66b22e8b4..36f81859c 100644 --- a/interfaces/innerkits/native/file_uri/include/file_uri.h +++ b/interfaces/innerkits/native/file_uri/include/file_uri.h @@ -33,7 +33,6 @@ public: explicit FileUri(const std::string &uriOrPath); ~FileUri() = default; -private: Uri uri_; }; } // ModuleFileUri 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 498e8f0af..3c1d0f39e 100644 --- a/interfaces/kits/js/file_uri/file_uri_n_exporter.cpp +++ b/interfaces/kits/js/file_uri/file_uri_n_exporter.cpp @@ -14,14 +14,18 @@ */ #include "file_uri_n_exporter.h" +#include +#include + #include "file_uri_entity.h" #include "file_utils.h" #include "log.h" +#include "uri.h" +using namespace std; namespace OHOS { namespace AppFileService { namespace ModuleFileUri { -using namespace std; using namespace FileManagement; using namespace FileManagement::LibN; @@ -126,6 +130,307 @@ napi_value FileUriNExporter::GetFileUriPath(napi_env env, napi_callback_info inf return NVal::CreateUTF8String(env, fileuriEntity->fileUri_.GetPath()).val_; } +static std::string Split(const std::string &path, Uri &uri) +{ + std::string normalizeUri = ""; + if (!uri.GetScheme().empty()) { + normalizeUri += uri.GetScheme() + ":"; + } + if (uri.GetPath().empty()) { + normalizeUri += uri.GetSchemeSpecificPart(); + } else { + if (!uri.GetHost().empty()) { + normalizeUri += "//"; + if (!uri.GetUserInfo().empty()) { + normalizeUri += uri.GetUserInfo() + "@"; + } + normalizeUri += uri.GetHost(); + if (uri.GetPort() != -1) { + normalizeUri += ":" + std::to_string(uri.GetPort()); + } + } else if (!uri.GetAuthority().empty()) { + normalizeUri += "//" + uri.GetAuthority(); + } + normalizeUri += path; + } + if (!uri.GetQuery().empty()) { + normalizeUri += "?" + uri.GetQuery(); + } + if (!uri.GetFragment().empty()) { + normalizeUri += "#" + uri.GetFragment(); + } + return normalizeUri; +} + +static std::string NormalizeUri(Uri &uri) +{ + std::vector temp; + size_t pathLen = uri.GetPath().size(); + if (pathLen == 0) { + return uri.ToString(); + } + size_t pos = 0; + size_t left = 0; + while ((pos = uri.GetPath().find('/', left)) != std::string::npos) { + temp.push_back(uri.GetPath().substr(left, pos - left)); + left = pos + 1; + } + if (left != pathLen) { + temp.push_back(uri.GetPath().substr(left)); + } + size_t tempLen = temp.size(); + std::vector normalizeTemp; + for (size_t i = 0; i < tempLen; ++i) { + if (!temp[i].empty() && !(temp[i] == ".") && !(temp[i] == "..")) { + normalizeTemp.push_back(temp[i]); + } + if (temp[i] == "..") { + if (!normalizeTemp.empty() && normalizeTemp.back() != "..") { + normalizeTemp.pop_back(); + } else { + normalizeTemp.push_back(temp[i]); + } + } + } + std::string normalizePath = ""; + tempLen = normalizeTemp.size(); + if (tempLen == 0) { + normalizePath = "/"; + } else { + for (size_t i = 0; i < tempLen; ++i) { + normalizePath += "/" + normalizeTemp[i]; + } + } + return Split(normalizePath, uri); +} + +napi_value FileUriNExporter::Normalize(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, NormalizeUri(fileuriEntity->fileUri_.uri_)).val_; +} + +napi_value FileUriNExporter::Equals(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE)) { + LOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + auto thisEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); + if (!thisEntity) { + LOGE("Failed to get file entity"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto otherEntity = NClass::GetEntityOf(env, funcArg[NARG_POS::FIRST]); + if (!otherEntity) { + LOGE("Failed to get file entity"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + return NVal::CreateBool(env, thisEntity->fileUri_.uri_.Equals(otherEntity->fileUri_.uri_)).val_; +} + +napi_value FileUriNExporter::EqualsTo(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 thisEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); + if (!thisEntity) { + LOGE("Failed to get file entity"); + NError(E_PARAMS).ThrowErr(env); + return nullptr; + } + + auto otherEntity = NClass::GetEntityOf(env, funcArg[NARG_POS::FIRST]); + if (!otherEntity) { + LOGE("Failed to get file entity"); + NError(E_PARAMS).ThrowErr(env); + return nullptr; + } + return NVal::CreateBool(env, thisEntity->fileUri_.uri_.Equals(otherEntity->fileUri_.uri_)).val_; +} + +napi_value FileUriNExporter::IsAbsolute(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::CreateBool(env, fileuriEntity->fileUri_.uri_.IsAbsolute()).val_; +} + +napi_value FileUriNExporter::GetScheme(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_.uri_.GetScheme()).val_; +} + +napi_value FileUriNExporter::GetAuthority(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_.uri_.GetAuthority()).val_; +} + +napi_value FileUriNExporter::GetSsp(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_.uri_.GetSchemeSpecificPart()).val_; +} + +napi_value FileUriNExporter::GetUserInfo(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_.uri_.GetUserInfo()).val_; +} + +napi_value FileUriNExporter::GetHost(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_.uri_.GetHost()).val_; +} + +napi_value FileUriNExporter::GetPort(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, to_string(fileuriEntity->fileUri_.uri_.GetPort())).val_; +} + +napi_value FileUriNExporter::GetQuery(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_.uri_.GetQuery()).val_; +} + +napi_value FileUriNExporter::GetFragment(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_.uri_.GetFragment()).val_; +} + bool FileUriNExporter::Export() { vector props = { @@ -133,6 +438,18 @@ bool FileUriNExporter::Export() NVal::DeclareNapiGetter("name", GetFileUriName), NVal::DeclareNapiGetter("path", GetFileUriPath), NVal::DeclareNapiFunction("getFullDirectoryUri", GetFullDirectoryUri), + NVal::DeclareNapiFunction("normalize", Normalize), + NVal::DeclareNapiFunction("equals", Equals), + NVal::DeclareNapiFunction("equalsTo", EqualsTo), + NVal::DeclareNapiFunction("checkIsAbsolute", IsAbsolute), + NVal::DeclareNapiGetter("scheme", GetScheme), + NVal::DeclareNapiGetter("authority", GetAuthority), + NVal::DeclareNapiGetter("ssp", GetSsp), + NVal::DeclareNapiGetter("userInfo", GetUserInfo), + NVal::DeclareNapiGetter("host", GetHost), + NVal::DeclareNapiGetter("port", GetPort), + NVal::DeclareNapiGetter("query", GetQuery), + NVal::DeclareNapiGetter("fragment", GetFragment), }; 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 90ba26917..59b450dbf 100644 --- a/interfaces/kits/js/file_uri/file_uri_n_exporter.h +++ b/interfaces/kits/js/file_uri/file_uri_n_exporter.h @@ -32,6 +32,18 @@ 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 Normalize(napi_env env, napi_callback_info info); + static napi_value Equals(napi_env env, napi_callback_info cbinfo); + static napi_value EqualsTo(napi_env env, napi_callback_info info); + static napi_value IsAbsolute(napi_env env, napi_callback_info info); + static napi_value GetScheme(napi_env env, napi_callback_info info); + static napi_value GetAuthority(napi_env env, napi_callback_info info); + static napi_value GetSsp(napi_env env, napi_callback_info info); + static napi_value GetUserInfo(napi_env env, napi_callback_info info); + static napi_value GetHost(napi_env env, napi_callback_info info); + 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); FileUriNExporter(napi_env env, napi_value exports); ~FileUriNExporter() override; -- Gitee