From 8ddd4326d011c12d8f1f2c2df703eaad5d198fe6 Mon Sep 17 00:00:00 2001 From: cuiruibin Date: Mon, 24 Feb 2025 19:32:20 +0800 Subject: [PATCH] =?UTF-8?q?path=E8=BD=ACuri=E6=96=B0=E8=A7=84=E5=88=99?= =?UTF-8?q?=E9=80=82=E9=85=8D=20Signed-off-by:=20cuiruibin=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- interfaces/common/src/common_func.cpp | 7 +- .../kits/js/file_uri/file_uri_n_exporter.cpp | 4 -- .../file_uri_ndk_test/file_uri_ndk_test.cpp | 71 ++++++++++++++++++- 3 files changed, 76 insertions(+), 6 deletions(-) diff --git a/interfaces/common/src/common_func.cpp b/interfaces/common/src/common_func.cpp index cdbe7bfdb..01307b9ca 100644 --- a/interfaces/common/src/common_func.cpp +++ b/interfaces/common/src/common_func.cpp @@ -38,6 +38,8 @@ const std::string FILE_SCHEME_PREFIX = "file://"; const char BACKFLASH = '/'; const std::string FILE_MANAGER_URI_HEAD = "/storage/"; const std::string FILE_MANAGER_AUTHORITY = "docs"; +const std::string MEDIA_FUSE_PATH_HEAD = "/data/storage/el2/media"; +const std::string MEDIA_AUTHORITY = "file://media"; std::string g_bundleName = ""; std::mutex g_globalMutex; } @@ -113,13 +115,16 @@ string CommonFunc::GetUriFromPath(const string &path) } string realPath = path; NormalizePath(realPath); + if (realPath.find(MEDIA_FUSE_PATH_HEAD) == 0) { + return realPath.replace(realPath.find(MEDIA_FUSE_PATH_HEAD), MEDIA_FUSE_PATH_HEAD.length(), MEDIA_AUTHORITY); + } { std::lock_guard lock(g_globalMutex); if (g_bundleName == "") { g_bundleName = GetSelfBundleName(); } } - string packageName = (path.find(FILE_MANAGER_URI_HEAD) == 0) ? FILE_MANAGER_AUTHORITY : g_bundleName; + string packageName = (realPath.find(FILE_MANAGER_URI_HEAD) == 0) ? FILE_MANAGER_AUTHORITY : g_bundleName; realPath = FILE_SCHEME_PREFIX + packageName + SandboxHelper::Encode(realPath); return realPath; } 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 f89d08bff..b2c4b72a6 100644 --- a/interfaces/kits/js/file_uri/file_uri_n_exporter.cpp +++ b/interfaces/kits/js/file_uri/file_uri_n_exporter.cpp @@ -146,10 +146,6 @@ napi_value FileUriNExporter::GetFileUriPath(napi_env env, napi_callback_info inf NError(EINVAL).ThrowErr(env); return nullptr; } - 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_; } diff --git a/test/unittest/file_uri_ndk_test/file_uri_ndk_test.cpp b/test/unittest/file_uri_ndk_test/file_uri_ndk_test.cpp index 7d82dafa8..d3903b13c 100644 --- a/test/unittest/file_uri_ndk_test/file_uri_ndk_test.cpp +++ b/test/unittest/file_uri_ndk_test/file_uri_ndk_test.cpp @@ -260,7 +260,7 @@ HWTEST_F(NDKFileUriTest, get_uri_from_path_test_001, TestSize.Level1) { GTEST_LOG_(INFO) << "get_uri_from_path_test_001 start"; const char filePath[] = "storage/Users/currentUser/Documents/GetPathFromUri001.txt"; - const char fileUri[] = "file://com.example.filesharea/storage/Users/currentUser/Documents/GetPathFromUri001.txt"; + const char fileUri[] = "file://docs/storage/Users/currentUser/Documents/GetPathFromUri001.txt"; char *result = nullptr; unsigned int length = strlen(filePath); FileManagement_ErrCode ret = OH_FileUri_GetUriFromPath(filePath, length, &result); @@ -296,6 +296,75 @@ HWTEST_F(NDKFileUriTest, get_uri_from_path_test_002, TestSize.Level1) GTEST_LOG_(INFO) << "get_uri_from_path_test_002 end"; } +/** + * @tc.number: get_uri_from_path_test_003 + * @tc.name: Test function of OH_FileUri_GetUriFromPath() interface for document uri + * @tc.desc: Set path and get uri + * @tc.type: FUNC + */ +HWTEST_F(NDKFileUriTest, get_uri_from_path_test_003, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "get_uri_from_path_test_003 start"; + const char filePath[] = "data/storage/el2/media/Photo/12/IMG_12345_999999/test.jpg"; + const char fileUri[] = "file://media/Photo/12/IMG_12345_999999/test.jpg"; + char *result = nullptr; + unsigned int length = strlen(filePath); + FileManagement_ErrCode ret = OH_FileUri_GetUriFromPath(filePath, length, &result); + EXPECT_EQ(ret, ERR_OK); + if (result != nullptr) { + GTEST_LOG_(INFO) << result; + EXPECT_EQ(strcmp(result, fileUri), 0); + FreeResult(&result); + } + GTEST_LOG_(INFO) << "get_uri_from_path_test_003 end"; +} + +/** + * @tc.number: get_uri_from_path_test_004 + * @tc.name: Test function of OH_FileUri_GetUriFromPath() interface for document uri + * @tc.desc: Set path and get uri + * @tc.type: FUNC + */ +HWTEST_F(NDKFileUriTest, get_uri_from_path_test_004, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "get_uri_from_path_test_004 start"; + const char filePath[] = "data/storage/el2/base/Photo/12/IMG_12345_999999/test.jpg"; + const char fileUri[] = "file://com.example.filesharea/data/storage/el2/base/Photo/12/IMG_12345_999999/test.jpg"; + char *result = nullptr; + unsigned int length = strlen(filePath); + FileManagement_ErrCode ret = OH_FileUri_GetUriFromPath(filePath, length, &result); + EXPECT_EQ(ret, ERR_OK); + if (result != nullptr) { + GTEST_LOG_(INFO) << result; + EXPECT_EQ(strcmp(result, fileUri), 0); + FreeResult(&result); + } + GTEST_LOG_(INFO) << "get_uri_from_path_test_003 end"; +} + +/** + * @tc.number: get_uri_from_path_test_005 + * @tc.name: Test function of OH_FileUri_GetUriFromPath() interface for document uri + * @tc.desc: Set path and get uri + * @tc.type: FUNC + */ +HWTEST_F(NDKFileUriTest, get_uri_from_path_test_005, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "get_uri_from_path_test_005 start"; + const char filePath[] = "data/storage/el1/media/Photo/12/IMG_12345_999999/test.jpg"; + const char fileUri[] = "file://com.example.filesharea/data/storage/el1/media/Photo/12/IMG_12345_999999/test.jpg"; + char *result = nullptr; + unsigned int length = strlen(filePath); + FileManagement_ErrCode ret = OH_FileUri_GetUriFromPath(filePath, length, &result); + EXPECT_EQ(ret, ERR_OK); + if (result != nullptr) { + GTEST_LOG_(INFO) << result; + EXPECT_EQ(strcmp(result, fileUri), 0); + FreeResult(&result); + } + GTEST_LOG_(INFO) << "get_uri_from_path_test_005 end"; +} + /** * @tc.number: get_full_directory_uri_test_001 * @tc.name: Test function of OH_FileUri_GetFullDirectoryUri() interface for unknown path -- Gitee