1 /*
2 * Copyright (c) 2021 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 "trans_event.h"
17
18 #include "utils_log.h"
19
20 namespace OHOS {
21 namespace Storage {
22 namespace DistributedFile {
ToJsObject(napi_env env)23 napi_value TransEvent::ToJsObject(napi_env env)
24 {
25 napi_value te;
26 napi_status status = napi_create_object(env, &te);
27 if (napi_ok == status) {
28 napi_value err;
29 status = napi_create_int32(env, errorCode_, &err);
30 if (napi_ok == status) {
31 status = napi_set_named_property(env, te, "errorCode", err);
32 if (napi_ok != status) {
33 LOGE("ToJsObject: package error code failed.\n");
34 return nullptr;
35 }
36 } else {
37 LOGE("ToJsObject: create error code failed.\n");
38 return nullptr;
39 }
40
41 napi_value fname;
42 status = napi_create_string_utf8(env, fileName_.c_str(), fileName_.size()+1, &fname);
43 if (napi_ok == status) {
44 status = napi_set_named_property(env, te, "fileName", fname);
45 if (napi_ok != status) {
46 LOGE("ToJsObject: package filename failed.\n");
47 return nullptr;
48 }
49 } else {
50 LOGE("ToJsObject: create filename failed.\n");
51 return nullptr;
52 }
53
54 napi_value fcount;
55 status = napi_create_int32(env, fileCount_, &fcount);
56 if (napi_ok == status) {
57 status = napi_set_named_property(env, te, "fileCount", fcount);
58 if (napi_ok != status) {
59 LOGE("ToJsObject: package file counts failed.\n");
60 return nullptr;
61 }
62 } else {
63 LOGE("ToJsObject: create file counts failed.\n");
64 return nullptr;
65 }
66 } else {
67 LOGE("ToJsObject: create object failed.\n");
68 return nullptr;
69 }
70
71 return te;
72 }
73 } // namespace DistributedFile
74 } // namespace Storage
75 } // namespace OHOS