1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "datashare_adapter_impl.h"
17 
18 #include <string>
19 #include <fcntl.h>
20 #include <sys/ioctl.h>
21 #include <sys/stat.h>
22 #include <sys/types.h>
23 #include <unistd.h>
24 #include <climits>
25 
26 #include "nweb_log.h"
27 #include "foundation/ability/ability_base/interfaces/kits/native/uri/include/uri.h"
28 #include "foundation/filemanagement/app_file_service/interfaces/innerkits/native/file_uri/include/file_uri.h"
29 #include "foundation/distributeddatamgr/data_share/interfaces/inner_api/consumer/include/datashare_helper.h"
30 #include "foundation/systemabilitymgr/samgr/interfaces/innerkits/samgr_proxy/include/system_ability_definition.h"
31 #include "foundation/systemabilitymgr/samgr/interfaces/innerkits/samgr_proxy/include/iservice_registry.h"
32 
33 namespace OHOS::NWeb {
34 
35 constexpr char MEDIALIBRARY_DATA_URI[] = "datashare:///media";
36 // static
GetInstance()37 DatashareAdapterImpl& DatashareAdapterImpl::GetInstance()
38 {
39     static DatashareAdapterImpl instance;
40     return instance;
41 }
42 
GetRealPath(const std::string & uriStr)43 std::string DatashareAdapterImpl::GetRealPath(const std::string& uriStr)
44 {
45     Uri uri = Uri(uriStr);
46     AppFileService::ModuleFileUri::FileUri fileUri(uriStr);
47     return fileUri.GetRealPath();
48 }
49 
OpenDataShareUriForRead(const std::string & uriStr)50 int DatashareAdapterImpl::OpenDataShareUriForRead(const std::string& uriStr)
51 {
52     auto sam = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
53     if (sam == nullptr) {
54         WVLOG_E("open datashare uri read, system ability manager is null");
55         return -1;
56     }
57     auto remoteObj = sam->GetSystemAbility(OHOS::STORAGE_MANAGER_MANAGER_ID);
58     if (remoteObj == nullptr) {
59         WVLOG_E("open datashare uri read, remoteObj is null");
60         return -1;
61     }
62     auto dataShareHelper = DataShare::DataShareHelper::Creator(remoteObj, MEDIALIBRARY_DATA_URI);
63     if (dataShareHelper == nullptr) {
64         WVLOG_E("open datashare uri read, dataShareHelper is null");
65         return -1;
66     }
67     Uri uri = Uri(uriStr);
68     return dataShareHelper->OpenFile(uri, "r");
69 }
70 
GetFileDisplayName(const std::string & uriStr)71 std::string DatashareAdapterImpl::GetFileDisplayName(const std::string& uriStr)
72 {
73     AppFileService::ModuleFileUri::FileUri fileUri(uriStr);
74     return fileUri.GetName();
75 }
76 } // namespace OHOS::NWeb
77