diff --git a/bundle.json b/bundle.json index 94d46a748365397619fc35077ab21fc4abdcf595..cd037d76e03e6d5635f0c95c21bbe56480760e84 100644 --- a/bundle.json +++ b/bundle.json @@ -74,15 +74,6 @@ "header_base": "//foundation/filemanagement/app_file_service/interfaces/innerkits/native/file_share/include" } }, - { - "name": "//foundation/filemanagement/app_file_service/interfaces/innerkits/native:fileuri_native", - "header": { - "header_files": [ - "file_uri.h" - ], - "header_base": "//foundation/filemanagement/app_file_service/interfaces/innerkits/native/file_uri/include" - } - }, { "name": "//foundation/filemanagement/app_file_service/interfaces/innerkits/native:remote_file_share_native", "header": { diff --git a/interfaces/common/include/common_func.h b/interfaces/common/include/common_func.h index da1facccfc215ea480c878402d20a33946ba86e3..cbeb03d3b1419e3123bec1bd66cec2904b2af103 100644 --- a/interfaces/common/include/common_func.h +++ b/interfaces/common/include/common_func.h @@ -27,9 +27,6 @@ public: static bool CheckValidPath(const std::string &filePath); static int32_t GetPhysicalPath(const std::string &fileUri, const std::string &userId, std::string &physicalPath); - static std::string GetSelfBundleName(); - static std::string GetUriFromPath(const std::string &path); - static bool CheckPublicDirPath(const std::string &sandboxPath); }; } // namespace AppFileService } // namespace OHOS diff --git a/interfaces/innerkits/native/file_uri/include/file_uri.h b/interfaces/common/include/media_share.h similarity index 41% rename from interfaces/innerkits/native/file_uri/include/file_uri.h rename to interfaces/common/include/media_share.h index eb76514c38f5429c35a898cda37eecc79c1c1fe7..375ebdfc549fdb88b09a09220f623c568a6aabb0 100644 --- a/interfaces/innerkits/native/file_uri/include/file_uri.h +++ b/interfaces/common/include/media_share.h @@ -13,27 +13,33 @@ * limitations under the License. */ -#ifndef APP_FILE_SERVICE_FILE_URI_FILE_URI_H -#define APP_FILE_SERVICE_FILE_URI_FILE_URI_H +#ifndef FILEMANAGEMENT_APP_FILE_SERVICE_INTERFACES_INNERKITS_NATIVE_COMMON_INCLUDE_MEDIA_SHARE_H +#define FILEMANAGEMENT_APP_FILE_SERVICE_INTERFACES_INNERKITS_NATIVE_COMMON_INCLUDE_MEDIA_SHARE_H #include -#include "uri.h" +#include "datashare_values_bucket.h" +#include "filemgmt_libn.h" +#include "iremote_broker.h" + namespace OHOS { namespace AppFileService { -namespace ModuleFileUri { -class FileUri { +class FileShareGrantToken : public IRemoteBroker { public: - std::string GetName(); - std::string GetPath(); - std::string ToString(); + DECLARE_INTERFACE_DESCRIPTOR(u"ohos.fileshare.grantUriPermission"); + + FileShareGrantToken() = default; + virtual ~FileShareGrantToken() noexcept = default; +}; - explicit FileUri(const std::string &uriOrPath); - ~FileUri() = default; -private: - Uri uri_; +class MediaShare { +public: + static int InsertByDatashare(napi_env env, const DataShare::DataShareValuesBucket &valuesBucket, + bool isApi10); + static int GetDataShareInfo(const std::string &uri, const std::string &bundleName, + DataShare::DataShareValuesBucket &valuesBucket, bool &isApi10, const std::string &mode); + static std::string GetModeFromFlag(unsigned int flag); }; -} // ModuleFileUri -} // namespace AppFileService -} // namespace OHOS -#endif // APP_FILE_SERVICE_FILE_URI_FILE_URI_H \ No newline at end of file +} // namespace AppFileService +} // namespace OHOS +#endif \ No newline at end of file diff --git a/interfaces/common/src/common_func.cpp b/interfaces/common/src/common_func.cpp index 53bf7968eab23f02c74bb5b24fec028a1d1afba9..fae72c9d98339f77d2e6f640b2b561cfed75b994 100644 --- a/interfaces/common/src/common_func.cpp +++ b/interfaces/common/src/common_func.cpp @@ -17,21 +17,14 @@ #include -#include "bundle_info.h" -#include "bundle_mgr_proxy.h" -#include "ipc_skeleton.h" -#include "iservice_registry.h" -#include "uri.h" -#include "system_ability_definition.h" - #include "log.h" #include "json_utils.h" +#include "uri.h" using namespace std; namespace OHOS { namespace AppFileService { -using namespace OHOS::AppExecFwk; namespace { const string PACKAGE_NAME_FLAG = ""; const string CURRENT_USER_ID_FLAG = ""; @@ -39,11 +32,6 @@ 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 FILE_SCHEME_PREFIX = "file://"; - const char BACKFLASH = '/'; - const std::vector PUBLIC_DIR_PATHS = { - "/Documents" - }; } std::unordered_map CommonFunc::sandboxPathMap_; @@ -134,76 +122,6 @@ bool CommonFunc::CheckValidPath(const std::string &filePath) return false; } } - -static sptr GetBundleMgrProxy() -{ - sptr systemAbilityManager = - SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); - if (!systemAbilityManager) { - LOGE("fail to get system ability mgr."); - return nullptr; - } - - sptr remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); - if (!remoteObject) { - LOGE("fail to get bundle manager proxy."); - return nullptr; - } - - return iface_cast(remoteObject); -} - -string CommonFunc::GetSelfBundleName() -{ - int uid = -1; - uid = IPCSkeleton::GetCallingUid(); - - sptr bundleMgrProxy = GetBundleMgrProxy(); - if (!bundleMgrProxy) { - LOGE("GetSelfBundleName: bundle mgr proxy is nullptr."); - return ""; - } - - BundleInfo bundleInfo; - auto ret = bundleMgrProxy->GetBundleInfoForSelf(uid, bundleInfo); - if (ret != ERR_OK) { - LOGE("GetSelfBundleName: bundleName get fail. uid is %{public}d", uid); - return ""; - } - - return bundleInfo.name; -} - -bool CommonFunc::CheckPublicDirPath(const std::string &sandboxPath) -{ - for (const std::string &path : PUBLIC_DIR_PATHS) { - if (sandboxPath.find(path) == 0) { - return true; - } - } - return false; -} - -static void NormalizePath(string &path) -{ - if (path.size() == 0) { - return; - } - - if (path[0] != BACKFLASH) { - path.insert(0, 1, BACKFLASH); - } -} - -string CommonFunc::GetUriFromPath(const string &path) -{ - string realPath = path; - NormalizePath(realPath); - - string packageName = GetSelfBundleName(); - realPath = FILE_SCHEME_PREFIX + packageName + realPath; - return realPath; -} } // namespace AppFileService } // namespace OHOS diff --git a/interfaces/common/src/media_share.cpp b/interfaces/common/src/media_share.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c9c0de3ab4fc3db5475aa452388064f863945195 --- /dev/null +++ b/interfaces/common/src/media_share.cpp @@ -0,0 +1,153 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "media_share.h" + +#include "datashare_helper.h" +#include "iremote_stub.h" +#include "log.h" +#include "want.h" + +using namespace std; +using namespace OHOS::DataShare; +namespace OHOS { +namespace AppFileService { +namespace { + const std::string MEDIALIBRARY_DATA_URI = "datashare:///media"; + const string MEDIA_GRANT_URI_PERMISSION = + "datashare:///media/bundle_permission_insert_operation/bundle_permission_insert_operation"; + const string MEDIA_API_VERSION_10 = "?api_version=10"; + const string PERMISSION_BUNDLE_NAME = "bundle_name"; + const string PERMISSION_FILE_ID = "file_id"; + const string PERMISSION_MODE = "mode"; + const string PERMISSION_TABLE_TYPE = "table_type"; + const string MEDIA_FILE_URI_PHOTO_PREFEX = "file://media/Photo/"; + const string MEDIA_FILE_URI_AUDIO_PREFEX = "file://media/Audio/"; + const string MEDIA_FILE_URI_VIDEO_PREFEX = "file://media/video/"; + const string MEDIA_FILE_URI_IMAGE_PREFEX = "file://media/image/"; + const string MEDIA_FILE_URI_FILE_PREFEX = "file://media/file/"; + const string MEDIA_FILE_URI_Audio_PREFEX = "file://media/audio/"; +} + +enum MediaFileTable { + FILE_TABLE = 0, + PHOTO_TABLE = 1, + AUDIO_TABLE = 2, +}; + +static int32_t GetMediaTypeAndApiFromUri(const std::string &uri, bool &isApi10) +{ + if (uri.find(MEDIA_FILE_URI_PHOTO_PREFEX) == 0) { + isApi10 = true; + return MediaFileTable::PHOTO_TABLE; + } else if (uri.find(MEDIA_FILE_URI_VIDEO_PREFEX) == 0 || + uri.find(MEDIA_FILE_URI_IMAGE_PREFEX) == 0) { + return MediaFileTable::PHOTO_TABLE; + } else if (uri.find(MEDIA_FILE_URI_AUDIO_PREFEX) == 0) { + isApi10 = true; + return MediaFileTable::AUDIO_TABLE; + } else if (uri.find(MEDIA_FILE_URI_Audio_PREFEX) == 0) { + return MediaFileTable::AUDIO_TABLE; + } else if (uri.find(MEDIA_FILE_URI_FILE_PREFEX) == 0) { + return MediaFileTable::FILE_TABLE; + } + + return MediaFileTable::FILE_TABLE; +} + +static bool IsAllDigits(string idStr) +{ + for (size_t i = 0; i < idStr.size(); i++) { + if (!isdigit(idStr[i])) { + return false; + } + } + return true; +} + +static string GetIdFromUri(string uri) +{ + string rowNum = ""; + size_t pos = uri.rfind('/'); + if (pos != string::npos) { + rowNum = uri.substr(pos + 1); + if (!IsAllDigits(rowNum)) { + rowNum = ""; + } + } + return rowNum; +} + +string MediaShare::GetModeFromFlag(unsigned int flag) +{ + string mode = ""; + if (flag & OHOS::AAFwk::Want::FLAG_AUTH_READ_URI_PERMISSION) { + mode += "r"; + } + if (flag & OHOS::AAFwk::Want::FLAG_AUTH_WRITE_URI_PERMISSION) { + mode += "w"; + } + return mode; +} + +int MediaShare::GetDataShareInfo(const string &uri, const string &bundleName, + DataShareValuesBucket &valuesBucket, bool &isApi10, const string &mode) +{ + string idStr = GetIdFromUri(uri); + if (idStr == "") { + LOGE("FileShare::GetJSArgs get fileId parameter failed!"); + return EINVAL; + } + + int32_t fileId = stoi(idStr); + int32_t filesType = GetMediaTypeAndApiFromUri(uri, isApi10); + valuesBucket.Put(PERMISSION_FILE_ID, fileId); + valuesBucket.Put(PERMISSION_BUNDLE_NAME, bundleName); + valuesBucket.Put(PERMISSION_MODE, mode); + valuesBucket.Put(PERMISSION_TABLE_TYPE, filesType); + + return 0; +} + +int MediaShare::InsertByDatashare(napi_env env, const DataShareValuesBucket &valuesBucket, bool isApi10) +{ + int ret = -1; + std::shared_ptr dataShareHelper = nullptr; + sptr remote = new IRemoteStub(); + if (remote == nullptr) { + LOGE("FileShare::InsertByDatashare get remoteObject failed!"); + return -ENOMEM; + } + + dataShareHelper = DataShareHelper::Creator(remote->AsObject(), MEDIALIBRARY_DATA_URI); + if (!dataShareHelper) { + LOGE("FileShare::InsertByDatashare connect to datashare failed!"); + return -FileManagement::LibN::E_PERMISSION; + } + string uriStr = MEDIA_GRANT_URI_PERMISSION; + if (isApi10) { + uriStr += MEDIA_API_VERSION_10; + } + + Uri uri(uriStr); + ret = dataShareHelper->Insert(uri, valuesBucket); + if (ret < 0) { + LOGE("FileShare::InsertByDatashare insert failed with error code %{public}d!", ret); + return ret; + } + return ret; +} +} // namespace AppFileService +} // namespace OHOS diff --git a/interfaces/innerkits/native/BUILD.gn b/interfaces/innerkits/native/BUILD.gn index 6e53db4b53d807eff6de983362539122298852fd..09fd5238d620d791c0d0ae85631a45b03b47d1d1 100644 --- a/interfaces/innerkits/native/BUILD.gn +++ b/interfaces/innerkits/native/BUILD.gn @@ -23,19 +23,11 @@ config("file_share_config") { ] } -config("file_uri_config") { - visibility = [ ":*" ] - include_dirs = [ - "file_uri/include", - "//third_party/json/include", - "../../common/include", - ] -} - ohos_shared_library("fileshare_native") { sources = [ "../../common/src/common_func.cpp", "../../common/src/json_utils.cpp", + "../../common/src/media_share.cpp", "file_share/src/file_share.cpp", ] @@ -45,41 +37,15 @@ ohos_shared_library("fileshare_native") { "ability_base:base", "ability_base:want", "ability_base:zuri", - "ability_runtime:abilitykit_native", - "access_token:libaccesstoken_sdk", - "bundle_framework:appexecfwk_base", - "bundle_framework:appexecfwk_core", - "c_utils:utils", - "hilog:libhilog", - "ipc:ipc_core", - "samgr:samgr_proxy", - ] - - part_name = "app_file_service" - subsystem_name = "filemanagement" -} - -ohos_shared_library("fileuri_native") { - sources = [ - "../../common/src/common_func.cpp", - "../../common/src/json_utils.cpp", - "file_uri/src/file_uri.cpp", - ] - - public_configs = [ ":file_uri_config" ] - - external_deps = [ - "ability_base:base", - "ability_base:want", - "ability_base:zuri", - "ability_runtime:abilitykit_native", "access_token:libaccesstoken_sdk", - "bundle_framework:appexecfwk_base", - "bundle_framework:appexecfwk_core", "c_utils:utils", + "data_share:datashare_common", + "data_share:datashare_consumer", + "file_api:filemgmt_libhilog", + "file_api:filemgmt_libn", "hilog:libhilog", "ipc:ipc_core", - "samgr:samgr_proxy", + "napi:ace_napi", ] part_name = "app_file_service" @@ -119,13 +85,8 @@ ohos_shared_library("remote_file_share_native") { "ability_base:base", "ability_base:want", "ability_base:zuri", - "ability_runtime:abilitykit_native", - "bundle_framework:appexecfwk_base", - "bundle_framework:appexecfwk_core", "c_utils:utils", "hilog:libhilog", - "ipc:ipc_core", - "samgr:samgr_proxy", ] innerapi_tags = [ "platformsdk_indirect" ] @@ -136,7 +97,6 @@ ohos_shared_library("remote_file_share_native") { group("app_file_service_native") { deps = [ ":fileshare_native", - ":fileuri_native", ":remote_file_share_native", ] } diff --git a/interfaces/innerkits/native/file_share/src/file_share.cpp b/interfaces/innerkits/native/file_share/src/file_share.cpp index 5cfea0e65d9ac21e6dcccc07951696d241162de2..39ad159b711bac013f890603d3360088c02b0493 100644 --- a/interfaces/innerkits/native/file_share/src/file_share.cpp +++ b/interfaces/innerkits/native/file_share/src/file_share.cpp @@ -26,6 +26,7 @@ #include "common_func.h" #include "hap_token_info.h" #include "log.h" +#include "media_share.h" #include "uri.h" namespace OHOS { @@ -46,6 +47,7 @@ const string DATA_APP_EL2_PATH = "/data/service/el2/"; const string SHARE_R_PATH = "/r/"; const string SHARE_RW_PATH = "/rw/"; const string SHARE_PATH = "/share/"; +const string MEDIA = "media"; } struct FileShareInfo { @@ -128,6 +130,26 @@ static int32_t GetShareFileType(FileShareInfo &info) return -ENOENT; } +static int32_t GetShareMediaInfo(const string &uri, const string &bundleName, uint32_t flag) +{ + DataShare::DataShareValuesBucket valuesBucket; + bool isApi10; + string mode = MediaShare::GetModeFromFlag(flag); + int32_t ret = MediaShare::GetDataShareInfo(uri, bundleName, valuesBucket, isApi10, mode); + if (ret != 0) { + LOGE("Failed to GetDataShareInfo %{public}d", ret); + return ret; + } + + ret = MediaShare::InsertByDatashare(nullptr, valuesBucket, isApi10); + if (ret != 0) { + LOGE("Failed to InsertByDatashare %{public}d", ret); + return ret; + } + + return 0; +} + static int32_t GetFileShareInfo(const string &uri, uint32_t tokenId, uint32_t flag, FileShareInfo &info) { int32_t ret = 0; @@ -138,6 +160,16 @@ static int32_t GetFileShareInfo(const string &uri, uint32_t tokenId, uint32_t fl } GetProviderInfo(uri, info); + + if (info.providerBundleName_ == MEDIA) { + ret = GetShareMediaInfo(uri, info.targetBundleName_, flag); + if (ret != 0) { + LOGE("Failed to GetShareMediaInfo %{public}d", ret); + return ret; + } + + return 0; + } ret = CommonFunc::GetPhysicalPath(uri, info.currentUid_, info.providerLowerPath_); if (ret != 0) { @@ -213,6 +245,7 @@ static int32_t PreparePreShareDir(FileShareInfo &info) int32_t CreateShareFile(const string &uri, uint32_t tokenId, uint32_t flag) { + LOGI("CreateShareFile Enter!"); FileShareInfo info; int32_t ret = GetFileShareInfo(uri, tokenId, flag, info); if (ret != 0) { @@ -220,6 +253,11 @@ int32_t CreateShareFile(const string &uri, uint32_t tokenId, uint32_t flag) return ret; } + if (info.providerBundleName_ == MEDIA) { + LOGI("Create Media Share File Successfully!"); + return 0; + } + if ((ret = PreparePreShareDir(info)) != 0) { LOGE("PreparePreShareDir failed"); return ret; diff --git a/interfaces/innerkits/native/file_uri/src/file_uri.cpp b/interfaces/innerkits/native/file_uri/src/file_uri.cpp deleted file mode 100644 index d30bc0a55240bd4b84a676f2fd711a53e56a9f8b..0000000000000000000000000000000000000000 --- a/interfaces/innerkits/native/file_uri/src/file_uri.cpp +++ /dev/null @@ -1,76 +0,0 @@ -/* - * Copyright (c) 2023 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "file_uri.h" - -#include - -#include "uri.h" - -#include "common_func.h" -#include "log.h" - -using namespace std; -namespace OHOS { -namespace AppFileService { -namespace ModuleFileUri { -const std::string PATH_SHARE = "/data/storage/el2/share"; -const std::string MODE_RW = "/rw/"; -const std::string MODE_R = "/r/"; -const std::string FILE_SCHEME_PREFIX = "file://"; -string FileUri::GetName() -{ - string sandboxPath = uri_.GetPath(); - size_t posLast = sandboxPath.find_last_of("/"); - if (posLast == string::npos) { - return sandboxPath; - } - - if (posLast == sandboxPath.size()) { - return ""; - } - - return sandboxPath.substr(posLast + 1); -} - -string FileUri::GetPath() -{ - string sandboxPath = uri_.GetPath(); - string realPath = sandboxPath; - string providerBundleName = uri_.GetAuthority(); - string targetBundleName = CommonFunc::GetSelfBundleName(); - if (CommonFunc::CheckPublicDirPath(realPath) || - ((targetBundleName != providerBundleName) && (providerBundleName != ""))) { - realPath = PATH_SHARE + MODE_RW + providerBundleName + sandboxPath; - if (access(realPath.c_str(), F_OK) != 0) { - realPath = PATH_SHARE + MODE_R + providerBundleName + sandboxPath; - } - } - - return realPath; -} - -string FileUri::ToString() -{ - return uri_.ToString(); -} - -FileUri::FileUri(const string &uriOrPath): uri_( - (uriOrPath.find(FILE_SCHEME_PREFIX) == 0) ? uriOrPath : CommonFunc::GetUriFromPath(uriOrPath) -) -{} -} -} // namespace AppFileService -} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index bc8a7129dd510a50fdb575133336b7e132bdf310..f46af5eb34efeba10f1e45c7f579eed48951a98b 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -82,7 +82,6 @@ ohos_shared_library("fileuri") { ] sources = [ - "../../common/src/common_func.cpp", "file_uri/file_uri_n_exporter.cpp", "file_uri/get_uri_from_path.cpp", ] diff --git a/interfaces/kits/js/file_uri/get_uri_from_path.cpp b/interfaces/kits/js/file_uri/get_uri_from_path.cpp index da98d8611ae92487b4aca1a7985b2b6b738ed2ec..0c84d6558cc108d914ae1aba57b0b8d69c6ea2d2 100644 --- a/interfaces/kits/js/file_uri/get_uri_from_path.cpp +++ b/interfaces/kits/js/file_uri/get_uri_from_path.cpp @@ -14,15 +14,71 @@ */ #include "get_uri_from_path.h" -#include "status_receiver_host.h" - -#include "common_func.h" +#include "bundle_info.h" +#include "bundle_mgr_proxy.h" +#include "ipc_skeleton.h" +#include "iservice_registry.h" #include "log.h" +#include "status_receiver_host.h" +#include "system_ability_definition.h" namespace OHOS { namespace AppFileService { namespace ModuleFileUri { using namespace OHOS::FileManagement::LibN; +using namespace OHOS::AppExecFwk; + +static sptr GetBundleMgrProxy() +{ + sptr systemAbilityManager = + SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (!systemAbilityManager) { + LOGE("fail to get system ability mgr."); + return nullptr; + } + + sptr remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); + if (!remoteObject) { + LOGE("fail to get bundle manager proxy."); + return nullptr; + } + + return iface_cast(remoteObject); +} + +static string GetBundleName() +{ + int uid = -1; + uid = IPCSkeleton::GetCallingUid(); + + sptr bundleMgrProxy = GetBundleMgrProxy(); + if (!bundleMgrProxy) { + LOGE("GetBundleName: bundle mgr proxy is nullptr."); + return nullptr; + } + + BundleInfo bundleInfo; + auto ret = bundleMgrProxy->GetBundleInfoForSelf(uid, bundleInfo); + if (ret != ERR_OK) { + LOGE("GetBundleName: bundleName get fail. uid is %{public}d", uid); + return nullptr; + } + + return bundleInfo.name; +} + +static bool NormalizePath(string &path) +{ + if (path.size() <= 0) { + return false; + } + + if (path[0] != SCHEME_PATH_BEGIN) { + path.insert(0, 1, SCHEME_PATH_BEGIN); + } + + return true; +} napi_value GetUriFromPath::Sync(napi_env env, napi_callback_info info) { @@ -39,13 +95,15 @@ napi_value GetUriFromPath::Sync(napi_env env, napi_callback_info info) return nullptr; } - std::string uri = CommonFunc::GetUriFromPath(path.get()); - if (uri == "") { - LOGE("GetUriFromPath failed!"); + string realPath = path.get(); + if (!realPath.empty() && !NormalizePath(realPath)) { + LOGE("GetUriFromPath::NormalizePath failed!"); NError(EINVAL).ThrowErr(env); return nullptr; } + string packageName = GetBundleName(); + string uri = SCHEME + SCHEME_SEPARATOR + PATH_SYMBOLS + packageName + realPath; return NVal::CreateUTF8String(env, uri).val_; } diff --git a/interfaces/kits/js/file_uri/get_uri_from_path.h b/interfaces/kits/js/file_uri/get_uri_from_path.h index 8adb88fb962b84965f874f356b11e41c509a4a79..46ee4fc43fa0d9e7e482009d3fcb12c147bdd4e5 100644 --- a/interfaces/kits/js/file_uri/get_uri_from_path.h +++ b/interfaces/kits/js/file_uri/get_uri_from_path.h @@ -22,6 +22,14 @@ namespace OHOS { namespace AppFileService { namespace ModuleFileUri { +using namespace std; + +const string SCHEME = "file"; +const char SCHEME_SEPARATOR = ':'; +const string PATH_SYMBOLS = "//"; +const string FRAGMENT_SYMBOLS = "#"; +const char SCHEME_PATH_BEGIN = '/'; + class GetUriFromPath final { public: static napi_value Sync(napi_env env, napi_callback_info info); diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index 53d8db6d17ff7d0eee037bc1c1d3354156b11729..bce4a610a06117da087b800566b9918c57b088bd 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -15,7 +15,6 @@ group("unittest") { testonly = true deps = [ "file_share_native:file_share_test", - "file_uri_native:file_uri_test", "remote_file_share:remote_file_share_test", ] } diff --git a/test/unittest/file_uri_native/BUILD.gn b/test/unittest/file_uri_native/BUILD.gn deleted file mode 100644 index be6bb9cafeb864c5f5cbd7af8d766e5a59ff9418..0000000000000000000000000000000000000000 --- a/test/unittest/file_uri_native/BUILD.gn +++ /dev/null @@ -1,36 +0,0 @@ -# Copyright (c) 2023 Huawei Device Co., Ltd. -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import("//build/test.gni") - -ohos_unittest("file_uri_test") { - module_out_path = "filemanagement/app_file_service" - sources = [ "file_uri_test.cpp" ] - - external_deps = [ - "ability_base:base", - "ability_base:want", - "ability_base:zuri", - "access_token:libaccesstoken_sdk", - "app_file_service:fileshare_native", - "app_file_service:fileuri_native", - "c_utils:utils", - "hilog:libhilog", - "ipc:ipc_core", - ] - - deps = [ - "//third_party/googletest:gmock_main", - "//third_party/googletest:gtest_main", - ] -} diff --git a/test/unittest/file_uri_native/file_uri_test.cpp b/test/unittest/file_uri_native/file_uri_test.cpp deleted file mode 100644 index 6da553a986e1a251b3f39d273cfd32b8cae23381..0000000000000000000000000000000000000000 --- a/test/unittest/file_uri_native/file_uri_test.cpp +++ /dev/null @@ -1,214 +0,0 @@ -/* - * Copyright (c) 2023 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "file_uri.h" - -#include -#include -#include - -#include "accesstoken_kit.h" -#include "ipc_skeleton.h" -#include "uri.h" - -#include "common_func.h" -#include "file_share.h" -#include "log.h" - -using namespace std; -using namespace OHOS::Security::AccessToken; -using namespace OHOS::AppFileService; - -const string bundleA = "com.ohos.systemui"; -string CommonFunc::GetSelfBundleName() -{ - return bundleA; -} - -namespace OHOS::AppFileService::ModuleFileUri { - const string PATH_SHARE = "/data/storage/el2/share"; - const string MODE_RW = "/rw/"; - const string MODE_R = "/r/"; - const int E_OK = 0; - - class FileUriTest : public testing::Test { - public: - static void SetUpTestCase(void) {}; - static void TearDownTestCase() {}; - void SetUp() {}; - void TearDown() {}; - }; - - /** - * @tc.name: file_uri_test_0000 - * @tc.desc: Test function of ToString() interface for SUCCESS. - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - * @tc.require: I7LW57 - */ - HWTEST_F(FileUriTest, File_uri_ToString_0000, testing::ext::TestSize.Level1) - { - GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_ToString_0000"; - - string fileStr = "/data/storage/el2/base/files/test.txt"; - string uri = "file://" + bundleA + fileStr; - FileUri fileUri(fileStr); - EXPECT_EQ(fileUri.ToString(), uri); - - FileUri fileUri2(uri); - EXPECT_EQ(fileUri2.ToString(), uri); - GTEST_LOG_(INFO) << "FileUriTest-end File_uri_ToString_0000"; - } - - /** - * @tc.name: file_uri_test_0001 - * @tc.desc: Test function of GetName() interface for SUCCESS. - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - * @tc.require: I7LW57 - */ - HWTEST_F(FileUriTest, File_uri_GetName_0000, testing::ext::TestSize.Level1) - { - GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetName_0000"; - - string fileStr = "/data/storage/el2/base/files/test.txt"; - string uri = "file://" + bundleA + fileStr; - FileUri fileUri(fileStr); - string name = fileUri.GetName(); - EXPECT_EQ(name, "test.txt"); - GTEST_LOG_(INFO) << "FileUriTest-end File_uri_GetName_0000"; - } - - /** - * @tc.name: file_uri_test_0002 - * @tc.desc: Test function of GetPath() interface for SUCCESS. - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - * @tc.require: I7LW57 - */ - HWTEST_F(FileUriTest, File_uri_GetPath_0000, testing::ext::TestSize.Level1) - { - GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetPath_0000"; - string fileStr = "/data/storage/el2/base/files/test.txt"; - string uri = "file://" + bundleA + fileStr; - FileUri fileUri(uri); - string path = fileUri.GetPath(); - EXPECT_EQ(path, fileStr); - GTEST_LOG_(INFO) << "FileUriTest-end File_uri_GetPath_0000"; - } - - /** - * @tc.name: file_uri_test_0003 - * @tc.desc: Test function of GetPath() interface for SUCCESS. - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - * @tc.require: I7LW57 - */ - HWTEST_F(FileUriTest, File_uri_GetPath_0001, testing::ext::TestSize.Level1) - { - GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetPath_0001"; - string fileStr = "/Documents/test.txt"; - string uri = "file://" + bundleA + fileStr; - string rltStr = PATH_SHARE + MODE_R + bundleA + fileStr; - FileUri fileUri(uri); - string path = fileUri.GetPath(); - EXPECT_EQ(path, rltStr); - GTEST_LOG_(INFO) << "FileUriTest-end File_uri_GetPath_0001"; - } - - /** - * @tc.name: file_uri_test_0004 - * @tc.desc: Test function of GetPath() interface for SUCCESS. - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - * @tc.require: I7LW57 - */ - HWTEST_F(FileUriTest, File_uri_GetPath_0002, testing::ext::TestSize.Level1) - { - GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetPath_0002"; - string fileStr = "/data/storage/el2/base/files/test.txt"; - string bundleB = "com.demo.b"; - string uri = "file://" + bundleB + fileStr; - string rltStr = PATH_SHARE + MODE_R + bundleB + fileStr; - FileUri fileUri(uri); - string path = fileUri.GetPath(); - EXPECT_EQ(path, rltStr); - GTEST_LOG_(INFO) << "FileUriTest-end File_uri_GetPath_0002"; - } - - /** - * @tc.name: file_uri_test_0005 - * @tc.desc: Test function of GetPath() interface for SUCCESS. - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - * @tc.require: I7LW57 - */ - HWTEST_F(FileUriTest, File_uri_GetPath_0003, testing::ext::TestSize.Level1) - { - GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetPath_0003"; - int32_t uid = -1; - uid = OHOS::IPCSkeleton::GetCallingUid(); - string bundleB = "com.ohos.settingsdata"; - string fileStr = "/data/app/el2/" + to_string(uid) + "/base/" + bundleB + "/files/test.txt"; - int32_t fd = open(fileStr.c_str(), O_RDWR | O_CREAT); - ASSERT_TRUE(fd != -1) << "FileShareTest Create File Failed!"; - - string actStr = "/data/storage/el2/base/files/test.txt"; - string uri = "file://" + bundleB + actStr; - uint32_t tokenId = AccessTokenKit::GetHapTokenID(uid, bundleA, 0); - - int32_t flag = 3; - int32_t ret = CreateShareFile(uri, tokenId, flag); - EXPECT_EQ(ret, E_OK); - - string rltStr = PATH_SHARE + MODE_R + bundleB + actStr; - FileUri fileUri(uri); - string path = fileUri.GetPath(); - EXPECT_EQ(path, rltStr); - - vector sharePathList; - sharePathList.push_back(uri); - ret = DeleteShareFile(tokenId, sharePathList); - EXPECT_EQ(ret, E_OK); - close(fd); - GTEST_LOG_(INFO) << "FileUriTest-end File_uri_GetPath_0003"; - } - - /** - * @tc.name: file_uri_test_0006 - * @tc.desc: Test function of GetPath() interface for SUCCESS. - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - * @tc.require: I7LW57 - */ - HWTEST_F(FileUriTest, File_uri_GetPath_0004, testing::ext::TestSize.Level1) - { - GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetPath_0004"; - string fileStr = "/data/storage/el2/base/files/test.txt"; - string uri = "file://" + fileStr; - FileUri fileUri(uri); - EXPECT_EQ(fileUri.ToString(), uri); - EXPECT_EQ(fileUri.GetName(), "test.txt"); - EXPECT_EQ(fileUri.GetPath(), fileStr); - GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetPath_0004"; - } -} \ No newline at end of file