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 EXPORT const std::unordered_set<int32_t> PORTRAIT_PAGE_MODE = { 71 PortraitPages::FIRST_PAGE, 72 PortraitPages::SECOND_PAGE, 73 PortraitPages::FAVORITE_PAGE, 74 PortraitPages::UNFAVORITE_PAGE 75 }; 76 77 /** 78 * @brief Utility class for file operations 79 * 80 * @since 1.0 81 * @version 1.0 82 */ 83 class MediaFileUtils { 84 public: 85 EXPORT static bool IsFileExists(const std::string &fileName); 86 EXPORT static bool IsFileValid(const std::string &fileName); 87 EXPORT static bool IsDirEmpty(const std::string &path); 88 EXPORT static bool CreateFile(const std::string &filePath); 89 EXPORT static bool DeleteFile(const std::string &fileName); 90 EXPORT static bool DeleteDir(const std::string &dirName); 91 EXPORT static std::string GetFileName(const std::string &filePath); 92 EXPORT static std::string GetParentPath(const std::string &path); 93 EXPORT static std::string GetTitleFromDisplayName(const std::string &displayName); 94 EXPORT static bool IsDirectory(const std::string &dirName, std::shared_ptr<int> errCodePtr = nullptr); 95 EXPORT static bool MoveFile(const std::string &oldPath, const std::string &newPath); 96 EXPORT static bool CopyDirAndDelSrc(const std::string &srcPath, const std::string &destPath, 97 unsigned short curRecursionDepth = 0); 98 EXPORT static bool CopyFileAndDelSrc(const std::string &srcFile, const std::string &destFile); 99 EXPORT static bool CopyFileUtil(const std::string &filePath, const std::string &newPath); 100 EXPORT static bool WriteStrToFile(const std::string &filePath, const std::string &str); 101 EXPORT static bool ReadStrFromFile(const std::string &filePath, std::string &fileContent); 102 EXPORT static bool CopyFile(int32_t rfd, int32_t wfd); 103 EXPORT static bool RenameDir(const std::string &oldPath, const std::string &newPath); 104 EXPORT static bool CreateDirectory(const std::string &dirPath, std::shared_ptr<int> errCodePtr = nullptr); 105 EXPORT static int32_t CheckAlbumName(const std::string &albumName); 106 EXPORT static int32_t CheckDentryName(const std::string &dentryName); 107 EXPORT static int32_t CheckDisplayName(const std::string &displayName); 108 EXPORT static int32_t CheckFileDisplayName(const std::string &displayName); 109 EXPORT static int32_t CheckRelativePath(const std::string &relativePath); 110 EXPORT static void FormatRelativePath(std::string &relativePath); 111 EXPORT static void GetRootDirFromRelativePath(const std::string &relativePath, std::string &rootDir); 112 EXPORT static int64_t GetAlbumDateModified(const std::string &albumPath); 113 EXPORT static int64_t UTCTimeSeconds(); 114 EXPORT static int64_t UTCTimeMilliSeconds(); 115 EXPORT static int64_t UTCTimeNanoSeconds(); 116 EXPORT static std::string GetIdFromUri(const std::string &uri); 117 EXPORT static std::string GetNetworkIdFromUri(const std::string &uri); 118 EXPORT static std::string UpdatePath(const std::string &path, const std::string &uri); 119 EXPORT static MediaType GetMediaType(const std::string &filePath); 120 EXPORT static MediaType GetMediaTypeNotSupported(const std::string &filePath); 121 EXPORT static std::string SplitByChar(const std::string &str, const char split); 122 EXPORT static std::string GetExtensionFromPath(const std::string &path); 123 EXPORT static int32_t OpenFile(const std::string &path, const std::string &mode, 124 const std::string &clientBundleName = ""); 125 EXPORT static int32_t CreateAsset(const std::string &filePath); 126 EXPORT static int32_t ModifyAsset(const std::string &oldPath, const std::string &newPath); 127 EXPORT static int32_t OpenAsset(const std::string &filePath, const std::string &mode); 128 EXPORT static int32_t CloseAsset(int32_t fd); 129 EXPORT static std::string GetMediaTypeUri(MediaType mediaType); 130 EXPORT static std::string GetMediaTypeUriV10(MediaType mediaType); 131 EXPORT static bool CheckMode(const std::string &mode); 132 EXPORT static size_t FindIgnoreCase(const std::string &str, const std::string &key); 133 EXPORT static int64_t GetVirtualIdByType(int32_t id, MediaType type); 134 EXPORT static double GetRealIdByTable(int32_t virtualId, const std::string &tableName); 135 EXPORT static std::string GetVirtualUriFromRealUri(const std::string &uri, const std::string &extrUri = ""); 136 EXPORT static std::string GetRealUriFromVirtualUri(const std::string &uri); 137 EXPORT static bool StartsWith(const std::string &str, const std::string &prefix); 138 EXPORT static void UriAppendKeyValue(std::string &uri, const std::string &key, std::string value = "10"); 139 EXPORT static std::string GetExtraUri(const std::string &displayName, const std::string &path, 140 const bool isNeedEncode = true); 141 EXPORT static std::string GetUriByExtrConditions(const std::string &prefix, const std::string &fileId, 142 const std::string &suffix = ""); 143 EXPORT static std::string Encode(const std::string &uri); 144 EXPORT static bool CheckDisplayLevel(const int32_t &displayLevel); 145 EXPORT static std::string GetHighlightPath(const std::string &uri); 146 EXPORT static std::string GetHighlightVideoPath(const std::string &uri); 147 EXPORT static std::string GetTableNameByDisplayName(const std::string &displayName); 148 #ifdef MEDIALIBRARY_COMPATIBILITY 149 EXPORT static std::string GetTableFromVirtualUri(const std::string &uri); 150 #endif 151 EXPORT static bool IsUriV10(const std::string &mediaType); 152 EXPORT static bool IsFileTablePath(const std::string &path); 153 EXPORT static bool IsPhotoTablePath(const std::string &path); 154 EXPORT static std::string StrCreateTime(const std::string &format, int64_t time); 155 EXPORT static std::string StrCreateTimeByMilliseconds(const std::string &format, int64_t time); 156 EXPORT static std::string AddDocsToRelativePath(const std::string &relativePath); 157 EXPORT static std::string RemoveDocsFromRelativePath(const std::string &relativePath); 158 EXPORT static int64_t Timespec2Millisecond(const struct timespec &time); 159 EXPORT static std::string GetMovingPhotoVideoPath(const std::string &imagePath); 160 EXPORT static bool CheckMovingPhotoExtension(const std::string &extension); 161 EXPORT static bool IsMovingPhotoMimeType(const std::string &mimeType); 162 EXPORT static bool CheckMovingPhotoVideoExtension(const std::string &extension); 163 EXPORT static bool CheckMovingPhotoImage(const std::string &path); 164 EXPORT static bool CheckMovingPhotoVideo(const std::string &path); 165 EXPORT static bool CheckMovingPhotoVideo(const UniqueFd &uniqueFd); 166 EXPORT static bool CheckMovingPhotoVideoDuration(int32_t duration); 167 EXPORT static bool CheckMovingPhotoEffectMode(int32_t effectMode); 168 EXPORT static bool GetFileSize(const std::string &filePath, size_t &size); 169 EXPORT static bool SplitMovingPhotoUri(const std::string &uri, std::vector<std::string> &ret); 170 EXPORT static bool IsMediaLibraryUri(const std::string &uri); 171 EXPORT static void PrintStatInformation(const std::string& path); 172 EXPORT static void MediaFileDeletionRecord(); 173 EXPORT static void SetDeletionRecord(int fd, const std::string &fileName); 174 EXPORT static void BackupPhotoDir(); 175 EXPORT static void RecoverMediaTempDir(); 176 EXPORT static std::string DesensitizePath(const std::string &path); 177 EXPORT static void CheckDirStatus(const std::unordered_set<std::string> &dirCheckSet, const std::string &dir); 178 EXPORT static std::string GetUriWithoutDisplayname(const std::string &uri); 179 EXPORT static int32_t CreateDirectoryAndCopyFiles(const std::string &srcDir, const std::string &dstDir); 180 EXPORT static void ModifyFile(const std::string path, int64_t modifiedTime); 181 182 private: 183 static bool Mkdir(const std::string &subStr, std::shared_ptr<int> errCodePtr); 184 static int32_t RemoveDirectory(const std::string &path); 185 static int32_t CheckStringSize(const std::string &str, const size_t max); 186 }; 187 } // namespace OHOS::Media 188 189 #endif // FRAMEWORKS_INNERKITSIMPL_MEDIA_LIBRARY_INCLUDE_MEDIA_FILE_UTILS_H_ 190