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 56c1ee38af7cfd28948175e4837d8d0b040e3aec..ea23b11186ec880a1e82fc574ab450d66e6ce677 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] != SCHEME_PATH_BEGIN) { + path.insert(0, 1, SCHEME_PATH_BEGIN); + } + + 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_; } 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 7dbea01cd50d4fb90e09facdaa349d028eb6fb8f..46ee4fc43fa0d9e7e482009d3fcb12c147bdd4e5 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: