1 /* 2 * Copyright (c) 2023 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 16 #ifndef OHOS_CLOUD_SYNC_SERVICE_FILE_DATA_HANDLER_H 17 #define OHOS_CLOUD_SYNC_SERVICE_FILE_DATA_HANDLER_H 18 19 #include "data_sync_notifier.h" 20 #include "medialibrary_db_const.h" 21 #include "medialibrary_type_const.h" 22 23 #include "cloud_pref_impl.h" 24 #include "rdb_data_handler.h" 25 #include "file_data_convertor.h" 26 27 namespace OHOS { 28 namespace FileManagement { 29 namespace CloudSync { 30 class FileDataHandler : public RdbDataHandler { 31 public: 32 enum Action { 33 RETAIN_DATA = 0, 34 CLEAR_DATA 35 }; 36 37 FileDataHandler(int32_t userId, const std::string &bundleName, std::shared_ptr<NativeRdb::RdbStore> rdb); 38 virtual ~FileDataHandler() = default; 39 40 /* download */ 41 void GetFetchCondition(FetchCondition &cond) override; 42 virtual int32_t OnFetchRecords(std::shared_ptr<std::vector<DriveKit::DKRecord>> &records, 43 OnFetchParams ¶ms) override; 44 virtual int32_t GetRetryRecords(std::vector<DriveKit::DKRecordId> &records) override; 45 virtual int32_t GetCheckRecords(std::vector<DriveKit::DKRecordId> &checkRecords, 46 const std::shared_ptr<std::vector<DriveKit::DKRecord>> &records) override; 47 virtual int32_t GetAssetsToDownload(std::vector<DriveKit::DKDownloadAsset> &outAssetsToDownload) override; 48 49 int32_t GetDownloadAsset(std::string cloudId, 50 std::vector<DriveKit::DKDownloadAsset> &outAssetsToDownload) override; 51 52 /*clean*/ 53 int32_t Clean(const int action) override; 54 55 /* upload */ 56 int32_t GetCreatedRecords(std::vector<DriveKit::DKRecord> &records) override; 57 int32_t GetDeletedRecords(std::vector<DriveKit::DKRecord> &records) override; 58 int32_t GetMetaModifiedRecords(std::vector<DriveKit::DKRecord> &records) override; 59 int32_t GetFileModifiedRecords(std::vector<DriveKit::DKRecord> &records) override; 60 61 /* callback */ 62 int32_t OnCreateRecords(const std::map<DriveKit::DKRecordId, 63 DriveKit::DKRecordOperResult> &map) override; 64 int32_t OnDeleteRecords(const std::map<DriveKit::DKRecordId, 65 DriveKit::DKRecordOperResult> &map) override; 66 int32_t OnModifyMdirtyRecords(const std::map<DriveKit::DKRecordId, 67 DriveKit::DKRecordOperResult> &map) override; 68 int32_t OnModifyFdirtyRecords(const std::map<DriveKit::DKRecordId, 69 DriveKit::DKRecordOperResult> &map) override; 70 int32_t OnDownloadSuccess(const DriveKit::DKDownloadAsset &asset) override; 71 int32_t OnDownloadThumb(const std::map<DriveKit::DKDownloadAsset, DriveKit::DKDownloadResult> &resultMap) override; 72 int32_t OnDownloadThumb(const DriveKit::DKDownloadAsset &asset) override; 73 74 /* reset */ 75 void Reset(); 76 77 private: 78 int32_t OnCreateRecordSuccess(const std::pair<DriveKit::DKRecordId, DriveKit::DKRecordOperResult> &entry, 79 const std::map<std::string, std::pair<std::int64_t, std::int64_t>> &localMap); 80 int32_t OnDeleteRecordSuccess(const std::pair<DriveKit::DKRecordId, DriveKit::DKRecordOperResult> &entry); 81 int32_t OnModifyRecordSuccess(const std::pair<DriveKit::DKRecordId, DriveKit::DKRecordOperResult> &entry, 82 const std::map<std::string, std::pair<std::int64_t, std::int64_t>> &localMap); 83 bool IfTimeChanged(const DriveKit::DKRecord &record, 84 const std::map<std::string, std::pair<std::int64_t, std::int64_t>> &localMap, 85 const std::string &path, 86 const std::string &type); 87 void GetLocalTimeMap(const std::map<DriveKit::DKRecordId, DriveKit::DKRecordOperResult> &map, 88 std::map<std::string, std::pair<std::int64_t, std::int64_t>> &cloudMap, 89 const std::string &type); 90 void OnResultSetConvertToMap(const std::shared_ptr<NativeRdb::ResultSet> resultSet, 91 std::map<std::string, std::pair<std::int64_t, std::int64_t>> &cloudMap, 92 const std::string &type); 93 int32_t OnRecordFailed(const std::pair<DriveKit::DKRecordId, DriveKit::DKRecordOperResult> &entry); 94 int32_t HandleCloudSpaceNotEnough(); 95 int32_t HandleATFailed(); 96 int32_t HandleNameConflict(); 97 int32_t HandleNameInvalid(); 98 int32_t HandleNetworkErr(); 99 int64_t UTCTimeSeconds(); 100 101 int32_t EraseLocalInfo(std::vector<DriveKit::DKRecord> &records); 102 103 /* data calculate*/ 104 int32_t CompensateFilePath(DriveKit::DKRecord &record); 105 int32_t CalculateFilePath(DriveKit::DKRecord &record, std::string &filePath); 106 int32_t GetMediaType(DriveKit::DKRecord &record, int32_t &mediaType); 107 int32_t GetAssetUniqueId(int32_t &type); 108 int32_t CreateAssetPathById(DriveKit::DKRecord &record, int32_t &uniqueId, 109 int32_t &mediaType, std::string &filePath); 110 int32_t CreateAssetBucket(int32_t &uniqueId, int32_t &bucketNum); 111 std::string GetFileExtension(DriveKit::DKRecord &record); 112 int32_t CreateAssetRealName(int32_t &fileId, int32_t &mediaType, 113 const std::string &extension, std::string &name); 114 115 /* album map */ 116 int32_t BindAlbums(std::vector<DriveKit::DKRecord> &records); 117 int32_t BindAlbumChanges(std::vector<DriveKit::DKRecord> &records); 118 int32_t UpdateLocalAlbumMap(const std::string &cloudId); 119 120 int32_t GetAlbumCloudIds(std::vector<int32_t> &localIds, std::vector<std::string> &cloudIds); 121 int32_t GetAlbumIdsFromResultSet(const std::shared_ptr<NativeRdb::ResultSet> resultSet, std::vector<int32_t> &ids); 122 int32_t GetAlbumIdsFromResultSet(const std::shared_ptr<NativeRdb::ResultSet> resultSet, 123 std::vector<int32_t> &add, std::vector<int32_t> &rm); 124 125 static inline const std::string TABLE_NAME = Media::PhotoColumn::PHOTOS_TABLE; 126 static inline const int32_t LIMIT_SIZE = 5; 127 DriveKit::DKRecordType recordType_ = "media"; 128 DriveKit::DKFieldKeyArray desiredKeys_; 129 DriveKit::DKFieldKeyArray checkedKeys_ = {"version", "id"}; 130 std::vector<std::string> modifyFailSet_; 131 std::vector<std::string> createFailSet_; 132 133 /* identifier */ 134 int32_t userId_; 135 std::string bundleName_; 136 137 /*clean*/ 138 FileDataConvertor cleanConvertor_ = { userId_, bundleName_, FileDataConvertor::FILE_CLEAN }; 139 int32_t ClearCloudInfo(const std::string &cloudId); 140 int32_t CleanCloudRecord(NativeRdb::ResultSet &local, const int action, const std::string &filePath); 141 int32_t CleanNotDirtyData(const std::string &thmbDir, const std::string &assetPath, const std::string &cloudId); 142 int32_t CleanNotPureCloudRecord(NativeRdb::ResultSet &local, const int action, const std::string &filePath); 143 int32_t CleanPureCloudRecord(NativeRdb::ResultSet &local, const int action, const std::string &filePath); 144 int32_t DeleteDentryFile(void); 145 146 /* create */ 147 FileDataConvertor createConvertor_ = { userId_, bundleName_, FileDataConvertor::FILE_CREATE }; 148 149 /* delete */ 150 FileDataConvertor deleteConvertor_ = { userId_, bundleName_, FileDataConvertor::FILE_DELETE }; 151 152 /* update */ 153 FileDataConvertor mdirtyConvertor_ = { userId_, bundleName_, FileDataConvertor::FILE_METADATA_MODIFY }; 154 FileDataConvertor fdirtyConvertor_ = { userId_, bundleName_, FileDataConvertor::FILE_DATA_MODIFY }; 155 156 /* file Conflict */ 157 static inline const std::string CON_SUFFIX = "_1"; 158 std::string ConflictRenameThumb(NativeRdb::ResultSet &resultSet, 159 std::string fullPath, 160 std::string &tmpPath, 161 std::string &newPath); 162 int32_t ConflictRename(NativeRdb::ResultSet &resultSet, std::string &fullPath, std::string &relativePath); 163 int32_t ConflictRenamePath(NativeRdb::ResultSet &resultSet, 164 std::string &fullPath, 165 std::string &rdbPath, 166 std::string &localPath, 167 std::string &newLocalPath); 168 int32_t ConflictDataMerge(const DriveKit::DKRecord &record, std::string &fullPath); 169 int32_t ConflictHandler(NativeRdb::ResultSet &resultSet, int64_t isize, int64_t crTime, bool &modifyPathflag); 170 int32_t ConflictMerge(NativeRdb::ResultSet &resultSet, 171 const DriveKit::DKRecord &record, 172 std::string &fullPath, 173 std::string &relativePath); 174 int32_t ConflictDownload(const DriveKit::DKRecord &record, std::string &fullPath, bool &comflag); 175 int32_t GetConflictData(DriveKit::DKRecord &record, 176 std::string &fullPath, 177 int64_t &isize, 178 int64_t &crTime, 179 std::string &relativePath); 180 int32_t PullRecordConflict(DriveKit::DKRecord &record, bool &comflag); 181 182 /* pull operations */ 183 std::tuple<std::shared_ptr<NativeRdb::ResultSet>, int> QueryLocalByCloudId(const std::string &recordId); 184 int32_t PullRecordInsert(DriveKit::DKRecord &record, OnFetchParams ¶ms); 185 int32_t PullRecordUpdate(DriveKit::DKRecord &record, NativeRdb::ResultSet &local, 186 OnFetchParams ¶ms); 187 void RemoveThmParentPath(const std::string &filePath); 188 int32_t PullRecordDelete(DriveKit::DKRecord &record, NativeRdb::ResultSet &local); 189 int32_t SetRetry(const std::string &recordId); 190 int32_t RecycleFile(const std::string &recordId); 191 void AppendToDownload(NativeRdb::ResultSet &local, 192 const std::string &fieldKey, 193 std::vector<DriveKit::DKDownloadAsset> &assetsToDownload); 194 void AppendToDownload(const DriveKit::DKRecord &record, 195 const std::string &fieldKey, 196 std::vector<DriveKit::DKDownloadAsset> &assetsToDownload); 197 int AddCloudThumbs(const DriveKit::DKRecord &record); 198 int DentryInsertThumb(const std::string &fullPath, 199 const std::string &recordId, 200 uint64_t size, 201 uint64_t mtime, 202 const std::string &type); 203 int DentryRemoveThumb(const std::string &downloadPath); 204 bool ThumbsAtLocal(const DriveKit::DKRecord &record); 205 int32_t UpdateAssetInPhotoMap(const DriveKit::DKRecord &record, int32_t fileId); 206 int32_t InsertAssetToPhotoMap(const DriveKit::DKRecord &record, OnFetchParams ¶ms); 207 int32_t DeleteAssetInPhotoMap(int32_t fileId); 208 int32_t GetAlbumIdFromCloudId(const std::string &recordId); 209 void QueryAndInsertMap(int32_t albumId, int32_t fileId); 210 void QueryAndDeleteMap(int32_t fileId, const std::set<int> &cloudMapIds); 211 int32_t BatchInsertAssetMaps(OnFetchParams ¶ms); 212 213 /* db result to record */ 214 FileDataConvertor localConvertor_ = { userId_, bundleName_, FileDataConvertor::FILE_DOWNLOAD }; 215 }; 216 } // namespace CloudSync 217 } // namespace FileManagement 218 } // namespace OHOS 219 #endif // OHOS_CLOUD_SYNC_SERVICE_FILE_DATA_HANDLER_H 220