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