1 /*
2  * Copyright (c) 2022 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 #include "get_uri_from_path.h"
16 
17 #include "status_receiver_host.h"
18 
19 #include "common_func.h"
20 #include "log.h"
21 
22 namespace OHOS {
23 namespace AppFileService {
24 namespace ModuleFileUri {
25 using namespace OHOS::FileManagement::LibN;
26 
Sync(napi_env env,napi_callback_info info)27 napi_value GetUriFromPath::Sync(napi_env env, napi_callback_info info)
28 {
29     NFuncArg funcArg(env, info);
30     if (!funcArg.InitArgs(NARG_CNT::ONE)) {
31         LOGE("GetUriFromPath::Sync Number of arguments unmatched");
32         NError(E_PARAMS).ThrowErr(env);
33         return nullptr;
34     }
35     auto [succPath, path, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String();
36     if (!succPath) {
37         LOGE("GetUriFromPath::Sync get path parameter failed!");
38         NError(E_PARAMS).ThrowErr(env);
39         return nullptr;
40     }
41 
42     std::string uri = CommonFunc::GetUriFromPath(path.get());
43     if (uri == "") {
44         LOGE("GetUriFromPath failed!");
45         NError(E_PARAMS).ThrowErr(env);
46         return nullptr;
47     }
48 
49     return NVal::CreateUTF8String(env, uri).val_;
50 }
51 
52 } // namespace ModuleFileUri
53 } // namespace AppFileService
54 } // namespace OHOS