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_CLONE 16 #define OHOS_MEDIA_PHOTOS_CLONE 17 18 #include <string> 19 #include <vector> 20 21 #include "rdb_store.h" 22 #include "backup_const.h" 23 #include "photos_dao.h" 24 #include "photo_album_dao.h" 25 26 namespace OHOS::Media { 27 class PhotosClone { 28 public: 29 /** 30 * @brief Restore Start Event Handler. 31 */ OnStart(std::shared_ptr<NativeRdb::RdbStore> mediaLibraryTargetRdb,std::shared_ptr<NativeRdb::RdbStore> mediaLibraryOriginalRdb)32 int32_t OnStart(std::shared_ptr<NativeRdb::RdbStore> mediaLibraryTargetRdb, 33 std::shared_ptr<NativeRdb::RdbStore> mediaLibraryOriginalRdb) 34 { 35 this->mediaLibraryTargetRdb_ = mediaLibraryTargetRdb; 36 this->mediaLibraryOriginalRdb_ = mediaLibraryOriginalRdb; 37 this->photosDao_.SetMediaLibraryRdb(mediaLibraryTargetRdb); 38 this->photosBasicInfo_ = this->photosDao_.GetBasicInfo(); 39 this->photoAlbumDao_.SetMediaLibraryRdb(mediaLibraryTargetRdb); 40 return 0; 41 } 42 OnStop(std::atomic<uint64_t> & totalNumber,std::atomic<int32_t> & processStatus)43 int32_t OnStop(std::atomic<uint64_t> &totalNumber, std::atomic<int32_t> &processStatus) 44 { 45 processStatus = ProcessStatus::START; 46 this->FixDuplicateBurstKeyInDifferentAlbum(totalNumber); 47 processStatus = ProcessStatus::STOP; 48 return 0; 49 } 50 FindSameFile(const FileInfo & fileInfo)51 PhotosDao::PhotosRowData FindSameFile(const FileInfo &fileInfo) 52 { 53 int32_t maxFileId = this->photosBasicInfo_.maxFileId; 54 return this->photosDao_.FindSameFile(fileInfo, maxFileId); 55 } 56 57 std::shared_ptr<NativeRdb::ResultSet> GetPhotosInPhotoMap(int32_t offset, int32_t pageSize); 58 std::shared_ptr<NativeRdb::ResultSet> GetPhotosNotInPhotoMap(int32_t offset, int32_t pageSize); 59 int32_t GetPhotosRowCountInPhotoMap(); 60 int32_t GetPhotosRowCountNotInPhotoMap(); 61 std::string FindlPath(const FileInfo &fileInfo); 62 int32_t FindAlbumId(const FileInfo &fileInfo); 63 std::string FindPackageName(const FileInfo &info); 64 std::string FindBundleName(const FileInfo &info); 65 int32_t FindPhotoQuality(const FileInfo &fileInfo); 66 67 private: 68 enum { UUID_STR_LENGTH = 37 }; 69 70 private: 71 PhotoAlbumDao::PhotoAlbumRowData FindAlbumInfo(const FileInfo &fileInfo); 72 int32_t FixDuplicateBurstKeyInDifferentAlbum(std::atomic<uint64_t> &totalNumber); 73 std::vector<PhotosDao::PhotosRowData> FindDuplicateBurstKey(); 74 std::string ToString(const std::vector<NativeRdb::ValueObject> &values); 75 std::string GenerateUuid(); 76 std::string ToString(const FileInfo &fileInfo); 77 std::string ToLower(const std::string &str); 78 79 private: 80 std::shared_ptr<NativeRdb::RdbStore> mediaLibraryTargetRdb_; 81 std::shared_ptr<NativeRdb::RdbStore> mediaLibraryOriginalRdb_; 82 PhotosDao::PhotosBasicInfo photosBasicInfo_; 83 PhotosDao photosDao_; 84 PhotoAlbumDao photoAlbumDao_; 85 86 private: 87 std::string SQL_PHOTOS_TABLE_COUNT_IN_PHOTO_MAP = "\ 88 SELECT COUNT(1) AS count \ 89 FROM PhotoAlbum \ 90 INNER JOIN PhotoMap \ 91 ON PhotoAlbum.album_id=PhotoMap.map_album \ 92 INNER JOIN Photos \ 93 ON PhotoMap.map_asset=Photos.file_id \ 94 WHERE Photos.position IN (1, 3) AND \ 95 (PhotoAlbum.album_type != 2048 OR PhotoAlbum.album_name != '.hiddenAlbum');"; 96 std::string SQL_PHOTOS_TABLE_QUERY_IN_PHOTO_MAP = "\ 97 SELECT PhotoAlbum.lpath, \ 98 Photos.* \ 99 FROM PhotoAlbum \ 100 INNER JOIN PhotoMap \ 101 ON PhotoAlbum.album_id=PhotoMap.map_album \ 102 INNER JOIN Photos \ 103 ON PhotoMap.map_asset=Photos.file_id \ 104 WHERE Photos.position IN (1, 3) AND \ 105 (PhotoAlbum.album_type != 2048 OR PhotoAlbum.album_name != '.hiddenAlbum') \ 106 ORDER BY Photos.file_id \ 107 LIMIT ?, ? ;"; 108 std::string SQL_PHOTOS_TABLE_COUNT_NOT_IN_PHOTO_MAP = "\ 109 SELECT COUNT(1) AS count \ 110 FROM Photos \ 111 LEFT JOIN PhotoAlbum \ 112 ON Photos.owner_album_id = PhotoAlbum.album_id \ 113 WHERE position IN (1, 3) AND \ 114 (COALESCE(PhotoAlbum.album_type, 0) != 2048 OR COALESCE(PhotoAlbum.album_name, '') != '.hiddenAlbum');"; 115 std::string SQL_PHOTOS_TABLE_QUERY_NOT_IN_PHOTO_MAP = "\ 116 SELECT \ 117 PhotoAlbum.lpath, \ 118 Photos.* \ 119 FROM Photos \ 120 LEFT JOIN PhotoAlbum \ 121 ON Photos.owner_album_id=PhotoAlbum.album_id \ 122 WHERE position IN (1, 3) AND \ 123 (COALESCE(PhotoAlbum.album_type, 0) != 2048 OR COALESCE(PhotoAlbum.album_name, '') != '.hiddenAlbum') \ 124 ORDER BY Photos.file_id \ 125 LIMIT ?, ? ;"; 126 std::string SQL_PHOTOS_TABLE_BURST_KEY_DUPLICATE_QUERY = "\ 127 SELECT DISTINCT \ 128 Photos.owner_album_id, \ 129 Photos.burst_key \ 130 FROM \ 131 ( \ 132 SELECT burst_key, \ 133 COUNT(1) AS count \ 134 FROM \ 135 ( \ 136 SELECT owner_album_id, burst_key \ 137 FROM Photos \ 138 WHERE burst_key IS NOT NULL \ 139 GROUP BY owner_album_id, burst_key \ 140 ) \ 141 GROUP BY burst_key \ 142 ) AS BURST \ 143 INNER JOIN Photos \ 144 ON BURST.burst_key = Photos.burst_key \ 145 WHERE BURST.count > 1 \ 146 ORDER BY Photos.burst_key \ 147 LIMIT ?, ? ;"; 148 std::string SQL_PHOTOS_TABLE_BURST_KEY_UPDATE = "\ 149 UPDATE Photos \ 150 SET burst_key = ? \ 151 WHERE owner_album_id = ? AND \ 152 burst_key = ?;"; 153 }; 154 } // namespace OHOS::Media 155 #endif