1 /* 2 * Copyright (C) 2021-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 FRAMEWORKS_INNERKITSIMPL_MEDIA_LIBRARY_INCLUDE_MEDIA_FILE_UTILS_H_ 17 #define FRAMEWORKS_INNERKITSIMPL_MEDIA_LIBRARY_INCLUDE_MEDIA_FILE_UTILS_H_ 18 19 #include <memory> 20 #include <string> 21 #include <unordered_set> 22 23 #include "userfile_manager_types.h" 24 25 namespace OHOS::Media { 26 #define EXPORT __attribute__ ((visibility ("default"))) 27 EXPORT const std::string MEDIA_FILEMODE_READONLY = "r"; 28 EXPORT const std::string MEDIA_FILEMODE_WRITEONLY = "w"; 29 EXPORT const std::string MEDIA_FILEMODE_READWRITE = "rw"; 30 EXPORT const std::string MEDIA_FILEMODE_WRITETRUNCATE = "wt"; 31 EXPORT const std::string MEDIA_FILEMODE_WRITEAPPEND = "wa"; 32 EXPORT const std::string MEDIA_FILEMODE_READWRITETRUNCATE = "rwt"; 33 EXPORT const std::string MEDIA_FILEMODE_READWRITEAPPEND = "rwa"; 34 EXPORT const std::unordered_set<std::string> MEDIA_OPEN_MODES = { 35 MEDIA_FILEMODE_READONLY, 36 MEDIA_FILEMODE_WRITEONLY, 37 MEDIA_FILEMODE_READWRITE, 38 MEDIA_FILEMODE_WRITETRUNCATE, 39 MEDIA_FILEMODE_WRITEAPPEND, 40 MEDIA_FILEMODE_READWRITETRUNCATE, 41 MEDIA_FILEMODE_READWRITEAPPEND 42 }; 43 44 constexpr int VIRTUAL_ID_DIVIDER = 5; 45 constexpr int PHOTO_VIRTUAL_IDENTIFIER = 4; 46 constexpr int AUDIO_VIRTUAL_IDENTIFIER = 3; 47 constexpr int FILE_VIRTUAL_IDENTIFIER = 2; 48 constexpr int CAMERA_SHOT_KEY_SIZE = 30; 49 50 constexpr int64_t MSEC_TO_SEC = 1e3; 51 constexpr int64_t MSEC_TO_NSEC = 1e6; 52 53 enum EXPORT TrashType { 54 NOT_TRASHED = 0, 55 TRASHED_ASSET, 56 TRASHED_DIR, 57 TRASHED_DIR_CHILD 58 }; 59 60 enum EXPORT PortraitPages : int32_t { 61 UNFAVORITE_PAGE = 0, 62 FIRST_PAGE, 63 SECOND_PAGE, 64 FAVORITE_PAGE 65 }; 66 EXPORT const std::unordered_set<int32_t> PORTRAIT_PAGE_MODE = { 67 PortraitPages::FIRST_PAGE, 68 PortraitPages::SECOND_PAGE, 69 PortraitPages::FAVORITE_PAGE, 70 PortraitPages::UNFAVORITE_PAGE 71 }; 72 73 /** 74 * @brief Utility class for file operations 75 * 76 * @since 1.0 77 * @version 1.0 78 */ 79 class MediaFileUtils { 80 public: 81 EXPORT static bool IsFileExists(const std::string &fileName); 82 EXPORT static bool IsDirEmpty(const std::string &path); 83 EXPORT static bool CreateFile(const std::string &filePath); 84 EXPORT static bool DeleteFile(const std::string &fileName); 85 EXPORT static bool DeleteDir(const std::string &dirName); 86 EXPORT static std::string GetFileName(const std::string &filePath); 87 EXPORT static std::string GetParentPath(const std::string &path); 88 EXPORT static std::string GetTitleFromDisplayName(const std::string &displayName); 89 EXPORT static bool IsDirectory(const std::string &dirName, std::shared_ptr<int> errCodePtr = nullptr); 90 EXPORT static bool MoveFile(const std::string &oldPath, const std::string &newPath); 91 EXPORT static bool CopyFileUtil(const std::string &filePath, const std::string &newPath); 92 EXPORT static bool WriteStrToFile(const std::string &filePath, const std::string &str); 93 EXPORT static bool CopyFile(int32_t rfd, int32_t wfd); 94 EXPORT static bool RenameDir(const std::string &oldPath, const std::string &newPath); 95 EXPORT static bool CreateDirectory(const std::string &dirPath, std::shared_ptr<int> errCodePtr = nullptr); 96 EXPORT static int32_t CheckAlbumName(const std::string &albumName); 97 EXPORT static int32_t CheckDentryName(const std::string &dentryName); 98 EXPORT static int32_t CheckDisplayName(const std::string &displayName); 99 EXPORT static int32_t CheckFileDisplayName(const std::string &displayName); 100 EXPORT static int32_t CheckRelativePath(const std::string &relativePath); 101 EXPORT static void FormatRelativePath(std::string &relativePath); 102 EXPORT static void GetRootDirFromRelativePath(const std::string &relativePath, std::string &rootDir); 103 EXPORT static int64_t GetAlbumDateModified(const std::string &albumPath); 104 EXPORT static int64_t UTCTimeSeconds(); 105 EXPORT static int64_t UTCTimeMilliSeconds(); 106 EXPORT static int64_t UTCTimeNanoSeconds(); 107 EXPORT static std::string GetIdFromUri(const std::string &uri); 108 EXPORT static std::string GetNetworkIdFromUri(const std::string &uri); 109 EXPORT static std::string UpdatePath(const std::string &path, const std::string &uri); 110 EXPORT static MediaType GetMediaType(const std::string &filePath); 111 EXPORT static std::string SplitByChar(const std::string &str, const char split); 112 EXPORT static std::string GetExtensionFromPath(const std::string &path); 113 EXPORT static int32_t OpenFile(const std::string &path, const std::string &mode); 114 EXPORT static int32_t CreateAsset(const std::string &filePath); 115 EXPORT static int32_t ModifyAsset(const std::string &oldPath, const std::string &newPath); 116 EXPORT static int32_t OpenAsset(const std::string &filePath, const std::string &mode); 117 EXPORT static int32_t CloseAsset(int32_t fd); 118 EXPORT static std::string GetMediaTypeUri(MediaType mediaType); 119 EXPORT static std::string GetMediaTypeUriV10(MediaType mediaType); 120 EXPORT static bool CheckMode(const std::string &mode); 121 EXPORT static size_t FindIgnoreCase(const std::string &str, const std::string &key); 122 EXPORT static int64_t GetVirtualIdByType(int32_t id, MediaType type); 123 EXPORT static double GetRealIdByTable(int32_t virtualId, const std::string &tableName); 124 EXPORT static std::string GetVirtualUriFromRealUri(const std::string &uri, const std::string &extrUri = ""); 125 EXPORT static std::string GetRealUriFromVirtualUri(const std::string &uri); 126 EXPORT static bool StartsWith(const std::string &str, const std::string &prefix); 127 EXPORT static void UriAppendKeyValue(std::string &uri, const std::string &key, std::string value = "10"); 128 EXPORT static std::string GetExtraUri(const std::string &displayName, const std::string &path, 129 const bool isNeedEncode = true); 130 EXPORT static std::string GetUriByExtrConditions(const std::string &prefix, const std::string &fileId, 131 const std::string &suffix = ""); 132 EXPORT static std::string Encode(const std::string &uri); 133 EXPORT static bool CheckDisplayLevel(const int32_t &displayLevel); 134 #ifdef MEDIALIBRARY_COMPATIBILITY 135 EXPORT static std::string GetTableFromVirtualUri(const std::string &uri); 136 #endif 137 EXPORT static bool IsUriV10(const std::string &mediaType); 138 EXPORT static bool IsFileTablePath(const std::string &path); 139 EXPORT static bool IsPhotoTablePath(const std::string &path); 140 EXPORT static std::string StrCreateTime(const std::string &format, int64_t time); 141 EXPORT static std::string StrCreateTimeByMilliseconds(const std::string &format, int64_t time); 142 EXPORT static std::string AddDocsToRelativePath(const std::string &relativePath); 143 EXPORT static std::string RemoveDocsFromRelativePath(const std::string &relativePath); 144 EXPORT static int64_t Timespec2Millisecond(const struct timespec &time); 145 146 private: 147 static bool Mkdir(const std::string &subStr, std::shared_ptr<int> errCodePtr); 148 static int32_t RemoveDirectory(const std::string &path); 149 static int32_t CheckStringSize(const std::string &str, const size_t max); 150 }; 151 } // namespace OHOS::Media 152 153 #endif // FRAMEWORKS_INNERKITSIMPL_MEDIA_LIBRARY_INCLUDE_MEDIA_FILE_UTILS_H_ 154