• 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"){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_download_controller_service.h"
19 
20 #include "media_log.h"
21 #include "media_column.h"
22 #include "user_define_ipc.h"
23 #include "media_req_vo.h"
24 #include "media_resp_vo.h"
25 #include "medialibrary_errno.h"
26 #include "get_download_thm_vo.h"
27 #include "get_download_thm_num_vo.h"
28 #include "get_download_thm_by_uri_vo.h"
29 #include "on_download_thms_vo.h"
30 #include "media_operate_result_vo.h"
31 #include "cloud_media_uri_utils.h"
32 #include "get_download_asset_vo.h"
33 #include "on_download_asset_vo.h"
34 
35 namespace OHOS::Media::CloudSync {
GetDownloadThms(MessageParcel & data,MessageParcel & reply)36 int32_t CloudMediaDownloadControllerService::GetDownloadThms(MessageParcel &data, MessageParcel &reply)
37 {
38     MEDIA_INFO_LOG("enter GetDownloadThms");
39     GetDownloadThmReqBody reqBody;
40     int32_t ret = IPC::UserDefineIPC().ReadRequestBody(data, reqBody);
41     CHECK_AND_RETURN_RET_LOG(ret == E_OK, IPC::UserDefineIPC().WriteResponseBody(reply, ret), "ReadRequestBody failed");
42     int32_t size = reqBody.size;
43     MEDIA_INFO_LOG("GetDownloadThms size: %{public}d.", reqBody.size);
44     if (size <= 0 || size > LIMIT_SIZE) {
45         MEDIA_ERR_LOG("QueryThumbsToDownload param error, size: %{public}d", size);
46         return IPC::UserDefineIPC().WriteResponseBody(reply, E_MEDIA_CLOUD_ARGS_INVAILD);
47     }
48     DownloadThumbnailQueryDto queryDto = this->processor_.GetDownloadThumbnailQueryDto(reqBody);
49     std::vector<PhotosDto> photosDtoVec;
50     ret = this->service_.GetDownloadThms(queryDto, photosDtoVec);
51     if (ret != E_OK) {
52         MEDIA_ERR_LOG("GetDownloadThms ret error, ret: %{public}d", ret);
53         return IPC::UserDefineIPC().WriteResponseBody(reply, ret);
54     }
55     std::vector<PhotosVo> photosVoList;
56     for (auto &photosDto : photosDtoVec) {
57         PhotosVo photosVo = this->processor_.ConvertPhotosDtoToPhotosVo(photosDto);
58         MEDIA_DEBUG_LOG("GetDownloadThms Dto: %{public}s, Vo: %{public}s.",
59             photosDto.ToString().c_str(),
60             photosVo.ToString().c_str());
61         photosVoList.push_back(photosVo);
62     }
63     GetDownloadThmRespBody respBody;
64     respBody.photos = photosVoList;
65     return IPC::UserDefineIPC().WriteResponseBody(reply, respBody);
66 }
67 
GetDownloadThmNum(MessageParcel & data,MessageParcel & reply)68 int32_t CloudMediaDownloadControllerService::GetDownloadThmNum(MessageParcel &data, MessageParcel &reply)
69 {
70     GetDownloadThmNumReqBody req;
71     GetDownloadThmNumRespBody resp;
72     int32_t ret = IPC::UserDefineIPC().ReadRequestBody(data, req);
73     CHECK_AND_RETURN_RET_LOG(ret == E_OK, IPC::UserDefineIPC().WriteResponseBody(reply, ret), "ReadRequestBody failed");
74     MEDIA_INFO_LOG("GetDownloadThmNum begin %{public}d", req.type);
75     ret = this->service_.GetDownloadThmNum(req.type, resp.totalNum);
76     MEDIA_INFO_LOG("GetDownloadThmNum end count %{public}d, ret %{public}d", resp.totalNum, ret);
77     if (ret != E_OK) {
78         MEDIA_ERR_LOG("GetDownloadThmNum ret error, ret: %{public}d", ret);
79         return IPC::UserDefineIPC().WriteResponseBody(reply, ret);
80     }
81     return IPC::UserDefineIPC().WriteResponseBody(reply, resp, ret);
82 }
83 
GetDownloadThmsByUri(MessageParcel & data,MessageParcel & reply)84 int32_t CloudMediaDownloadControllerService::GetDownloadThmsByUri(MessageParcel &data, MessageParcel &reply)
85 {
86     MEDIA_INFO_LOG("enter GetDownloadThmsByUri");
87     GetDownloadThmsByUriReqBody reqBody;
88     int32_t ret = IPC::UserDefineIPC().ReadRequestBody(data, reqBody);
89     if (ret != E_OK) {
90         MEDIA_INFO_LOG("GetDownloadThmsByUri Read Req Error");
91         return IPC::UserDefineIPC().WriteResponseBody(reply, ret);
92     }
93     std::vector<int32_t> fileIds;
94     ret = CloudMediaUriUtils::GetFileIds(reqBody.pathList, fileIds);
95     if (ret != E_OK) {
96         MEDIA_INFO_LOG("GetFileIds Error, ret = %{public}d", ret);
97         return IPC::UserDefineIPC().WriteResponseBody(reply, ret);
98     }
99     std::vector<PhotosVo> photosVoList;
100     std::vector<PhotosDto> photosDtoList = this->service_.GetDownloadThmsByUri(fileIds, reqBody.thmType);
101     MEDIA_INFO_LOG(
102         "GetDownloadThmsByUri Query:%{public}zu, Result:%{public}zu", photosDtoList.size(), photosVoList.size());
103     for (auto &photosDto : photosDtoList) {
104         PhotosVo photosVo = this->processor_.ConvertPhotosDtoToPhotosVo(photosDto);
105         MEDIA_DEBUG_LOG("GetDownloadThmsByUri PhotoVo: %{public}s", photosVo.ToString().c_str());
106         photosVoList.push_back(photosVo);
107     }
108     GetDownloadThmsByUriRespBody respBody;
109     respBody.photos = photosVoList;
110     return IPC::UserDefineIPC().WriteResponseBody(reply, respBody);
111 }
112 
OnDownloadThms(MessageParcel & data,MessageParcel & reply)113 int32_t CloudMediaDownloadControllerService::OnDownloadThms(MessageParcel &data, MessageParcel &reply)
114 {
115     MEDIA_INFO_LOG("enter OnDownloadThms");
116     OnDownloadThmsReqBody reqBody;
117     int32_t ret = IPC::UserDefineIPC().ReadRequestBody(data, reqBody);
118     if (ret != E_OK) {
119         return IPC::UserDefineIPC().WriteResponseBody(reply, ret);
120     }
121     int32_t size = static_cast<int32_t>(reqBody.downloadThmsDataList.size());
122     if (size <= 0 || size > LIMIT_SIZE) {
123         MEDIA_ERR_LOG("OnDownloadThms param error, size: %{public}d", size);
124         return IPC::UserDefineIPC().WriteResponseBody(reply, E_MEDIA_CLOUD_ARGS_INVAILD);
125     }
126     MEDIA_INFO_LOG("downloadThumbnailMap: %{public}s", reqBody.ToString().c_str());
127     std::unordered_map<std::string, int32_t> downloadThumbnailMap;
128     for (size_t i = 0; i < reqBody.downloadThmsDataList.size(); i++) {
129         OnDownloadThmsReqBody::DownloadThmsData downloadThmsData = reqBody.downloadThmsDataList[i];
130         downloadThumbnailMap[downloadThmsData.cloudId] = downloadThmsData.thumbStatus;
131     }
132     MEDIA_INFO_LOG("downloadThumbnailMap: %{public}zu", downloadThumbnailMap.size());
133     std::vector<MediaOperateResultDto> resultDtos;
134     ret = this->service_.OnDownloadThms(downloadThumbnailMap, resultDtos);
135     MediaOperateResultRespBody respBody;
136     respBody.result = this->processor_.GetMediaOperateResult(resultDtos);
137     return IPC::UserDefineIPC().WriteResponseBody(reply, respBody, ret);
138 }
139 
GetDownloadAsset(MessageParcel & data,MessageParcel & reply)140 int32_t CloudMediaDownloadControllerService::GetDownloadAsset(MessageParcel &data, MessageParcel &reply)
141 {
142     MEDIA_INFO_LOG("enter CloudMediaDataControllerService::GetDownloadAsset");
143     GetDownloadAssetReqBody reqBody;
144     int32_t ret = IPC::UserDefineIPC().ReadRequestBody(data, reqBody);
145     if (ret != E_OK) {
146         MEDIA_INFO_LOG("CloudMediaDataControllerService::GetDownloadAsset Read Req Error");
147         return IPC::UserDefineIPC().WriteResponseBody(reply, ret);
148     }
149     std::vector<int32_t> fileIds;
150     ret = CloudMediaUriUtils::GetFileIds(reqBody.pathList, fileIds);
151     if (ret != E_OK) {
152         MEDIA_INFO_LOG("GetFileIds Error, ret = %{public}d", ret);
153         return IPC::UserDefineIPC().WriteResponseBody(reply, ret);
154     }
155     std::vector<PhotosDto> photosDtoList = this->service_.GetDownloadAsset(fileIds);
156     std::vector<PhotosVo> photosVoList;
157     for (auto &photosDto : photosDtoList) {
158         PhotosVo photosVo = this->processor_.ConvertPhotosDtoToPhotosVo(photosDto);
159         MEDIA_DEBUG_LOG("GetDownloadAsset PhotoVo: %{public}s", photosVo.ToString().c_str());
160         photosVoList.push_back(photosVo);
161     }
162     MEDIA_INFO_LOG("CloudMediaDataControllerService::GetDownloadAsset Query:%{public}zu, Result:%{public}zu",
163         photosDtoList.size(),
164         photosVoList.size());
165     GetDownloadAssetRespBody respBody;
166     respBody.photos = photosVoList;
167     return IPC::UserDefineIPC().WriteResponseBody(reply, respBody);
168 }
169 
OnDownloadAsset(MessageParcel & data,MessageParcel & reply)170 int32_t CloudMediaDownloadControllerService::OnDownloadAsset(MessageParcel &data, MessageParcel &reply)
171 {
172     MEDIA_INFO_LOG("enter CloudMediaDataControllerService::OnDownloadAsset");
173     OnDownloadAssetReqBody reqBody;
174     int32_t ret = IPC::UserDefineIPC().ReadRequestBody(data, reqBody);
175     if (ret != E_OK) {
176         return IPC::UserDefineIPC().WriteResponseBody(reply, ret);
177     }
178     MEDIA_INFO_LOG("OnDownloadAsset: %{public}s", reqBody.ToString().c_str());
179     std::vector<MediaOperateResultDto> resultDtos;
180     ret = this->service_.OnDownloadAsset(reqBody.cloudIds, resultDtos);
181     MediaOperateResultRespBody respBody;
182     respBody.result = this->processor_.GetMediaOperateResult(resultDtos);
183     return IPC::UserDefineIPC().WriteResponseBody(reply, respBody, ret);
184 }
185 }  // namespace OHOS::Media::CloudSync