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 "medialibrary_kvstore_utils.h"
17
18 #include <algorithm>
19
20 #include "medialibrary_errno.h"
21 #include "medialibrary_kvstore_manager.h"
22 #include "media_log.h"
23
24 namespace OHOS::Media {
CopyAstcDataToKvStoreByType(const KvStoreValueType & type,const std::string & oldKey,const std::string & newKey)25 int32_t MediaLibraryKvStoreUtils::CopyAstcDataToKvStoreByType(const KvStoreValueType &type, const std::string &oldKey,
26 const std::string &newKey)
27 {
28 std::shared_ptr<MediaLibraryKvStore> kvStore;
29 switch (type) {
30 case KvStoreValueType::MONTH_ASTC:
31 kvStore = MediaLibraryKvStoreManager::GetInstance()
32 .GetKvStore(KvStoreRoleType::OWNER, KvStoreValueType::MONTH_ASTC);
33 break;
34 case KvStoreValueType::YEAR_ASTC:
35 kvStore = MediaLibraryKvStoreManager::GetInstance()
36 .GetKvStore(KvStoreRoleType::OWNER, KvStoreValueType::YEAR_ASTC);
37 break;
38 default:
39 MEDIA_ERR_LOG("Invalid thumbnailType");
40 return E_ERR;
41 }
42
43 if (kvStore == nullptr) {
44 MEDIA_ERR_LOG("KvStore is nullptr");
45 return E_ERR;
46 }
47
48 std::vector<uint8_t> value;
49 int32_t ret = kvStore->Query(oldKey, value);
50 if (ret != E_OK) {
51 MEDIA_ERR_LOG("Query failed, type:%{public}d, field_id:%{public}s, ret:%{public}d", type, oldKey.c_str(), ret);
52 return E_ERR;
53 }
54
55 ret = kvStore->Insert(newKey, value);
56 if (ret != E_OK) {
57 MEDIA_ERR_LOG("Insert failed,type:%{public}d, field_id:%{public}s, ret:%{public}d", type, newKey.c_str(), ret);
58 return E_ERR;
59 }
60 MEDIA_INFO_LOG("Success to save astc data, type:%{public}d, oldKey:%{public}s, newKey:%{public}s", type,
61 oldKey.c_str(), newKey.c_str());
62 return ret;
63 }
64 } // namespace OHOS::Media