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 #define LOG_TAG "ExtensionAssetLoaderImpl"
17 #include "asset_loader_impl.h"
18 #include "extension_util.h"
19 #include "log_print.h"
20
21 namespace OHOS::CloudData {
AssetLoaderImpl(OhCloudExtCloudAssetLoader * loader)22 AssetLoaderImpl::AssetLoaderImpl(OhCloudExtCloudAssetLoader *loader) : loader_(loader)
23 {
24 }
25
~AssetLoaderImpl()26 AssetLoaderImpl::~AssetLoaderImpl()
27 {
28 if (loader_ != nullptr) {
29 OhCloudExtCloudAssetLoaderFree(loader_);
30 loader_ = nullptr;
31 }
32 }
33
Download(const std::string & tableName,const std::string & gid,const DBValue & prefix,DBVBucket & assets)34 int32_t AssetLoaderImpl::Download(
35 const std::string &tableName, const std::string &gid, const DBValue &prefix, DBVBucket &assets)
36 {
37 std::string pref;
38 auto pre = std::get_if<std::string>(&prefix);
39 if (pre != nullptr) {
40 pref = *pre;
41 }
42 OhCloudExtUpDownloadInfo downInfo {
43 .tableName = reinterpret_cast<const unsigned char *>(tableName.c_str()),
44 .tableNameLen = tableName.size(),
45 .gid = reinterpret_cast<const unsigned char *>(gid.c_str()),
46 .gidLen = gid.size(),
47 .prefix = reinterpret_cast<const unsigned char *>(pref.c_str()),
48 .prefixLen = pref.size()
49 };
50 for (auto &[key, value] : assets) {
51 auto dbAssets = std::get_if<DBAssets>(&value);
52 if (dbAssets == nullptr) {
53 return DBErr::E_INVALID_ARGS;
54 }
55 auto data = ExtensionUtil::Convert(*dbAssets);
56 if (data.first == nullptr) {
57 return DBErr::E_ERROR;
58 }
59 auto status = OhCloudExtCloudAssetLoaderDownload(loader_, &downInfo, data.first);
60 if (status != ERRNO_SUCCESS) {
61 OhCloudExtVectorFree(data.first);
62 return ExtensionUtil::ConvertStatus(status);
63 }
64 value = ExtensionUtil::ConvertAssets(data.first);
65 OhCloudExtVectorFree(data.first);
66 }
67 return DBErr::E_OK;
68 }
69
RemoveLocalAssets(const std::string & tableName,const std::string & gid,const DBValue & prefix,DBVBucket & assets)70 int32_t AssetLoaderImpl::RemoveLocalAssets(
71 const std::string &tableName, const std::string &gid, const DBValue &prefix, DBVBucket &assets)
72 {
73 for (auto &[key, value] : assets) {
74 auto dbAssets = std::get_if<DBAssets>(&value);
75 if (dbAssets != nullptr) {
76 for (auto &dbAsset : *dbAssets) {
77 RemoveLocalAsset(dbAsset);
78 }
79 return DBErr::E_OK;
80 }
81 auto dbAsset = std::get_if<DBAsset>(&value);
82 if (dbAssets != nullptr) {
83 RemoveLocalAsset(*dbAsset);
84 return DBErr::E_OK;
85 }
86 return DBErr::E_INVALID_ARGS;
87 }
88 return DBErr::E_OK;
89 }
90
RemoveLocalAsset(const DBAsset & dbAsset)91 int32_t AssetLoaderImpl::RemoveLocalAsset(const DBAsset &dbAsset)
92 {
93 auto data = ExtensionUtil::Convert(dbAsset);
94 if (data.first == nullptr) {
95 return DBErr::E_ERROR;
96 }
97 auto status = OhCloudExtCloudAssetLoaderRemoveLocalAssets(data.first);
98 OhCloudExtCloudAssetFree(data.first);
99 if (status != ERRNO_SUCCESS) {
100 return DBErr::E_ERROR;
101 }
102 return DBErr::E_OK;
103 }
104 } // namespace OHOS::CloudData