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