1 /*
2 * Copyright (C) 2023 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 #ifndef OHOS_MEDIA_CLONE_RESTORE_H
17 #define OHOS_MEDIA_CLONE_RESTORE_H
18
19 #include <optional>
20 #include <type_traits>
21 #include <set>
22 #include <sstream>
23 #include <algorithm>
24 #include <iterator>
25 #include <vector>
26
27 #include "base_restore.h"
28 #include "backup_const.h"
29 #include "clone_restore_cv_analysis.h"
30 #include "clone_restore_highlight.h"
31 #include "medialibrary_rdb_utils.h"
32 #include "medialibrary_errno.h"
33 #include "medialibrary_kvstore_manager.h"
34 #include "backup_database_utils.h"
35 #include "photo_album_clone.h"
36 #include "photos_clone.h"
37 #include "clone_restore_classify.h"
38 #include "clone_restore_geo.h"
39 #include "clone_restore_geo_dictionary.h"
40
41 namespace OHOS {
42 namespace Media {
43 struct CloudPhotoFileExistFlag {
44 bool isLcdExist {false};
45 bool isThmExist {false};
46 bool isDayAstcExist {false};
47 bool isYearAstcExist {false};
48 bool isExLcdExist {false};
49 bool isExThmExist {false};
50 };
51 class CloneRestore : public BaseRestore {
52 public:
53 CloneRestore();
54 virtual ~CloneRestore() = default;
55 // upgradePath is useless now
56 void StartRestore(const std::string &backupRestorePath, const std::string &upgradePath) override;
57 int32_t Init(const std::string &backupRestoreDir, const std::string &upgradeFilePath, bool isUpgrade) override;
58 NativeRdb::ValuesBucket GetInsertValue(const FileInfo &fileInfo, const std::string &newPath,
59 int32_t sourceType) override;
60 NativeRdb::ValuesBucket GetCloudInsertValue(const FileInfo &fileInfo, const std::string &newPath,
61 int32_t sourceType);
62 std::string GetBackupInfo() override;
63 void StartBackup() override;
64 using CoverUriInfo = std::pair<std::string, std::pair<std::string, int32_t>>;
65
66 protected:
67 void MoveMigrateCloudFile(std::vector<FileInfo> &fileInfos, int32_t &fileMoveCount, int32_t &videoFileMoveCount,
68 int32_t sceneCode) override;
69 void GetCloudPhotoFileExistFlag(const FileInfo &fileInfo, CloudPhotoFileExistFlag &resultExistFlag);
70 void CloudPhotoFilesVerify(const std::vector<FileInfo> &fileInfos, std::vector<FileInfo> &LCDNotFound,
71 std::vector<FileInfo> &THMNotFound, unordered_map<string, CloudPhotoFileExistFlag> &resultExistMap);
72
73 private:
74 void RestorePhoto(void) override;
75 void RestorePhotoForCloud(void);
76 void HandleRestData(void) override;
77 std::vector<FileInfo> QueryFileInfos(int32_t offset, int32_t isRelatedToPhotoMap = 0);
78 std::vector<FileInfo> QueryCloudFileInfos(int32_t offset, int32_t isRelatedToPhotoMap = 0);
79 bool ParseResultSet(const std::shared_ptr<NativeRdb::ResultSet> &resultSet, FileInfo &info,
80 std::string dbName = "") override;
81 bool ParseResultSetForAudio(const std::shared_ptr<NativeRdb::ResultSet> &resultSet, FileInfo &info) override;
82 void AnalyzeSource() override;
83 void RestoreAlbum(void);
84 void RestoreAudio(void) override;
85 int InsertPhoto(std::vector<FileInfo> &fileInfos);
86 std::vector<NativeRdb::ValuesBucket> GetInsertValues(int32_t sceneCode, std::vector<FileInfo> &fileInfos,
87 int32_t sourceType);
88 std::vector<NativeRdb::ValuesBucket> GetCloudInsertValues(int32_t sceneCode, std::vector<FileInfo> &fileInfos,
89 int32_t sourceType) override;
90 int InsertCloudPhoto(int32_t sceneCode, std::vector<FileInfo> &fileInfos, int32_t sourceType) override;
91 std::vector<NativeRdb::ValuesBucket> GetInsertValues(std::vector<AnalysisAlbumTbl> &analysisAlbumTbl);
92 int32_t MoveAsset(FileInfo &fileInfo);
93 bool IsFilePathExist(const std::string &filePath) const;
94 int32_t QueryTotalNumber(const std::string &tableName);
95 std::vector<AlbumInfo> QueryAlbumInfos(const std::string &tableName, int32_t offset);
96 bool ParseAlbumResultSet(const std::string &tableName, const std::shared_ptr<NativeRdb::ResultSet> &resultSet,
97 AlbumInfo &albumInfo);
98 bool PrepareCommonColumnInfoMap(const std::string &tableName,
99 const std::unordered_map<std::string, std::string> &srcColumnInfoMap,
100 const std::unordered_map<std::string, std::string> &dstColumnInfoMap);
101 bool HasSameColumn(const std::unordered_map<std::string, std::string> &columnInfoMap, const std::string &columnName,
102 const std::string &columnType);
103 void GetValFromResultSet(const std::shared_ptr<NativeRdb::ResultSet> &resultSet,
104 std::unordered_map<std::string, std::variant<int32_t, int64_t, double, std::string>> &valMap,
105 const std::string &columnName, const std::string &columnType);
106 void PrepareCommonColumnVal(NativeRdb::ValuesBucket &values, const std::string &columnName,
107 const std::variant<int32_t, int64_t, double, std::string> &columnVal,
108 const std::unordered_map<std::string, std::string> &commonColumnInfoMap) const;
109 void GetQueryWhereClause(const std::string &tableName,
110 const std::unordered_map<std::string, std::string> &columnInfoMap);
111 void BatchQueryPhoto(std::vector<FileInfo> &fileInfos);
112 void BatchNotifyPhoto(const std::vector<FileInfo> &fileInfos);
113 void InsertAlbum(std::vector<AlbumInfo> &albumInfos, const std::string &tableName);
114 std::vector<NativeRdb::ValuesBucket> GetInsertValues(std::vector<AlbumInfo> &albumInfos,
115 const std::string &tableName);
116 bool HasSameAlbum(const AlbumInfo &albumInfo, const std::string &tableName);
117 void BatchQueryAlbum(std::vector<AlbumInfo> &albumInfos, const std::string &tableName);
118 void BatchInsertMap(const std::vector<FileInfo> &fileInfos, int64_t &totalRowNum);
119 NativeRdb::ValuesBucket GetInsertValue(const MapInfo &mapInfo) const;
120 NativeRdb::ValuesBucket GetInsertValue(const AlbumInfo &albumInfo, const std::string &tableName) const;
121 void CheckTableColumnStatus(std::shared_ptr<NativeRdb::RdbStore> rdbStore,
122 const std::vector<std::vector<std::string>> &cloneTableList);
123 bool HasColumns(const std::unordered_map<std::string, std::string> &columnInfoMap,
124 const std::unordered_set<std::string> &columnSet);
125 bool HasColumn(const std::unordered_map<std::string, std::string> &columnInfoMap, const std::string &columnName);
126 void GetAlbumExtraQueryWhereClause(const std::string &tableName);
127 bool IsReadyForRestore(const std::string &tableName);
128 void PrepareEditTimeVal(NativeRdb::ValuesBucket &values, int64_t editTime, const FileInfo &fileInfo,
129 const std::unordered_map<std::string, std::string> &commonColumnInfoMap) const;
130 void RestoreGallery();
131 bool PrepareCloudPath(const std::string &tableName, FileInfo &fileInfo);
132 void RestoreMusic();
133 std::vector<FileInfo> QueryFileInfos(const std::string &tableName, int32_t offset);
134 bool ParseResultSet(const std::string &tableName, const std::shared_ptr<NativeRdb::ResultSet> &resultSet,
135 FileInfo &fileInfo);
136 void InsertAudio(std::vector<FileInfo> &fileInfos);
137 int32_t QueryTotalNumberByMediaType(std::shared_ptr<NativeRdb::RdbStore> rdbStore, const std::string &tableName,
138 MediaType mediaType);
139 size_t StatClonetotalSize(std::shared_ptr<NativeRdb::RdbStore> mediaRdb);
140 std::string GetBackupInfoByCount(int32_t photoCount, int32_t videoCount, int32_t audioCount, size_t totalSize);
141 void MoveMigrateFile(std::vector<FileInfo> &fileInfos, int64_t &fileMoveCount, int64_t &videoFileMoveCount);
142 void RestorePhotoBatch(int32_t offset, int32_t isRelatedToPhotoMap = 0);
143 void RestoreBatchForCloud(int32_t offset, int32_t isRelatedToPhotoMap = 0);
144 void RestoreAudioBatch(int32_t offset);
145 void InsertPhotoRelated(std::vector<FileInfo> &fileInfos);
146 void SetFileIdReference(const std::vector<FileInfo> &fileInfos, std::string &selection,
147 std::unordered_map<int32_t, int32_t> &fileIdMap);
148 int32_t QueryMapTotalNumber(const std::string &baseQuerySql);
149 std::vector<MapInfo> QueryMapInfos(const std::string &tableName, const std::string &baseQuerySql, int32_t offset,
150 const std::unordered_map<int32_t, int32_t> &fileIdMap, const std::unordered_map<int32_t, int32_t> &albumIdMap);
151 int64_t InsertMapByTable(const std::string &tableName, const std::vector<MapInfo> &mapInfos,
152 std::unordered_set<int32_t> &albumSet);
153 std::vector<NativeRdb::ValuesBucket> GetInsertValues(const std::vector<MapInfo> &mapInfos);
154 std::string GetQueryWhereClauseByTable(const std::string &tableName);
155 void SetSpecialAttributes(const std::string &tableName, const std::shared_ptr<NativeRdb::ResultSet> &resultSet,
156 FileInfo &fileInfo);
157 bool IsSameFileForClone(const std::string &tableName, FileInfo &fileInfo);
158 NativeRdb::ValuesBucket GetInsertValue(const AnalysisAlbumTbl &portraitAlbumInfo);
159 int32_t InsertPortraitAlbumByTable(std::vector<AnalysisAlbumTbl> &analysisAlbumTbl);
160 void InsertPortraitAlbum(std::vector<AnalysisAlbumTbl> &analysisAlbumTbl);
161 void ParsePortraitAlbumResultSet(const std::shared_ptr<NativeRdb::ResultSet> &resultSet,
162 AnalysisAlbumTbl &analysisAlbumTbl);
163 std::vector<AnalysisAlbumTbl> QueryPortraitAlbumTbl(int32_t offset,
164 const std::vector<std::string>& commonColumns);
165 void RestoreFromGalleryPortraitAlbum();
166 int32_t QueryPortraitAlbumTotalNumber(std::shared_ptr<NativeRdb::RdbStore> rdbPtr, std::string query);
167 std::unordered_map<std::string, std::string> CreateImgFaceColumnFieldMap();
168 void ParseImageFaceResultSet(const std::shared_ptr<NativeRdb::ResultSet> &resultSet, ImageFaceTbl &imageFaceTbl);
169 void ParseFaceTagResultSet(const std::shared_ptr<NativeRdb::ResultSet> &resultSet, FaceTagTbl &faceTagTbl);
170 NativeRdb::ValuesBucket CreateValuesBucketFromImageFaceTbl(const ImageFaceTbl& imageFaceTbl);
171 void BatchInsertImageFaces(const std::vector<ImageFaceTbl>& imageFaceTbls);
172 std::vector<ImageFaceTbl> ProcessImageFaceTbls(const std::vector<ImageFaceTbl>& imageFaceTbls,
173 const std::vector<FileIdPair>& fileIdPairs);
174 std::vector<ImageFaceTbl> QueryImageFaceTbl(int32_t offset, std::string &fileIdClause,
175 const std::vector<std::string>& commonColumns);
176 std::vector<PortraitAlbumDfx> QueryAllPortraitAlbum(int32_t& offset, int32_t& rowCount);
177 void RecordOldPortraitAlbumDfx();
178 std::unordered_set<std::string> QueryAllPortraitAlbum();
179 void LogPortraitCloneDfx();
180 void RestoreImageFaceInfo(std::vector<FileInfo> &fileInfos);
181 NativeRdb::ValuesBucket CreateValuesBucketFromFaceTagTbl(const FaceTagTbl& faceTagTbl);
182 void BatchInsertFaceTags(const std::vector<FaceTagTbl>& faceTagTbls);
183 void DeleteExistingFaceTagData(const std::string& inClause);
184 std::vector<FaceTagTbl> QueryFaceTagTbl(int32_t offset, const std::string& inClause);
185 void RestorePortraitClusteringInfo();
186 void ReportPortraitCloneStat(int32_t sceneCode);
187 void AppendExtraWhereClause(std::string& whereClause, const std::string& tableName);
188 void GenNewCoverUris(const std::vector<CoverUriInfo>& coverUriInfo,
189 std::vector<FileInfo> &fileInfos);
190 bool GetFileInfoByFileId(int32_t fileId, const std::vector<FileInfo>& fileInfos, FileInfo& outFileInfo);
191 std::string GenCoverUriUpdateSql(const std::unordered_map<std::string, std::pair<std::string, int32_t>>&
192 tagIdToCoverInfo, const std::unordered_map<std::string, int32_t>& oldToNewFileId,
193 const std::vector<FileInfo>& fileInfos, std::vector<std::string>& tagIds);
194 std::string ProcessUriAndGenNew(const std::string& tagId, const std::string& oldCoverUri,
195 const std::unordered_map<std::string, int32_t>& oldToNewFileId, const std::vector<FileInfo>& fileInfos);
196 int32_t MovePicture(FileInfo &fileInfo);
197 int32_t MoveMovingPhotoVideo(FileInfo &fileInfo);
198 int32_t MoveEditedData(FileInfo &fileInfo);
199 int32_t MoveThumbnail(FileInfo &fileInfo);
200 int32_t MoveThumbnailDir(FileInfo &fileInfo);
201 int32_t MoveCloudThumbnailDir(FileInfo &fileInfo);
202 int32_t MoveAstc(FileInfo &fileInfo);
203 void InitThumbnailStatus();
204 bool InitAllKvStore();
205 void CloseAllKvStore();
206 bool BackupKvStore();
207 void GetThumbnailInsertValue(const FileInfo &fileInfo, NativeRdb::ValuesBucket &values);
208 void GetCloudThumbnailInsertValue(const FileInfo &fileInfo, NativeRdb::ValuesBucket &values);
209 int32_t GetNoNeedMigrateCount() override;
210 void GetAccountValid() override;
211 int32_t GetHighlightCloudMediaCnt();
212 void RestoreHighlightAlbums();
213
214 template<typename T>
215 static void PutIfPresent(NativeRdb::ValuesBucket& values, const std::string& columnName,
216 const std::optional<T>& optionalValue);
217
218 template<typename T>
219 void PutWithDefault(NativeRdb::ValuesBucket& values, const std::string& columnName,
220 const std::optional<T>& optionalValue, const T& defaultValue);
221 std::string GetThumbnailLocalPath(const string path);
222 void BatchUpdateFileInfoData(std::vector<FileInfo> &fileInfos,
223 unordered_map<string, CloudPhotoFileExistFlag> &resultExistMap);
224 int32_t CheckThumbReady(const FileInfo &fileInfo,
225 const CloudPhotoFileExistFlag &cloudPhotoFileExistFlag);
226 int32_t CheckThumbStatus(const FileInfo &fileInfo,
227 const CloudPhotoFileExistFlag &cloudPhotoFileExistFlag);
228 int32_t CheckLcdVisitTime(const CloudPhotoFileExistFlag &cloudPhotoFileExistFlag);
229
230 private:
231 std::atomic<uint64_t> migrateDatabaseAlbumNumber_{0};
232 std::atomic<uint64_t> migrateDatabaseMapNumber_{0};
233 std::shared_ptr<NativeRdb::RdbStore> mediaRdb_;
234 std::string filePath_;
235 std::string dbPath_;
236 std::unordered_map<std::string, bool> tableColumnStatusMap_;
237 std::unordered_map<std::string, std::string> tableQueryWhereClauseMap_;
238 std::unordered_map<std::string, std::string> tableExtraQueryWhereClauseMap_;
239 std::unordered_map<std::string, std::unordered_map<int32_t, int32_t>> tableAlbumIdMap_;
240 std::unordered_map<std::string, std::unordered_map<std::string, std::string>> tableCommonColumnInfoMap_;
241 std::string garbagePath_;
242 std::vector<CoverUriInfo> coverUriInfo_;
243 std::vector<PortraitAlbumDfx> portraitAlbumDfx_;
244 PhotoAlbumClone photoAlbumClone_;
245 PhotosClone photosClone_;
246 static constexpr int32_t INVALID_COVER_SATISFIED_STATUS = -1;
247 bool hasCloneThumbnailDir_{false};
248 bool isInitKvstoreSuccess_{false};
249 std::shared_ptr<MediaLibraryKvStore> oldMonthKvStorePtr_ = nullptr;
250 std::shared_ptr<MediaLibraryKvStore> oldYearKvStorePtr_ = nullptr;
251 std::shared_ptr<MediaLibraryKvStore> newMonthKvStorePtr_ = nullptr;
252 std::shared_ptr<MediaLibraryKvStore> newYearKvStorePtr_ = nullptr;
253 std::vector<int> photosFailedOffsets;
254 CloneRestoreClassify cloneRestoreClassify_;
255 CloneRestoreGeo cloneRestoreGeo_;
256 CloneRestoreHighlight cloneRestoreHighlight_;
257 CloneRestoreCVAnalysis cloneRestoreCVAnalysis_;
258 std::atomic<uint64_t> lcdMigrateFileNumber_{0};
259 std::atomic<uint64_t> thumbMigrateFileNumber_{0};
260 std::atomic<uint64_t> migrateCloudSuccessNumber_{0};
261 CloneRestoreGeoDictionary cloneRestoreGeoDictionary_;
262 };
263
264 template<typename T>
PutIfPresent(NativeRdb::ValuesBucket & values,const std::string & columnName,const std::optional<T> & optionalValue)265 void CloneRestore::PutIfPresent(NativeRdb::ValuesBucket& values, const std::string& columnName,
266 const std::optional<T>& optionalValue)
267 {
268 if (optionalValue.has_value()) {
269 if constexpr (std::is_same_v<std::decay_t<T>, int32_t>) {
270 values.PutInt(columnName, optionalValue.value());
271 } else if constexpr (std::is_same_v<std::decay_t<T>, int64_t>) {
272 values.PutLong(columnName, optionalValue.value());
273 } else if constexpr (std::is_same_v<std::decay_t<T>, std::string>) {
274 values.PutString(columnName, optionalValue.value());
275 } else if constexpr (std::is_same_v<std::decay_t<T>, double>) {
276 values.PutDouble(columnName, optionalValue.value());
277 }
278 }
279 }
280
281 template<typename T>
PutWithDefault(NativeRdb::ValuesBucket & values,const std::string & columnName,const std::optional<T> & optionalValue,const T & defaultValue)282 void CloneRestore::PutWithDefault(NativeRdb::ValuesBucket& values, const std::string& columnName,
283 const std::optional<T>& optionalValue, const T& defaultValue)
284 {
285 if (optionalValue.has_value()) {
286 PutIfPresent(values, columnName, optionalValue);
287 } else {
288 PutIfPresent(values, columnName, std::optional<T>(defaultValue));
289 }
290 }
291 } // namespace Media
292 } // namespace OHOS
293
294 #endif // OHOS_MEDIA_CLONE_RESTORE_H
295