• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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 #ifndef OHOS_MEDIA_PHOTOS_RESTORE
16 #define OHOS_MEDIA_PHOTOS_RESTORE
17 
18 #include <mutex>
19 #include <string>
20 #include <sstream>
21 #include <unordered_set>
22 
23 #include "rdb_store.h"
24 #include "photo_album_restore.h"
25 #include "photos_dao.h"
26 #include "photo_album_dao.h"
27 #include "gallery_media_dao.h"
28 
29 namespace OHOS::Media {
30 class PhotosRestore {
31 public:
32     /**
33      * @brief Restore Start Event Handler.
34      */
OnStart(std::shared_ptr<NativeRdb::RdbStore> mediaLibraryRdb,std::shared_ptr<NativeRdb::RdbStore> galleryRdb)35     void OnStart(std::shared_ptr<NativeRdb::RdbStore> mediaLibraryRdb, std::shared_ptr<NativeRdb::RdbStore> galleryRdb)
36     {
37         this->SetMediaLibraryRdb(mediaLibraryRdb).SetGalleryRdb(galleryRdb).LoadBasicInfo();
38     }
39 
40     /**
41      * @brief Load the PhotoAlbum cache of target media_library.db for quick access.
42      */
LoadPhotoAlbums()43     void LoadPhotoAlbums()
44     {
45         this->photoAlbumDao_.LoadPhotoAlbums();
46     }
47 
FindSameFile(const FileInfo & fileInfo)48     PhotosDao::PhotosRowData FindSameFile(const FileInfo &fileInfo)
49     {
50         int32_t maxFileId = this->photosBasicInfo_.maxFileId;
51         return this->photosDao_.FindSameFile(fileInfo, maxFileId);
52     }
53 
54     std::shared_ptr<NativeRdb::ResultSet> GetGalleryMedia(
55         int32_t offset, int pageSize, bool shouldIncludeSd, bool hasLowQualityImage);
56     std::shared_ptr<NativeRdb::ResultSet> GetCloudGalleryMedia(
57         int32_t offset, int pageSize, bool shouldIncludeSd, bool hasLowQualityImage);
58     int32_t GetGalleryMediaCount(bool shouldIncludeSd, bool hasLowQualityImage);
59     int32_t GetCloudMetaCount(bool shouldIncludeSd, bool hasLowQualityImage);
60     void GetDuplicateData(int32_t duplicateDataCount);
61     bool IsDuplicateData(const std::string &data);
62 
63 public:
64     std::string FindlPath(const FileInfo &fileInfo);
65     std::string FindPackageName(const FileInfo &fileInfo);
66     std::string FindBundleName(const FileInfo &fileInfo);
67     int32_t FindAlbumId(const FileInfo &fileInfo);
68     int32_t FindSubtype(const FileInfo &fileInfo);
69     int32_t FindDirty(const FileInfo &fileInfo);
70     std::string FindBurstKey(const FileInfo &fileInfo);
71     int32_t FindBurstCoverLevel(const FileInfo &fileInfo);
72     int64_t FindDateTrashed(const FileInfo &fileInfo);
73     int32_t FindPhotoQuality(const FileInfo &fileInfo);
74     int32_t FindMediaType(const FileInfo &fileInfo);
75     std::string FindSourcePath(const FileInfo &fileInfo);
76     int32_t FindStrongAssociation(const FileInfo &fileInfo);
77     int32_t FindCeAvailable(const FileInfo &fileInfo);
78 
79 private:
SetMediaLibraryRdb(std::shared_ptr<NativeRdb::RdbStore> mediaLibraryRdb)80     PhotosRestore &SetMediaLibraryRdb(std::shared_ptr<NativeRdb::RdbStore> mediaLibraryRdb)
81     {
82         this->mediaLibraryRdb_ = mediaLibraryRdb;
83         this->photosDao_.SetMediaLibraryRdb(mediaLibraryRdb);
84         this->photoAlbumDao_.SetMediaLibraryRdb(mediaLibraryRdb);
85         return *this;
86     }
SetGalleryRdb(std::shared_ptr<NativeRdb::RdbStore> galleryRdb)87     PhotosRestore &SetGalleryRdb(std::shared_ptr<NativeRdb::RdbStore> galleryRdb)
88     {
89         this->galleryRdb_ = galleryRdb;
90         this->galleryMediaDao_.SetGalleryRdb(galleryRdb);
91         return *this;
92     }
LoadBasicInfo()93     void LoadBasicInfo()
94     {
95         this->photosBasicInfo_ = this->photosDao_.GetBasicInfo();
96     }
97     PhotoAlbumDao::PhotoAlbumRowData FindAlbumInfo(const FileInfo &fileInfo);
ToLower(const std::string & str)98     std::string ToLower(const std::string &str)
99     {
100         std::string lowerStr;
101         std::transform(
102             str.begin(), str.end(), std::back_inserter(lowerStr), [](unsigned char c) { return std::tolower(c); });
103         return lowerStr;
104     }
ToString(const FileInfo & fileInfo)105     std::string ToString(const FileInfo &fileInfo)
106     {
107         std::stringstream ss;
108         ss << "FileInfo[ fileId: " << fileInfo.fileIdOld << ", displayName: " << fileInfo.displayName
109            << ", bundleName: " << fileInfo.bundleName << ", lPath: " << fileInfo.lPath
110            << ", size: " << fileInfo.fileSize << ", fileType: " << fileInfo.fileType
111            << ", oldPath: " << fileInfo.oldPath << ", sourcePath: " << fileInfo.sourcePath << " ]";
112         return ss.str();
113     }
114     std::string GetSuffix(const std::string &displayName);
115 
116 private:
117     std::shared_ptr<NativeRdb::RdbStore> mediaLibraryRdb_;
118     std::shared_ptr<NativeRdb::RdbStore> galleryRdb_;
119     PhotosDao::PhotosBasicInfo photosBasicInfo_;
120     PhotosDao photosDao_;
121     PhotoAlbumDao photoAlbumDao_;
122     std::mutex duplicateDataUsedCountMutex_;
123     std::unordered_map<std::string, int32_t> duplicateDataUsedCountMap_;
124     GalleryMediaDao galleryMediaDao_;
125 
126 private:
127     const std::string SQL_GALLERY_MEDIA_QUERY_DUPLICATE_DATA = "\
128         SELECT _data, count(1) as count \
129         FROM gallery_media \
130         GROUP BY _data \
131         HAVING count(1) > 1 \
132         LIMIT ?, ?;";
133     const std::string SOURCE_PATH_PREFIX = "/storage/emulated/0";
134 };
135 }  // namespace OHOS::Media
136 
137 #endif  // OHOS_MEDIA_PHOTOS_RESTORE