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 MEDIALIBRARY_FILE_OPERATIONS 17 #define MEDIALIBRARY_FILE_OPERATIONS 18 19 #include <memory> 20 #include <string> 21 #include <vector> 22 #include <unordered_map> 23 24 #include "abs_predicates.h" 25 #include "abs_shared_result_set.h" 26 #include "datashare_predicates.h" 27 #include "datashare_values_bucket.h" 28 #include "file_asset.h" 29 #include "imedia_scanner_callback.h" 30 #include "media_column.h" 31 #include "medialibrary_async_worker.h" 32 #include "medialibrary_command.h" 33 #include "value_object.h" 34 #include "values_bucket.h" 35 #include "medialibrary_rdb_transaction.h" 36 37 namespace OHOS { 38 namespace Media { 39 #define EXPORT __attribute__ ((visibility ("default"))) 40 EXPORT const std::unordered_map<std::string, int> FILEASSET_MEMBER_MAP = { 41 { MediaColumn::MEDIA_ID, MEMBER_TYPE_INT32 }, 42 { MediaColumn::MEDIA_FILE_PATH, MEMBER_TYPE_STRING }, 43 { MediaColumn::MEDIA_SIZE, MEMBER_TYPE_INT64 }, 44 { MediaColumn::MEDIA_TITLE, MEMBER_TYPE_STRING }, 45 { MediaColumn::MEDIA_NAME, MEMBER_TYPE_STRING }, 46 { MediaColumn::MEDIA_TYPE, MEMBER_TYPE_INT32 }, 47 { MediaColumn::MEDIA_MIME_TYPE, MEMBER_TYPE_STRING }, 48 { MediaColumn::MEDIA_OWNER_PACKAGE, MEMBER_TYPE_STRING }, 49 { MediaColumn::MEDIA_OWNER_APPID, MEMBER_TYPE_STRING }, 50 { MediaColumn::MEDIA_PACKAGE_NAME, MEMBER_TYPE_STRING }, 51 { MediaColumn::MEDIA_DEVICE_NAME, MEMBER_TYPE_STRING }, 52 { MediaColumn::MEDIA_DATE_ADDED, MEMBER_TYPE_INT64 }, 53 { MediaColumn::MEDIA_DATE_MODIFIED, MEMBER_TYPE_INT64 }, 54 { MediaColumn::MEDIA_DATE_TAKEN, MEMBER_TYPE_INT64 }, 55 { MediaColumn::MEDIA_DATE_DELETED, MEMBER_TYPE_INT64 }, 56 { MediaColumn::MEDIA_DURATION, MEMBER_TYPE_INT32 }, 57 { MediaColumn::MEDIA_TIME_PENDING, MEMBER_TYPE_INT64 }, 58 { MediaColumn::MEDIA_IS_FAV, MEMBER_TYPE_INT32 }, 59 { MediaColumn::MEDIA_DATE_TRASHED, MEMBER_TYPE_INT64 }, 60 { MediaColumn::MEDIA_HIDDEN, MEMBER_TYPE_INT32 }, 61 { MediaColumn::MEDIA_PARENT_ID, MEMBER_TYPE_INT32 }, 62 { MediaColumn::MEDIA_RELATIVE_PATH, MEMBER_TYPE_STRING }, 63 { MediaColumn::MEDIA_VIRTURL_PATH, MEMBER_TYPE_STRING }, 64 { PhotoColumn::PHOTO_ORIENTATION, MEMBER_TYPE_INT32 }, 65 { PhotoColumn::PHOTO_LATITUDE, MEMBER_TYPE_DOUBLE }, 66 { PhotoColumn::PHOTO_LONGITUDE, MEMBER_TYPE_DOUBLE }, 67 { PhotoColumn::PHOTO_HEIGHT, MEMBER_TYPE_INT32 }, 68 { PhotoColumn::PHOTO_WIDTH, MEMBER_TYPE_INT32 }, 69 { PhotoColumn::PHOTO_ALL_EXIF, MEMBER_TYPE_STRING }, 70 { PhotoColumn::PHOTO_LCD_VISIT_TIME, MEMBER_TYPE_INT64 }, 71 { PhotoColumn::PHOTO_EDIT_TIME, MEMBER_TYPE_INT64 }, 72 { PhotoColumn::PHOTO_SUBTYPE, MEMBER_TYPE_INT32 }, 73 { PhotoColumn::PHOTO_ORIGINAL_SUBTYPE, MEMBER_TYPE_INT32 }, 74 { PhotoColumn::MOVING_PHOTO_EFFECT_MODE, MEMBER_TYPE_INT32 }, 75 { PhotoColumn::PHOTO_COVER_POSITION, MEMBER_TYPE_INT64 }, 76 { PhotoColumn::PHOTO_CE_AVAILABLE, MEMBER_TYPE_INT32 }, 77 { AudioColumn::AUDIO_ALBUM, MEMBER_TYPE_STRING }, 78 { AudioColumn::AUDIO_ARTIST, MEMBER_TYPE_STRING }, 79 { PhotoColumn::PHOTO_OWNER_ALBUM_ID, MEMBER_TYPE_INT32 }, 80 { PhotoColumn::PHOTO_BURST_KEY, MEMBER_TYPE_STRING }, 81 { PhotoColumn::PHOTO_BURST_COVER_LEVEL, MEMBER_TYPE_INT32 }, 82 { PhotoColumn::PHOTO_THUMBNAIL_READY, MEMBER_TYPE_INT64 }, 83 { PhotoColumn::SUPPORTED_WATERMARK_TYPE, MEMBER_TYPE_INT32 }, 84 }; 85 86 class MediaLibraryAssetOperations { 87 public: 88 static int32_t HandleInsertOperation(MediaLibraryCommand &cmd); 89 static int32_t DeleteOperation(MediaLibraryCommand &cmd); 90 static std::shared_ptr<NativeRdb::ResultSet> QueryOperation(MediaLibraryCommand &cmd, 91 const std::vector<std::string> &columns); 92 EXPORT static int32_t UpdateOperation(MediaLibraryCommand &cmd); 93 static int32_t OpenOperation(MediaLibraryCommand &cmd, const std::string &mode); 94 static int32_t DeleteToolOperation(MediaLibraryCommand &cmd); 95 96 EXPORT static int32_t CreateAssetBucket(int32_t fileId, int32_t &bucketNum); 97 EXPORT static int32_t CreateAssetUniqueId(int32_t type, 98 std::shared_ptr<TransactionOperations> trans = nullptr); 99 EXPORT static int32_t CreateAssetPathById(int32_t fileId, int32_t mediaType, const std::string &extension, 100 std::string &filePath); 101 EXPORT static int32_t DeleteFromDisk(NativeRdb::AbsRdbPredicates &predicates, const bool isAging, 102 const bool compatible = false); 103 static std::string GetEditDataSourcePath(const std::string &path); 104 EXPORT static int32_t GetAlbumIdByPredicates(const std::string &whereClause, 105 const std::vector<std::string> &whereArgs); 106 107 protected: 108 static std::shared_ptr<FileAsset> GetFileAssetFromDb(const std::string &column, const std::string &value, 109 OperationObject oprnObject, const std::vector<std::string> &columns = {}, const std::string &networkId = ""); 110 static std::shared_ptr<FileAsset> GetFileAssetFromDb(NativeRdb::AbsPredicates &predicates, 111 OperationObject oprnObject, const std::vector<std::string> &columns = {}, const std::string &networkId = ""); 112 static int32_t GetFileAssetVectorFromDb(NativeRdb::AbsPredicates &predicates, OperationObject oprnObject, 113 std::vector<std::shared_ptr<FileAsset>> &fileAssetVector, const std::vector<std::string> &columns = {}, 114 const std::string &networkId = ""); 115 static std::shared_ptr<FileAsset> GetFileAssetByUri(const std::string &fileUri, bool isPhoto, 116 const std::vector<std::string> &columns, const std::string &pendingStatus = ""); 117 118 static int32_t CreateOperation(MediaLibraryCommand &cmd); 119 static int32_t CloseOperation(MediaLibraryCommand &cmd); 120 static int32_t InsertAssetInDb(std::shared_ptr<TransactionOperations> trans, 121 MediaLibraryCommand &cmd, const FileAsset &fileAsset); 122 static int32_t CheckWithType(bool isContains, const std::string &displayName, 123 const std::string &extention, int32_t mediaType); 124 static int32_t CheckDisplayNameWithType(const std::string &displayName, int32_t mediaType); 125 static int32_t CheckExtWithType(const std::string &extention, int32_t mediaType); 126 static int32_t CheckRelativePathWithType(const std::string &relativePath, int32_t mediaType); 127 static void GetAssetRootDir(int32_t mediaType, std::string &rootDirPath); 128 EXPORT static int32_t SetAssetPathInCreate(FileAsset &fileAsset, 129 std::shared_ptr<TransactionOperations> trans = nullptr); 130 EXPORT static int32_t SetAssetPath(FileAsset &fileAsset, const std::string &extention, 131 std::shared_ptr<TransactionOperations> trans = nullptr); 132 static int32_t DeleteAssetInDb(MediaLibraryCommand &cmd); 133 134 static int32_t UpdateFileName(MediaLibraryCommand &cmd, const std::shared_ptr<FileAsset> &fileAsset, 135 bool &isNameChanged); 136 static int32_t UpdateAllExif(MediaLibraryCommand &cmd, const std::shared_ptr<FileAsset> &fileAsset); 137 static int32_t SetUserComment(MediaLibraryCommand &cmd, const std::shared_ptr<FileAsset> &fileAsset); 138 static int32_t UpdateRelativePath(MediaLibraryCommand &cmd, const std::shared_ptr<FileAsset> &fileAsset, 139 bool &isNameChanged); 140 static void UpdateVirtualPath(MediaLibraryCommand &cmd, const std::shared_ptr<FileAsset> &fileAsset); 141 static int32_t UpdateFileInDb(MediaLibraryCommand &cmd); 142 static int32_t OpenAsset(const std::shared_ptr<FileAsset> &fileAsset, const std::string &mode, 143 MediaLibraryApi api, bool isMovingPhotoVideo = false); 144 static int32_t OpenHighlightCover(MediaLibraryCommand &cmd, const std::string &mode); 145 static int32_t CloseAsset(const std::shared_ptr<FileAsset> &fileAsset, bool isCreateThumbSync = false); 146 static int32_t OpenHighlightVideo(MediaLibraryCommand &cmd, const std::string &mode); 147 static void InvalidateThumbnail(const std::string &fileId, int32_t mediaType); 148 static int32_t SendTrashNotify(MediaLibraryCommand &cmd, int32_t rowId, const std::string &extraUri = ""); 149 static void SendFavoriteNotify(MediaLibraryCommand &cmd, std::shared_ptr<FileAsset> &fileAsset, 150 const std::string &extraUri = ""); 151 static void UpdateOwnerAlbumIdOnMove(MediaLibraryCommand &cmd, int32_t &targetAlbumId, int32_t &oriAlbumId); 152 static int32_t SendModifyUserCommentNotify(MediaLibraryCommand &cmd, int32_t rowId, 153 const std::string &extraUri = ""); 154 static int32_t SetPendingStatus(MediaLibraryCommand &cmd); 155 static int32_t GrantUriPermission(const std::string &uri, const std::string &bundleName, 156 const std::string &path, bool isMovingPhoto = false); 157 158 static bool GetInt32FromValuesBucket(const NativeRdb::ValuesBucket &values, const std::string &column, 159 int32_t &value); 160 EXPORT static std::string CreateExtUriForV10Asset(FileAsset &fileAsset); 161 static bool GetStringFromValuesBucket(const NativeRdb::ValuesBucket &values, const std::string &column, 162 std::string &value); 163 static int32_t OpenFileWithPrivacy(const std::string &filePath, const std::string &mode, 164 const std::string &fileId); 165 static void ScanFile(const std::string &path, bool isCreateThumbSync, bool isInvalidateThumb, 166 bool isForceScan = false, int32_t fileId = 0); 167 static void ScanFileWithoutAlbumUpdate(const std::string &path, bool isCreateThumbSync, bool isInvalidateThumb, 168 bool isForceScan = false, int32_t fileId = 0); 169 170 static std::string GetEditDataDirPath(const std::string &path); 171 static std::string GetEditDataPath(const std::string &path); 172 static std::string GetEditDataCameraPath(const std::string &path); 173 static std::string GetAssetCacheDir(); 174 175 private: 176 static int32_t CreateAssetRealName(int32_t fileId, int32_t mediaType, const std::string &extension, 177 std::string &name); 178 static int32_t SetPendingTrue(const std::shared_ptr<FileAsset> &fileAsset); 179 static int32_t SetPendingFalse(const std::shared_ptr<FileAsset> &fileAsset); 180 static std::shared_ptr<FileAsset> GetAssetFromResultSet(const std::shared_ptr<NativeRdb::ResultSet> &resultSet, 181 const std::vector<std::string> &columns); 182 183 static constexpr int ASSET_MAX_COMPLEMENT_ID = 999; 184 185 class ScanAssetCallback : public IMediaScannerCallback { 186 public: 187 ScanAssetCallback() = default; 188 ~ScanAssetCallback() = default; 189 int32_t OnScanFinished(const int32_t status, const std::string &uri, const std::string &path) override; SetSync(bool isSync)190 void SetSync(bool isSync) 191 { 192 isCreateThumbSync = isSync; 193 } SetIsInvalidateThumb(bool isInvalidate)194 void SetIsInvalidateThumb(bool isInvalidate) 195 { 196 isInvalidateThumb = isInvalidate; 197 } 198 private: 199 bool isCreateThumbSync = false; 200 bool isInvalidateThumb = true; 201 }; 202 }; 203 204 class DeleteNotifyAsyncTaskData : public AsyncTaskData { 205 public: 206 DeleteNotifyAsyncTaskData() = default; 207 virtual ~DeleteNotifyAsyncTaskData() override = default; 208 int32_t updateRows = 0; 209 std::vector<std::string> notifyUris; 210 std::string notifyUri; 211 int64_t trashDate = 0; 212 }; 213 214 using VerifyFunction = bool (*) (NativeRdb::ValueObject&, MediaLibraryCommand&); 215 class AssetInputParamVerification { 216 public: 217 static bool CheckParamForUpdate(MediaLibraryCommand &cmd); 218 219 private: 220 static bool Forbidden(NativeRdb::ValueObject &value, MediaLibraryCommand &cmd); 221 static bool IsInt32(NativeRdb::ValueObject &value, MediaLibraryCommand &cmd); 222 static bool IsInt64(NativeRdb::ValueObject &value, MediaLibraryCommand &cmd); 223 static bool IsBool(NativeRdb::ValueObject &value, MediaLibraryCommand &cmd); 224 static bool IsString(NativeRdb::ValueObject &value, MediaLibraryCommand &cmd); 225 static bool IsDouble(NativeRdb::ValueObject &value, MediaLibraryCommand &cmd); 226 static bool IsBelowApi9(NativeRdb::ValueObject &value, MediaLibraryCommand &cmd); 227 static bool IsStringNotNull(NativeRdb::ValueObject &value, MediaLibraryCommand &cmd); 228 static bool IsUniqueValue(NativeRdb::ValueObject &value, MediaLibraryCommand &cmd); 229 230 static const std::unordered_map<std::string, std::vector<VerifyFunction>> UPDATE_VERIFY_PARAM_MAP; 231 }; 232 } // namespace Media 233 } // namespace OHOS 234 235 #endif // MEDIALIBRARY_FILE_OPERATIONS 236