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