1 /*
2 * Copyright (C) 2024 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 "ParamParsing.h"
17
ExtractNodePath(NapiApi::Object params)18 BASE_NS::string ExtractNodePath(NapiApi::Object params)
19 {
20 const auto env = params.GetEnv();
21 if (auto path = NapiApi::Value<BASE_NS::string>{env, params.Get("path")}; path.IsDefined()) {
22 return path;
23 }
24 return ExtractName(params);
25 }
26
ExtractName(NapiApi::Object params)27 BASE_NS::string ExtractName(NapiApi::Object params)
28 {
29 return NapiApi::Value<BASE_NS::string>{params.GetEnv(), params.Get("name")};
30 }
31
ExtractUri(NapiApi::FunctionContext<> & ctx)32 BASE_NS::string ExtractUri(NapiApi::FunctionContext<>& ctx)
33 {
34 BASE_NS::string uri;
35 NapiApi::FunctionContext<BASE_NS::string> uriStr(ctx);
36 if (uriStr) {
37 // actually not supported anymore.
38 uri = uriStr.Arg<0>();
39 // check if there is a protocol
40 auto t = uri.find("://");
41 if (t == BASE_NS::string::npos) {
42 // no proto . so use default
43 uri.insert(0, "file://");
44 }
45 return uri;
46 }
47 // rawfile
48 NapiApi::FunctionContext<NapiApi::Object> rawFileCtx(ctx);
49 NapiApi::Object uriRawFile = rawFileCtx.Arg<0>();
50 uint32_t id = uriRawFile.Get<uint32_t>("id");
51 uint32_t resourceType = uriRawFile.Get<uint32_t>("type");
52 NapiApi::Array parms = uriRawFile.Get<NapiApi::Array>("params");
53
54 if ((id == 0) && (resourceType == 30000)) { // 30000: param
55 // seems like a correct rawfile.
56 uri = parms.Get<BASE_NS::string>(0);
57 }
58 if (!uri.empty()) {
59 // add the schema then
60 uri.insert(0, "OhosRawFile://");
61 }
62
63 return uri;
64 }
65
ExtractUri(NapiApi::Object resource)66 BASE_NS::string ExtractUri(NapiApi::Object resource)
67 {
68 uint32_t id = resource.Get<uint32_t>("id");
69 uint32_t resourceType = resource.Get<uint32_t>("type");
70 NapiApi::Array parms = resource.Get<NapiApi::Array>("params");
71 BASE_NS::string uri;
72 if ((id == 0) && (resourceType == 30000)) { // 30000: param
73 // seems like a correct rawfile.
74 uri = parms.Get<BASE_NS::string>(0);
75 }
76 if (!uri.empty()) {
77 // add the schema then
78 uri.insert(0, "OhosRawFile://");
79 return uri;
80 }
81 auto e = resource.GetEnv();
82 auto v = resource.ToNapiValue();
83 napi_valuetype type;
84 napi_typeof(e, v, &type);
85 if (type == napi_string) {
86 uri = NapiApi::Value<BASE_NS::string>(e, v);
87 // check if there is a protocol
88 auto t = uri.find("://");
89 if (t == BASE_NS::string::npos) {
90 // no proto . so use default
91 // set system file as default format
92 uri.insert(0, "file://");
93 }
94 }
95 return uri;
96 }
97
ExtractUri(BASE_NS::string uri)98 BASE_NS::string ExtractUri(BASE_NS::string uri)
99 {
100 auto t = uri.find("://");
101 if (t == BASE_NS::string::npos) {
102 // no proto . so use default
103 // set system file as default format
104 uri.insert(0, "file://");
105 }
106 return uri;
107 }