• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 };
84 
85 class MediaLibraryAssetOperations {
86 public:
87     static int32_t HandleInsertOperation(MediaLibraryCommand &cmd);
88     static int32_t DeleteOperation(MediaLibraryCommand &cmd);
89     static std::shared_ptr<NativeRdb::ResultSet> QueryOperation(MediaLibraryCommand &cmd,
90         const std::vector<std::string> &columns);
91     EXPORT static int32_t UpdateOperation(MediaLibraryCommand &cmd);
92     static int32_t OpenOperation(MediaLibraryCommand &cmd, const std::string &mode);
93     static int32_t DeleteToolOperation(MediaLibraryCommand &cmd);
94 
95     EXPORT static int32_t CreateAssetBucket(int32_t fileId, int32_t &bucketNum);
96     EXPORT static int32_t CreateAssetUniqueId(int32_t type,
97         std::shared_ptr<TransactionOperations> trans = nullptr);
98     EXPORT static int32_t CreateAssetPathById(int32_t fileId, int32_t mediaType, const std::string &extension,
99         std::string &filePath);
100     EXPORT static int32_t DeleteFromDisk(NativeRdb::AbsRdbPredicates &predicates, const bool isAging,
101         const bool compatible = false);
102     static std::string GetEditDataSourcePath(const std::string &path);
103     EXPORT static int32_t GetAlbumIdByPredicates(const std::string &whereClause,
104         const std::vector<std::string> &whereArgs);
105 
106 protected:
107     static std::shared_ptr<FileAsset> GetFileAssetFromDb(const std::string &column, const std::string &value,
108         OperationObject oprnObject, const std::vector<std::string> &columns = {}, const std::string &networkId = "");
109     static std::shared_ptr<FileAsset> GetFileAssetFromDb(NativeRdb::AbsPredicates &predicates,
110         OperationObject oprnObject, const std::vector<std::string> &columns = {}, const std::string &networkId = "");
111     static int32_t GetFileAssetVectorFromDb(NativeRdb::AbsPredicates &predicates, OperationObject oprnObject,
112         std::vector<std::shared_ptr<FileAsset>> &fileAssetVector, const std::vector<std::string> &columns = {},
113         const std::string &networkId = "");
114     static std::shared_ptr<FileAsset> GetFileAssetByUri(const std::string &fileUri, bool isPhoto,
115         const std::vector<std::string> &columns, const std::string &pendingStatus = "");
116 
117     static int32_t CreateOperation(MediaLibraryCommand &cmd);
118     static int32_t CloseOperation(MediaLibraryCommand &cmd);
119     static int32_t InsertAssetInDb(std::shared_ptr<TransactionOperations> trans,
120         MediaLibraryCommand &cmd, const FileAsset &fileAsset);
121     static int32_t CheckWithType(bool isContains, const std::string &displayName,
122          const std::string &extention, int32_t mediaType);
123     static int32_t CheckDisplayNameWithType(const std::string &displayName, int32_t mediaType);
124     static int32_t CheckExtWithType(const std::string &extention, int32_t mediaType);
125     static int32_t CheckRelativePathWithType(const std::string &relativePath, int32_t mediaType);
126     static void GetAssetRootDir(int32_t mediaType, std::string &rootDirPath);
127     EXPORT static int32_t SetAssetPathInCreate(FileAsset &fileAsset,
128         std::shared_ptr<TransactionOperations> trans = nullptr);
129     EXPORT static int32_t SetAssetPath(FileAsset &fileAsset, const std::string &extention,
130         std::shared_ptr<TransactionOperations> trans = nullptr);
131     static int32_t DeleteAssetInDb(MediaLibraryCommand &cmd);
132 
133     static int32_t UpdateFileName(MediaLibraryCommand &cmd, const std::shared_ptr<FileAsset> &fileAsset,
134         bool &isNameChanged);
135     static int32_t UpdateAllExif(MediaLibraryCommand &cmd, const std::shared_ptr<FileAsset> &fileAsset);
136     static int32_t SetUserComment(MediaLibraryCommand &cmd, const std::shared_ptr<FileAsset> &fileAsset);
137     static int32_t UpdateRelativePath(MediaLibraryCommand &cmd, const std::shared_ptr<FileAsset> &fileAsset,
138         bool &isNameChanged);
139     static void UpdateVirtualPath(MediaLibraryCommand &cmd, const std::shared_ptr<FileAsset> &fileAsset);
140     static int32_t UpdateFileInDb(MediaLibraryCommand &cmd);
141     static int32_t OpenAsset(const std::shared_ptr<FileAsset> &fileAsset, const std::string &mode,
142         MediaLibraryApi api, bool isMovingPhotoVideo = false);
143     static int32_t OpenHighlightCover(MediaLibraryCommand &cmd, const std::string &mode);
144     static int32_t CloseAsset(const std::shared_ptr<FileAsset> &fileAsset, bool isCreateThumbSync = false);
145     static int32_t OpenHighlightVideo(MediaLibraryCommand &cmd, const std::string &mode);
146     static void InvalidateThumbnail(const std::string &fileId, int32_t mediaType);
147     static int32_t SendTrashNotify(MediaLibraryCommand &cmd, int32_t rowId, const std::string &extraUri = "");
148     static void SendFavoriteNotify(MediaLibraryCommand &cmd, std::shared_ptr<FileAsset> &fileAsset,
149         const std::string &extraUri = "");
150     static void UpdateOwnerAlbumIdOnMove(MediaLibraryCommand &cmd, int32_t &targetAlbumId, int32_t &oriAlbumId);
151     static int32_t SendModifyUserCommentNotify(MediaLibraryCommand &cmd, int32_t rowId,
152         const std::string &extraUri = "");
153     static int32_t SetPendingStatus(MediaLibraryCommand &cmd);
154     static int32_t GrantUriPermission(const std::string &uri, const std::string &bundleName,
155         const std::string &path, bool isMovingPhoto = false);
156 
157     static bool GetInt32FromValuesBucket(const NativeRdb::ValuesBucket &values, const std::string &column,
158         int32_t &value);
159     EXPORT static std::string CreateExtUriForV10Asset(FileAsset &fileAsset);
160     static bool GetStringFromValuesBucket(const NativeRdb::ValuesBucket &values, const std::string &column,
161         std::string &value);
162     static int32_t OpenFileWithPrivacy(const std::string &filePath, const std::string &mode,
163         const std::string &fileId);
164     static void ScanFile(const std::string &path, bool isCreateThumbSync, bool isInvalidateThumb,
165         bool isForceScan = false, int32_t fileId = 0);
166     static void ScanFileWithoutAlbumUpdate(const std::string &path, bool isCreateThumbSync, bool isInvalidateThumb,
167         bool isForceScan = false, int32_t fileId = 0);
168 
169     static std::string GetEditDataDirPath(const std::string &path);
170     static std::string GetEditDataPath(const std::string &path);
171     static std::string GetEditDataCameraPath(const std::string &path);
172     static std::string GetAssetCacheDir();
173 
174 private:
175     static int32_t CreateAssetRealName(int32_t fileId, int32_t mediaType, const std::string &extension,
176         std::string &name);
177     static int32_t SetPendingTrue(const std::shared_ptr<FileAsset> &fileAsset);
178     static int32_t SetPendingFalse(const std::shared_ptr<FileAsset> &fileAsset);
179     static std::shared_ptr<FileAsset> GetAssetFromResultSet(const std::shared_ptr<NativeRdb::ResultSet> &resultSet,
180         const std::vector<std::string> &columns);
181 
182     static constexpr int ASSET_MAX_COMPLEMENT_ID = 999;
183 
184     class ScanAssetCallback : public IMediaScannerCallback {
185     public:
186         ScanAssetCallback() = default;
187         ~ScanAssetCallback() = default;
188         int32_t OnScanFinished(const int32_t status, const std::string &uri, const std::string &path) override;
SetSync(bool isSync)189         void SetSync(bool isSync)
190         {
191             isCreateThumbSync = isSync;
192         }
SetIsInvalidateThumb(bool isInvalidate)193         void SetIsInvalidateThumb(bool isInvalidate)
194         {
195             isInvalidateThumb = isInvalidate;
196         }
197     private:
198         bool isCreateThumbSync = false;
199         bool isInvalidateThumb = true;
200     };
201 };
202 
203 class DeleteNotifyAsyncTaskData : public AsyncTaskData {
204 public:
205     DeleteNotifyAsyncTaskData() = default;
206     virtual ~DeleteNotifyAsyncTaskData() override = default;
207     int32_t updateRows = 0;
208     std::vector<std::string> notifyUris;
209     std::string notifyUri;
210     int64_t trashDate = 0;
211 };
212 
213 using VerifyFunction = bool (*) (NativeRdb::ValueObject&, MediaLibraryCommand&);
214 class AssetInputParamVerification {
215 public:
216     static bool CheckParamForUpdate(MediaLibraryCommand &cmd);
217 
218 private:
219     static bool Forbidden(NativeRdb::ValueObject &value, MediaLibraryCommand &cmd);
220     static bool IsInt32(NativeRdb::ValueObject &value, MediaLibraryCommand &cmd);
221     static bool IsInt64(NativeRdb::ValueObject &value, MediaLibraryCommand &cmd);
222     static bool IsBool(NativeRdb::ValueObject &value, MediaLibraryCommand &cmd);
223     static bool IsString(NativeRdb::ValueObject &value, MediaLibraryCommand &cmd);
224     static bool IsDouble(NativeRdb::ValueObject &value, MediaLibraryCommand &cmd);
225     static bool IsBelowApi9(NativeRdb::ValueObject &value, MediaLibraryCommand &cmd);
226     static bool IsStringNotNull(NativeRdb::ValueObject &value, MediaLibraryCommand &cmd);
227     static bool IsUniqueValue(NativeRdb::ValueObject &value, MediaLibraryCommand &cmd);
228 
229     static const std::unordered_map<std::string, std::vector<VerifyFunction>> UPDATE_VERIFY_PARAM_MAP;
230 };
231 } // namespace Media
232 } // namespace OHOS
233 
234 #endif // MEDIALIBRARY_FILE_OPERATIONS
235