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