1 /*
2 * Copyright (C) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License"){return 0;}
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 "Media_Cloud_Controller"
17
18 #include "cloud_media_album_controller_service.h"
19
20 #include "media_log.h"
21 #include "media_column.h"
22 #include "cloud_media_data_controller_processor.h"
23 #include "media_req_vo.h"
24 #include "media_resp_vo.h"
25 #include "cloud_media_data_service.h"
26 #include "medialibrary_errno.h"
27 #include "cloud_mdkrecord_photo_album_vo.h"
28 #include "on_create_records_album_vo.h"
29 #include "on_delete_records_album_vo.h"
30 #include "on_delete_albums_vo.h"
31 #include "on_mdirty_records_album_vo.h"
32 #include "get_check_records_album_vo.h"
33 #include "failed_size_resp_vo.h"
34
35 namespace OHOS::Media::CloudSync {
OnFetchRecords(MessageParcel & data,MessageParcel & reply)36 int32_t CloudMediaAlbumControllerService::OnFetchRecords(MessageParcel &data, MessageParcel &reply)
37 {
38 MEDIA_INFO_LOG("OnFetchRecords enter");
39 OnFetchRecordsAlbumReqBody req;
40 OnFetchRecordsAlbumRespBody resp;
41 int32_t ret = IPC::UserDefineIPC().ReadRequestBody(data, req);
42 CHECK_AND_RETURN_RET_LOG(ret == E_OK, IPC::UserDefineIPC().WriteResponseBody(reply, resp, ret),
43 "OnFetchRecords Read Req Error");
44 if (req.albums.empty()) {
45 MEDIA_ERR_LOG("OnFetchRecords Param Error");
46 return IPC::UserDefineIPC().WriteResponseBody(reply, resp);
47 }
48 MEDIA_INFO_LOG("OnFetchRecords Request: %{public}s", req.ToString().c_str());
49 std::vector<PhotoAlbumDto> albumDtoList;
50 for (const auto &album : req.albums) {
51 PhotoAlbumDto albumDto;
52 albumDto.albumId = album.albumId;
53 albumDto.cloudId = album.cloudId;
54 albumDto.lPath = album.localPath;
55 albumDto.albumName = album.albumName;
56 albumDto.bundleName = album.albumBundleName;
57 albumDto.localLanguage = album.localLanguage;
58 albumDto.priority = album.priority;
59 albumDto.albumType = album.albumType;
60 albumDto.albumSubType = album.albumSubType;
61 albumDto.albumDateCreated = album.albumDateCreated;
62 albumDto.albumDateAdded = album.albumDateAdded;
63 albumDto.albumDateModified = album.albumDateModified;
64 albumDto.isDelete = album.isDelete;
65 albumDto.coverUriSource = album.coverUriSource;
66 albumDto.coverCloudId = album.coverCloudId;
67 albumDtoList.emplace_back(albumDto);
68 MEDIA_DEBUG_LOG("OnFetchRecords albumDto: %{public}s", albumDto.ToString().c_str());
69 MEDIA_DEBUG_LOG("OnFetchRecords album: %{public}s", album.ToString().c_str());
70 }
71 ret = this->albumService_.OnFetchRecords(albumDtoList, resp);
72 MEDIA_INFO_LOG("OnFetchRecords Resp: %{public}s", resp.ToString().c_str());
73 return IPC::UserDefineIPC().WriteResponseBody(reply, resp, ret);
74 }
75
OnDentryFileInsert(MessageParcel & data,MessageParcel & reply)76 int32_t CloudMediaAlbumControllerService::OnDentryFileInsert(MessageParcel &data, MessageParcel &reply)
77 {
78 int32_t ret = this->albumService_.OnDentryFileInsert();
79 return IPC::UserDefineIPC().WriteResponseBody(reply, ret);
80 }
81
GetCheckRecords(MessageParcel & data,MessageParcel & reply)82 int32_t CloudMediaAlbumControllerService::GetCheckRecords(MessageParcel &data, MessageParcel &reply)
83 {
84 GetCheckRecordsAlbumReqBody reqBody;
85 int32_t ret = IPC::UserDefineIPC().ReadRequestBody(data, reqBody);
86 if (ret != E_OK) {
87 return IPC::UserDefineIPC().WriteResponseBody(reply, ret);
88 }
89 GetCheckRecordsAlbumRespBody respBody;
90
91 std::vector<PhotoAlbumPo> albumsPoList = this->albumService_.GetCheckRecords(reqBody.cloudIds);
92 for (auto albumsPo : albumsPoList) {
93 CheckDataAlbum checkData;
94 checkData.cloudId = albumsPo.cloudId.value_or("");
95 respBody.checkDataAlbumList[checkData.cloudId] = checkData;
96 }
97 return IPC::UserDefineIPC().WriteResponseBody(reply, respBody);
98 }
99
GetCreatedRecords(MessageParcel & data,MessageParcel & reply)100 int32_t CloudMediaAlbumControllerService::GetCreatedRecords(MessageParcel &data, MessageParcel &reply)
101 {
102 CloudMdkRecordPhotoAlbumReqBody reqBody;
103 int32_t ret = IPC::UserDefineIPC().ReadRequestBody(data, reqBody);
104 if (ret != E_OK) {
105 MEDIA_ERR_LOG("ReadRequestBody failed, ret: %{public}d", ret);
106 return IPC::UserDefineIPC().WriteResponseBody(reply, ret);
107 }
108 int32_t size = reqBody.size;
109 if (size <= 0 || size > LIMIT_SIZE) {
110 MEDIA_ERR_LOG("GetCreatedRecords param error, size: %{public}d", size);
111 return IPC::UserDefineIPC().WriteResponseBody(reply, E_MEDIA_CLOUD_ARGS_INVAILD);
112 }
113 std::vector<PhotoAlbumPo> photoAlbumPoList = this->albumService_.GetAlbumCreatedRecords(reqBody.size);
114 std::vector<CloudMdkRecordPhotoAlbumVo> recordsList;
115 for (const auto &record : photoAlbumPoList) {
116 CloudMdkRecordPhotoAlbumVo recordVo = this->processor_.ConvertRecordPoToVo(record);
117 recordsList.push_back(recordVo);
118 }
119 CloudMdkRecordPhotoAlbumRespBody respBody{recordsList};
120 return IPC::UserDefineIPC().WriteResponseBody(reply, respBody);
121 }
122
GetMetaModifiedRecords(MessageParcel & data,MessageParcel & reply)123 int32_t CloudMediaAlbumControllerService::GetMetaModifiedRecords(MessageParcel &data, MessageParcel &reply)
124 {
125 CloudMdkRecordPhotoAlbumReqBody reqBody;
126 int32_t ret = IPC::UserDefineIPC().ReadRequestBody(data, reqBody);
127 if (ret != E_OK) {
128 return IPC::UserDefineIPC().WriteResponseBody(reply, ret);
129 }
130 int32_t size = reqBody.size;
131 if (size <= 0 || size > LIMIT_SIZE) {
132 MEDIA_ERR_LOG("GetMetaModifiedRecords param error, size: %{public}d", size);
133 return IPC::UserDefineIPC().WriteResponseBody(reply, E_MEDIA_CLOUD_ARGS_INVAILD);
134 }
135 std::vector<PhotoAlbumPo> photoAlbumPoList = this->albumService_.GetAlbumMetaModifiedRecords(reqBody.size);
136 std::vector<CloudMdkRecordPhotoAlbumVo> recordsList;
137 for (const auto &record : photoAlbumPoList) {
138 CloudMdkRecordPhotoAlbumVo recordVo = this->processor_.ConvertRecordPoToVo(record);
139 recordsList.push_back(recordVo);
140 }
141 MEDIA_INFO_LOG("CloudMediaAlbumControllerService::GetMetaModifiedRecords size: %{public}zu, %{public}zu",
142 photoAlbumPoList.size(),
143 recordsList.size());
144 CloudMdkRecordPhotoAlbumRespBody respBody{recordsList};
145 return IPC::UserDefineIPC().WriteResponseBody(reply, respBody);
146 }
147
GetDeletedRecords(MessageParcel & data,MessageParcel & reply)148 int32_t CloudMediaAlbumControllerService::GetDeletedRecords(MessageParcel &data, MessageParcel &reply)
149 {
150 CloudMdkRecordPhotoAlbumReqBody reqBody;
151 int32_t ret = IPC::UserDefineIPC().ReadRequestBody(data, reqBody);
152 if (ret != E_OK) {
153 MEDIA_ERR_LOG("get album deleted records error %{public}d", ret);
154 return IPC::UserDefineIPC().WriteResponseBody(reply, ret);
155 }
156 int32_t size = reqBody.size;
157 if (size <= 0 || size > LIMIT_SIZE) {
158 MEDIA_ERR_LOG("GetDeletedRecords param error, size: %{public}d", size);
159 return IPC::UserDefineIPC().WriteResponseBody(reply, E_MEDIA_CLOUD_ARGS_INVAILD);
160 }
161 std::vector<PhotoAlbumPo> photoAlbumPoList = this->albumService_.GetAlbumDeletedRecords(reqBody.size);
162 std::vector<CloudMdkRecordPhotoAlbumVo> recordsList;
163 for (const auto &record : photoAlbumPoList) {
164 CloudMdkRecordPhotoAlbumVo recordVo = this->processor_.ConvertRecordPoToVo(record);
165 recordsList.push_back(recordVo);
166 }
167 CloudMdkRecordPhotoAlbumRespBody respBody{recordsList};
168 return IPC::UserDefineIPC().WriteResponseBody(reply, respBody);
169 }
170
OnCreateRecords(MessageParcel & data,MessageParcel & reply)171 int32_t CloudMediaAlbumControllerService::OnCreateRecords(MessageParcel &data, MessageParcel &reply)
172 {
173 MEDIA_INFO_LOG("enter Album OnCreateRecords");
174 OnCreateRecordsAlbumReqBody reqBody;
175 int32_t ret = IPC::UserDefineIPC().ReadRequestBody(data, reqBody);
176 if (ret != E_OK) {
177 MEDIA_ERR_LOG("OnCreateRecords Album Read Req Error ret: %{public}d", ret);
178 return IPC::UserDefineIPC().WriteResponseBody(reply, ret);
179 }
180 MEDIA_INFO_LOG("OnCreateRecords %{public}zu", reqBody.albums.size());
181 std::vector<PhotoAlbumDto> albumDtoList;
182 for (const auto &album : reqBody.albums) {
183 PhotoAlbumDto albumDto = this->processor_.ConvertToPhotoAlbumDto(album);
184 MEDIA_DEBUG_LOG("OnCreateRecords record:%{public}s", albumDto.ToString().c_str());
185 albumDtoList.emplace_back(albumDto);
186 }
187 FailedSizeResp resp;
188 ret = this->albumService_.OnCreateRecords(albumDtoList, resp.failedSize);
189 MEDIA_INFO_LOG("OnCreateRecords Album ret: %{public}d, failedSize:%{public}d", ret, resp.failedSize);
190 return IPC::UserDefineIPC().WriteResponseBody(reply, resp, ret);
191 }
192
OnMdirtyRecords(MessageParcel & data,MessageParcel & reply)193 int32_t CloudMediaAlbumControllerService::OnMdirtyRecords(MessageParcel &data, MessageParcel &reply)
194 {
195 OnMdirtyRecordsAlbumReqBody reqBody;
196 OnMdirtyRecordsAlbumRespBody respBody;
197 respBody.failSize = 0;
198 int32_t ret = IPC::UserDefineIPC().ReadRequestBody(data, reqBody);
199 if (ret != E_OK) {
200 MEDIA_ERR_LOG("CloudMediaAlbumControllerService::OnMdirtyRecords Read Req Error");
201 return IPC::UserDefineIPC().WriteResponseBody(reply, ret);
202 }
203 std::vector<OnMdirtyAlbumRecord> records = reqBody.GetMdirtyRecords();
204 std::vector<PhotoAlbumDto> albumDtoList;
205 for (const auto &record : records) {
206 PhotoAlbumDto albumDto = this->processor_.ConvertToPhotoAlbumDto(record);
207 albumDtoList.emplace_back(albumDto);
208 MEDIA_DEBUG_LOG("OnMdirtyRecords OnModifyRecord: %{public}s", record.ToString().c_str());
209 }
210 ret = this->albumService_.OnMdirtyRecords(albumDtoList, respBody.failSize);
211 MEDIA_INFO_LOG("CloudMediaAlbumControllerService::OnMdirtyRecords end ret: %{public}d, failSize: %{public}d",
212 ret,
213 respBody.failSize);
214 return IPC::UserDefineIPC().WriteResponseBody(reply, respBody, ret);
215 }
216
OnFdirtyRecords(MessageParcel & data,MessageParcel & reply)217 int32_t CloudMediaAlbumControllerService::OnFdirtyRecords(MessageParcel &data, MessageParcel &reply)
218 {
219 int32_t ret = this->albumService_.OnFdirtyRecords();
220 return IPC::UserDefineIPC().WriteResponseBody(reply, ret);
221 }
222
OnDeleteRecords(MessageParcel & data,MessageParcel & reply)223 int32_t CloudMediaAlbumControllerService::OnDeleteRecords(MessageParcel &data, MessageParcel &reply)
224 {
225 OnDeleteRecordsAlbumReqBody reqBody;
226 OnDeleteRecordsAlbumRespBody respBody;
227 int32_t ret = IPC::UserDefineIPC().ReadRequestBody(data, reqBody);
228 if (ret != E_OK) {
229 MEDIA_ERR_LOG("CloudMediaAlbumControllerService::OnDeleteRecords Read Req Error");
230 return IPC::UserDefineIPC().WriteResponseBody(reply, ret);
231 }
232 std::vector<PhotoAlbumDto> albumDtoList;
233 for (const auto &album : reqBody.albums) {
234 PhotoAlbumDto albumDto;
235 albumDto.cloudId = album.cloudId;
236 albumDto.isSuccess = album.isSuccess;
237 albumDtoList.emplace_back(albumDto);
238 }
239 ret = this->albumService_.OnDeleteRecords(albumDtoList, respBody.failSize);
240 MEDIA_INFO_LOG("CloudMediaAlbumControllerService::OnDeleteRecords ret: %{public}d, failSize: %{public}d",
241 ret,
242 respBody.failSize);
243 return IPC::UserDefineIPC().WriteResponseBody(reply, respBody, ret);
244 }
245
OnCopyRecords(MessageParcel & data,MessageParcel & reply)246 int32_t CloudMediaAlbumControllerService::OnCopyRecords(MessageParcel &data, MessageParcel &reply)
247 {
248 int32_t ret = this->albumService_.OnCopyRecords();
249 return IPC::UserDefineIPC().WriteResponseBody(reply, ret);
250 }
251
OnStartSync(MessageParcel & data,MessageParcel & reply)252 int32_t CloudMediaAlbumControllerService::OnStartSync(MessageParcel &data, MessageParcel &reply)
253 {
254 int32_t ret = this->albumService_.OnStartSync();
255 return IPC::UserDefineIPC().WriteResponseBody(reply, ret);
256 }
257
OnCompleteSync(MessageParcel & data,MessageParcel & reply)258 int32_t CloudMediaAlbumControllerService::OnCompleteSync(MessageParcel &data, MessageParcel &reply)
259 {
260 int32_t ret = this->albumService_.OnCompleteSync();
261 return IPC::UserDefineIPC().WriteResponseBody(reply, ret);
262 }
263
OnCompletePull(MessageParcel & data,MessageParcel & reply)264 int32_t CloudMediaAlbumControllerService::OnCompletePull(MessageParcel &data, MessageParcel &reply)
265 {
266 int32_t ret = this->albumService_.OnCompletePull();
267 return IPC::UserDefineIPC().WriteResponseBody(reply, ret);
268 }
269
OnCompletePush(MessageParcel & data,MessageParcel & reply)270 int32_t CloudMediaAlbumControllerService::OnCompletePush(MessageParcel &data, MessageParcel &reply)
271 {
272 int32_t ret = this->albumService_.OnCompletePush();
273 return IPC::UserDefineIPC().WriteResponseBody(reply, ret);
274 }
275
OnCompleteCheck(MessageParcel & data,MessageParcel & reply)276 int32_t CloudMediaAlbumControllerService::OnCompleteCheck(MessageParcel &data, MessageParcel &reply)
277 {
278 int32_t ret = this->albumService_.OnCompleteCheck();
279 return IPC::UserDefineIPC().WriteResponseBody(reply, ret);
280 }
281 } // namespace OHOS::Media::CloudSync