• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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 #define MLOG_TAG "PhotoMimetypeOperation"
16 
17 #include "photo_mimetype_operation.h"
18 
19 #include "abs_rdb_predicates.h"
20 #include "media_column.h"
21 #include "media_file_utils.h"
22 #include "media_log.h"
23 #include "medialibrary_subscriber.h"
24 #include "medialibrary_unistore_manager.h"
25 #include "result_set_utils.h"
26 #include "values_bucket.h"
27 
28 using namespace OHOS::NativeRdb;
29 
30 namespace OHOS::Media {
31 const int32_t BATCH_QUERY_NUMBER = 1000;
32 const std::string PREFIX_IMAGE = "image/";
33 const std::string COLUMN_COUNT = "count(*)";
34 
GetCountOfAllAssets(int32_t & assetsCount)35 static int32_t GetCountOfAllAssets(int32_t &assetsCount)
36 {
37     auto rdbStore = MediaLibraryUnistoreManager::GetInstance().GetRdbStore();
38     CHECK_AND_RETURN_RET_LOG(rdbStore != nullptr, E_ERR, "GetCountOfAllAssets failed. rdbStore is null.");
39     std::vector<std::string> columns = { COLUMN_COUNT };
40     AbsRdbPredicates predicates = AbsRdbPredicates(PhotoColumn::PHOTOS_TABLE);
41     auto resultSet = rdbStore->Query(predicates, columns);
42     CHECK_AND_RETURN_RET_LOG(resultSet != nullptr, E_ERR, "GetCountOfAllAssets failed. rdbStore is null.");
43 
44     while (resultSet->GoToNextRow() == NativeRdb::E_OK) {
45         assetsCount = GetInt32Val(COLUMN_COUNT, resultSet);
46         return E_OK;
47     }
48     return E_ERR;
49 }
50 
UpdateInvalidMimeType()51 int32_t PhotoMimetypeOperation::UpdateInvalidMimeType()
52 {
53     MEDIA_INFO_LOG("enter UpdateInvalidMimeType.");
54     auto rdbStore = MediaLibraryUnistoreManager::GetInstance().GetRdbStore();
55     CHECK_AND_RETURN_RET_LOG(rdbStore != nullptr, E_ERR, "HasDataForUpdate failed. rdbStore is null.");
56     int32_t assetsCount = 0;
57     CHECK_AND_RETURN_RET_LOG(GetCountOfAllAssets(assetsCount) == E_OK, E_ERR, "Failed to GetCountOfAllAssets.");
58 
59     AbsRdbPredicates predicates = AbsRdbPredicates(PhotoColumn::PHOTOS_TABLE);
60     predicates.EqualTo(MediaColumn::MEDIA_TYPE, to_string(MEDIA_TYPE_IMAGE));
61     predicates.NotLike(MediaColumn::MEDIA_MIME_TYPE, "image/%");
62     const std::vector<std::string> columns = { MediaColumn::MEDIA_ID, MediaColumn::MEDIA_NAME };
63 
64     int32_t startCount = 0;
65     while (MedialibrarySubscriber::IsCurrentStatusOn() && startCount < assetsCount) {
66         predicates.Limit(startCount, BATCH_QUERY_NUMBER);
67         auto resultSet = rdbStore->Query(predicates, columns);
68         CHECK_AND_RETURN_RET_LOG(resultSet != nullptr, E_ERR, "UpdateMimeType failed. resultSet is null.");
69 
70         std::unordered_map<std::string, std::vector<std::string>> invalidMimeTypeMap;
71         while (resultSet->GoToNextRow() == NativeRdb::E_OK) {
72             std::string fileId = GetStringVal(MediaColumn::MEDIA_ID, resultSet);
73             std::string displayName = GetStringVal(MediaColumn::MEDIA_NAME, resultSet);
74             std::string mimeType = MediaFileUtils::GetMimeTypeFromDisplayName(displayName);
75             if (fileId.empty() || mimeType.empty() || !MediaFileUtils::StartsWith(mimeType, PREFIX_IMAGE)) {
76                 MEDIA_WARN_LOG("Invalid, fileId: %{public}s, mimeType: %{public}s.", fileId.c_str(), mimeType.c_str());
77                 continue;
78             }
79             invalidMimeTypeMap[mimeType].emplace_back(fileId);
80         }
81         resultSet->Close();
82         if (!invalidMimeTypeMap.empty()) {
83             CHECK_AND_RETURN_RET_LOG(HandleUpdateInvalidMimeType(rdbStore, invalidMimeTypeMap) == E_OK, E_ERR,
84                 "Failed to HandleUpdateInvalidMimeType.");
85         }
86         startCount += BATCH_QUERY_NUMBER;
87     }
88     MEDIA_INFO_LOG("end UpdateInvalidMimeType.");
89     return E_OK;
90 }
91 
HandleUpdateInvalidMimeType(const std::shared_ptr<MediaLibraryRdbStore> rdbStore,const std::unordered_map<std::string,std::vector<std::string>> & invalidMimeTypeMap)92 int32_t PhotoMimetypeOperation::HandleUpdateInvalidMimeType(const std::shared_ptr<MediaLibraryRdbStore> rdbStore,
93     const std::unordered_map<std::string, std::vector<std::string>> &invalidMimeTypeMap)
94 {
95     CHECK_AND_RETURN_RET_LOG(rdbStore != nullptr, E_ERR, "rdbStore is nullptr.");
96     MEDIA_INFO_LOG("start to update invalid mimeType.");
97     for (const auto &invalidPair : invalidMimeTypeMap) {
98         ValuesBucket values;
99         values.Put(MediaColumn::MEDIA_MIME_TYPE, invalidPair.first);
100         AbsRdbPredicates predicates = AbsRdbPredicates(PhotoColumn::PHOTOS_TABLE);
101         predicates.In(MediaColumn::MEDIA_ID, invalidPair.second);
102         int32_t totalCount = static_cast<int32_t>(invalidPair.second.size());
103         int32_t changedRows = -1;
104         int32_t ret = rdbStore->Update(changedRows, values, predicates);
105         if (ret != E_OK || changedRows != totalCount) {
106             MEDIA_ERR_LOG("update failed, mimeType: %{public}s, totalCount: %{public}d, changedRows: %{public}d.",
107                 invalidPair.first.c_str(), totalCount, changedRows);
108             continue;
109         }
110         MEDIA_INFO_LOG("update successfully, mimeType: %{public}s, totalCount: %{public}d, changedRows: %{public}d.",
111             invalidPair.first.c_str(), totalCount, changedRows);
112     }
113     MEDIA_INFO_LOG("end to update invalid mimeType.");
114     return E_OK;
115 }
116 }  // namespace OHOS::Media