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 #define MLOG_TAG "CloudAlbum"
16
17 #include "cloud_album_handler.h"
18
19 #include "rdb_predicates.h"
20 #include "media_file_utils.h"
21 #include "medialibrary_album_operations.h"
22 #include "medialibrary_notify.h"
23 #include "medialibrary_rdb_utils.h"
24 #include "medialibrary_unistore_manager.h"
25
26 #include "photo_album_column.h"
27 #include "medialibrary_rdb_utils.h"
28 #include "medialibrary_photo_operations.h"
29 #include "result_set_utils.h"
30 #include "medialibrary_notify.h"
31 #include "rdb_predicates.h"
32 #include "media_file_utils.h"
33
34
35 using namespace std;
36 namespace OHOS {
37 namespace Media {
38
39 using ChangeType = DataShare::DataShareObserver::ChangeType;
40
GetIds(const CloudSyncHandleData & handleData)41 static vector<string> GetIds(const CloudSyncHandleData &handleData)
42 {
43 vector<string> fileIds;
44 for (auto &uri : handleData.orgInfo.uris) {
45 string uriString = uri.ToString();
46 auto index = uriString.rfind('/');
47 if (index == string::npos) {
48 continue;
49 }
50 auto fileIdStr = uriString.substr(index + 1);
51 fileIds.push_back(fileIdStr);
52 }
53 return fileIds;
54 }
55
UpdateCloudAlbum(const string & id,int32_t count)56 static void UpdateCloudAlbum(const string &id, int32_t count)
57 {
58 auto rdbStore = MediaLibraryUnistoreManager::GetInstance().GetRdbStore();
59 if (rdbStore == nullptr) {
60 MEDIA_ERR_LOG("Can not get rdbstore");
61 return;
62 }
63 int changeRows = 0;
64 NativeRdb::ValuesBucket valuesNew;
65 valuesNew.PutInt(PhotoAlbumColumns::ALBUM_DIRTY, static_cast<int32_t>(DirtyTypes::TYPE_NEW));
66 valuesNew.PutInt(PhotoAlbumColumns::ALBUM_COUNT, count);
67
68 NativeRdb::RdbPredicates rdbPredicates(PhotoAlbumColumns::TABLE);
69 rdbPredicates.EqualTo(PhotoAlbumColumns::ALBUM_ID, id);
70 rdbStore->Update(changeRows, valuesNew, rdbPredicates);
71 if (changeRows < 0) {
72 MEDIA_ERR_LOG("Failed to update cloudAlbum , ret = %{public}d", changeRows);
73 }
74 }
75
GetCloudAlbumCount(const string & id)76 static int32_t GetCloudAlbumCount(const string &id)
77 {
78 const std::vector<std::string> columnInfo = {"count(*) AS count"};
79
80 NativeRdb::RdbPredicates predicates(PhotoColumn::PHOTOS_TABLE);
81 predicates.EqualTo(PhotoColumn::PHOTO_OWNER_ALBUM_ID, id);
82 predicates.And()->EqualTo(PhotoColumn::MEDIA_HIDDEN, 0);
83 predicates.And()->EqualTo(PhotoColumn::MEDIA_DATE_TRASHED, 0);
84 predicates.And()->EqualTo(PhotoColumn::PHOTO_CLEAN_FLAG,
85 to_string(static_cast<int32_t>(CleanType::TYPE_NOT_CLEAN)));
86
87 auto resultSet = MediaLibraryRdbStore::Query(predicates, columnInfo);
88 if (resultSet == nullptr || resultSet->GoToFirstRow() != E_OK) {
89 MEDIA_ERR_LOG("GetCloudAlbumCount error: %{public}d", errno);
90 return E_HAS_DB_ERROR;
91 }
92 return GetInt32Val("count", resultSet);
93 }
94
UpdateSourcePath(const shared_ptr<MediaLibraryRdbStore> rdbStore,NativeRdb::RdbPredicates & predicates)95 static void UpdateSourcePath(const shared_ptr<MediaLibraryRdbStore> rdbStore,
96 NativeRdb::RdbPredicates &predicates)
97 {
98 for (auto albumId: predicates.GetWhereArgs()) {
99 const std::string QUERY_FILE_ASSET_INFO = "SELECT file_id FROM Photos WHERE owner_album_id = " + albumId +
100 " AND clean_flag =0 AND hidden =0";
101 shared_ptr<NativeRdb::ResultSet> resultSet = rdbStore->QuerySql(QUERY_FILE_ASSET_INFO);
102 if (resultSet == nullptr) {
103 MEDIA_ERR_LOG("UpdateSourcePath fail error: %{public}d", errno);
104 return;
105 }
106 vector<string> fileAssetsIds, fileAssetsUri;
107 while (resultSet->GoToNextRow() == NativeRdb::E_OK) {
108 int32_t fileId = GetInt32Val(MediaColumn::MEDIA_ID, resultSet);
109 fileAssetsIds.push_back(to_string(fileId));
110 }
111 MediaLibraryPhotoOperations::UpdateSourcePath(fileAssetsIds);
112 }
113 }
114
DeletePhotoAlbum(NativeRdb::RdbPredicates & predicates)115 static int32_t DeletePhotoAlbum(NativeRdb::RdbPredicates &predicates)
116 {
117 constexpr int32_t AFTER_AGR_SIZE = 2;
118 auto rdbStore = MediaLibraryUnistoreManager::GetInstance().GetRdbStore();
119 if (rdbStore == nullptr) {
120 MEDIA_ERR_LOG("DeletePhotoAlbum failed. rdbStore is null");
121 return E_HAS_DB_ERROR;
122 }
123 UpdateSourcePath(rdbStore, predicates);
124 predicates.And()->BeginWrap();
125 predicates.BeginWrap()->EqualTo(PhotoAlbumColumns::ALBUM_TYPE, to_string(PhotoAlbumType::USER));
126 predicates.EqualTo(PhotoAlbumColumns::ALBUM_SUBTYPE, to_string(PhotoAlbumSubType::USER_GENERIC));
127 predicates.EndWrap();
128 predicates.Or()->EqualTo(PhotoAlbumColumns::ALBUM_TYPE, to_string(PhotoAlbumType::SOURCE));
129 predicates.EndWrap();
130 int deleteRow = -1;
131 auto ret = rdbStore->Delete(deleteRow, predicates);
132 if (ret != NativeRdb::E_OK || deleteRow <= 0) {
133 MEDIA_ERR_LOG("DeletePhotoAlbum failed, errCode = %{public}d, deleteRow = %{public}d", ret, deleteRow);
134 }
135 auto watch = MediaLibraryNotify::GetInstance();
136 const vector<string> ¬ifyUris = predicates.GetWhereArgs();
137 size_t count = notifyUris.size() - AFTER_AGR_SIZE;
138 for (size_t i = 0; i < count; i++) {
139 if (deleteRow > 0) {
140 watch->Notify(MediaFileUtils::GetUriByExtrConditions(PhotoAlbumColumns::ALBUM_URI_PREFIX,
141 notifyUris[i]), NotifyType::NOTIFY_REMOVE);
142 }
143 }
144 return deleteRow;
145 }
146
DeleteOrUpdateCloudAlbums(const vector<string> & ids)147 void DeleteOrUpdateCloudAlbums(const vector<string> &ids)
148 {
149 for (const auto &id : ids) {
150 auto count = GetCloudAlbumCount(id);
151 if (count == 0) {
152 NativeRdb::RdbPredicates rdbPredicate(PhotoAlbumColumns::TABLE);
153 rdbPredicate.EqualTo(PhotoAlbumColumns::ALBUM_ID, id);
154 if (MediaLibraryAlbumOperations::DeletePhotoAlbum(rdbPredicate) > 0) {
155 MEDIA_INFO_LOG("delete Album {%{public}s} succ", id.c_str());
156 } else {
157 MEDIA_INFO_LOG("delete Album {%{public}s} fail", id.c_str());
158 }
159 } else {
160 MEDIA_INFO_LOG("Album {%{public}s} not empty, count %{public}d", id.c_str(), count);
161 UpdateCloudAlbum(id, count);
162 }
163 }
164 }
165
Handle(const CloudSyncHandleData & handleData)166 void CloudAlbumHandler::Handle(const CloudSyncHandleData &handleData)
167 {
168 if (handleData.orgInfo.type == ChangeType::DELETE) {
169 vector<string> fileIds;
170 fileIds = GetIds(handleData);
171 DeleteOrUpdateCloudAlbums(fileIds);
172 }
173 if (nextHandler_ != nullptr) {
174 nextHandler_->Handle(handleData);
175 }
176 }
177
178 } //namespace Media
179 } //namespace OHOS
180