From cc9923f11e2bf92ce11e26df6d37a8ebc68657b9 Mon Sep 17 00:00:00 2001 From: cuiruibin Date: Sat, 15 Mar 2025 11:35:25 +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 | 8 ++- .../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(+), 7 deletions(-) diff --git a/interfaces/common/src/common_func.cpp b/interfaces/common/src/common_func.cpp index 8cdb66b62..2a862e1fb 100644 --- a/interfaces/common/src/common_func.cpp +++ b/interfaces/common/src/common_func.cpp @@ -35,6 +35,8 @@ namespace { 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"; } static sptr GetBundleMgrProxy() { @@ -87,8 +89,10 @@ string CommonFunc::GetUriFromPath(const string &path) { string realPath = path; NormalizePath(realPath); - - string packageName = (path.find(FILE_MANAGER_URI_HEAD) == 0) ? FILE_MANAGER_AUTHORITY : GetSelfBundleName(); + if (realPath.find(MEDIA_FUSE_PATH_HEAD) == 0) { + return realPath.replace(realPath.find(MEDIA_FUSE_PATH_HEAD), MEDIA_FUSE_PATH_HEAD.length(), MEDIA_AUTHORITY); + } + string packageName = (realPath.find(FILE_MANAGER_URI_HEAD) == 0) ? FILE_MANAGER_AUTHORITY : GetSelfBundleName(); 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 f1a212a3e..8c096ae7d 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 d34f45de8..5f2ed12f6 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 @@ -233,7 +233,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); @@ -246,6 +246,75 @@ HWTEST_F(NDKFileUriTest, get_uri_from_path_test_001, TestSize.Level1) GTEST_LOG_(INFO) << "get_uri_from_path_test_001 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