From c57e6611437bcac12ae114e8a2ee5194a70c4560 Mon Sep 17 00:00:00 2001 From: zhangkaixiang Date: Tue, 22 Nov 2022 04:54:50 +0000 Subject: [PATCH] support to open the media uri Signed-off-by: zhangkaixiang --- interfaces/kits/js/BUILD.gn | 19 ++++++- .../js/src/mod_fileio/properties/open_v9.cpp | 45 +++++++++++++++- .../js/src/mod_fileio/properties/open_v9.h | 12 +++++ .../kits/native/remote_uri/remote_uri.cpp | 51 +++++++++++++++++++ .../kits/native/remote_uri/remote_uri.h | 3 ++ 5 files changed, 127 insertions(+), 3 deletions(-) diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index 8b4b5914d..dc04f46b1 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -123,9 +123,24 @@ ohos_shared_library("fs") { "src/mod_fileio/properties/truncate_v9.cpp", ] - deps = [ "//foundation/arkui/napi:ace_napi" ] + deps = [ + "//foundation/arkui/napi:ace_napi", + "//foundation/filemanagement/file_api/interfaces/kits/native:remote_uri_native", + ] - external_deps = [ "hiviewdfx_hilog_native:libhilog" ] + external_deps = [ + "ability_base:want", + "ability_base:zuri", + "ability_runtime:ability_manager", + "ability_runtime:abilitykit_native", + "ability_runtime:napi_base_context", + "ability_runtime:static_subscriber_extension", + "c_utils:utils", + "data_share:datashare_common", + "data_share:datashare_consumer", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + ] } ohos_shared_library("file") { diff --git a/interfaces/kits/js/src/mod_fileio/properties/open_v9.cpp b/interfaces/kits/js/src/mod_fileio/properties/open_v9.cpp index fad436c57..35e3804b3 100644 --- a/interfaces/kits/js/src/mod_fileio/properties/open_v9.cpp +++ b/interfaces/kits/js/src/mod_fileio/properties/open_v9.cpp @@ -27,6 +27,10 @@ #include "../../common/napi/n_func_arg.h" #include "../../common/uni_error.h" #include "../common_func.h" +#include "datashare_helper.h" +#include "napi_base_context.h" +#include "remote_uri.h" +#include "ability.h" #include "../class_file/file_entity.h" #include "../class_file/file_n_exporter.h" @@ -71,6 +75,25 @@ static NVal InstantiateFile(napi_env env, int fd, string path) return { env, objRAF }; } +static int OpenFileByDatashare(napi_env env, napi_value argv, string path) +{ + std::shared_ptr dataShareHelper = nullptr; + int fd = -1; + sptr remote = new IRemoteStub(); + if (remote == nullptr) { + UniError(errno).ThrowErr(env); + return fd; + } + + dataShareHelper = DataShare::DataShareHelper::Creator(remote->AsObject(), MEDIALIBRARY_DATA_URI); + Uri uri(path); + fd = dataShareHelper->OpenFile(uri, MEDIA_FILEMODE_READONLY); + if (fd == -1) { + UniError(errno).ThrowErr(env); + } + return fd; +} + napi_value OpenV9::Sync(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); @@ -87,6 +110,15 @@ napi_value OpenV9::Sync(napi_env env, napi_callback_info info) if (!succMode) { return nullptr; } + if (DistributedFS::ModuleRemoteUri::RemoteUri::IsMediaUri(path.get())) { + auto fd = OpenFileByDatashare(env, funcArg[NARG_POS::FIRST], path.get()); + if (fd >= 0) { + auto File = InstantiateFile(env, fd, path.get()).val_; + return File; + } + UniError(errno).ThrowErr(env); + return nullptr; + } uv_loop_s *loop = nullptr; napi_get_uv_event_loop(env, &loop); uv_fs_t open_req; @@ -124,7 +156,18 @@ napi_value OpenV9::Async(napi_env env, napi_callback_info info) return nullptr; } auto arg = make_shared(); - auto cbExec = [arg, path = string(path.get()), mode = mode](napi_env env) -> UniError { + auto argv = funcArg[NARG_POS::FIRST]; + auto cbExec = [arg, argv, path = string(path.get()), mode = mode](napi_env env) -> UniError { + if (DistributedFS::ModuleRemoteUri::RemoteUri::IsMediaUri(path)) { + auto fd = OpenFileByDatashare(env, argv, path); + if (fd >= 0) { + arg->fd = fd; + arg->path = path; + arg->uri = ""; + return UniError(ERRNO_NOERR); + } + return UniError(errno); + } uv_loop_s *loop = nullptr; napi_get_uv_event_loop(env, &loop); uv_fs_t open_req; diff --git a/interfaces/kits/js/src/mod_fileio/properties/open_v9.h b/interfaces/kits/js/src/mod_fileio/properties/open_v9.h index 93fbfd3e0..ac8921b4c 100644 --- a/interfaces/kits/js/src/mod_fileio/properties/open_v9.h +++ b/interfaces/kits/js/src/mod_fileio/properties/open_v9.h @@ -16,6 +16,7 @@ #ifndef INTERFACES_KITS_JS_SRC_MOD_FILEIO_PROPERTIES_OPEN_V9_H #define INTERFACES_KITS_JS_SRC_MOD_FILEIO_PROPERTIES_OPEN_V9_H +#include "iremote_broker.h" #include "../../common/napi/uni_header.h" namespace OHOS { @@ -26,7 +27,18 @@ public: static napi_value Async(napi_env env, napi_callback_info info); static napi_value Sync(napi_env env, napi_callback_info info); }; + +class FileIoToken : public IRemoteBroker { +public: + DECLARE_INTERFACE_DESCRIPTOR(u"ohos.fileio.open"); + + FileIoToken() = default; + virtual ~FileIoToken() noexcept = default; +}; + const std::string PROCEDURE_OPEN_NAME = "FileIOOpenV9"; +const std::string MEDIALIBRARY_DATA_URI = "datashare:///media"; +const std::string MEDIA_FILEMODE_READONLY = "r"; } // namespace ModuleFileIO } // namespace DistributedFS } // namespace OHOS diff --git a/interfaces/kits/native/remote_uri/remote_uri.cpp b/interfaces/kits/native/remote_uri/remote_uri.cpp index dddc68725..6e88a13b5 100644 --- a/interfaces/kits/native/remote_uri/remote_uri.cpp +++ b/interfaces/kits/native/remote_uri/remote_uri.cpp @@ -28,6 +28,57 @@ namespace ModuleRemoteUri { using namespace std; +bool RemoteUri::IsMediaUri(const string &path) +{ + string::size_type posDataShare = path.find(SCHEME_TAG); + if (posDataShare == string::npos) { + return false; + } + string scheme = path.substr(0, posDataShare); + if (scheme != SCHEME) { + return false; + } + + string::size_type pathSlashPos = path.find(PATH_SYMBOL); + if (pathSlashPos == string::npos) { + return false; + } + + string pathNoScheme = path.substr(pathSlashPos); + if (pathNoScheme.empty() || pathNoScheme.length() <= MEDIA.length()) { + return false; + } + + char s1 = pathNoScheme[0]; + char s2 = pathNoScheme[1]; + if (s1 != PATH_SYMBOL[0] || s2 != PATH_SYMBOL[0]) { + return false; + } + + string str = pathNoScheme.substr(2); + if (str.find(PATH_SYMBOL) == string::npos) { + return false; + } + + int position = str.find_first_of(PATH_SYMBOL); + int len = position + 1; + if (str.length() == len) { + return false; + } + + string s = str.substr(len); + if (s.empty() || s.length() < MEDIA.length()) { + return false; + } + + string media = str.substr(len, MEDIA.length()); + if (media != MEDIA) { + return false; + } + + return true; +} + static bool IsAllDigits(string fdStr) { for (size_t i = 0; i < fdStr.size(); i++) { diff --git a/interfaces/kits/native/remote_uri/remote_uri.h b/interfaces/kits/native/remote_uri/remote_uri.h index edbbc13aa..be6a535ea 100644 --- a/interfaces/kits/native/remote_uri/remote_uri.h +++ b/interfaces/kits/native/remote_uri/remote_uri.h @@ -32,6 +32,8 @@ const string REMOTE_URI_TAG = "fdFromBinder"; const string SCHEME_TAG = ":"; const string SCHEME = "datashare"; const string ZERO_FD = "0"; +const string MEDIA = "media"; +const string PATH_SYMBOL = "/"; const int MAX_URI_SIZE = 128; class RemoteUri { static setfdFromBinder; @@ -41,6 +43,7 @@ public: static bool IsRemoteUri(const string& path, int &fd, const int& flags = O_RDONLY); static int ConvertUri(const int &fd, string &remoteUri); static int OpenRemoteUri(const string &remoteUri); + static bool IsMediaUri(const string &path); ~RemoteUri() {} }; std::setRemoteUri::fdFromBinder; -- Gitee