From c5b0f3c4b3dfbaacce5c9e675ae6f1441bc5caa8 Mon Sep 17 00:00:00 2001 From: lvyuanyuan Date: Tue, 1 Aug 2023 08:12:36 +0000 Subject: [PATCH] parse physical path from media uri Signed-off-by: lvyuanyuan Change-Id: I799a522683ba9e109dd7da56c04e39b659351c81 --- interfaces/common/src/sandbox_helper.cpp | 108 ++++++++++++++++++ .../file_share_native/file_share_test.cpp | 94 +++++++++++++++ 2 files changed, 202 insertions(+) diff --git a/interfaces/common/src/sandbox_helper.cpp b/interfaces/common/src/sandbox_helper.cpp index e30b78e7c..ceb157797 100644 --- a/interfaces/common/src/sandbox_helper.cpp +++ b/interfaces/common/src/sandbox_helper.cpp @@ -35,7 +35,21 @@ namespace { const string SANDBOX_PATH_KEY = "sandbox-path"; const string MOUNT_PATH_MAP_KEY = "mount-path-map"; const string SANDBOX_JSON_FILE_PATH = "/etc/app_file_service/file_share_sandbox.json"; + const std::string SHAER_PATH_HEAD = "/mnt/hmdfs/"; + const std::string SHAER_PATH_MID = "/account/merge_view/files/"; + const string BACKFLASH = "/"; + const string MEDIA = "media"; + const int ASSET_IN_BUCKET_NUM_MAX = 1000; + const int ASSET_DIR_START_NUM = 16; } + +struct MediaUriInfo { + string mediaType; + string fileId; + string realName; + string displayName; +}; + std::unordered_map SandboxHelper::sandboxPathMap_; string SandboxHelper::Encode(const string &uri) @@ -123,6 +137,96 @@ static void GetSandboxPathMap(unordered_map &sandboxPathMap) return; } +static int32_t GetPathSuffix(const std::string &path, string &pathSuffix) +{ + size_t pos = path.rfind('.'); + if (pos != string::npos) { + pathSuffix = path.substr(pos); + return 0; + } + return -EINVAL; +} + +static int32_t CalAssetBucket(const int32_t &fileId) +{ + int32_t bucketNum = 0; + if (fileId < 0) { + LOGE("input fileId %{private}d is invalid", fileId); + return -EINVAL; + } + + int32_t start = ASSET_DIR_START_NUM; + int32_t divider = ASSET_DIR_START_NUM; + while (fileId > start * ASSET_IN_BUCKET_NUM_MAX) { + divider = start; + start <<= 1; + } + + int32_t fileIdRemainder = fileId % divider; + if (fileIdRemainder == 0) { + bucketNum = start + fileIdRemainder; + } else { + bucketNum = (start - divider) + fileIdRemainder; + } + return bucketNum; +} + +static int32_t GetFileIdFromFileName(const std::string &fileName) +{ + size_t pos = fileName.find_last_of('_'); + if (pos == std::string::npos || pos == fileName.size() - 1) { + return -EINVAL; + } + + std::string idStr = fileName.substr(pos + 1); + if (!std::all_of(idStr.begin(), idStr.end(), ::isdigit)) { + return -EINVAL; + } + + return std::stoi(idStr); +} + +static int32_t GetBucketNum(const std::string &fileName) +{ + int32_t fileId = GetFileIdFromFileName(fileName); + if (fileId < 0) { + LOGE("GetFileIdFromFileName failed with %{public}s", fileName.c_str()); + return fileId; + } + return CalAssetBucket(fileId); +} + +static void ParseMediaSandboxPath(const string &sandboxPath, MediaUriInfo &mediaUriInfo) +{ + string path = sandboxPath; + std::replace(path.begin(), path.end(), '/', ' '); + stringstream ss; + ss << path; + ss >> mediaUriInfo.mediaType >> mediaUriInfo.fileId >> mediaUriInfo.realName >> mediaUriInfo.displayName; +} + +static int32_t GetMediaPhysicalPath(const std::string &sandboxPath, const std::string &userId, + std::string &physicalPath) +{ + MediaUriInfo mediaUriInfo; + ParseMediaSandboxPath(sandboxPath, mediaUriInfo); + + int32_t bucketNum = GetBucketNum(mediaUriInfo.realName); + if (bucketNum < 0) { + return -EINVAL; + } + + std::string mediaSuffix; + if (GetPathSuffix(sandboxPath, mediaSuffix) != 0) { + LOGE("GetPathSuffix failed"); + return -EINVAL; + } + + physicalPath = SHAER_PATH_HEAD + userId + SHAER_PATH_MID + mediaUriInfo.mediaType + + BACKFLASH + to_string(bucketNum) + BACKFLASH + mediaUriInfo.realName + mediaSuffix; + return 0; +} + int32_t SandboxHelper::GetPhysicalPath(const std::string &fileUri, const std::string &userId, std::string &physicalPath) { @@ -130,6 +234,10 @@ int32_t SandboxHelper::GetPhysicalPath(const std::string &fileUri, const std::st string bundleName = uri.GetAuthority(); string sandboxPath = uri.GetPath(); + if (bundleName == MEDIA) { + return GetMediaPhysicalPath(sandboxPath, userId, physicalPath); + } + string lowerPathTail = ""; string lowerPathHead = ""; diff --git a/test/unittest/file_share_native/file_share_test.cpp b/test/unittest/file_share_native/file_share_test.cpp index 3ff1bada1..b464ef075 100644 --- a/test/unittest/file_share_native/file_share_test.cpp +++ b/test/unittest/file_share_native/file_share_test.cpp @@ -21,9 +21,11 @@ #include #include #include + #include "accesstoken_kit.h" #include "file_share.h" #include "ipc_skeleton.h" +#include "sandbox_helper.h" namespace { using namespace std; @@ -231,4 +233,96 @@ namespace { EXPECT_EQ(ret, -EINVAL); GTEST_LOG_(INFO) << "FileShareTest-end File_share_DeleteShareFile_0006"; } + + /** + * @tc.name: File_share_GetPhysicalPath_0001 + * @tc.desc: Test function of GetPhysicalPath() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I7PDZL + */ + HWTEST_F(FileShareTest, File_share_GetPhysicalPath_0001, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "FileShareTest-begin File_share_GetPhysicalPath_0001"; + std::string fileUri = "file://media/Photo/12/IMG_12345_0011/test.jpg"; + std::string physicalPath; + int32_t ret = SandboxHelper::GetPhysicalPath(fileUri, "100", physicalPath); + EXPECT_EQ(ret, E_OK); + EXPECT_EQ(physicalPath, "/mnt/hmdfs/100/account/merge_view/files/Photo/11/IMG_12345_0011.jpg"); + GTEST_LOG_(INFO) << "FileShareTest-end File_share_GetPhysicalPath_0001"; + } + + /** + * @tc.name: File_share_GetPhysicalPath_0002 + * @tc.desc: Test function of GetPhysicalPath() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I7PDZL + */ + HWTEST_F(FileShareTest, File_share_GetPhysicalPath_0002, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "FileShareTest-begin File_share_GetPhysicalPath_0002"; + std::string fileUri = "file://media/Photo/12/IMG_12345_/test.jpg"; + std::string physicalPath; + int32_t ret = SandboxHelper::GetPhysicalPath(fileUri, "100", physicalPath); + EXPECT_EQ(ret, -EINVAL); + GTEST_LOG_(INFO) << "FileShareTest-end File_share_GetPhysicalPath_0002"; + } + + /** + * @tc.name: File_share_GetPhysicalPath_0003 + * @tc.desc: Test function of GetPhysicalPath() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I7PDZL + */ + HWTEST_F(FileShareTest, File_share_GetPhysicalPath_0003, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "FileShareTest-begin File_share_GetPhysicalPath_0003"; + std::string fileUri = "file://media/Photo/12/IMG_12345_a0011/test.jpg"; + std::string physicalPath; + int32_t ret = SandboxHelper::GetPhysicalPath(fileUri, "100", physicalPath); + EXPECT_EQ(ret, -EINVAL); + GTEST_LOG_(INFO) << "FileShareTest-end File_share_GetPhysicalPath_0003"; + } + + /** + * @tc.name: File_share_GetPhysicalPath_0004 + * @tc.desc: Test function of GetPhysicalPath() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I7PDZL + */ + HWTEST_F(FileShareTest, File_share_GetPhysicalPath_0004, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "FileShareTest-begin File_share_GetPhysicalPath_0004"; + std::string fileUri = "file://media/Photo/IMG_12345_999999/test.jpg"; + std::string physicalPath; + int32_t ret = SandboxHelper::GetPhysicalPath(fileUri, "100", physicalPath); + EXPECT_EQ(ret, -EINVAL); + GTEST_LOG_(INFO) << "FileShareTest-end File_share_GetPhysicalPath_0004"; + } + + /** + * @tc.name: File_share_GetPhysicalPath_0005 + * @tc.desc: Test function of GetPhysicalPath() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I7PDZL + */ + HWTEST_F(FileShareTest, File_share_GetPhysicalPath_0005, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "FileShareTest-begin File_share_GetPhysicalPath_0005"; + std::string fileUri = "file://media/Photo/12/IMG_12345_999999/test.jpg"; + std::string physicalPath; + int32_t ret = SandboxHelper::GetPhysicalPath(fileUri, "100", physicalPath); + EXPECT_EQ(ret, E_OK); + EXPECT_EQ(physicalPath, "/mnt/hmdfs/100/account/merge_view/files/Photo/575/IMG_12345_999999.jpg"); + GTEST_LOG_(INFO) << "FileShareTest-end File_share_GetPhysicalPath_0005"; + } } \ No newline at end of file -- Gitee