1 /* 2 * Copyright (C) 2023-2024 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 INTERFACES_INNERKITS_NATIVE_INCLUDE_MEDIA_FILE_URI_H_ 17 #define INTERFACES_INNERKITS_NATIVE_INCLUDE_MEDIA_FILE_URI_H_ 18 19 #include <string> 20 #include <unordered_map> 21 22 #include "medialibrary_db_const.h" 23 #include "uri.h" 24 #include "userfile_manager_types.h" 25 26 namespace OHOS { 27 namespace Media { 28 #define EXPORT __attribute__ ((visibility ("default"))) 29 30 const std::string ML_FILE_SCHEME = "file"; 31 const std::string ML_FILE_PREFIX = "file://"; 32 const std::string ML_FILE_URI_PREFIX = "file://media"; 33 const std::string ML_URI_NETWORKID = "networkid"; 34 const std::string ML_URI_NETWORKID_EQUAL = "?networkid="; 35 const std::string ML_URI_TIME_ID = "&time_id="; 36 const std::string ML_URI_OFFSET = "&offset="; 37 const std::string ML_URI_DATE_ADDED = "date_added"; 38 const std::string ML_URI_DATE_TAKEN = "date_taken"; 39 const std::string ML_URI_DATE_MODIFIED = "date_modified"; 40 const std::string ML_URI_AUTHORITY = "media"; 41 const std::string ML_DATA_SHARE_SCHEME = "datashare"; 42 const std::string MEDIALIBRARY_DATA_ABILITY_PREFIX = "datashare://"; 43 const std::string MEDIALIBRARY_DATA_URI_IDENTIFIER = "/media"; 44 const std::string MEDIALIBRARY_MEDIA_PREFIX = MEDIALIBRARY_DATA_ABILITY_PREFIX + 45 MEDIALIBRARY_DATA_URI_IDENTIFIER; 46 const std::string MEDIALIBRARY_TYPE_AUDIO_URI = "/audio"; 47 const std::string MEDIALIBRARY_TYPE_VIDEO_URI = "/video"; 48 const std::string MEDIALIBRARY_TYPE_IMAGE_URI = "/image"; 49 const std::string MEDIALIBRARY_TYPE_FILE_URI = "/file"; 50 const std::string MEDIALIBRARY_TYPE_ALBUM_URI = "/album"; 51 const std::string MEDIALIBRARY_TYPE_SMARTALBUM_CHANGE_URI = "/smartalbum"; 52 const std::string MEDIALIBRARY_TYPE_DEVICE_URI = "/device"; 53 const std::string MEDIALIBRARY_TYPE_SMART_URI = "/smart"; 54 const std::string MEDIALIBRARY_TYPE_HIGHLIGHT_URI = "/highlight"; 55 56 const std::string AUDIO_URI_PREFIX = ML_FILE_URI_PREFIX + MEDIALIBRARY_TYPE_AUDIO_URI; 57 const std::string VIDEO_URI_PREFIX = ML_FILE_URI_PREFIX + MEDIALIBRARY_TYPE_VIDEO_URI; 58 const std::string IMAGE_URI_PREFIX = ML_FILE_URI_PREFIX + MEDIALIBRARY_TYPE_IMAGE_URI; 59 const std::string FILE_URI_PREFIX = ML_FILE_URI_PREFIX + MEDIALIBRARY_TYPE_FILE_URI; 60 const std::string ALBUM_URI_PREFIX = ML_FILE_URI_PREFIX + MEDIALIBRARY_TYPE_ALBUM_URI; 61 const std::string HIGHLIGHT_URI_PREFIX = ML_FILE_URI_PREFIX + MEDIALIBRARY_TYPE_HIGHLIGHT_URI; 62 63 const std::string URI_TYPE_PHOTO = "Photo"; 64 const std::string URI_TYPE_AUDIO_V10 = "Audio"; 65 const std::string URI_TYPE_PHOTO_ALBUM = "PhotoAlbum"; 66 67 enum { 68 API10_PHOTO_URI, 69 API10_PHOTOALBUM_URI, 70 API10_AUDIO_URI, 71 API9_URI, 72 API10_ANALYSISALBUM_URI, 73 }; 74 75 const std::string MEDIA_FILE_URI_EMPTY = "empty"; 76 class MediaFileUri : public OHOS::Uri { 77 std::string networkId_ { MEDIA_FILE_URI_EMPTY }; 78 std::string fileId_ { MEDIA_FILE_URI_EMPTY }; 79 std::unordered_map<std::string, std::string> queryMap_; 80 std::string MediaFileUriConstruct(MediaType mediaType, const std::string &networkId, const std::string &fileId, 81 const int32_t &apiVersion, const std::string &extrUri); 82 int uriType_ {0}; 83 void ParseUri(const std::string& uri); 84 public: MediaFileUri(const std::string & uriStr)85 EXPORT explicit MediaFileUri(const std::string &uriStr) : Uri(uriStr) {ParseUri(uriStr);} 86 EXPORT explicit MediaFileUri(MediaType mediaType, 87 const std::string &fileId, 88 const std::string &networkId = "", 89 const int32_t &apiVersion = MEDIA_API_VERSION_V9, Uri(MediaFileUriConstruct (mediaType,fileId,networkId,apiVersion,extrUri))90 const std::string &extrUri = "") : Uri( 91 MediaFileUriConstruct(mediaType, fileId, networkId, apiVersion, extrUri)) {} 92 EXPORT ~MediaFileUri() = default; 93 94 EXPORT const std::string& GetNetworkId(); 95 EXPORT std::string GetFileId(); 96 EXPORT std::string GetFilePath(); 97 EXPORT std::unordered_map<std::string, std::string> &GetQueryKeys(); 98 EXPORT std::string GetTableName(); 99 EXPORT bool IsValid(); 100 EXPORT bool IsApi10(); 101 EXPORT int GetUriType(); 102 EXPORT static MediaType GetMediaTypeFromUri(const std::string &uri); 103 EXPORT static std::string GetPathFirstDentry(Uri &uri); 104 EXPORT static std::string GetPathSecondDentry(Uri &uri); 105 EXPORT static void RemoveAllFragment(std::string &uri); 106 EXPORT static std::string GetMediaTypeUri(MediaType mediaType, const int32_t &apiVersion); 107 EXPORT static std::string GetPhotoId(const std::string &uri); 108 EXPORT static void GetTimeIdFromUri(const std::vector<std::string> &uriBatch, 109 std::vector<std::string> &timeIdBatch); 110 EXPORT static void GetTimeIdFromUri(const std::vector<std::string> &uriBatch, 111 std::vector<std::string> &timeIdBatch, int32_t &start, int32_t &count); 112 EXPORT static int32_t CreateAssetBucket(int32_t fileId, int32_t &bucketNum); 113 EXPORT static std::string GetPathFromUri(const std::string &uri, bool isPhoto); 114 EXPORT static std::string GetPhotoUri(const std::string &fileId, const std::string &path, 115 const std::string &displayName); 116 }; 117 } // namespace Media 118 } // namespace OHOS 119 120 #endif // INTERFACES_INNERKITS_NATIVE_INCLUDE_MEDIA_FILE_URI_H_ 121