• 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->mediaLibraryRdb_ = mediaLibraryRdb;
38         this->galleryRdb_ = galleryRdb;
39         this->photosDao_.SetMediaLibraryRdb(mediaLibraryRdb);
40         this->photoAlbumDao_.SetMediaLibraryRdb(mediaLibraryRdb);
41         this->photosBasicInfo_ = this->photosDao_.GetBasicInfo();
42         this->galleryMediaDao_.SetGalleryRdb(galleryRdb);
43     }
44 
FindSameFile(const FileInfo & fileInfo)45     PhotosDao::PhotosRowData FindSameFile(const FileInfo &fileInfo)
46     {
47         int32_t maxFileId = this->photosBasicInfo_.maxFileId;
48         return this->photosDao_.FindSameFile(fileInfo, maxFileId);
49     }
50 
51     std::shared_ptr<NativeRdb::ResultSet> GetGalleryMedia(
52         int32_t offset, int pageSize, bool shouldIncludeSd, bool hasLowQualityImage);
53     int32_t GetGalleryMediaCount(bool shouldIncludeSd, bool hasLowQualityImage);
54     void GetDuplicateData(int32_t duplicateDataCount);
55     bool IsDuplicateData(const std::string &data);
56 
57 public:
58     std::string FindlPath(const FileInfo &fileInfo);
59     std::string FindPackageName(const FileInfo &fileInfo);
60     std::string FindBundleName(const FileInfo &fileInfo);
61     int32_t FindAlbumId(const FileInfo &fileInfo);
62     int32_t FindSubtype(const FileInfo &fileInfo);
63     int32_t FindDirty(const FileInfo &fileInfo);
64     std::string FindBurstKey(const FileInfo &fileInfo);
65     int32_t FindBurstCoverLevel(const FileInfo &fileInfo);
66     int64_t FindDateTrashed(const FileInfo &fileInfo);
67     int32_t FindPhotoQuality(const FileInfo &fileInfo);
68     int32_t FindMediaType(const FileInfo &fileInfo);
69 
70 private:
71     std::string ParseSourcePathToLPath(const std::string &sourcePath);
72     PhotoAlbumDao::PhotoAlbumRowData BuildAlbumInfoByLPath(const std::string &lPath);
73     PhotoAlbumDao::PhotoAlbumRowData FindAlbumInfo(const FileInfo &fileInfo);
ToLower(const std::string & str)74     std::string ToLower(const std::string &str)
75     {
76         std::string lowerStr;
77         std::transform(
78             str.begin(), str.end(), std::back_inserter(lowerStr), [](unsigned char c) { return std::tolower(c); });
79         return lowerStr;
80     }
ToString(const FileInfo & fileInfo)81     std::string ToString(const FileInfo &fileInfo)
82     {
83         std::stringstream ss;
84         ss << "FileInfo[ fileId: " << fileInfo.fileIdOld << ", displayName: " << fileInfo.displayName
85            << ", bundleName: " << fileInfo.bundleName << ", lPath: " << fileInfo.lPath
86            << ", size: " << fileInfo.fileSize << ", fileType: " << fileInfo.fileType
87            << ", oldPath: " << fileInfo.oldPath << ", sourcePath: " << fileInfo.sourcePath << " ]";
88         return ss.str();
89     }
90     std::string GetSuffix(const std::string &displayName);
91 
92 private:
93     std::shared_ptr<NativeRdb::RdbStore> mediaLibraryRdb_;
94     std::shared_ptr<NativeRdb::RdbStore> galleryRdb_;
95     PhotosDao::PhotosBasicInfo photosBasicInfo_;
96     PhotosDao photosDao_;
97     PhotoAlbumDao photoAlbumDao_;
98     std::mutex duplicateDataUsedCountMutex_;
99     std::unordered_map<std::string, int32_t> duplicateDataUsedCountMap_;
100     GalleryMediaDao galleryMediaDao_;
101 
102 private:
103     const std::string SQL_GALLERY_MEDIA_QUERY_DUPLICATE_DATA = "\
104         SELECT _data, count(1) as count \
105         FROM gallery_media \
106         GROUP BY _data \
107         HAVING count(1) > 1 \
108         LIMIT ?, ?;";
109 };
110 }  // namespace OHOS::Media
111 
112 #endif  // OHOS_MEDIA_PHOTOS_RESTORE