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 "Media_Client"
16
17 #include "cloud_media_data_client.h"
18
19 #include <string>
20 #include <vector>
21
22 #include "medialibrary_errno.h"
23 #include "media_log.h"
24 #include "cloud_media_data_client_handler.h"
25 #include "cloud_media_thread_limiter.h"
26
27 namespace OHOS::Media::CloudSync {
CloudMediaDataClient(const int32_t & userId)28 CloudMediaDataClient::CloudMediaDataClient(const int32_t &userId) : userId_(userId)
29 {
30 this->dataHandler_ = std::make_shared<CloudMediaDataClientHandler>();
31 this->dataHandler_->SetUserId(userId);
32 }
33
SetUserId(const int32_t & userId)34 void CloudMediaDataClient::SetUserId(const int32_t &userId)
35 {
36 this->userId_ = userId;
37 }
38
SetTraceId(const std::string & traceId)39 void CloudMediaDataClient::SetTraceId(const std::string &traceId)
40 {
41 if (this->dataHandler_ == nullptr) {
42 MEDIA_ERR_LOG("No data handler found!");
43 return;
44 }
45 this->dataHandler_->SetTraceId(traceId);
46 }
47
GetTraceId() const48 std::string CloudMediaDataClient::GetTraceId() const
49 {
50 if (this->dataHandler_ == nullptr) {
51 MEDIA_ERR_LOG("No data handler found!");
52 return "";
53 }
54 return this->dataHandler_->GetTraceId();
55 }
56
UpdateDirty(const std::string & cloudId,DirtyTypes dirtyType)57 int32_t CloudMediaDataClient::UpdateDirty(const std::string &cloudId, DirtyTypes dirtyType)
58 {
59 if (this->dataHandler_ == nullptr) {
60 MEDIA_ERR_LOG("No data handler found!");
61 return E_IPC_INVAL_ARG;
62 }
63 CLOUD_SYNC_HANDLER_WRITE_LOCK;
64 return this->dataHandler_->UpdateDirty(cloudId, dirtyType);
65 }
66
UpdatePosition(const std::vector<std::string> & cloudIds,int32_t position)67 int32_t CloudMediaDataClient::UpdatePosition(const std::vector<std::string> &cloudIds, int32_t position)
68 {
69 if (this->dataHandler_ == nullptr) {
70 MEDIA_ERR_LOG("No data handler found!");
71 return E_IPC_INVAL_ARG;
72 }
73 CLOUD_SYNC_HANDLER_WRITE_LOCK;
74 return this->dataHandler_->UpdatePosition(cloudIds, position);
75 }
76
UpdateSyncStatus(const std::string & cloudId,int32_t syncStatus)77 int32_t CloudMediaDataClient::UpdateSyncStatus(const std::string &cloudId, int32_t syncStatus)
78 {
79 if (this->dataHandler_ == nullptr) {
80 MEDIA_ERR_LOG("No data handler found!");
81 return E_IPC_INVAL_ARG;
82 }
83 CLOUD_SYNC_HANDLER_WRITE_LOCK;
84 return this->dataHandler_->UpdateSyncStatus(cloudId, syncStatus);
85 }
86
UpdateThmStatus(const std::string & cloudId,int32_t thmStatus)87 int32_t CloudMediaDataClient::UpdateThmStatus(const std::string &cloudId, int32_t thmStatus)
88 {
89 if (this->dataHandler_ == nullptr) {
90 MEDIA_ERR_LOG("No data handler found!");
91 return E_IPC_INVAL_ARG;
92 }
93 CLOUD_SYNC_HANDLER_WRITE_LOCK;
94 return this->dataHandler_->UpdateThmStatus(cloudId, thmStatus);
95 }
96
GetAgingFile(const int64_t time,int32_t mediaType,int32_t sizeLimit,int32_t offset,std::vector<CloudMetaData> & metaData)97 int32_t CloudMediaDataClient::GetAgingFile(
98 const int64_t time, int32_t mediaType, int32_t sizeLimit, int32_t offset, std::vector<CloudMetaData> &metaData)
99 {
100 if (this->dataHandler_ == nullptr) {
101 MEDIA_ERR_LOG("No data handler found!");
102 return E_IPC_INVAL_ARG;
103 }
104 return this->dataHandler_->GetAgingFile(time, mediaType, sizeLimit, offset, metaData);
105 }
106
GetActiveAgingFile(const int64_t time,int32_t mediaType,int32_t sizeLimit,int32_t offset,std::vector<CloudMetaData> & metaData)107 int32_t CloudMediaDataClient::GetActiveAgingFile(
108 const int64_t time, int32_t mediaType, int32_t sizeLimit, int32_t offset, std::vector<CloudMetaData> &metaData)
109 {
110 if (this->dataHandler_ == nullptr) {
111 MEDIA_ERR_LOG("No data handler found!");
112 return E_IPC_INVAL_ARG;
113 }
114 return this->dataHandler_->GetActiveAgingFile(time, mediaType, sizeLimit, offset, metaData);
115 }
116
GetDownloadAsset(const std::vector<std::string> & uris,std::vector<CloudMetaData> & cloudMetaDataVec)117 int32_t CloudMediaDataClient::GetDownloadAsset(
118 const std::vector<std::string> &uris, std::vector<CloudMetaData> &cloudMetaDataVec)
119 {
120 if (this->dataHandler_ == nullptr) {
121 MEDIA_ERR_LOG("No data handler found!");
122 return E_IPC_INVAL_ARG;
123 }
124 return this->dataHandler_->GetDownloadAsset(uris, cloudMetaDataVec);
125 }
126
GetDownloadThmsByUri(const std::vector<std::string> & uri,int32_t type,std::vector<CloudMetaData> & metaData)127 int32_t CloudMediaDataClient::GetDownloadThmsByUri(
128 const std::vector<std::string> &uri, int32_t type, std::vector<CloudMetaData> &metaData)
129 {
130 if (this->dataHandler_ == nullptr) {
131 MEDIA_ERR_LOG("No data handler found!");
132 return E_IPC_INVAL_ARG;
133 }
134 return this->dataHandler_->GetDownloadThmsByUri(uri, type, metaData);
135 }
136
OnDownloadAsset(const std::vector<std::string> & cloudIds,std::vector<MediaOperateResult> & result)137 int32_t CloudMediaDataClient::OnDownloadAsset(
138 const std::vector<std::string> &cloudIds, std::vector<MediaOperateResult> &result)
139 {
140 if (this->dataHandler_ == nullptr) {
141 MEDIA_ERR_LOG("No data handler found!");
142 return E_IPC_INVAL_ARG;
143 }
144 CLOUD_SYNC_HANDLER_WRITE_LOCK;
145 return this->dataHandler_->OnDownloadAsset(cloudIds, result);
146 }
147
GetDownloadThms(std::vector<CloudMetaData> & cloudMetaDataVec,const DownloadThumPara & param)148 int32_t CloudMediaDataClient::GetDownloadThms(
149 std::vector<CloudMetaData> &cloudMetaDataVec, const DownloadThumPara ¶m)
150 {
151 if (this->dataHandler_ == nullptr) {
152 MEDIA_ERR_LOG("No data handler found!");
153 return E_IPC_INVAL_ARG;
154 }
155 return this->dataHandler_->GetDownloadThms(cloudMetaDataVec, param);
156 }
157
OnDownloadThms(const std::unordered_map<std::string,int32_t> & resMap,int32_t & failSize)158 int32_t CloudMediaDataClient::OnDownloadThms(const std::unordered_map<std::string, int32_t> &resMap, int32_t &failSize)
159 {
160 if (this->dataHandler_ == nullptr) {
161 MEDIA_ERR_LOG("No data handler found!");
162 return E_IPC_INVAL_ARG;
163 }
164 CLOUD_SYNC_HANDLER_WRITE_LOCK;
165 return this->dataHandler_->OnDownloadThms(resMap, failSize);
166 }
167
GetVideoToCache(std::vector<CloudMetaData> & cloudMetaDataVec,int32_t size)168 int32_t CloudMediaDataClient::GetVideoToCache(std::vector<CloudMetaData> &cloudMetaDataVec, int32_t size)
169 {
170 if (this->dataHandler_ == nullptr) {
171 MEDIA_ERR_LOG("No data handler found!");
172 return E_IPC_INVAL_ARG;
173 }
174 return this->dataHandler_->GetVideoToCache(cloudMetaDataVec, size);
175 }
176
GetFilePosStat(std::vector<uint64_t> & filePosStat)177 int32_t CloudMediaDataClient::GetFilePosStat(std::vector<uint64_t> &filePosStat)
178 {
179 if (this->dataHandler_ == nullptr) {
180 MEDIA_ERR_LOG("No data handler found!");
181 return E_IPC_INVAL_ARG;
182 }
183 return this->dataHandler_->GetFilePosStat(filePosStat);
184 }
185
GetCloudThmStat(std::vector<uint64_t> & cloudThmStat)186 int32_t CloudMediaDataClient::GetCloudThmStat(std::vector<uint64_t> &cloudThmStat)
187 {
188 if (this->dataHandler_ == nullptr) {
189 MEDIA_ERR_LOG("No data handler found!");
190 return E_IPC_INVAL_ARG;
191 }
192 return this->dataHandler_->GetCloudThmStat(cloudThmStat);
193 }
194
GetDirtyTypeStat(std::vector<uint64_t> & dirtyTypeStat)195 int32_t CloudMediaDataClient::GetDirtyTypeStat(std::vector<uint64_t> &dirtyTypeStat)
196 {
197 if (this->dataHandler_ == nullptr) {
198 MEDIA_ERR_LOG("No data handler found!");
199 return E_IPC_INVAL_ARG;
200 }
201 return this->dataHandler_->GetDirtyTypeStat(dirtyTypeStat);
202 }
203
GetDownloadThmNum(int32_t & totalNum,int32_t type)204 int32_t CloudMediaDataClient::GetDownloadThmNum(int32_t &totalNum, int32_t type)
205 {
206 if (this->dataHandler_ == nullptr) {
207 MEDIA_ERR_LOG("No data handler found!");
208 return E_IPC_INVAL_ARG;
209 }
210 return this->dataHandler_->GetDownloadThmNum(totalNum, type);
211 }
212
UpdateLocalFileDirty(std::vector<MDKRecord> & records)213 int32_t CloudMediaDataClient::UpdateLocalFileDirty(std::vector<MDKRecord> &records)
214 {
215 if (this->dataHandler_ == nullptr) {
216 MEDIA_ERR_LOG("No data handler found!");
217 return E_IPC_INVAL_ARG;
218 }
219 CLOUD_SYNC_HANDLER_WRITE_LOCK;
220 return this->dataHandler_->UpdateLocalFileDirty(records);
221 }
222
GetCloudSyncUnPreparedData(int32_t & result)223 int32_t CloudMediaDataClient::GetCloudSyncUnPreparedData(int32_t &result)
224 {
225 if (this->dataHandler_ == nullptr) {
226 MEDIA_ERR_LOG("No data handler found!");
227 return E_IPC_INVAL_ARG;
228 }
229 return this->dataHandler_->GetCloudSyncUnPreparedData(result);
230 }
231
SubmitCloudSyncPreparedDataTask()232 int32_t CloudMediaDataClient::SubmitCloudSyncPreparedDataTask()
233 {
234 if (this->dataHandler_ == nullptr) {
235 MEDIA_ERR_LOG("No data handler found!");
236 return E_IPC_INVAL_ARG;
237 }
238 return this->dataHandler_->SubmitCloudSyncPreparedDataTask();
239 }
240 } // namespace OHOS::Media::CloudSync