From 078fd14e8ded498d03e9e53fa5da717ea1fe45cc Mon Sep 17 00:00:00 2001 From: lvyuanyuan Date: Mon, 10 Jul 2023 02:25:47 +0000 Subject: [PATCH 1/2] =?UTF-8?q?GetUriFromPath=E5=A2=9E=E5=8A=A0=E8=BE=93?= =?UTF-8?q?=E5=85=A5=E8=B7=AF=E5=BE=84=E4=B8=8D=E4=BB=A5'/'=E5=BC=80?= =?UTF-8?q?=E5=A4=B4=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: lvyuanyuan Change-Id: I03d0b23b5a922eb2a409a3d0c5b6ef6b54b43b95 --- .../kits/js/file_uri/get_uri_from_path.cpp | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/interfaces/kits/js/file_uri/get_uri_from_path.cpp b/interfaces/kits/js/file_uri/get_uri_from_path.cpp index 56c1ee38a..c0cd8f714 100644 --- a/interfaces/kits/js/file_uri/get_uri_from_path.cpp +++ b/interfaces/kits/js/file_uri/get_uri_from_path.cpp @@ -67,6 +67,19 @@ static string GetBundleName() return bundleInfo.name; } +static bool NormalizePath(string &path) +{ + if (path.size() <= 0) { + return false; + } + + if (path[0] != '/') { + path.insert(0, 1, '/'); + } + + return true; +} + napi_value GetUriFromPath::Sync(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); @@ -81,8 +94,16 @@ napi_value GetUriFromPath::Sync(napi_env env, napi_callback_info info) NError(EINVAL).ThrowErr(env); return nullptr; } + + string realPath = path.get(); + if (!NormalizePath(realPath)) { + LOGE("GetUriFromPath::NormalizePath failed!"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + string packageName = GetBundleName(); - string uri = SCHEME + SCHEME_SEPARATOR + PATH_SYMBOLS + packageName + path.get(); + string uri = SCHEME + SCHEME_SEPARATOR + PATH_SYMBOLS + packageName + realPath; return NVal::CreateUTF8String(env, uri).val_; } -- Gitee From 72fc768cc41daebc6bc97f3aba7e35c7bb29050c Mon Sep 17 00:00:00 2001 From: lvyuanyuan Date: Mon, 10 Jul 2023 02:35:57 +0000 Subject: [PATCH 2/2] =?UTF-8?q?GetUriFromPath=E5=A2=9E=E5=8A=A0=E8=BE=93?= =?UTF-8?q?=E5=85=A5=E8=B7=AF=E5=BE=84=E4=B8=8D=E4=BB=A5'/'=E5=BC=80?= =?UTF-8?q?=E5=A4=B4=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: lvyuanyuan Change-Id: Ia24098a77890e6f8304c260be2b021b6d64593b6 --- interfaces/kits/js/file_uri/get_uri_from_path.cpp | 4 ++-- interfaces/kits/js/file_uri/get_uri_from_path.h | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/interfaces/kits/js/file_uri/get_uri_from_path.cpp b/interfaces/kits/js/file_uri/get_uri_from_path.cpp index c0cd8f714..ea23b1118 100644 --- a/interfaces/kits/js/file_uri/get_uri_from_path.cpp +++ b/interfaces/kits/js/file_uri/get_uri_from_path.cpp @@ -73,8 +73,8 @@ static bool NormalizePath(string &path) return false; } - if (path[0] != '/') { - path.insert(0, 1, '/'); + if (path[0] != SCHEME_PATH_BEGIN) { + path.insert(0, 1, SCHEME_PATH_BEGIN); } return true; diff --git a/interfaces/kits/js/file_uri/get_uri_from_path.h b/interfaces/kits/js/file_uri/get_uri_from_path.h index 7dbea01cd..46ee4fc43 100644 --- a/interfaces/kits/js/file_uri/get_uri_from_path.h +++ b/interfaces/kits/js/file_uri/get_uri_from_path.h @@ -28,6 +28,7 @@ const string SCHEME = "file"; const char SCHEME_SEPARATOR = ':'; const string PATH_SYMBOLS = "//"; const string FRAGMENT_SYMBOLS = "#"; +const char SCHEME_PATH_BEGIN = '/'; class GetUriFromPath final { public: -- Gitee