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     int32_t GetGalleryMediaCount(bool shouldIncludeSd, bool hasLowQualityImage);
57     void GetDuplicateData(int32_t duplicateDataCount);
58     bool IsDuplicateData(const std::string &data);
59 
60 public:
61     std::string FindlPath(const FileInfo &fileInfo);
62     std::string FindPackageName(const FileInfo &fileInfo);
63     std::string FindBundleName(const FileInfo &fileInfo);
64     int32_t FindAlbumId(const FileInfo &fileInfo);
65     int32_t FindSubtype(const FileInfo &fileInfo);
66     int32_t FindDirty(const FileInfo &fileInfo);
67     std::string FindBurstKey(const FileInfo &fileInfo);
68     int32_t FindBurstCoverLevel(const FileInfo &fileInfo);
69     int64_t FindDateTrashed(const FileInfo &fileInfo);
70     int32_t FindPhotoQuality(const FileInfo &fileInfo);
71     int32_t FindMediaType(const FileInfo &fileInfo);
72     std::string FindSourcePath(const FileInfo &fileInfo);
73     int32_t FindStrongAssociation(const FileInfo &fileInfo);
74     int32_t FindCeAvailable(const FileInfo &fileInfo);
75 
76 private:
SetMediaLibraryRdb(std::shared_ptr<NativeRdb::RdbStore> mediaLibraryRdb)77     PhotosRestore &SetMediaLibraryRdb(std::shared_ptr<NativeRdb::RdbStore> mediaLibraryRdb)
78     {
79         this->mediaLibraryRdb_ = mediaLibraryRdb;
80         this->photosDao_.SetMediaLibraryRdb(mediaLibraryRdb);
81         this->photoAlbumDao_.SetMediaLibraryRdb(mediaLibraryRdb);
82         return *this;
83     }
SetGalleryRdb(std::shared_ptr<NativeRdb::RdbStore> galleryRdb)84     PhotosRestore &SetGalleryRdb(std::shared_ptr<NativeRdb::RdbStore> galleryRdb)
85     {
86         this->galleryRdb_ = galleryRdb;
87         this->galleryMediaDao_.SetGalleryRdb(galleryRdb);
88         return *this;
89     }
LoadBasicInfo()90     void LoadBasicInfo()
91     {
92         this->photosBasicInfo_ = this->photosDao_.GetBasicInfo();
93     }
94     PhotoAlbumDao::PhotoAlbumRowData FindAlbumInfo(const FileInfo &fileInfo);
ToLower(const std::string & str)95     std::string ToLower(const std::string &str)
96     {
97         std::string lowerStr;
98         std::transform(
99             str.begin(), str.end(), std::back_inserter(lowerStr), [](unsigned char c) { return std::tolower(c); });
100         return lowerStr;
101     }
ToString(const FileInfo & fileInfo)102     std::string ToString(const FileInfo &fileInfo)
103     {
104         std::stringstream ss;
105         ss << "FileInfo[ fileId: " << fileInfo.fileIdOld << ", displayName: " << fileInfo.displayName
106            << ", bundleName: " << fileInfo.bundleName << ", lPath: " << fileInfo.lPath
107            << ", size: " << fileInfo.fileSize << ", fileType: " << fileInfo.fileType
108            << ", oldPath: " << fileInfo.oldPath << ", sourcePath: " << fileInfo.sourcePath << " ]";
109         return ss.str();
110     }
111     std::string GetSuffix(const std::string &displayName);
112 
113 private:
114     std::shared_ptr<NativeRdb::RdbStore> mediaLibraryRdb_;
115     std::shared_ptr<NativeRdb::RdbStore> galleryRdb_;
116     PhotosDao::PhotosBasicInfo photosBasicInfo_;
117     PhotosDao photosDao_;
118     PhotoAlbumDao photoAlbumDao_;
119     std::mutex duplicateDataUsedCountMutex_;
120     std::unordered_map<std::string, int32_t> duplicateDataUsedCountMap_;
121     GalleryMediaDao galleryMediaDao_;
122 
123 private:
124     const std::string SQL_GALLERY_MEDIA_QUERY_DUPLICATE_DATA = "\
125         SELECT _data, count(1) as count \
126         FROM gallery_media \
127         GROUP BY _data \
128         HAVING count(1) > 1 \
129         LIMIT ?, ?;";
130     const std::string SOURCE_PATH_PREFIX = "/storage/emulated/0";
131 };
132 }  // namespace OHOS::Media
133 
134 #endif  // OHOS_MEDIA_PHOTOS_RESTORE