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_UPGRADE_RESTORE_H 17 #define OHOS_MEDIA_UPGRADE_RESTORE_H 18 19 #include <libxml/tree.h> 20 #include <libxml/parser.h> 21 22 #include "backup_database_helper.h" 23 #include "base_restore.h" 24 #include "burst_key_generator.h" 25 #include "ffrt.h" 26 #include "ffrt_inner.h" 27 #include "photos_restore.h" 28 29 namespace OHOS { 30 namespace Media { 31 class UpgradeRestore : public BaseRestore { 32 public: 33 UpgradeRestore(const std::string &galleryAppName, const std::string &mediaAppName, int32_t sceneCode); 34 UpgradeRestore(const std::string &galleryAppName, const std::string &mediaAppName, int32_t sceneCode, 35 const std::string &dualDirName); 36 virtual ~UpgradeRestore() = default; 37 int32_t Init(const std::string &backupRestorePath, const std::string &upgradePath, bool isUpgrade) override; 38 std::vector<FileInfo> QueryFileInfos(int32_t offset); 39 std::vector<FileInfo> QueryCloudFileInfos(int32_t offset); 40 NativeRdb::ValuesBucket GetInsertValue(const FileInfo &fileInfo, const std::string &newPath, 41 int32_t sourceType) override; 42 std::vector<FileInfo> QueryFileInfosFromExternal(int32_t offset, int32_t maxId, bool isCamera); 43 std::vector<FileInfo> QueryAudioFileInfosFromAudio(int32_t offset); 44 int32_t QueryNotSyncTotalNumber(int32_t offset, bool isCamera); 45 void InitGarbageAlbum(); 46 void InheritManualCover(); 47 int32_t RecordAlbumCoverInfo(const std::shared_ptr<NativeRdb::ResultSet> &resultSet, 48 AlbumCoverInfo& albumCoverInfo); 49 void UpdatePhotoAlbumCoverUri(vector<AlbumCoverInfo>& albumCoverInfos); 50 void DeleteEmptyAlbums(); 51 52 protected: 53 virtual void RestoreAnalysisAlbum(); 54 55 int32_t GetHighlightCloudMediaCnt(); 56 void RestoreSmartAlbums(); 57 void RestoreHighlightAlbums(); 58 void RestorePhoto(void) override; 59 void RestoreAudio(void) override; 60 void HandleRestData(void) override; 61 bool ParseResultSet(const std::shared_ptr<NativeRdb::ResultSet> &resultSet, FileInfo &info, 62 std::string dbName = "") override; 63 bool ParseResultSetForAudio(const std::shared_ptr<NativeRdb::ResultSet> &resultSet, FileInfo &info) override; 64 bool NeedBatchQueryPhotoForPortrait(const std::vector<FileInfo> &fileInfos, NeedQueryMap &needQueryMap) override; 65 void InsertFaceAnalysisData(const std::vector<FileInfo> &fileInfos, const NeedQueryMap &needQueryMap, 66 int64_t &faceRowNum, int64_t &mapRowNum, int64_t &photoNum) override; 67 bool ParseResultSetFromExternal(const std::shared_ptr<NativeRdb::ResultSet> &resultSet, FileInfo &info, 68 int mediaType = DUAL_MEDIA_TYPE::IMAGE_TYPE); 69 bool ParseResultSetFromAudioDb(const std::shared_ptr<NativeRdb::ResultSet> &resultSet, FileInfo &info); 70 virtual bool ParseResultSetFromGallery(const std::shared_ptr<NativeRdb::ResultSet> &resultSet, FileInfo &info); 71 void RestoreFromGallery(); 72 void RestoreCloudFromGallery(); 73 void RestoreFromExternal(bool isCamera); 74 void RestoreAudioFromFile(); 75 bool IsValidDir(const std::string &path); 76 void RestoreBatch(int32_t offset); 77 void RestoreBatchForCloud(int32_t offset); 78 void RestoreAudioBatch(int32_t offset); 79 void RestoreExternalBatch(int32_t offset, int32_t maxId, bool isCamera, int32_t type); 80 bool ConvertPathToRealPath(const std::string &srcPath, const std::string &prefix, std::string &newPath, 81 std::string &relativePath) override; 82 void AnalyzeSource() override; 83 void AnalyzeGalleryErrorSource(); 84 void AnalyzeGalleryDuplicateData(); 85 void AnalyzeGallerySource(); 86 int32_t ParseXml(std::string path); 87 int StringToInt(const std::string& str); 88 int32_t InitDbAndXml(std::string xmlPath, bool isUpgrade); 89 int32_t HandleXmlNode(xmlNodePtr cur); 90 virtual bool ConvertPathToRealPath(const std::string &srcPath, const std::string &prefix, std::string &newPath, 91 std::string &relativePath, FileInfo &fileInfo); 92 bool HasSameFileForDualClone(FileInfo &fileInfo) override; 93 void RestoreFromGalleryPortraitAlbum(); 94 int32_t QueryPortraitAlbumTotalNumber(); 95 std::vector<PortraitAlbumInfo> QueryPortraitAlbumInfos(int32_t offset, 96 std::vector<std::string>& tagNameToDeleteSelection); 97 bool ParsePortraitAlbumResultSet(const std::shared_ptr<NativeRdb::ResultSet> &resultSet, 98 PortraitAlbumInfo &portraitAlbumInfo); 99 bool SetAttributes(PortraitAlbumInfo &portraitAlbumInfo); 100 void InsertPortraitAlbum(std::vector<PortraitAlbumInfo> &portraitAlbumInfos); 101 int32_t InsertPortraitAlbumByTable(std::vector<PortraitAlbumInfo> &portraitAlbumInfos, bool isAlbum); 102 std::vector<NativeRdb::ValuesBucket> GetInsertValues(std::vector<PortraitAlbumInfo> &portraitAlbumInfos, 103 bool isAlbum); 104 NativeRdb::ValuesBucket GetInsertValue(const PortraitAlbumInfo &portraitAlbumInfo, bool isAlbum); 105 void BatchQueryAlbum(std::vector<PortraitAlbumInfo> &portraitAlbumInfos); 106 void SetHashReference(const std::vector<FileInfo> &fileInfos, const NeedQueryMap &needQueryMap, 107 std::string &hashSelection, std::unordered_map<std::string, FileInfo> &fileInfoMap); 108 int32_t QueryFaceTotalNumber(const std::string &hashSelection); 109 std::vector<FaceInfo> QueryFaceInfos(const std::string &hashSelection, 110 const std::unordered_map<std::string, FileInfo> &fileInfoMap, int32_t offset, 111 std::unordered_set<std::string> &excludedFiles); 112 bool ParseFaceResultSet(const std::shared_ptr<NativeRdb::ResultSet> &resultSet, FaceInfo &faceInfo); 113 bool SetAttributes(FaceInfo &faceInfo, const std::unordered_map<std::string, FileInfo> &fileInfoMap); 114 int32_t InsertFaceAnalysisDataByTable(const std::vector<FaceInfo> &faceInfos, bool isMap, 115 const std::unordered_set<std::string> &excludedFiles); 116 std::vector<NativeRdb::ValuesBucket> GetInsertValues(const std::vector<FaceInfo> &faceInfos, bool isMap, 117 const std::unordered_set<std::string> &excludedFiles); 118 NativeRdb::ValuesBucket GetInsertValue(const FaceInfo &faceInfo, bool isMap); 119 void UpdateFilesWithFace(std::unordered_set<std::string> &filesWithFace, const std::vector<FaceInfo> &faceInfos); 120 void UpdateFaceAnalysisStatus(); 121 void UpdateDualCloneFaceAnalysisStatus(); 122 bool HasLowQualityImage(); 123 std::string CheckInvalidFile(const FileInfo &fileInfo, int32_t errCode) override; 124 int32_t GetNoNeedMigrateCount() override; 125 bool IsBasicInfoValid(const std::shared_ptr<NativeRdb::ResultSet> &resultSet, FileInfo &info, 126 const std::string &dbName); 127 std::string CheckGalleryDbIntegrity(); 128 void RestorePhotoInner(); 129 void AddToGalleryFailedOffsets(int32_t offset); 130 void AddToExternalFailedOffsets(int32_t offset); 131 void ProcessGalleryFailedOffsets(); 132 void ProcessCloudGalleryFailedOffsets(); 133 void ProcessExternalFailedOffsets(int32_t maxId, bool isCamera, int32_t type); 134 void SetCloneParameterAndStopSync(); 135 int32_t InitDb(bool isUpgrade); 136 std::vector<int32_t> GetCloudPhotoMinIds(); 137 std::vector<int32_t> GetLocalPhotoMinIds(); 138 void SetOrientationAndExifRotate(FileInfo &info, NativeRdb::ValuesBucket &value, 139 std::unique_ptr<Metadata> &data) override; 140 141 protected: 142 std::shared_ptr<NativeRdb::RdbStore> galleryRdb_; 143 std::shared_ptr<NativeRdb::RdbStore> externalRdb_; 144 std::shared_ptr<NativeRdb::RdbStore> audioRdb_; 145 BurstKeyGenerator burstKeyGenerator_; 146 std::string galleryDbPath_; 147 std::string filePath_; 148 std::string externalDbPath_; 149 std::string appDataPath_; 150 std::string galleryAppName_; 151 std::string mediaAppName_; 152 std::string audioAppName_; 153 std::set<std::string> cacheSet_; 154 std::unordered_map<std::string, std::string> nickMap_; 155 std::unordered_map<std::string, GalleryAlbumInfo> galleryAlbumMap_; 156 std::vector<AlbumInfo> photoAlbumInfos_; 157 std::string audioDbPath_; 158 std::string hiddenAlbumBucketId_; 159 int32_t mediaScreenreCorderAlbumId_{-1}; 160 bool shouldIncludeSd_{false}; 161 PhotoAlbumRestore photoAlbumRestore_; 162 PhotosRestore photosRestore_; 163 BackupDatabaseHelper backupDatabaseHelper_; 164 std::vector<int> galleryFailedOffsets_; 165 std::vector<int> externalFailedOffsets_; 166 ffrt::mutex galleryFailedMutex_; 167 ffrt::mutex externalFailedMutex_; 168 int32_t maxId_{-1}; 169 int64_t maxAnalysisAlbumId_ {0}; 170 171 private: 172 void BatchDeleteEmptyAlbums(const std::vector<int32_t> &batchAlbumIds, int32_t &deleteRows); 173 }; 174 } // namespace Media 175 } // namespace OHOS 176 177 #endif // OHOS_MEDIA_UPGRADE_RESTORE_H 178