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 "unique_fd.h" 24 #include "userfile_manager_types.h" 25 26 namespace OHOS::Media { 27 #define EXPORT __attribute__ ((visibility ("default"))) 28 EXPORT const std::string MOVING_PHOTO_URI_SPLIT = ";"; 29 EXPORT const uint8_t MOVING_PHOTO_IMAGE_POS = 0; 30 EXPORT const uint8_t MOVING_PHOTO_VIDEO_POS = 1; 31 EXPORT const std::string MEDIA_FILEMODE_READONLY = "r"; 32 EXPORT const std::string MEDIA_FILEMODE_WRITEONLY = "w"; 33 EXPORT const std::string MEDIA_FILEMODE_READWRITE = "rw"; 34 EXPORT const std::string MEDIA_FILEMODE_WRITETRUNCATE = "wt"; 35 EXPORT const std::string MEDIA_FILEMODE_WRITEAPPEND = "wa"; 36 EXPORT const std::string MEDIA_FILEMODE_READWRITETRUNCATE = "rwt"; 37 EXPORT const std::string MEDIA_FILEMODE_READWRITEAPPEND = "rwa"; 38 EXPORT const std::unordered_set<std::string> MEDIA_OPEN_MODES = { 39 MEDIA_FILEMODE_READONLY, 40 MEDIA_FILEMODE_WRITEONLY, 41 MEDIA_FILEMODE_READWRITE, 42 MEDIA_FILEMODE_WRITETRUNCATE, 43 MEDIA_FILEMODE_WRITEAPPEND, 44 MEDIA_FILEMODE_READWRITETRUNCATE, 45 MEDIA_FILEMODE_READWRITEAPPEND 46 }; 47 48 constexpr int VIRTUAL_ID_DIVIDER = 5; 49 constexpr int PHOTO_VIRTUAL_IDENTIFIER = 4; 50 constexpr int AUDIO_VIRTUAL_IDENTIFIER = 3; 51 constexpr int FILE_VIRTUAL_IDENTIFIER = 2; 52 constexpr int CAMERA_SHOT_KEY_SIZE = 30; 53 54 constexpr int64_t MSEC_TO_SEC = 1e3; 55 constexpr int64_t MSEC_TO_NSEC = 1e6; 56 57 enum EXPORT TrashType { 58 NOT_TRASHED = 0, 59 TRASHED_ASSET, 60 TRASHED_DIR, 61 TRASHED_DIR_CHILD 62 }; 63 64 enum EXPORT PortraitPages : int32_t { 65 UNFAVORITE_PAGE = 0, 66 FIRST_PAGE, 67 SECOND_PAGE, 68 FAVORITE_PAGE 69 }; 70 71 EXPORT const std::unordered_set<int32_t> PORTRAIT_PAGE_MODE = { 72 PortraitPages::FIRST_PAGE, 73 PortraitPages::SECOND_PAGE, 74 PortraitPages::FAVORITE_PAGE, 75 PortraitPages::UNFAVORITE_PAGE 76 }; 77 78 /** 79 * @brief Utility class for file operations 80 * 81 * @since 1.0 82 * @version 1.0 83 */ 84 class MediaFileUtils { 85 public: 86 EXPORT static bool IsFileExists(const std::string &fileName); 87 EXPORT static bool IsFileValid(const std::string &fileName); 88 EXPORT static bool IsDirEmpty(const std::string &path); 89 EXPORT static bool CreateFile(const std::string &filePath); 90 EXPORT static bool DeleteFile(const std::string &fileName); 91 EXPORT static bool DeleteFileWithRetry(const std::string &fileName); 92 EXPORT static bool DeleteDir(const std::string &dirName); 93 EXPORT static std::string GetFileName(const std::string &filePath); 94 EXPORT static std::string GetParentPath(const std::string &path); 95 EXPORT static std::string GetTitleFromDisplayName(const std::string &displayName); 96 EXPORT static bool IsDirectory(const std::string &dirName, std::shared_ptr<int> errCodePtr = nullptr); 97 EXPORT static bool MoveFile(const std::string &oldPath, const std::string &newPath, 98 bool isSupportCrossPolicy = false); 99 EXPORT static bool CopyDirAndDelSrc(const std::string &srcPath, const std::string &destPath, 100 unsigned short curRecursionDepth = 0); 101 EXPORT static bool CopyFileAndDelSrc(const std::string &srcFile, const std::string &destFile); 102 EXPORT static bool CopyFileUtil(const std::string &filePath, const std::string &newPath); 103 EXPORT static bool CopyFileSafe(const std::string &filePath, const std::string &newPath); 104 EXPORT static bool WriteStrToFile(const std::string &filePath, const std::string &str); 105 EXPORT static bool ReadStrFromFile(const std::string &filePath, std::string &fileContent); 106 EXPORT static bool CopyFile(int32_t rfd, int32_t wfd); 107 EXPORT static bool RenameDir(const std::string &oldPath, const std::string &newPath); 108 EXPORT static bool CreateDirectory(const std::string &dirPath, std::shared_ptr<int> errCodePtr = nullptr); 109 EXPORT static int32_t CheckAlbumName(const std::string &albumName); 110 EXPORT static int32_t CheckHighlightSubtitle(const std::string &highlightSubtitle); 111 EXPORT static int32_t CheckDentryName(const std::string &dentryName); 112 EXPORT static int32_t CheckDisplayName(const std::string &displayName, const bool compatibleCheckTitle = false); 113 EXPORT static int32_t CheckTitle(const std::string& title); 114 EXPORT static int32_t CheckTitleCompatible(const std::string& title); 115 EXPORT static int32_t CheckFileDisplayName(const std::string &displayName); 116 EXPORT static std::string GetFileAssetUri(const std::string &fileAssetData, const std::string &displayName, 117 const int32_t &fileId); 118 EXPORT static int32_t CheckRelativePath(const std::string &relativePath); 119 EXPORT static void FormatRelativePath(std::string &relativePath); 120 EXPORT static void GetRootDirFromRelativePath(const std::string &relativePath, std::string &rootDir); 121 EXPORT static int64_t GetAlbumDateModified(const std::string &albumPath); 122 EXPORT static int64_t UTCTimeSeconds(); 123 EXPORT static int64_t UTCTimeMilliSeconds(); 124 EXPORT static int64_t UTCTimeNanoSeconds(); 125 EXPORT static std::string GetIdFromUri(const std::string &uri); 126 EXPORT static std::string GetNetworkIdFromUri(const std::string &uri); 127 EXPORT static std::string UpdatePath(const std::string &path, const std::string &uri); 128 EXPORT static MediaType GetMediaType(const std::string &filePath); 129 EXPORT static MediaType GetMediaTypeNotSupported(const std::string &filePath); 130 EXPORT static std::string SplitByChar(const std::string &str, const char split); 131 EXPORT static std::string GetExtensionFromPath(const std::string &path); 132 EXPORT static int32_t OpenFile(const std::string &path, const std::string &mode, 133 const std::string &clientbundleName = ""); 134 EXPORT static int32_t CreateAsset(const std::string &filePath); 135 EXPORT static int32_t ModifyAsset(const std::string &oldPath, const std::string &newPath); 136 EXPORT static int32_t OpenAsset(const std::string &filePath, const std::string &mode); 137 EXPORT static int32_t CloseAsset(int32_t fd); 138 EXPORT static std::string GetMediaTypeUri(MediaType mediaType); 139 EXPORT static std::string GetMediaTypeUriV10(MediaType mediaType); 140 EXPORT static bool CheckMode(const std::string &mode); 141 EXPORT static size_t FindIgnoreCase(const std::string &str, const std::string &key); 142 EXPORT static int64_t GetVirtualIdByType(int32_t id, MediaType type); 143 EXPORT static double GetRealIdByTable(int32_t virtualId, const std::string &tableName); 144 EXPORT static std::string GetVirtualUriFromRealUri(const std::string &uri, const std::string &extrUri = ""); 145 EXPORT static std::string GetRealUriFromVirtualUri(const std::string &uri); 146 EXPORT static bool StartsWith(const std::string &str, const std::string &prefix); 147 EXPORT static bool EndsWith(const std::string &str, const std::string &suffix); 148 EXPORT static void ReplaceAll(std::string &str, const std::string &from, const std::string &to); 149 EXPORT static void UriAppendKeyValue(std::string &uri, const std::string &key, std::string value = "10"); 150 EXPORT static std::string GetExtraUri(const std::string &displayName, const std::string &path, 151 const bool isNeedEncode = true); 152 EXPORT static std::string GetUriByExtrConditions(const std::string &prefix, const std::string &fileId, 153 const std::string &suffix = ""); 154 EXPORT static std::string Encode(const std::string &uri); 155 EXPORT static bool CheckDisplayLevel(const int32_t &displayLevel); 156 EXPORT static std::string GetHighlightPath(const std::string &uri); 157 EXPORT static std::string GetHighlightVideoPath(const std::string &uri); 158 EXPORT static std::string GetTableNameByDisplayName(const std::string &displayName); 159 EXPORT static bool GetDateModified(const std::string &path, int64_t &dateModified); 160 #ifdef MEDIALIBRARY_COMPATIBILITY 161 EXPORT static std::string GetTableFromVirtualUri(const std::string &uri); 162 #endif 163 EXPORT static bool IsUriV10(const std::string &mediaType); 164 EXPORT static bool IsFileTablePath(const std::string &path); 165 EXPORT static bool IsPhotoTablePath(const std::string &path); 166 EXPORT static std::string StrCreateTime(const std::string &format, int64_t time); 167 EXPORT static std::string StrCreateTimeByMilliseconds(const std::string &format, int64_t time); 168 EXPORT static std::string AddDocsToRelativePath(const std::string &relativePath); 169 EXPORT static std::string RemoveDocsFromRelativePath(const std::string &relativePath); 170 EXPORT static int64_t Timespec2Millisecond(const struct timespec &time); 171 EXPORT static std::string GetTempMovingPhotoVideoPath(const std::string &imagePath); 172 EXPORT static std::string GetMovingPhotoVideoPath(const std::string &imagePath); 173 EXPORT static bool CheckMovingPhotoExtension(const std::string &extension); 174 EXPORT static bool IsMovingPhotoMimeType(const std::string &mimeType); 175 EXPORT static bool CheckMovingPhotoVideoExtension(const std::string &extension); 176 EXPORT static bool CheckMovingPhotoImage(const std::string &path); 177 EXPORT static bool CheckMovingPhotoVideo(const std::string &path); 178 EXPORT static bool CheckMovingPhotoVideo(const UniqueFd &uniqueFd); 179 EXPORT static bool CheckMovingPhotoVideoDuration(int32_t duration); 180 EXPORT static bool CheckMovingPhotoEffectMode(int32_t effectMode); 181 EXPORT static bool GetFileSize(const std::string& filePath, size_t& size); 182 EXPORT static bool SplitMovingPhotoUri(const std::string& uri, std::vector<std::string>& ret); 183 EXPORT static bool IsMediaLibraryUri(const std::string& uri); 184 EXPORT static void PrintStatInformation(const std::string& path); 185 EXPORT static void MediaFileDeletionRecord(); 186 EXPORT static void SetDeletionRecord(int fd, const std::string &fileName); 187 EXPORT static void BackupPhotoDir(); 188 EXPORT static void RecoverMediaTempDir(); 189 EXPORT static std::string DesensitizePath(const std::string &path); 190 EXPORT static void CheckDirStatus(const std::unordered_set<std::string> &dirCheckSet, const std::string &dir); 191 EXPORT static int32_t CreateDirectoryAndCopyFiles(const std::string &srcDir, const std::string &dstDir); 192 EXPORT static void ModifyFile(const std::string path, int64_t modifiedTime); 193 EXPORT static std::string GetUriWithoutDisplayname(const std::string &uri); 194 EXPORT static bool CheckSupportedWatermarkType(int32_t watermarkType); 195 EXPORT static int32_t CopyDirectory(const std::string &srcDir, const std::string &dstDir); 196 EXPORT static bool GenerateKvStoreKey(const std::string &fileId, const std::string &dateKey, std::string &key); 197 EXPORT static bool IsCalledBySelf(); 198 EXPORT static std::vector<std::string> GetAllTypes(const int32_t extension); 199 EXPORT static bool IsValidInteger(const std::string &value); 200 EXPORT static int32_t CreateAssetRealName( 201 int32_t fileId, int32_t mediaType, const std::string &extension, std::string &name); 202 EXPORT static int64_t GetTotalSize(); 203 EXPORT static int64_t GetFreeSize(); 204 EXPORT static void StatDirSize(const std::string& rootPath, size_t& totalSize); 205 EXPORT static std::string GetMimeTypeFromDisplayName(const std::string &displayName); 206 EXPORT static std::string DesensitizeUri(const std::string &fileUri); 207 EXPORT static bool DeleteFileOrFolder(const std::string &path, bool isFile); 208 EXPORT static std::string GetReplacedPathByPrefix(const std::string srcPrefix, const std::string dstPrefix, 209 const std::string &path); 210 EXPORT static bool ConvertFormatCopy(const std::string &srcFile, const std::string &dstFile, 211 const std::string &extension); 212 EXPORT static bool ConvertFormatExtraDataDirectory(const std::string &srcDir, const std::string &dstDir, 213 const std::string &extension); 214 EXPORT static int64_t GetFileModificationTime(const std::string &path); 215 EXPORT static int64_t StrToInt64(const std::string &value); 216 EXPORT static bool IsDirExists(const std::string &path); 217 218 private: 219 static bool Mkdir(const std::string &subStr, std::shared_ptr<int> errCodePtr); 220 static int32_t RemoveDirectory(const std::string &path); 221 static int32_t CheckStringSize(const std::string &str, const size_t max); 222 }; 223 } // namespace OHOS::Media 224 225 #endif // FRAMEWORKS_INNERKITSIMPL_MEDIA_LIBRARY_INCLUDE_MEDIA_FILE_UTILS_H_ 226