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_command.h" 32 #include "value_object.h" 33 #include "values_bucket.h" 34 35 namespace OHOS { 36 namespace Media { 37 static constexpr int UNCREATE_FILE_TIMEPENDING = -1; 38 static constexpr int UNCLOSE_FILE_TIMEPENDING = -2; 39 static constexpr int UNOPEN_FILE_COMPONENT_TIMEPENDING = -3; 40 41 const std::unordered_map<std::string, int> FILEASSET_MEMBER_MAP = { 42 { MediaColumn::MEDIA_ID, MEMBER_TYPE_INT32 }, 43 { MediaColumn::MEDIA_FILE_PATH, MEMBER_TYPE_STRING }, 44 { MediaColumn::MEDIA_SIZE, MEMBER_TYPE_INT64 }, 45 { MediaColumn::MEDIA_TITLE, MEMBER_TYPE_STRING }, 46 { MediaColumn::MEDIA_NAME, MEMBER_TYPE_STRING }, 47 { MediaColumn::MEDIA_TYPE, MEMBER_TYPE_INT32 }, 48 { MediaColumn::MEDIA_MIME_TYPE, MEMBER_TYPE_STRING }, 49 { MediaColumn::MEDIA_OWNER_PACKAGE, 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_TIME_VISIT, MEMBER_TYPE_INT64 }, 57 { MediaColumn::MEDIA_DURATION, MEMBER_TYPE_INT32 }, 58 { MediaColumn::MEDIA_TIME_PENDING, MEMBER_TYPE_INT64 }, 59 { MediaColumn::MEDIA_IS_FAV, MEMBER_TYPE_INT32 }, 60 { MediaColumn::MEDIA_DATE_TRASHED, MEMBER_TYPE_INT64 }, 61 { MediaColumn::MEDIA_HIDDEN, MEMBER_TYPE_INT32 }, 62 { MediaColumn::MEDIA_PARENT_ID, MEMBER_TYPE_INT32 }, 63 { MediaColumn::MEDIA_RELATIVE_PATH, MEMBER_TYPE_STRING }, 64 { MediaColumn::MEDIA_VIRTURL_PATH, MEMBER_TYPE_STRING }, 65 { PhotoColumn::PHOTO_ORIENTATION, MEMBER_TYPE_INT32 }, 66 { PhotoColumn::PHOTO_LATITUDE, MEMBER_TYPE_DOUBLE }, 67 { PhotoColumn::PHOTO_LONGITUDE, MEMBER_TYPE_DOUBLE }, 68 { PhotoColumn::PHOTO_HEIGHT, MEMBER_TYPE_INT32 }, 69 { PhotoColumn::PHOTO_WIDTH, MEMBER_TYPE_INT32 }, 70 { PhotoColumn::PHOTO_LCD_VISIT_TIME, MEMBER_TYPE_INT64 }, 71 { PhotoColumn::PHOTO_SUBTYPE, MEMBER_TYPE_INT32 }, 72 { AudioColumn::AUDIO_ALBUM, MEMBER_TYPE_STRING }, 73 { AudioColumn::AUDIO_ARTIST, MEMBER_TYPE_STRING } 74 }; 75 76 class MediaLibraryAssetOperations { 77 public: 78 static int32_t HandleInsertOperation(MediaLibraryCommand &cmd); 79 static int32_t CreateOperation(MediaLibraryCommand &cmd); 80 static int32_t DeleteOperation(MediaLibraryCommand &cmd); 81 static std::shared_ptr<NativeRdb::ResultSet> QueryOperation(MediaLibraryCommand &cmd, 82 const std::vector<std::string> &columns); 83 static int32_t UpdateOperation(MediaLibraryCommand &cmd); 84 static int32_t OpenOperation(MediaLibraryCommand &cmd, const std::string &mode); 85 static int32_t CloseOperation(MediaLibraryCommand &cmd); 86 static int32_t DeleteToolOperation(MediaLibraryCommand &cmd); 87 88 static int32_t CreateAssetBucket(int32_t fileId, int32_t &bucketNum); 89 90 protected: 91 static std::shared_ptr<FileAsset> GetFileAssetFromDb(const std::string &column, const std::string &value, 92 OperationObject oprnObject, const std::vector<std::string> &columns = {}, const std::string &networkId = ""); 93 static std::shared_ptr<FileAsset> GetFileAssetFromDb(NativeRdb::AbsPredicates &predicates, 94 OperationObject oprnObject, const std::vector<std::string> &columns = {}, const std::string &networkId = ""); 95 96 static int32_t InsertAssetInDb(MediaLibraryCommand &cmd, const FileAsset &fileAsset); 97 static int32_t CheckWithType(bool isContains, const std::string &displayName, 98 const std::string &extention, int32_t mediaType); 99 static int32_t CheckDisplayNameWithType(const std::string &displayName, int32_t mediaType); 100 static int32_t CheckExtWithType(const std::string &extention, int32_t mediaType); 101 static int32_t CheckRelativePathWithType(const std::string &relativePath, int32_t mediaType); 102 static void GetAssetRootDir(int32_t mediaType, std::string &rootDirPath); 103 static int32_t SetAssetPathInCreate(FileAsset &fileAsset); 104 static int32_t SetAssetPath(FileAsset &fileAsset, const std::string &extention); 105 static int32_t DeleteAssetInDb(MediaLibraryCommand &cmd); 106 107 static bool IsContainsValue(NativeRdb::ValuesBucket &values, const std::string &key); 108 static int32_t ModifyAssetInDb(MediaLibraryCommand &cmd); 109 static int32_t UpdateFileName(MediaLibraryCommand &cmd, const std::shared_ptr<FileAsset> &fileAsset, 110 bool &isNameChanged); 111 static int32_t SetUserComment(MediaLibraryCommand &cmd, const std::shared_ptr<FileAsset> &fileAsset); 112 static int32_t UpdateRelativePath(MediaLibraryCommand &cmd, const std::shared_ptr<FileAsset> &fileAsset, 113 bool &isNameChanged); 114 static void UpdateVirtualPath(MediaLibraryCommand &cmd, const std::shared_ptr<FileAsset> &fileAsset); 115 static int32_t UpdateFileInDb(MediaLibraryCommand &cmd); 116 static int32_t OpenAsset(const std::shared_ptr<FileAsset> &fileAsset, const std::string &mode, 117 MediaLibraryApi api); 118 static int32_t CloseAsset(const std::shared_ptr<FileAsset> &fileAsset, bool isCreateThumbSync = false); 119 static void InvalidateThumbnail(const std::string &fileId, int32_t mediaType); 120 static int32_t SendTrashNotify(MediaLibraryCommand &cmd, int32_t rowId, const std::string &extraUri = ""); 121 static void SendFavoriteNotify(MediaLibraryCommand &cmd, int32_t rowId, const std::string &extraUri = ""); 122 static int32_t SendHideNotify(MediaLibraryCommand &cmd, int32_t rowId, const std::string &extraUri = ""); 123 static int32_t SendModifyUserCommentNotify(MediaLibraryCommand &cmd, int32_t rowId, 124 const std::string &extraUri = ""); 125 static int32_t SetPendingStatus(MediaLibraryCommand &cmd); 126 static int32_t GrantUriPermission(const std::string &uri, const std::string &bundleName, 127 const std::string &path); 128 129 static bool GetInt32FromValuesBucket(const NativeRdb::ValuesBucket &values, const std::string &column, 130 int32_t &value); 131 static std::string CreateExtUriForV10Asset(FileAsset &fileAsset); 132 static bool GetStringFromValuesBucket(const NativeRdb::ValuesBucket &values, const std::string &column, 133 std::string &value); 134 135 private: 136 static int32_t CreateAssetUniqueId(int32_t type); 137 static int32_t CreateAssetRealName(int32_t fileId, int32_t mediaType, const std::string &extension, 138 std::string &name); 139 static int32_t CreateAssetPathById(int32_t fileId, int32_t mediaType, const std::string &extension, 140 std::string &filePath); 141 static void ScanFile(const std::string &path, bool isCreateThumbSync = false); 142 static int32_t SetPendingTrue(const std::shared_ptr<FileAsset> &fileAsset); 143 static int32_t SetPendingFalse(const std::shared_ptr<FileAsset> &fileAsset); 144 static std::shared_ptr<FileAsset> GetAssetFromResultSet(const std::shared_ptr<NativeRdb::ResultSet> &resultSet, 145 const std::vector<std::string> &columns); 146 147 static constexpr int ASSET_IN_BUCKET_NUM_MAX = 1000; 148 static constexpr int ASSET_DIR_START_NUM = 16; 149 static constexpr int ASSET_MAX_COMPLEMENT_ID = 999; 150 151 class ScanAssetCallback : public IMediaScannerCallback { 152 public: 153 ScanAssetCallback() = default; 154 ~ScanAssetCallback() = default; 155 int32_t OnScanFinished(const int32_t status, const std::string &uri, const std::string &path) override; SetSync(bool isSync)156 void SetSync(bool isSync) 157 { 158 isCreateThumbSync = isSync; 159 } 160 private: 161 bool isCreateThumbSync = false; 162 }; 163 }; 164 165 using VerifyFunction = bool (*) (NativeRdb::ValueObject&, MediaLibraryCommand&); 166 class AssetInputParamVerification { 167 public: 168 static bool CheckParamForUpdate(MediaLibraryCommand &cmd); 169 170 private: 171 static bool Forbidden(NativeRdb::ValueObject &value, MediaLibraryCommand &cmd); 172 static bool IsInt32(NativeRdb::ValueObject &value, MediaLibraryCommand &cmd); 173 static bool IsInt64(NativeRdb::ValueObject &value, MediaLibraryCommand &cmd); 174 static bool IsBool(NativeRdb::ValueObject &value, MediaLibraryCommand &cmd); 175 static bool IsString(NativeRdb::ValueObject &value, MediaLibraryCommand &cmd); 176 static bool IsDouble(NativeRdb::ValueObject &value, MediaLibraryCommand &cmd); 177 static bool IsBelowApi9(NativeRdb::ValueObject &value, MediaLibraryCommand &cmd); 178 static bool IsStringNotNull(NativeRdb::ValueObject &value, MediaLibraryCommand &cmd); 179 static bool IsUniqueValue(NativeRdb::ValueObject &value, MediaLibraryCommand &cmd); 180 181 static const std::unordered_map<std::string, std::vector<VerifyFunction>> UPDATE_VERIFY_PARAM_MAP; 182 }; 183 } // namespace Media 184 } // namespace OHOS 185 186 #endif // MEDIALIBRARY_FILE_OPERATIONS 187