1 /*
2  * Copyright (C) 2023-2024 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 "medialibrary_rdb_transaction.h"
25 #include "nlohmann/json.hpp"
26 #include "rdb_helper.h"
27 #include "result_set.h"
28 #include "media_file_utils.h"
29 #include "tab_old_photos_restore.h"
30 
31 namespace OHOS {
32 namespace Media {
33 class BaseRestore {
34 public:
35     BaseRestore() = default;
36     virtual ~BaseRestore() = default;
37     virtual void StartRestore(const std::string &backupRetorePath, const std::string &upgradePath);
38     virtual int32_t Init(const std::string &backupRetorePath, const std::string &upgradePath, bool isUpgrade) = 0;
39     virtual NativeRdb::ValuesBucket GetInsertValue(const FileInfo &fileInfo, const std::string &newPath,
40         int32_t sourceType) = 0;
41     virtual std::string GetBackupInfo();
42     void StartRestoreEx(const std::string &backupRetorePath, const std::string &upgradePath,
43         std::string &restoreExInfo);
44     std::string GetRestoreExInfo();
45     void ReportPortraitStat(int32_t sceneCode);
46     std::string GetProgressInfo();
47     virtual void StartBackup();
48 
49 protected:
50     int32_t Init(void);
51 
52     virtual void RestorePhoto(void) = 0;
53     virtual void RestoreAudio(void) = 0;
54     virtual void HandleRestData(void) = 0;
55 
56     virtual bool ParseResultSet(const std::shared_ptr<NativeRdb::ResultSet> &resultSet, FileInfo &info,
57         std::string dbName = "") = 0;
58     virtual bool ParseResultSetForAudio(const std::shared_ptr<NativeRdb::ResultSet> &resultSet, FileInfo &info) = 0;
59     virtual void AnalyzeSource() = 0;
60     virtual bool ConvertPathToRealPath(const std::string &srcPath, const std::string &prefix, std::string &newPath,
61         std::string &relativePath);
62     virtual bool NeedBatchQueryPhotoForPortrait(const std::vector<FileInfo> &fileInfos, NeedQueryMap &needQueryMap);
63     virtual void InsertFaceAnalysisData(const std::vector<FileInfo> &fileInfos, const NeedQueryMap &needQueryMap,
64         int64_t &faceRowNum, int64_t &mapRowNum, int64_t &photoNum);
65     virtual void NotifyAlbum();
66     virtual std::string CheckInvalidFile(const FileInfo &fileInfo, int32_t errCode);
67     std::vector<NativeRdb::ValuesBucket> GetInsertValues(int32_t sceneCode, std::vector<FileInfo> &fileInfos,
68         int32_t sourceType);
69     int32_t CopyFile(const std::string &srcFile, const std::string &dstFile) const;
70     int32_t MoveFile(const std::string &srcFile, const std::string &dstFile) const;
71     std::shared_ptr<NativeRdb::ResultSet> QuerySql(const std::string &sql,
72         const std::vector<std::string> &selectionArgs = std::vector<std::string>()) const;
73     int InsertPhoto(int32_t sceneCode, std::vector<FileInfo> &fileInfos, int32_t sourceType);
74     void InsertAudio(int32_t sceneCode, std::vector<FileInfo> &fileInfos);
75     void SetValueFromMetaData(FileInfo &info, NativeRdb::ValuesBucket &value);
76     int32_t BatchInsertWithRetry(const std::string &tableName, std::vector<NativeRdb::ValuesBucket> &value,
77         int64_t &rowNum);
78     int32_t MoveDirectory(const std::string &srcDir, const std::string &dstDir, bool deleteOriginalFile = true) const;
79     bool IsSameAudioFile(const std::shared_ptr<NativeRdb::RdbStore> &rdbStore, const std::string &tableName,
80         FileInfo &fileInfo);
81     bool HasSameAudioFile(const std::shared_ptr<NativeRdb::RdbStore> &rdbStore, const std::string &tableName,
82         FileInfo &fileInfo);
HasSameFileForDualClone(FileInfo & fileInfo)83     virtual bool HasSameFileForDualClone(FileInfo &fileInfo)
84     {
85         return false;
86     }
87     void InsertPhotoMap(std::vector<FileInfo> &fileInfos, int64_t &mapRowNum);
88     void BatchQueryPhoto(std::vector<FileInfo> &fileInfos, bool isFull, const NeedQueryMap &needQueryMap);
89     void BatchInsertMap(const std::vector<FileInfo> &fileInfos, int64_t &totalRowNum);
90     nlohmann::json GetErrorInfoJson();
91     nlohmann::json GetCountInfoJson(const std::vector<std::string> &countInfoTypes);
92     SubCountInfo GetSubCountInfo(const std::string &type);
93     std::unordered_map<std::string, FailedFileInfo> GetFailedFiles(const std::string &type);
94     nlohmann::json GetSubCountInfoJson(const std::string &type, const SubCountInfo &subCountInfo, size_t &limit);
95     void SetErrorCode(int32_t errorCode);
96     void UpdateFailedFileByFileType(int32_t fileType, const FileInfo &fileInfo, int32_t errorCode);
97     void UpdateFailedFiles(int32_t fileType, const FileInfo &fileInfo, int32_t errorCode);
98     void UpdateFailedFiles(const std::vector<FileInfo> &fileInfos, int32_t errorCode);
99     void UpdateDuplicateNumber(int32_t fileType);
100     void DeleteMoveFailedData(std::vector<std::string> &moveFailedData);
101     void MoveMigrateFile(std::vector<FileInfo> &fileInfos, int32_t &fileMoveCount, int32_t &videoFileMoveCount,
102         int32_t sceneCode);
103     void SetParameterForClone();
104     void StopParameterForClone(int32_t sceneCode);
105     void InsertPhotoRelated(std::vector<FileInfo> &fileInfos, int32_t sourceType);
106     bool NeedBatchQueryPhoto(const std::vector<FileInfo> &fileInfos, NeedQueryMap &needQueryMap);
107     bool NeedBatchQueryPhotoForPhotoMap(const std::vector<FileInfo> &fileInfos, NeedQueryMap &needQueryMap);
108     bool NeedQuery(const FileInfo &fileInfo, const NeedQueryMap &needQueryMap);
109     bool NeedQueryByPhotoRelatedType(const FileInfo &fileInfo, PhotoRelatedType photoRelatedType,
110         const std::unordered_set<std::string> &needQuerySet);
111     int32_t GetUniqueId(int32_t fileType);
112     int32_t IsFileValid(FileInfo &fileInfo, const int32_t sceneCode);
113     void CreateDir(std::string &dir);
114     void RecursiveCreateDir(std::string &relativePath, std::string &suffix);
115     SubProcessInfo GetSubProcessInfo(const std::string &type);
116     void UpdateProcessedNumber(const std::atomic<int32_t> &processStatus, std::atomic<uint64_t> &processedNumber,
117         const std::atomic<uint64_t> &totalNumber);
118     nlohmann::json GetSubProcessInfoJson(const std::string &type, const SubProcessInfo &subProcessInfo);
119     void UpdateDatabase();
120     void GetUpdateTotalCount();
121     void GetUpdateAllAlbumsCount();
122     void GetUpdateUniqueNumberCount();
123     void RestoreThumbnail();
124     std::string GetRestoreTotalInfo();
125     virtual int32_t GetNoNeedMigrateCount();
126     void UpdatePhotosByFileInfoMap(std::shared_ptr<NativeRdb::RdbStore> mediaLibraryRdb,
127         const std::vector<FileInfo>& fileInfos);
128 
129 protected:
130     std::atomic<uint64_t> migrateDatabaseNumber_{0};
131     std::atomic<uint64_t> migrateFileNumber_{0};
132     std::atomic<uint64_t> migrateVideoFileNumber_{0};
133     std::atomic<uint64_t> migrateAudioDatabaseNumber_{0};
134     std::atomic<uint64_t> migrateAudioFileNumber_{0};
135     std::atomic<uint64_t> totalNumber_{0};
136     std::atomic<uint64_t> audioTotalNumber_{0};
137     std::atomic<uint64_t> updateTotalNumber_{0};
138     std::atomic<uint64_t> otherTotalNumber_{0};
139     std::atomic<uint64_t> ongoingTotalNumber_{0};
140     std::atomic<uint64_t> updateProcessedNumber_{0};
141     std::atomic<uint64_t> otherProcessedNumber_{0};
142     std::atomic<uint64_t> migratePhotoDuplicateNumber_{0};
143     std::atomic<uint64_t> migrateVideoDuplicateNumber_{0};
144     std::atomic<uint64_t> migrateAudioDuplicateNumber_{0};
145     std::atomic<uint64_t> migratePortraitPhotoNumber_{0};
146     std::atomic<uint64_t> migratePortraitFaceNumber_{0};
147     std::atomic<uint64_t> migratePortraitAlbumNumber_{0};
148     std::atomic<uint64_t> migratePortraitTotalTimeCost_{0};
149     std::atomic<uint32_t> imageNumber_{0};
150     std::atomic<uint32_t> videoNumber_{0};
151     std::atomic<uint64_t> migrateDatabaseMapNumber_{0};
152     std::atomic<uint32_t> audioNumber_{0};
153     std::atomic<int32_t> updateProcessStatus_{ProcessStatus::STOP};
154     std::atomic<int32_t> otherProcessStatus_{ProcessStatus::STOP};
155     std::string dualDirName_ = "";
156     std::shared_ptr<NativeRdb::RdbStore> mediaLibraryRdb_;
157     std::string backupRestoreDir_;
158     std::mutex imageMutex_;
159     std::mutex videoMutex_;
160     std::mutex audioMutex_;
161     std::mutex failedFilesMutex_;
162     int32_t errorCode_{RestoreError::SUCCESS};
163     std::string errorInfo_;
164     std::unordered_map<std::string, std::unordered_map<std::string, FailedFileInfo>> failedFilesMap_;
165     int fileMinSize_ = 0;
166     int32_t sceneCode_ = DEFAULT_RESTORE_ID;
167     std::unordered_map<std::string, std::string> tagIdMap_;
168     std::unordered_map<std::string, int32_t> portraitAlbumIdMap_;
169     bool hasLowQualityImage_ = false;
170     std::string taskId_ = std::to_string(MediaFileUtils::UTCTimeSeconds());
171     TabOldPhotosRestore tabOldPhotosRestore_;
172     bool needReportFailed_ = false;
173 };
174 } // namespace Media
175 } // namespace OHOS
176 
177 #endif  // OHOS_MEDIA_BASE_RESTORE_H
178