!986 提供公共目录跨端搜索后的uri转换接口

Merge pull request !986 from zhangyaomaggie/master
This commit is contained in:
openharmony_ci 2024-07-25 03:04:56 +00:00 committed by Gitee
commit 54b71b8c46
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
3 changed files with 166 additions and 0 deletions

View File

@ -41,6 +41,10 @@ public:
static int32_t GetDfsUrisFromLocal(const std::vector<std::string> &uriList,
const int32_t &userId,
std::unordered_map<std::string, HmdfsUriInfo> &uriToDfsUriMaps);
static int32_t TransRemoteUriToLocal(const std::vector<std::string> &uriList,
const std::string &networkId,
const std::string &deviceId,
std::vector<std::string> &resultList);
~RemoteFileShare() {}
};
} // namespace ModuleRemoteFileShare

View File

@ -58,6 +58,8 @@ namespace {
const std::string PACKAGE_NAME = "get_dfs_uri_from_local";
const std::string NETWORK_PARA = "?networkid=";
const std::string MEDIA_BUNDLE_NAME = "com.ohos.medialibrary.medialibrarydata";
const std::string FILE_MANAGER_URI_HEAD = "/storage/";
const std::string REMOTE_SHARE_PATH_MID = "hmdfs/";
}
#define HMDFS_IOC_SET_SHARE_PATH _IOW(HMDFS_IOC, 1, struct HmdfsShareControl)
@ -456,6 +458,59 @@ int32_t RemoteFileShare::GetDfsUrisFromLocal(const std::vector<std::string> &uri
LOGD("GetDfsUriFromLocal successfully");
return 0;
}
int32_t RemoteFileShare::TransRemoteUriToLocal(const std::vector<std::string> &uriList,
const std::string &networkId,
const std::string &deviceId,
std::vector<std::string> &resultList)
{
if (networkId.empty() || deviceId.empty()) {
LOGE("RemoteFileShare::TransRemoteUriToLocal, invalid argument with %{public}d", EINVAL);
return EINVAL;
}
constexpr int splitThree = 3;
bool allValid = true;
std::vector<std::string> tmpResultList;
for (auto &uriStr : uriList) {
Uri uri(uriStr);
std::string bundleName = uri.GetAuthority();
std::string sandboxPath = SandboxHelper::Decode(uri.GetPath());
if (!SandboxHelper::IsValidPath(sandboxPath) || uri.GetScheme() != FILE_SCHEME) {
LOGE("Sandbox path from uri is error");
allValid = false;
break;
}
if ((bundleName != FILE_MANAGER_AUTHORITY) || (sandboxPath.find(FILE_MANAGER_URI_HEAD) != 0)) {
LOGE("Sandbox path doesn't begin with docs/storage");
allValid = false;
break;
}
int cnt = 0;
size_t pos = 0;
std::string part;
while (cnt < splitThree && pos != std::string::npos) {
pos = sandboxPath.find('/', pos + 1);
cnt++;
}
if (pos != std::string::npos) {
part = sandboxPath.substr(pos + 1);
}
if (part.empty()) {
allValid = false;
break;
}
std::string localUri = FILE_SCHEME + "://" + bundleName + FILE_MANAGER_URI_HEAD +
REMOTE_SHARE_PATH_MID + deviceId + "/" + part;
tmpResultList.push_back(localUri);
}
if (!allValid) {
LOGW("Failed to update uriList");
resultList = uriList;
return -EINVAL;
}
resultList = tmpResultList;
return 0;
}
} // namespace ModuleRemoteFileShare
} // namespace AppFileService
} // namespace OHOS

View File

@ -395,4 +395,111 @@ namespace {
GTEST_LOG_(INFO) << "RemoteFileShareTest file size is " << hui.fileSize;
GTEST_LOG_(INFO) << "RemoteFileShareTest-end Remote_file_share_GetDfsUriFromLocal_0011";
}
/**
* @tc.name: remote_file_share_test_0012
* @tc.desc: Test function of TransRemoteUriToLocal() interface for SUCCESS.
* @tc.size: MEDIUM
* @tc.type: FUNC
* @tc.level Level 1
* @tc.require: I7KDF7
*/
HWTEST_F(RemoteFileShareTest, Remote_file_share_TransRemoteUriToLocal_0012, testing::ext::TestSize.Level1)
{
GTEST_LOG_(INFO) << "RemoteFileShareTest-begin Remote_file_share_TransRemoteUriToLocal_0012";
const vector<string> uriList = {"file://docs/storage/Users/currentUser/Document/1.txt",
"file://docs/storage/Users/currentUser/Download/Subject/2.jpg",
"file://docs/storage/Users/currentUser/Document/Subject1/Subject2/1.txt",
"file://docs/storage/100/account/Document/Subject1/Subject2/1.txt"};
const vector<string> expectedList = {"file://docs/storage/hmdfs/001/Document/1.txt",
"file://docs/storage/hmdfs/001/Download/Subject/2.jpg",
"file://docs/storage/hmdfs/001/Document/Subject1/Subject2/1.txt",
"file://docs/storage/hmdfs/001/Document/Subject1/Subject2/1.txt"};
const string networkId = "100";
const string deviceId = "001";
vector<string> resultList;
int ret = RemoteFileShare::TransRemoteUriToLocal(uriList, networkId, deviceId, resultList);
EXPECT_EQ(ret, E_OK);
EXPECT_EQ(resultList, expectedList);
GTEST_LOG_(INFO) << "RemoteFileShareTest-end Remote_file_share_TransRemoteUriToLocal_0012";
}
/**
* @tc.name: remote_file_share_test_0013
* @tc.desc: Test function of TransRemoteUriToLocal() interface for FAILURE.
* the sandboxPath of uri does not equal to "storage"
* @tc.size: MEDIUM
* @tc.type: FUNC
* @tc.level Level 1
* @tc.require: I7KDF7
*/
HWTEST_F(RemoteFileShareTest, Remote_file_share_TransRemoteUriToLocal_0013, testing::ext::TestSize.Level1)
{
GTEST_LOG_(INFO) << "RemoteFileShareTest-begin Remote_file_share_TransRemoteUriToLocal_0013";
const vector<string> uriList = {"file://docs/storage/Users/currentUser/Document/1.txt",
"file://docs/hmdfs/Users/currentUser/Download/Subject/2.jpg",
"file://docs/tmp/Users/currentUser/Document/Subject1/Subject2/1.txt",
"file://docs/storage/100/account/Document/Subject1/Subject2/1.txt"};
const string networkId = "100";
const string deviceId = "001";
vector<string> resultList;
int ret = RemoteFileShare::TransRemoteUriToLocal(uriList, networkId, deviceId, resultList);
EXPECT_NE(ret, E_OK);
EXPECT_EQ(resultList, uriList);
GTEST_LOG_(INFO) << "RemoteFileShareTest-end Remote_file_share_TransRemoteUriToLocal_0013";
}
/**
* @tc.name: remote_file_share_test_0014
* @tc.desc: Test function of TransRemoteUriToLocal() interface for FAILURE.
* the bundlename of uri does not equal to "docs"
* @tc.size: MEDIUM
* @tc.type: FUNC
* @tc.level Level 1
* @tc.require: I7KDF7
*/
HWTEST_F(RemoteFileShareTest, Remote_file_share_TransRemoteUriToLocal_0014, testing::ext::TestSize.Level1)
{
GTEST_LOG_(INFO) << "RemoteFileShareTest-begin Remote_file_share_TransRemoteUriToLocal_0014";
const vector<string> uriList = {"file://docs/storage/Users/currentUser/Document/1.txt",
"file://doc/storage/Users/currentUser/Download/Subject/2.jpg",
"file://docs/storage/Users/currentUser/Document/Subject1/Subject2/1.txt",
"file://doc/storage/100/account/Document/Subject1/Subject2/1.txt"};
const string networkId = "100";
const string deviceId = "001";
vector<string> resultList;
int ret = RemoteFileShare::TransRemoteUriToLocal(uriList, networkId, deviceId, resultList);
EXPECT_NE(ret, E_OK);
EXPECT_EQ(resultList, uriList);
GTEST_LOG_(INFO) << "RemoteFileShareTest-end Remote_file_share_TransRemoteUriToLocal_0014";
}
/**
* @tc.name: remote_file_share_test_0015
* @tc.desc: Test function of TransRemoteUriToLocal() interface for FAILURE.
* the scheme of uri does not equal to "file"
* @tc.size: MEDIUM
* @tc.type: FUNC
* @tc.level Level 1
* @tc.require: I7KDF7
*/
HWTEST_F(RemoteFileShareTest, Remote_file_share_TransRemoteUriToLocal_0015, testing::ext::TestSize.Level1)
{
GTEST_LOG_(INFO) << "RemoteFileShareTest-begin Remote_file_share_TransRemoteUriToLocal_0015";
const vector<string> uriList = {"FILE://docs/storage/Users/currentUser/Document/1.txt",
"file://docs/storage/Users/currentUser/Download/Subject/2.jpg",
"file://docs/storage/Users/currentUser/Document/Subject1/Subject2/1.txt",
"file://docs/storage/100/account/Document/Subject1/Subject2/1.txt"};
const string networkId = "100";
const string deviceId = "001";
vector<string> resultList;
int ret = RemoteFileShare::TransRemoteUriToLocal(uriList, networkId, deviceId, resultList);
EXPECT_NE(ret, E_OK);
EXPECT_EQ(resultList, uriList);
GTEST_LOG_(INFO) << "RemoteFileShareTest-end Remote_file_share_TransRemoteUriToLocal_0015";
}
}