• 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");
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_Service"
17 
18 #include "cloud_media_data_service.h"
19 
20 #include <fcntl.h>
21 #include <string>
22 #include <vector>
23 
24 #include "cloud_media_operation_code.h"
25 #include "cloud_sync_notify_handler.h"
26 #include "cloud_media_sync_utils.h"
27 #include "directory_ex.h"
28 #include "cloud_media_file_utils.h"
29 #include "media_file_utils.h"
30 #include "cloud_media_attachment_utils.h"
31 #include "media_itypes_utils.h"
32 #include "media_log.h"
33 #include "medialibrary_errno.h"
34 #include "moving_photo_file_utils.h"
35 #include "parameters.h"
36 #include "result_set_utils.h"
37 
38 namespace OHOS::Media::CloudSync {
UpdateDirty(const std::string & cloudId,const int32_t dirtyType)39 int32_t CloudMediaDataService::UpdateDirty(const std::string &cloudId, const int32_t dirtyType)
40 {
41     MEDIA_INFO_LOG("UpdateDirty begin, cloudId: %{public}s, dirtyType: %{public}d", cloudId.c_str(), dirtyType);
42     int32_t ret = this->dataDao_.UpdateDirty(cloudId, dirtyType);
43     CHECK_AND_RETURN_RET_LOG(ret == E_OK, ret, "Failed to UpdateDirty.");
44     return ret;
45 }
46 
UpdatePosition(const std::vector<std::string> & cloudIds,const int32_t position)47 int32_t CloudMediaDataService::UpdatePosition(const std::vector<std::string> &cloudIds, const int32_t position)
48 {
49     MEDIA_INFO_LOG("UpdatePosition begin, cloudIds size: %{public}zu, position: %{public}d", cloudIds.size(), position);
50     int32_t ret = this->dataDao_.UpdatePosition(cloudIds, position);
51     CHECK_AND_RETURN_RET_LOG(ret == E_OK, ret, "Failed to UpdatePosition.");
52     return ret;
53 }
54 
UpdateSyncStatus(const std::string & cloudId,const int32_t syncStatus)55 int32_t CloudMediaDataService::UpdateSyncStatus(const std::string &cloudId, const int32_t syncStatus)
56 {
57     MEDIA_INFO_LOG("UpdateSyncStatus, cloudId: %{public}s, syncStatus: %{public}d", cloudId.c_str(), syncStatus);
58     int32_t ret = this->dataDao_.UpdateSyncStatus(cloudId, syncStatus);
59     CHECK_AND_RETURN_RET_LOG(ret == E_OK, ret, "Failed to UpdateSyncStatus.");
60     return ret;
61 }
62 
UpdateThmStatus(const std::string & cloudId,const int32_t thmStatus)63 int32_t CloudMediaDataService::UpdateThmStatus(const std::string &cloudId, const int32_t thmStatus)
64 {
65     MEDIA_INFO_LOG("UpdateThmStatus, cloudId: %{public}s, thmStatus: %{public}d", cloudId.c_str(), thmStatus);
66     int32_t ret = this->dataDao_.UpdateThmStatus(cloudId, thmStatus);
67     CHECK_AND_RETURN_RET_LOG(ret == E_OK, ret, "Failed to UpdateThmStatus.");
68     return ret;
69 }
70 
GetAgingFile(const AgingFileQueryDto & queryDto,std::vector<PhotosDto> & photosDtos)71 int32_t CloudMediaDataService::GetAgingFile(const AgingFileQueryDto &queryDto, std::vector<PhotosDto> &photosDtos)
72 {
73     MEDIA_INFO_LOG("GetAgingFile, queryDto: %{public}s", queryDto.ToString().c_str());
74     std::vector<PhotosPo> photosPos;
75     int32_t ret = this->dataDao_.GetAgingFile(queryDto, photosPos);
76     CHECK_AND_RETURN_RET_LOG(ret == E_OK, ret, "Failed to GetAgingFile, ret: %{public}d", ret);
77     this->processor_.GetPhotosDto(photosPos, photosDtos);
78     return E_OK;
79 }
80 
GetActiveAgingFile(const AgingFileQueryDto & queryDto,std::vector<PhotosDto> & photosDtos)81 int32_t CloudMediaDataService::GetActiveAgingFile(const AgingFileQueryDto &queryDto, std::vector<PhotosDto> &photosDtos)
82 {
83     MEDIA_INFO_LOG("GetActiveAgingFile, queryDto: %{public}s", queryDto.ToString().c_str());
84     std::vector<PhotosPo> photosPos;
85     int32_t ret = this->dataDao_.GetActiveAgingFile(queryDto, photosPos);
86     CHECK_AND_RETURN_RET_LOG(ret == E_OK, ret, "Failed to GetActiveAgingFile, ret: %{public}d", ret);
87     this->processor_.GetPhotosDto(photosPos, photosDtos);
88     return E_OK;
89 }
90 
GetVideoToCache(std::vector<PhotosDto> & photosDtos)91 int32_t CloudMediaDataService::GetVideoToCache(std::vector<PhotosDto> &photosDtos)
92 {
93     MEDIA_INFO_LOG("GetVideoToCache, photosDtos size: %{public}zu", photosDtos.size());
94     std::vector<PhotosPo> photosPos;
95     int32_t ret = this->dataDao_.GetVideoToCache(photosPos);
96     CHECK_AND_RETURN_RET_LOG(ret == E_OK, ret, "Failed to GetVideoToCache, ret = %{public}d", ret);
97     this->processor_.GetPhotosDtoOfVideoCache(photosPos, photosDtos);
98     return E_OK;
99 }
100 
GetFilePosStat()101 std::vector<uint64_t> CloudMediaDataService::GetFilePosStat()
102 {
103     MEDIA_INFO_LOG("GetFilePosStat");
104     std::vector<uint64_t> filePosStat = {0, 0, 0};
105 
106     /* get local file status */
107     int32_t ret;
108     int32_t num;
109     ret = this->dataDao_.QueryFilePosStat(static_cast<int32_t>(PhotoPositionType::LOCAL), num);
110     filePosStat[INDEX_LOCAL] = static_cast<uint64_t>(num);
111 
112     /* get cloud file status */
113     ret = this->dataDao_.QueryFilePosStat(static_cast<int32_t>(PhotoPositionType::CLOUD), num);
114     filePosStat[INDEX_CLOUD] = static_cast<uint64_t>(num);
115 
116     /* get both file status */
117     ret = this->dataDao_.QueryFilePosStat(static_cast<int32_t>(PhotoPositionType::LOCAL_AND_CLOUD), num);
118     filePosStat[INDEX_LOCAL_AND_CLOUD] = static_cast<uint64_t>(num);
119     return filePosStat;
120 }
121 
GetCloudThmStat()122 std::vector<uint64_t> CloudMediaDataService::GetCloudThmStat()
123 {
124     MEDIA_INFO_LOG("GetCloudThmStat");
125     std::vector<uint64_t> cloudThmStat{0, 0, 0, 0};
126     /* get DOWNLOADED thm status */
127     int num = 0;
128     this->dataDao_.QueryCloudThmStat(static_cast<int32_t>(ThumbState::DOWNLOADED), num);
129     cloudThmStat[INDEX_DOWNLOADED] = static_cast<uint64_t>(num);
130     MEDIA_INFO_LOG("ThumbState[0]: %{public}d", num);
131 
132     /* get LCD_TO_DOWNLOAD thm status */
133     this->dataDao_.QueryCloudThmStat(static_cast<int32_t>(ThumbState::LCD_TO_DOWNLOAD), num);
134     cloudThmStat[INDEX_LCD_TO_DOWNLOAD] = static_cast<uint64_t>(num);
135     MEDIA_INFO_LOG("ThumbState[1]: %{public}d", num);
136 
137     /* get THM_TO_DOWNLOAD thm status */
138     this->dataDao_.QueryCloudThmStat(static_cast<int32_t>(ThumbState::THM_TO_DOWNLOAD), num);
139     cloudThmStat[INDEX_THM_TO_DOWNLOAD] = static_cast<uint64_t>(num);
140     MEDIA_INFO_LOG("ThumbState[2]: %{public}d", num);
141 
142     /* get TO_DOWNLOAD thm status */
143     this->dataDao_.QueryCloudThmStat(static_cast<int32_t>(ThumbState::TO_DOWNLOAD), num);
144     cloudThmStat[INDEX_TO_DOWNLOAD] = static_cast<uint64_t>(num);
145     MEDIA_INFO_LOG("ThumbState[3]: %{public}d", num);
146 
147     return cloudThmStat;
148 }
149 
GetDirtyTypeStat(std::vector<uint64_t> & dirtyTypeStat)150 int32_t CloudMediaDataService::GetDirtyTypeStat(std::vector<uint64_t> &dirtyTypeStat)
151 {
152     return this->dataDao_.GetDirtyTypeStat(dirtyTypeStat);
153 }
154 
IsLocalDirty(int32_t dirty,bool isDelete)155 static bool IsLocalDirty(int32_t dirty, bool isDelete)
156 {
157     bool localDirty = (dirty == static_cast<int32_t>(DirtyType::TYPE_MDIRTY)) ||
158                       (dirty == static_cast<int32_t>(DirtyType::TYPE_FDIRTY));
159     if (isDelete) {
160         return localDirty;
161     } else {
162         return localDirty || (dirty == static_cast<int32_t>(DirtyType::TYPE_DELETED));
163     }
164 }
165 
UpdateLocalFileDirty(const std::vector<std::string> & cloudIds)166 int32_t CloudMediaDataService::UpdateLocalFileDirty(const std::vector<std::string> &cloudIds)
167 {
168     MEDIA_INFO_LOG("UpdateLocalFileDirty enter");
169     std::vector<std::string> queryColums = {
170         PhotoColumn::MEDIA_FILE_PATH, PhotoColumn::PHOTO_CLOUD_ID, PhotoColumn::PHOTO_DIRTY};
171     std::vector<PhotosPo> photos;
172     int32_t ret = this->commonDao_.QueryLocalByCloudId(cloudIds, queryColums, photos);
173     CHECK_AND_RETURN_RET_LOG(ret == E_OK, E_ERR, "UpdateLocalFileDirty Query Error");
174 
175     for (auto &photo : photos) {
176         std::string cloudId = photo.cloudId.value_or("");
177         MEDIA_INFO_LOG("UpdateLocalFileDirty Query CloudId: %{public}s", cloudId.c_str());
178         if (cloudId.empty() || std::find(cloudIds.begin(), cloudIds.end(), cloudId) == cloudIds.end()) {
179             continue;
180         }
181         if (IsLocalDirty(photo.dirty.value_or(-1), false)) {
182             MEDIA_ERR_LOG("UpdateLocalFileDirty record dirty: %{public}s", cloudId.c_str());
183             continue;
184         }
185         std::string path = photo.data.value_or("");
186         if (path.empty()) {
187             MEDIA_ERR_LOG("UpdateLocalFileDirty path empty: %{public}s", cloudId.c_str());
188             continue;
189         }
190         std::string filePath = CloudMediaSyncUtils::GetLocalPath(path);
191         if (access(filePath.c_str(), F_OK) != E_OK) {
192             MEDIA_ERR_LOG("UpdateLocalFileDirty no original file, cloudId: %{public}s", cloudId.c_str());
193             continue;
194         }
195         ret = this->dataDao_.UpdateLocalFileDirty(cloudId);
196         MEDIA_INFO_LOG("UpdateLocalFileDirty update cloudId: %{public}s, ret: %{public}d", cloudId.c_str(), ret);
197     }
198     return ret;
199 }
200 }  // namespace OHOS::Media::CloudSync