diff --git a/interfaces/common/src/common_func.cpp b/interfaces/common/src/common_func.cpp index cdbe7bfdbb244ee31c36700ffdc853ecf4d5ced1..01307b9ca6fb9db416e5581932d35a90df6a7e01 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 f89d08bff8845a0e42a4d0d8122306adbebcf70f..b2c4b72a69e96d726d3bed94e6a35fc825f6bdde 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 7d82dafa8164e658cfb3fbe16d28df79ef2845f3..d3903b13c9ed4be5c7d6ee2c241d32a25797b331 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