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