1 /* 2 * Copyright (C) 2021-2022 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 <string> 20 #include <unordered_set> 21 22 #include "userfile_manager_types.h" 23 24 namespace OHOS { 25 namespace Media { 26 const std::string MEDIA_FILEMODE_READONLY = "r"; 27 const std::string MEDIA_FILEMODE_WRITEONLY = "w"; 28 const std::string MEDIA_FILEMODE_READWRITE = "rw"; 29 const std::string MEDIA_FILEMODE_WRITETRUNCATE = "wt"; 30 const std::string MEDIA_FILEMODE_WRITEAPPEND = "wa"; 31 const std::string MEDIA_FILEMODE_READWRITETRUNCATE = "rwt"; 32 const std::string MEDIA_FILEMODE_READWRITEAPPEND = "rwa"; 33 const std::unordered_set<std::string> MEDIA_OPEN_MODES = { 34 MEDIA_FILEMODE_READONLY, 35 MEDIA_FILEMODE_WRITEONLY, 36 MEDIA_FILEMODE_READWRITE, 37 MEDIA_FILEMODE_WRITETRUNCATE, 38 MEDIA_FILEMODE_WRITEAPPEND, 39 MEDIA_FILEMODE_READWRITETRUNCATE, 40 MEDIA_FILEMODE_READWRITEAPPEND 41 }; 42 43 /** 44 * @brief Utility class for file operations 45 * 46 * @since 1.0 47 * @version 1.0 48 */ 49 class MediaFileUtils { 50 public: 51 static bool IsFileExists(const std::string &fileName); 52 static bool IsDirEmpty(const std::string &path); 53 static bool CreateFile(const std::string &fileName); 54 static bool DeleteFile(const std::string &fileName); 55 static bool DeleteDir(const std::string &dirName); 56 static int32_t RemoveDirectory(const std::string &path); 57 static std::string GetFilename(const std::string &filePath); 58 static std::string GetParentPath(const std::string &path); 59 static bool IsDirectory(const std::string &dirName); 60 static bool MoveFile(const std::string &oldPath, const std::string &newPath); 61 static bool CopyFile(const std::string &filePath, const std::string &newPath); 62 static bool RenameDir(const std::string &oldPath, const std::string &newPath); 63 static bool CreateDirectory(const std::string &dirPath); 64 static bool CheckDisplayName(const std::string &displayName); 65 static bool CheckTitle(const std::string &title); 66 static int64_t GetAlbumDateModified(const std::string &albumPath); 67 static int64_t UTCTimeSeconds(); 68 static std::string GetNetworkIdFromUri(const std::string &uri); 69 static std::string UpdatePath(const std::string &path, const std::string &uri); 70 static std::string GetFileMediaTypeUri(int32_t mediaType, const std::string &networkId); 71 static std::string GetUriByNameAndId(const std::string &displayName, const std::string &networkId, int32_t id); 72 static MediaType GetMediaType(const std::string &filePath); 73 static std::string SplitByChar(const std::string &str, const char split); 74 static std::string GetExtensionFromPath(const std::string &path); 75 static int32_t OpenFile(const std::string &path, const std::string &mode); 76 }; 77 } // namespace Media 78 } // namespace OHOS 79 80 #endif // FRAMEWORKS_INNERKITSIMPL_MEDIA_LIBRARY_INCLUDE_MEDIA_FILE_UTILS_H_ 81