• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023-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 #ifndef OHOS_MEDIA_BASE_RESTORE_H
17 #define OHOS_MEDIA_BASE_RESTORE_H
18 
19 #include <atomic>
20 #include <mutex>
21 #include <unordered_map>
22 
23 #include "backup_const.h"
24 #include "ffrt.h"
25 #include "ffrt_inner.h"
26 #include "media_file_utils.h"
27 #include "nlohmann/json.hpp"
28 #include "photos_dao.h"
29 #include "photos_data_handler.h"
30 #include "rdb_helper.h"
31 #include "result_set.h"
32 #include "metadata.h"
33 #include "tab_old_photos_restore.h"
34 #include "geo_knowledge_restore.h"
35 #include "highlight_restore.h"
36 
37 namespace OHOS {
38 namespace Media {
39 class BaseRestore {
40 public:
41     BaseRestore() = default;
42     virtual ~BaseRestore() = default;
43     virtual void StartRestore(const std::string &backupRetorePath, const std::string &upgradePath);
44     virtual int32_t Init(const std::string &backupRetorePath, const std::string &upgradePath, bool isUpgrade) = 0;
45     virtual NativeRdb::ValuesBucket GetInsertValue(const FileInfo &fileInfo, const std::string &newPath,
46         int32_t sourceType) = 0;
47     virtual std::string GetBackupInfo();
48     void StartRestoreEx(const std::string &backupRetorePath, const std::string &upgradePath,
49         std::string &restoreExInfo);
50     std::string GetRestoreExInfo();
51     void ReportPortraitStat(int32_t sceneCode);
52     std::string GetProgressInfo();
53     virtual void StartBackup();
54     void StartBackupEx(std::string& backupExInfo);
55     void Release(ReleaseScene releaseScene);
56     std::string restoreInfo_;
57 
58 protected:
59     int32_t Init(void);
60     static std::mutex fileInfoMutext_;
61 
62     virtual void RestorePhoto(void) = 0;
63     virtual void RestoreAudio(void) = 0;
64     virtual void HandleRestData(void) = 0;
65 
66     virtual bool ParseResultSet(const std::shared_ptr<NativeRdb::ResultSet> &resultSet, FileInfo &info,
67         std::string dbName = "") = 0;
68     virtual bool ParseResultSetForAudio(const std::shared_ptr<NativeRdb::ResultSet> &resultSet, FileInfo &info) = 0;
69     virtual void AnalyzeSource() = 0;
70     virtual bool ConvertPathToRealPath(const std::string &srcPath, const std::string &prefix, std::string &newPath,
71         std::string &relativePath);
72     virtual bool NeedBatchQueryPhotoForPortrait(const std::vector<FileInfo> &fileInfos, NeedQueryMap &needQueryMap);
73     virtual void InsertFaceAnalysisData(const std::vector<FileInfo> &fileInfos, const NeedQueryMap &needQueryMap,
74         int64_t &faceRowNum, int64_t &mapRowNum, int64_t &photoNum);
75     void NotifyAlbum();
76     virtual std::string CheckInvalidFile(const FileInfo &fileInfo, int32_t errCode);
77     std::vector<NativeRdb::ValuesBucket> GetInsertValues(int32_t sceneCode, std::vector<FileInfo> &fileInfos,
78         int32_t sourceType);
79     bool PrepareInsertValue(const int32_t sceneCode, FileInfo &fileInfo, int32_t sourceType,
80         NativeRdb::ValuesBucket &value);
81     virtual std::vector<NativeRdb::ValuesBucket> GetCloudInsertValues(int32_t sceneCode,
82         std::vector<FileInfo> &fileInfos, int32_t sourceType);
83     int32_t CopyFile(const std::string &srcFile, const std::string &dstFile) const;
84     virtual void GetAccountValid();
85     virtual void GetSyncSwitchOn();
86     void GetSourceDeviceInfo();
87     bool IsRestorePhoto();
88     int32_t MoveFile(const std::string &srcFile, const std::string &dstFile) const;
89     std::shared_ptr<NativeRdb::ResultSet> QuerySql(const std::string &sql,
90         const std::vector<std::string> &selectionArgs = std::vector<std::string>()) const;
91     int InsertPhoto(int32_t sceneCode, std::vector<FileInfo> &fileInfos, int32_t sourceType);
92     virtual int InsertCloudPhoto(int32_t sceneCode, std::vector<FileInfo> &fileInfos, int32_t sourceType);
93     void InsertAudio(int32_t sceneCode, std::vector<FileInfo> &fileInfos);
94     void SetMetaDataValue(const FileInfo &fileInfo, std::unique_ptr<Metadata> &metadata);
95     double GetDataLongitude(const FileInfo &fileInfo, std::unique_ptr<Metadata> &metadata);
96     double GetDataLatitude(const FileInfo &fileInfo, std::unique_ptr<Metadata> &metadata);
97     virtual void SetValueFromMetaData(FileInfo &info, NativeRdb::ValuesBucket &value);
98     virtual void SetOrientationAndExifRotate(FileInfo &info, NativeRdb::ValuesBucket &value,
99         std::unique_ptr<Metadata> &data);
100     int32_t BatchInsertWithRetry(const std::string &tableName, std::vector<NativeRdb::ValuesBucket> &value,
101         int64_t &rowNum);
102     int32_t MoveDirectory(const std::string &srcDir, const std::string &dstDir, bool deleteOriginalFile = true) const;
103     bool IsSameAudioFile(const std::shared_ptr<NativeRdb::RdbStore> &rdbStore, const std::string &tableName,
104         FileInfo &fileInfo);
105     bool HasSameAudioFile(const std::shared_ptr<NativeRdb::RdbStore> &rdbStore, const std::string &tableName,
106         FileInfo &fileInfo);
HasSameFileForDualClone(FileInfo & fileInfo)107     virtual bool HasSameFileForDualClone(FileInfo &fileInfo)
108     {
109         return false;
110     }
111     void InsertPhotoMap(std::vector<FileInfo> &fileInfos, int64_t &mapRowNum);
112     void BatchQueryPhoto(std::vector<FileInfo> &fileInfos, bool isFull, const NeedQueryMap &needQueryMap);
113     void BatchQueryPhoto(std::vector<FileInfo> &fileInfos);
114     void BatchInsertMap(const std::vector<FileInfo> &fileInfos, int64_t &totalRowNum);
115     nlohmann::json GetErrorInfoJson();
116     nlohmann::json GetCountInfoJson(const std::vector<std::string> &countInfoTypes);
117     SubCountInfo GetSubCountInfo(const std::string &type);
118     std::unordered_map<std::string, FailedFileInfo> GetFailedFiles(const std::string &type);
119     nlohmann::json GetSubCountInfoJson(const std::string &type, const SubCountInfo &subCountInfo, size_t &limit);
120     void SetErrorCode(int32_t errorCode);
121     void UpdateFailedFileByFileType(int32_t fileType, const FileInfo &fileInfo, int32_t errorCode);
122     void UpdateFailedFiles(int32_t fileType, const FileInfo &fileInfo, int32_t errorCode);
123     void UpdateFailedFiles(const std::vector<FileInfo> &fileInfos, int32_t errorCode);
124 
125     void UpdateDuplicateNumber(int32_t fileType);
126     void DeleteMoveFailedData(std::vector<std::string> &moveFailedData);
127     void MoveMigrateFile(std::vector<FileInfo> &fileInfos, int32_t &fileMoveCount, int32_t &videoFileMoveCount,
128         int32_t sceneCode);
129 
130     virtual bool RestoreLcdAndThumbFromCloud(const FileInfo &fileInfo, int32_t type, int32_t sceneCode);
131     virtual bool RestoreLcdAndThumbFromKvdb(const FileInfo &fileInfo, int32_t type, int32_t sceneCode);
132     std::string GetThumbFile(const FileInfo &fileInfo, int32_t type, int32_t sceneCode);
133 
134     int32_t BatchCreateDentryFile(std::vector<FileInfo> &fileInfos, std::vector<std::string> &failCloudIds,
135         std::string fileType);
136     int32_t SetVisiblePhoto(std::vector<FileInfo> &fileInfos);
137     void HandleFailData(std::vector<FileInfo> &fileInfos, std::vector<std::string> &failCloudIds,
138         std::string fileType);
139     virtual void MoveMigrateCloudFile(std::vector<FileInfo> &fileInfos, int32_t &fileMoveCount,
140         int32_t &videoFileMoveCount, int32_t sceneCode);
141     void SetParameterForClone();
142     void StopParameterForClone();
143     virtual void InsertPhotoRelated(std::vector<FileInfo> &fileInfos, int32_t sourceType);
144     void UpdateLcdVisibleColumn(const FileInfo &fileInfo);
145     bool NeedBatchQueryPhoto(const std::vector<FileInfo> &fileInfos, NeedQueryMap &needQueryMap);
146     bool NeedBatchQueryPhotoForPhotoMap(const std::vector<FileInfo> &fileInfos, NeedQueryMap &needQueryMap);
147     bool NeedQuery(const FileInfo &fileInfo, const NeedQueryMap &needQueryMap);
148     bool NeedQueryByPhotoRelatedType(const FileInfo &fileInfo, PhotoRelatedType photoRelatedType,
149         const std::unordered_set<std::string> &needQuerySet);
150     int32_t GetUniqueId(int32_t fileType);
151     int32_t IsFileValid(FileInfo &fileInfo, const int32_t sceneCode);
152     void CreateDir(std::string &dir);
153     void RecursiveCreateDir(std::string &relativePath, std::string &suffix);
154     SubProcessInfo GetSubProcessInfo(const std::string &type);
155     void UpdateProcessedNumber(const std::atomic<int32_t> &processStatus, std::atomic<uint64_t> &processedNumber,
156         const std::atomic<uint64_t> &totalNumber);
157     nlohmann::json GetSubProcessInfoJson(const std::string &type, const SubProcessInfo &subProcessInfo);
158     void UpdateDatabase();
159     void UpdatePhotoAlbumDateModified(const std::vector<std::string> &albumIds, const std::string &tableName);
160     void GetUpdateTotalCount();
161     void GetUpdateAllAlbumsCount();
162     std::string GetUpgradeEnhance();
163     void ProcessBurstPhotos();
164     void GetUpdateUniqueNumberCount();
165     void RestoreThumbnail();
166     std::string GetRestoreTotalInfo();
167     virtual int32_t GetNoNeedMigrateCount();
168     bool ExtraCheckForCloneSameFile(FileInfo &fileInfo, PhotosDao::PhotosRowData &rowData);
169     void UpdatePhotosByFileInfoMap(std::shared_ptr<NativeRdb::RdbStore> mediaLibraryRdb,
170         std::vector<FileInfo>& fileInfos);
171     int32_t RemoveDentryFileWithConflict(const FileInfo &fileInfo);
172     int32_t GetRestoreMode();
173     uint64_t GetNotFoundNumber();
174     bool IsCloudRestoreSatisfied();
175     void CheckAndDelete(NativeRdb::ValuesBucket &value, const std::string &column);
176     void InsertFileDuration(const std::unique_ptr<Metadata> &metadata, NativeRdb::ValuesBucket &value,
177         FileInfo &fileInfo);
178     void SetCoverPosition(const FileInfo &fileInfo, NativeRdb::ValuesBucket &value);
179     void AddToPhotoInfoMap(std::vector<FileInfo> &fileInfos);
180     void InsertDetailTime(NativeRdb::ValuesBucket &value, FileInfo &fileInfo);
181     virtual bool HasExThumbnail(const FileInfo &info);
182     bool WaitSouthDeviceExitTimeout();
183     std::string GetBackupExInfo();
184     virtual void BackupRelease();
185     virtual void RestoreRelease();
186     void SetParameterForBackup();
187     void StopParameterForBackup();
188     nlohmann::json GetBackupErrorInfoJson();
189     void SetParameterForRestore();
190     void StopParameterForRestore();
191 
192 protected:
193     std::atomic<uint64_t> migrateDatabaseNumber_{0};
194     std::atomic<uint64_t> migrateFileNumber_{0};
195     std::atomic<uint64_t> migrateVideoFileNumber_{0};
196     std::atomic<uint64_t> migrateAudioDatabaseNumber_{0};
197     std::atomic<uint64_t> totalCloudMetaNumber_{0};
198     std::atomic<uint64_t> successCloudMetaNumber_{0};
199     std::atomic<uint64_t> migrateAudioFileNumber_{0};
200     std::atomic<uint64_t> totalNumber_{0};
201     std::atomic<uint64_t> notFoundNumber_{0};
202     std::atomic<uint64_t> audioTotalNumber_{0};
203     std::atomic<uint64_t> updateTotalNumber_{0};
204     std::atomic<uint64_t> localLcdCount_{0};
205     std::atomic<uint64_t> localThumbnailCount_{0};
206     std::atomic<uint64_t> cloudLcdCount_{0};
207     std::atomic<uint64_t> cloudThumbnailCount_{0};
208     std::atomic<uint64_t> cloudMetaCount_{0};
209     std::atomic<uint64_t> thumbnailTotalNumber_{0};
210     std::atomic<uint64_t> otherTotalNumber_{0};
211     std::atomic<uint64_t> ongoingTotalNumber_{0};
212     std::atomic<uint64_t> updateProcessedNumber_{0};
213     std::atomic<uint64_t> thumbnailProcessedNumber_{0};
214     std::atomic<uint64_t> otherProcessedNumber_{0};
215     std::atomic<uint64_t> migratePhotoDuplicateNumber_{0};
216     std::atomic<uint64_t> migrateVideoDuplicateNumber_{0};
217     std::atomic<uint64_t> migrateAudioDuplicateNumber_{0};
218     std::atomic<uint64_t> migratePortraitPhotoNumber_{0};
219     std::atomic<uint64_t> migratePortraitFaceNumber_{0};
220     std::atomic<uint64_t> migratePortraitAlbumNumber_{0};
221     std::atomic<uint64_t> migratePortraitTotalTimeCost_{0};
222     std::atomic<uint32_t> imageNumber_{0};
223     std::atomic<uint32_t> videoNumber_{0};
224     std::atomic<uint64_t> migrateDatabaseMapNumber_{0};
225     std::atomic<uint32_t> audioNumber_{0};
226     std::atomic<uint64_t> rotateLcdMigrateFileNumber_{0};
227     std::atomic<uint64_t> rotateThmMigrateFileNumber_{0};
228     std::atomic<int32_t> updateProcessStatus_{ProcessStatus::STOP};
229     std::atomic<int32_t> otherProcessStatus_{ProcessStatus::STOP};
230     std::string dualDirName_ = "";
231     std::shared_ptr<NativeRdb::RdbStore> mediaLibraryRdb_;
232     std::string backupRestoreDir_;
233     std::string upgradeRestoreDir_;
234     std::string albumOdid_;
235     std::string dualDeviceSoftName_;
236     ffrt::mutex imageMutex_;
237     ffrt::mutex videoMutex_;
238     ffrt::mutex audioMutex_;
239     ffrt::mutex photoInfoMutex_;
240     ffrt::mutex photosInfoMutex_;
241     std::mutex failedFilesMutex_;
242     int32_t errorCode_{RestoreError::SUCCESS};
243     std::string errorInfo_;
244     std::unordered_map<std::string, std::unordered_map<std::string, FailedFileInfo>> failedFilesMap_;
245     int fileMinSize_ = 0;
246     int32_t sceneCode_ = DEFAULT_RESTORE_ID;
247     std::unordered_map<std::string, std::string> tagIdMap_;
248     std::unordered_map<std::string, int32_t> portraitAlbumIdMap_;
249     bool hasLowQualityImage_ = false;
250     std::string taskId_ = std::to_string(MediaFileUtils::UTCTimeSeconds());
251     TabOldPhotosRestore tabOldPhotosRestore_;
252     bool needReportFailed_ = false;
253     bool isAccountValid_ = false;
254     bool isSyncSwitchOn_ = false;
255     GeoKnowledgeRestore geoKnowledgeRestore_;
256     HighlightRestore highlightRestore_;
257     PhotosDataHandler photosDataHandler_;
258     int32_t restoreMode_;
259     int32_t syncSwitchType_;
260     size_t totalFailCount_{0};
261     std::unordered_map<int32_t, PhotoInfo> photoInfoMap_;
262 };
263 } // namespace Media
264 } // namespace OHOS
265 
266 #endif  // OHOS_MEDIA_BASE_RESTORE_H
267