• 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 "base_restore.h"
23 #include "burst_key_generator.h"
24 #include "photos_restore.h"
25 
26 namespace OHOS {
27 namespace Media {
28 class UpgradeRestore : public BaseRestore {
29 public:
30     UpgradeRestore(const std::string &galleryAppName, const std::string &mediaAppName, int32_t sceneCode);
31     UpgradeRestore(const std::string &galleryAppName, const std::string &mediaAppName, int32_t sceneCode,
32         const std::string &dualDirName);
33     virtual ~UpgradeRestore() = default;
34     int32_t Init(const std::string &backupRestorePath, const std::string &upgradePath, bool isUpgrade) override;
35     std::vector<FileInfo> QueryFileInfos(int32_t offset);
36     NativeRdb::ValuesBucket GetInsertValue(const FileInfo &fileInfo, const std::string &newPath,
37         int32_t sourceType) override;
38     std::vector<FileInfo> QueryFileInfosFromExternal(int32_t offset, int32_t maxId, bool isCamera);
39     std::vector<FileInfo> QueryAudioFileInfosFromAudio(int32_t offset);
40     int32_t QueryNotSyncTotalNumber(int32_t offset, bool isCamera);
41     void InitGarbageAlbum();
42     void HandleClone();
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     void HandleCloneBatch(int32_t offset, int32_t maxId);
72     void UpdateCloneWithRetry(const std::shared_ptr<NativeRdb::ResultSet> &resultSet, int32_t &number);
73     int32_t InitDbAndXml(std::string xmlPath, bool isUpgrade);
74     int32_t ParseXml(const std::string &path);
75     int StringToInt(const std::string& str);
76     int32_t HandleXmlNode(xmlNodePtr cur);
77     bool ConvertPathToRealPath(const std::string &srcPath, const std::string &prefix, std::string &newPath,
78         std::string &relativePath, FileInfo &fileInfo);
79     void RestoreFromGalleryPortraitAlbum();
80     bool HasSameFileForDualClone(FileInfo &fileInfo) override;
81     int32_t QueryPortraitAlbumTotalNumber();
82     std::vector<PortraitAlbumInfo> QueryPortraitAlbumInfos(int32_t offset,
83         std::vector<std::string>& tagNameToDeleteSelection);
84     bool ParsePortraitAlbumResultSet(const std::shared_ptr<NativeRdb::ResultSet> &resultSet,
85         PortraitAlbumInfo &portraitAlbumInfo);
86     bool SetAttributes(PortraitAlbumInfo &portraitAlbumInfo);
87     void InsertPortraitAlbum(std::vector<PortraitAlbumInfo> &portraitAlbumInfos);
88     int32_t InsertPortraitAlbumByTable(std::vector<PortraitAlbumInfo> &portraitAlbumInfos, bool isAlbum);
89     std::vector<NativeRdb::ValuesBucket> GetInsertValues(std::vector<PortraitAlbumInfo> &portraitAlbumInfos,
90         bool isAlbum);
91     NativeRdb::ValuesBucket GetInsertValue(const PortraitAlbumInfo &portraitAlbumInfo, bool isAlbum);
92     void BatchQueryAlbum(std::vector<PortraitAlbumInfo> &portraitAlbumInfos);
93     void SetHashReference(const std::vector<FileInfo> &fileInfos, const NeedQueryMap &needQueryMap,
94         std::string &hashSelection, std::unordered_map<std::string, FileInfo> &fileInfoMap);
95     int32_t QueryFaceTotalNumber(const std::string &hashSelection);
96     std::vector<FaceInfo> QueryFaceInfos(const std::string &hashSelection,
97         const std::unordered_map<std::string, FileInfo> &fileInfoMap, int32_t offset,
98         std::unordered_set<std::string> &excludedFiles);
99     bool ParseFaceResultSet(const std::shared_ptr<NativeRdb::ResultSet> &resultSet, FaceInfo &faceInfo);
100     bool SetAttributes(FaceInfo &faceInfo, const std::unordered_map<std::string, FileInfo> &fileInfoMap);
101     int32_t InsertFaceAnalysisDataByTable(const std::vector<FaceInfo> &faceInfos, bool isMap,
102         const std::unordered_set<std::string> &excludedFiles);
103     std::vector<NativeRdb::ValuesBucket> GetInsertValues(const std::vector<FaceInfo> &faceInfos, bool isMap,
104         const std::unordered_set<std::string> &excludedFiles);
105     NativeRdb::ValuesBucket GetInsertValue(const FaceInfo &faceInfo, bool isMap);
106     void UpdateFilesWithFace(std::unordered_set<std::string> &filesWithFace, const std::vector<FaceInfo> &faceInfos);
107     bool HasLowQualityImage();
108     void UpdateFaceAnalysisStatus();
109     void UpdateDualCloneFaceAnalysisStatus();
110 
111 private:
112     std::shared_ptr<NativeRdb::RdbStore> galleryRdb_;
113     std::shared_ptr<NativeRdb::RdbStore> externalRdb_;
114     std::shared_ptr<NativeRdb::RdbStore> audioRdb_;
115     BurstKeyGenerator burstKeyGenerator_;
116     std::string galleryDbPath_;
117     std::string filePath_;
118     std::string externalDbPath_;
119     std::string appDataPath_;
120     std::string galleryAppName_;
121     std::string mediaAppName_;
122     std::string audioAppName_;
123     std::set<std::string> cacheSet_;
124     std::unordered_map<std::string, std::string> nickMap_;
125     std::unordered_map<std::string, GalleryAlbumInfo> galleryAlbumMap_;
126     std::vector<AlbumInfo> photoAlbumInfos_;
127     std::string audioDbPath_;
128     std::string hiddenAlbumBucketId_;
129     int32_t mediaScreenreCorderAlbumId_{-1};
130     bool shouldIncludeSd_{false};
131     PhotoAlbumRestore photoAlbumRestore_;
132     PhotosRestore photosRestore_;
133 };
134 } // namespace Media
135 } // namespace OHOS
136 
137 #endif  // OHOS_MEDIA_UPGRADE_RESTORE_H
138