1 /*
2 * Copyright (c) 2022-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 #include "file_uri_n_exporter.h"
16
17 #include "file_uri_entity.h"
18 #include "file_utils.h"
19 #include "log.h"
20
21 namespace OHOS {
22 namespace AppFileService {
23 namespace ModuleFileUri {
24 using namespace std;
25 using namespace FileManagement;
26 using namespace FileManagement::LibN;
27
Constructor(napi_env env,napi_callback_info info)28 napi_value FileUriNExporter::Constructor(napi_env env, napi_callback_info info)
29 {
30 NFuncArg funcArg(env, info);
31 if (!funcArg.InitArgs(NARG_CNT::ONE)) {
32 LOGE("Number of arguments unmatched");
33 NError(EINVAL).ThrowErr(env);
34 return nullptr;
35 }
36 auto [succPath, path, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String();
37 if (!succPath) {
38 LOGE("Failed to get path");
39 NError(EINVAL).ThrowErr(env);
40 return nullptr;
41 }
42 auto fileuriEntity = CreateUniquePtr<FileUriEntity>(string(path.get()));
43 if (fileuriEntity == nullptr) {
44 LOGE("Failed to request heap memory.");
45 NError(ENOMEM).ThrowErr(env);
46 return nullptr;
47 }
48 if (!NClass::SetEntityFor<FileUriEntity>(env, funcArg.GetThisVar(), move(fileuriEntity))) {
49 LOGE("Failed to set file entity");
50 NError(EIO).ThrowErr(env);
51 return nullptr;
52 }
53 return funcArg.GetThisVar();
54 }
55
56
UriToString(napi_env env,napi_callback_info info)57 napi_value FileUriNExporter::UriToString(napi_env env, napi_callback_info info)
58 {
59 NFuncArg funcArg(env, info);
60 if (!funcArg.InitArgs(NARG_CNT::ZERO)) {
61 LOGE("Number of arguments unmatched");
62 NError(EINVAL).ThrowErr(env);
63 return nullptr;
64 }
65 auto fileuriEntity = NClass::GetEntityOf<FileUriEntity>(env, funcArg.GetThisVar());
66 if (!fileuriEntity) {
67 LOGE("Failed to get file entity");
68 NError(EINVAL).ThrowErr(env);
69 return nullptr;
70 }
71 return NVal::CreateUTF8String(env, fileuriEntity->fileUri_.ToString()).val_;
72 }
73
GetFileUriName(napi_env env,napi_callback_info info)74 napi_value FileUriNExporter::GetFileUriName(napi_env env, napi_callback_info info)
75 {
76 NFuncArg funcArg(env, info);
77 if (!funcArg.InitArgs(NARG_CNT::ZERO)) {
78 LOGE("Number of arguments unmatched");
79 NError(EINVAL).ThrowErr(env);
80 return nullptr;
81 }
82 auto fileuriEntity = NClass::GetEntityOf<FileUriEntity>(env, funcArg.GetThisVar());
83 if (!fileuriEntity) {
84 LOGE("Failed to get file entity");
85 NError(EINVAL).ThrowErr(env);
86 return nullptr;
87 }
88 return NVal::CreateUTF8String(env, fileuriEntity->fileUri_.GetName()).val_;
89 }
90
GetFileUriPath(napi_env env,napi_callback_info info)91 napi_value FileUriNExporter::GetFileUriPath(napi_env env, napi_callback_info info)
92 {
93 NFuncArg funcArg(env, info);
94 if (!funcArg.InitArgs(NARG_CNT::ZERO)) {
95 LOGE("Number of arguments unmatched");
96 NError(EINVAL).ThrowErr(env);
97 return nullptr;
98 }
99 auto fileuriEntity = NClass::GetEntityOf<FileUriEntity>(env, funcArg.GetThisVar());
100 if (!fileuriEntity) {
101 LOGE("Failed to get file entity");
102 NError(EINVAL).ThrowErr(env);
103 return nullptr;
104 }
105 return NVal::CreateUTF8String(env, fileuriEntity->fileUri_.GetPath()).val_;
106 }
107
Export()108 bool FileUriNExporter::Export()
109 {
110 vector<napi_property_descriptor> props = {
111 NVal::DeclareNapiFunction("toString", UriToString),
112 NVal::DeclareNapiGetter("name", GetFileUriName),
113 NVal::DeclareNapiGetter("path", GetFileUriPath),
114 };
115
116 auto [succ, classValue] = NClass::DefineClass(exports_.env_, className, Constructor, std::move(props));
117 if (!succ) {
118 LOGE("Failed to define class");
119 NError(EIO).ThrowErr(exports_.env_);
120 return false;
121 }
122 succ = NClass::SaveClass(exports_.env_, className, classValue);
123 if (!succ) {
124 LOGE("Failed to save class");
125 NError(EIO).ThrowErr(exports_.env_);
126 return false;
127 }
128
129 return exports_.AddProp(className, classValue);
130 }
131
GetClassName()132 string FileUriNExporter::GetClassName()
133 {
134 return FileUriNExporter::className;
135 }
136
FileUriNExporter(napi_env env,napi_value exports)137 FileUriNExporter::FileUriNExporter(napi_env env, napi_value exports) : NExporter(env, exports) {}
138
~FileUriNExporter()139 FileUriNExporter::~FileUriNExporter() {}
140 } // namespace ModuleFileUri
141 } // namespace AppFileService
142 } // namespace OHOS
143