1 /* 2 * Copyright (C) 2022 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_MEDIALIBRARY_OBJECT_UTILS_H 17 #define OHOS_MEDIALIBRARY_OBJECT_UTILS_H 18 19 #include <string> 20 #include <vector> 21 22 #include "datashare_abs_result_set.h" 23 #include "datashare_values_bucket.h" 24 #include "dir_asset.h" 25 #include "file_asset.h" 26 #include "medialibrary_command.h" 27 #include "medialibrary_db_const.h" 28 #include "medialibrary_data_manager_utils.h" 29 #include "medialibrary_unistore_manager.h" 30 #include "native_album_asset.h" 31 #include "rdb_utils.h" 32 #include "uri.h" 33 #include "value_object.h" 34 #include "values_bucket.h" 35 36 namespace OHOS { 37 namespace Media { 38 const std::string MEDIA_NO_FILE = ".nofile"; 39 40 class MediaLibraryObjectUtils { 41 public: 42 static int32_t CreateFileObj(MediaLibraryCommand &cmd); 43 static int32_t CreateDirWithPath(const std::string &path); 44 static int32_t CreateDirObj(MediaLibraryCommand &cmd, int64_t &rowId); 45 static int32_t DeleteFileObj(const std::shared_ptr<FileAsset> &fileAsset); 46 static int32_t DeleteDirObj(const std::shared_ptr<FileAsset> &dirAsset); 47 static int32_t DeleteMisc(const int32_t fileId, const std::string &filePath, const int32_t parentId); 48 static int32_t RenameFileObj(MediaLibraryCommand &cmd, const std::string &srcFilePath, 49 const std::string &dstFilePath); 50 static int32_t RenameDirObj(MediaLibraryCommand &cmd, const std::string &srcDirPath, 51 const std::string &dstDirPath); 52 static int32_t OpenFile(MediaLibraryCommand &cmd, const std::string &mode); 53 static int32_t CloseFile(MediaLibraryCommand &cmd); 54 static void ScanFileAsync(const std::string &path, const std::string &id, MediaLibraryApi api); 55 static int32_t GetIdByPathFromDb(const std::string &path); 56 static std::string GetPathByIdFromDb(const std::string &id, const bool isDelete = false); 57 static std::string GetRecyclePathByIdFromDb(const std::string &id); 58 static int32_t GetParentIdByIdFromDb(const std::string &fileId); 59 static std::unique_ptr<FileAsset> GetFileAssetByPredicates(const NativeRdb::AbsRdbPredicates &predicates, 60 const std::vector<std::string> &columns); 61 static std::shared_ptr<FileAsset> GetFileAssetFromId(const std::string &id, const std::string &networkId = ""); 62 static std::shared_ptr<FileAsset> GetFileAssetFromUri(const std::string &uriStr); 63 static void GetDefaultRelativePath(const int32_t mediaType, string &relativePath); 64 65 static int32_t InsertInDb(MediaLibraryCommand &cmd); 66 static int32_t ModifyInfoByIdInDb(MediaLibraryCommand &cmd, const std::string &fileId = ""); 67 static int32_t DeleteInfoByIdInDb(MediaLibraryCommand &cmd, const std::string &fileId = ""); 68 static std::shared_ptr<NativeRdb::ResultSet> QueryWithCondition(MediaLibraryCommand &cmd, 69 const std::vector<std::string> &columns, const std::string &conditionColumn = ""); 70 71 static bool IsColumnValueExist(const std::string &value, const std::string &column); 72 static bool IsAssetExistInDb(const int32_t id, const bool isIncludeTrash = false); 73 static bool IsFileExistInDb(const std::string &path); 74 static bool CheckUriPending(const std::string &uri); 75 static int32_t CopyAsset(const std::shared_ptr<FileAsset> &srcFileAsset, const std::string &relativePath); 76 static int32_t CopyDir(const std::shared_ptr<FileAsset> &srcDirAsset, const std::string &relativePath); 77 static NativeAlbumAsset GetDirAsset(const std::string &relativePath); 78 static bool IsSmartAlbumExistInDb(const int32_t id); 79 static bool IsParentSmartAlbum(const int32_t id, const bool includeEmptyAlbum = false); 80 static int32_t CheckDirExtension(const std::string &destFilePath); 81 static int32_t CheckDirExtension(const std::string &relativePath, const std::string &displayName); 82 static int32_t UpdateDateModified(const std::string &dirPath); 83 static int32_t DeleteEmptyDirsRecursively(int32_t dirId); 84 static void ScanFile(const std::string &srcPath, MediaLibraryApi api = MediaLibraryApi::API_OLD); 85 static void InvalidateThumbnail(const string &id, const string &tableName = MEDIALIBRARY_TABLE, 86 const string &path = ""); 87 88 private: 89 static int32_t ModifyInfoByPathInDb(MediaLibraryCommand &cmd, const std::string &path); 90 static int32_t DeleteInfoByPathInDb(MediaLibraryCommand &cmd, const std::string &path); 91 static std::string GetStringColumnByIdFromDb(const std::string &id, 92 const std::string &column, const bool isDelete = false); 93 static int32_t InsertFileInDb(MediaLibraryCommand &cmd, const FileAsset &fileAsset, 94 const NativeAlbumAsset &dirAsset); 95 static int32_t DeleteInvalidRowInDb(const std::string &path); 96 static NativeAlbumAsset GetLastDirExistInDb(const std::string &dirPath); 97 static int32_t DeleteRows(const std::vector<int64_t> &rowIds); 98 static int32_t InsertDirToDbRecursively(const std::string &dirPath, int64_t &rowId); 99 static int32_t ProcessNoMediaFile(const std::string &dstFileName, const std::string &dstAlbumPath); 100 static int32_t ProcessHiddenFile(const std::string &dstFileName, const std::string &srcPath); 101 static int32_t ProcessHiddenDir(const std::string &dstDirName, const std::string &srcDirPath); 102 static int32_t UpdateFileInfoInDb(MediaLibraryCommand &cmd, const std::string &dstPath, 103 const int &bucketId, const std::string &bucketName); 104 static int32_t CopyAssetByFd(int32_t srcFd, int32_t srcId, int32_t destFd, int32_t destId); 105 static void CloseFileById(int32_t fileId); 106 static int32_t GetFileResult(std::shared_ptr<NativeRdb::ResultSet> &resultSet, 107 int count, const string &relativePath, const string &displayName); 108 static std::shared_ptr<NativeRdb::ResultSet> QuerySmartAlbum(MediaLibraryCommand &cmd); 109 static int32_t DeleteInfoRecursively(const shared_ptr<FileAsset> &fileAsset); 110 }; 111 } // namespace Media 112 } // namespace OHOS 113 114 #endif // OHOS_MEDIALIBRARY_OBJECT_UTILS_H 115