• 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     predicates.EqualTo(MediaColumn::MEDIA_TYPE, to_string(MEDIA_TYPE_IMAGE));
42     predicates.NotLike(MediaColumn::MEDIA_MIME_TYPE, "image/%");
43     auto resultSet = rdbStore->Query(predicates, columns);
44     CHECK_AND_RETURN_RET_LOG(resultSet != nullptr, E_ERR, "GetCountOfAllAssets failed. rdbStore is null.");
45 
46     while (resultSet->GoToNextRow() == NativeRdb::E_OK) {
47         assetsCount = GetInt32Val(COLUMN_COUNT, resultSet);
48         return E_OK;
49     }
50     return E_ERR;
51 }
52 
UpdateInvalidMimeType()53 int32_t PhotoMimetypeOperation::UpdateInvalidMimeType()
54 {
55     MEDIA_INFO_LOG("enter UpdateInvalidMimeType.");
56     auto rdbStore = MediaLibraryUnistoreManager::GetInstance().GetRdbStore();
57     CHECK_AND_RETURN_RET_LOG(rdbStore != nullptr, E_ERR, "HasDataForUpdate failed. rdbStore is null.");
58     int32_t assetsCount = 0;
59     CHECK_AND_RETURN_RET_LOG(GetCountOfAllAssets(assetsCount) == E_OK, E_ERR, "Failed to GetCountOfAllAssets.");
60     CHECK_AND_RETURN_RET_INFO_LOG(assetsCount > 0, E_OK, "no Invalid mimeType.");
61     AbsRdbPredicates predicates = AbsRdbPredicates(PhotoColumn::PHOTOS_TABLE);
62     predicates.EqualTo(MediaColumn::MEDIA_TYPE, to_string(MEDIA_TYPE_IMAGE));
63     predicates.NotLike(MediaColumn::MEDIA_MIME_TYPE, "image/%");
64     const std::vector<std::string> columns = { MediaColumn::MEDIA_ID, MediaColumn::MEDIA_NAME };
65 
66     int32_t startCount = 0;
67     while (MedialibrarySubscriber::IsCurrentStatusOn() && startCount < assetsCount) {
68         predicates.Limit(BATCH_QUERY_NUMBER);
69         auto resultSet = rdbStore->Query(predicates, columns);
70         CHECK_AND_RETURN_RET_LOG(resultSet != nullptr, E_ERR, "UpdateMimeType failed. resultSet is null.");
71 
72         std::unordered_map<std::string, std::vector<std::string>> invalidMimeTypeMap;
73         while (resultSet->GoToNextRow() == NativeRdb::E_OK) {
74             std::string fileId = GetStringVal(MediaColumn::MEDIA_ID, resultSet);
75             std::string displayName = GetStringVal(MediaColumn::MEDIA_NAME, resultSet);
76             std::string mimeType = MediaFileUtils::GetMimeTypeFromDisplayName(displayName);
77             if (fileId.empty() || mimeType.empty() || !MediaFileUtils::StartsWith(mimeType, PREFIX_IMAGE)) {
78                 MEDIA_WARN_LOG("Invalid, fileId: %{public}s, mimeType: %{public}s.", fileId.c_str(), mimeType.c_str());
79                 continue;
80             }
81             invalidMimeTypeMap[mimeType].emplace_back(fileId);
82         }
83         resultSet->Close();
84         if (!invalidMimeTypeMap.empty()) {
85             CHECK_AND_RETURN_RET_LOG(HandleUpdateInvalidMimeType(rdbStore, invalidMimeTypeMap) == E_OK, E_ERR,
86                 "Failed to HandleUpdateInvalidMimeType.");
87         }
88         startCount += BATCH_QUERY_NUMBER;
89     }
90     MEDIA_INFO_LOG("end UpdateInvalidMimeType.");
91     return E_OK;
92 }
93 
HandleUpdateInvalidMimeType(const std::shared_ptr<MediaLibraryRdbStore> rdbStore,const std::unordered_map<std::string,std::vector<std::string>> & invalidMimeTypeMap)94 int32_t PhotoMimetypeOperation::HandleUpdateInvalidMimeType(const std::shared_ptr<MediaLibraryRdbStore> rdbStore,
95     const std::unordered_map<std::string, std::vector<std::string>> &invalidMimeTypeMap)
96 {
97     CHECK_AND_RETURN_RET_LOG(rdbStore != nullptr, E_ERR, "rdbStore is nullptr.");
98     MEDIA_INFO_LOG("start to update invalid mimeType.");
99     for (const auto &invalidPair : invalidMimeTypeMap) {
100         ValuesBucket values;
101         values.Put(MediaColumn::MEDIA_MIME_TYPE, invalidPair.first);
102         AbsRdbPredicates predicates = AbsRdbPredicates(PhotoColumn::PHOTOS_TABLE);
103         predicates.In(MediaColumn::MEDIA_ID, invalidPair.second);
104         int32_t totalCount = static_cast<int32_t>(invalidPair.second.size());
105         int32_t changedRows = -1;
106         int32_t ret = rdbStore->Update(changedRows, values, predicates);
107         if (ret != E_OK || changedRows != totalCount) {
108             MEDIA_ERR_LOG("update failed, mimeType: %{public}s, totalCount: %{public}d, changedRows: %{public}d.",
109                 invalidPair.first.c_str(), totalCount, changedRows);
110             continue;
111         }
112         MEDIA_INFO_LOG("update successfully, mimeType: %{public}s, totalCount: %{public}d, changedRows: %{public}d.",
113             invalidPair.first.c_str(), totalCount, changedRows);
114     }
115     MEDIA_INFO_LOG("end to update invalid mimeType.");
116     return E_OK;
117 }
118 }  // namespace OHOS::Media