• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "photos_restore.h"
26 
27 namespace OHOS {
28 namespace Media {
29 class UpgradeRestore : public BaseRestore {
30 public:
31     UpgradeRestore(const std::string &galleryAppName, const std::string &mediaAppName, int32_t sceneCode);
32     UpgradeRestore(const std::string &galleryAppName, const std::string &mediaAppName, int32_t sceneCode,
33         const std::string &dualDirName);
34     virtual ~UpgradeRestore() = default;
35     int32_t Init(const std::string &backupRestorePath, const std::string &upgradePath, bool isUpgrade) override;
36     std::vector<FileInfo> QueryFileInfos(int32_t offset);
37     std::vector<FileInfo> QueryCloudFileInfos(int32_t offset);
38     NativeRdb::ValuesBucket GetInsertValue(const FileInfo &fileInfo, const std::string &newPath,
39         int32_t sourceType) override;
40     std::vector<FileInfo> QueryFileInfosFromExternal(int32_t offset, int32_t maxId, bool isCamera);
41     std::vector<FileInfo> QueryAudioFileInfosFromAudio(int32_t offset);
42     int32_t QueryNotSyncTotalNumber(int32_t offset, bool isCamera);
43     void InitGarbageAlbum();
44 
45 private:
46     int32_t GetHighlightCloudMediaCnt();
47     void RestoreHighlightAlbums();
48     void RestorePhoto(void) override;
49     void RestoreAudio(void) override;
50     void HandleRestData(void) override;
51     bool ParseResultSet(const std::shared_ptr<NativeRdb::ResultSet> &resultSet, FileInfo &info,
52         std::string dbName = "") override;
53     bool ParseResultSetForAudio(const std::shared_ptr<NativeRdb::ResultSet> &resultSet, FileInfo &info) override;
54     bool NeedBatchQueryPhotoForPortrait(const std::vector<FileInfo> &fileInfos, NeedQueryMap &needQueryMap) override;
55     void InsertFaceAnalysisData(const std::vector<FileInfo> &fileInfos, const NeedQueryMap &needQueryMap,
56         int64_t &faceRowNum, int64_t &mapRowNum, int64_t &photoNum) override;
57     bool ParseResultSetFromExternal(const std::shared_ptr<NativeRdb::ResultSet> &resultSet, FileInfo &info,
58         int mediaType = DUAL_MEDIA_TYPE::IMAGE_TYPE);
59     bool ParseResultSetFromAudioDb(const std::shared_ptr<NativeRdb::ResultSet> &resultSet, FileInfo &info);
60     bool ParseResultSetFromGallery(const std::shared_ptr<NativeRdb::ResultSet> &resultSet, FileInfo &info);
61     void RestoreFromGallery();
62     void RestoreCloudFromGallery();
63     void RestoreFromExternal(bool isCamera);
64     void RestoreAudioFromFile();
65     bool IsValidDir(const std::string &path);
66     void RestoreBatch(int32_t offset);
67     void RestoreBatchForCloud(int32_t offset);
68     void RestoreAudioBatch(int32_t offset);
69     void RestoreExternalBatch(int32_t offset, int32_t maxId, bool isCamera, int32_t type);
70     bool ConvertPathToRealPath(const std::string &srcPath, const std::string &prefix, std::string &newPath,
71         std::string &relativePath) override;
72     void AnalyzeSource() override;
73     void AnalyzeGalleryErrorSource();
74     void AnalyzeGalleryDuplicateData();
75     void AnalyzeGallerySource();
76     int32_t ParseXml(std::string path);
77     int StringToInt(const std::string& str);
78     int32_t InitDbAndXml(std::string xmlPath, bool isUpgrade);
79     int32_t HandleXmlNode(xmlNodePtr cur);
80     bool ConvertPathToRealPath(const std::string &srcPath, const std::string &prefix, std::string &newPath,
81         std::string &relativePath, FileInfo &fileInfo);
82     bool HasSameFileForDualClone(FileInfo &fileInfo) override;
83     void RestoreFromGalleryPortraitAlbum();
84     int32_t QueryPortraitAlbumTotalNumber();
85     std::vector<PortraitAlbumInfo> QueryPortraitAlbumInfos(int32_t offset,
86         std::vector<std::string>& tagNameToDeleteSelection);
87     bool ParsePortraitAlbumResultSet(const std::shared_ptr<NativeRdb::ResultSet> &resultSet,
88         PortraitAlbumInfo &portraitAlbumInfo);
89     bool SetAttributes(PortraitAlbumInfo &portraitAlbumInfo);
90     void InsertPortraitAlbum(std::vector<PortraitAlbumInfo> &portraitAlbumInfos);
91     int32_t InsertPortraitAlbumByTable(std::vector<PortraitAlbumInfo> &portraitAlbumInfos, bool isAlbum);
92     std::vector<NativeRdb::ValuesBucket> GetInsertValues(std::vector<PortraitAlbumInfo> &portraitAlbumInfos,
93         bool isAlbum);
94     NativeRdb::ValuesBucket GetInsertValue(const PortraitAlbumInfo &portraitAlbumInfo, bool isAlbum);
95     void BatchQueryAlbum(std::vector<PortraitAlbumInfo> &portraitAlbumInfos);
96     void SetHashReference(const std::vector<FileInfo> &fileInfos, const NeedQueryMap &needQueryMap,
97         std::string &hashSelection, std::unordered_map<std::string, FileInfo> &fileInfoMap);
98     int32_t QueryFaceTotalNumber(const std::string &hashSelection);
99     std::vector<FaceInfo> QueryFaceInfos(const std::string &hashSelection,
100         const std::unordered_map<std::string, FileInfo> &fileInfoMap, int32_t offset,
101         std::unordered_set<std::string> &excludedFiles);
102     bool ParseFaceResultSet(const std::shared_ptr<NativeRdb::ResultSet> &resultSet, FaceInfo &faceInfo);
103     bool SetAttributes(FaceInfo &faceInfo, const std::unordered_map<std::string, FileInfo> &fileInfoMap);
104     int32_t InsertFaceAnalysisDataByTable(const std::vector<FaceInfo> &faceInfos, bool isMap,
105         const std::unordered_set<std::string> &excludedFiles);
106     std::vector<NativeRdb::ValuesBucket> GetInsertValues(const std::vector<FaceInfo> &faceInfos, bool isMap,
107         const std::unordered_set<std::string> &excludedFiles);
108     NativeRdb::ValuesBucket GetInsertValue(const FaceInfo &faceInfo, bool isMap);
109     void UpdateFilesWithFace(std::unordered_set<std::string> &filesWithFace, const std::vector<FaceInfo> &faceInfos);
110     void UpdateFaceAnalysisStatus();
111     void UpdateDualCloneFaceAnalysisStatus();
112     bool HasLowQualityImage();
113     std::string CheckInvalidFile(const FileInfo &fileInfo, int32_t errCode) override;
114     int32_t GetNoNeedMigrateCount() override;
115     bool IsBasicInfoValid(const std::shared_ptr<NativeRdb::ResultSet> &resultSet, FileInfo &info,
116         const std::string &dbName);
117     std::string CheckGalleryDbIntegrity();
118     void RestorePhotoInner();
119     void PrcoessBurstPhotos();
120 
121 private:
122     std::shared_ptr<NativeRdb::RdbStore> galleryRdb_;
123     std::shared_ptr<NativeRdb::RdbStore> externalRdb_;
124     std::shared_ptr<NativeRdb::RdbStore> audioRdb_;
125     BurstKeyGenerator burstKeyGenerator_;
126     std::string galleryDbPath_;
127     std::string filePath_;
128     std::string externalDbPath_;
129     std::string appDataPath_;
130     std::string galleryAppName_;
131     std::string mediaAppName_;
132     std::string audioAppName_;
133     std::set<std::string> cacheSet_;
134     std::unordered_map<std::string, std::string> nickMap_;
135     std::unordered_map<std::string, GalleryAlbumInfo> galleryAlbumMap_;
136     std::vector<AlbumInfo> photoAlbumInfos_;
137     std::string audioDbPath_;
138     std::string hiddenAlbumBucketId_;
139     int32_t mediaScreenreCorderAlbumId_{-1};
140     bool shouldIncludeSd_{false};
141     PhotoAlbumRestore photoAlbumRestore_;
142     PhotosRestore photosRestore_;
143     BackupDatabaseHelper backupDatabaseHelper_;
144     std::vector<int> galleryFailedOffsets;
145     std::vector<int> externalFailedOffsets;
146     int32_t maxId_{-1};
147 };
148 } // namespace Media
149 } // namespace OHOS
150 
151 #endif  // OHOS_MEDIA_UPGRADE_RESTORE_H
152