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 CheckTitleName(const std::string &title);
108      EXPORT static std::string GetFileAssetUri(const std::string &fileAssetData, const std::string &displayName,
109          const int32_t &fileId);
110      EXPORT static int32_t CheckDisplayName(const std::string &displayName);
111      EXPORT static int32_t CheckFileDisplayName(const std::string &displayName);
112      EXPORT static int32_t CheckRelativePath(const std::string &relativePath);
113      EXPORT static void FormatRelativePath(std::string &relativePath);
114      EXPORT static void GetRootDirFromRelativePath(const std::string &relativePath, std::string &rootDir);
115      EXPORT static int64_t GetAlbumDateModified(const std::string &albumPath);
116      EXPORT static int64_t UTCTimeSeconds();
117      EXPORT static int64_t UTCTimeMilliSeconds();
118      EXPORT static int64_t UTCTimeNanoSeconds();
119      EXPORT static std::string GetIdFromUri(const std::string &uri);
120      EXPORT static std::string GetNetworkIdFromUri(const std::string &uri);
121      EXPORT static std::string UpdatePath(const std::string &path, const std::string &uri);
122      EXPORT static MediaType GetMediaType(const std::string &filePath);
123      EXPORT static MediaType GetMediaTypeNotSupported(const std::string &filePath);
124      EXPORT static std::string SplitByChar(const std::string &str, const char split);
125      EXPORT static std::string GetExtensionFromPath(const std::string &path);
126      EXPORT static int32_t OpenFile(const std::string &path, const std::string &mode,
127          const std::string &clientBundleName = "");
128      EXPORT static int32_t CreateAsset(const std::string &filePath);
129      EXPORT static int32_t ModifyAsset(const std::string &oldPath, const std::string &newPath);
130      EXPORT static int32_t OpenAsset(const std::string &filePath, const std::string &mode);
131      EXPORT static int32_t CloseAsset(int32_t fd);
132      EXPORT static std::string GetMediaTypeUri(MediaType mediaType);
133      EXPORT static std::string GetMediaTypeUriV10(MediaType mediaType);
134      EXPORT static bool CheckMode(const std::string &mode);
135      EXPORT static size_t FindIgnoreCase(const std::string &str, const std::string &key);
136      EXPORT static int64_t GetVirtualIdByType(int32_t id, MediaType type);
137      EXPORT static double GetRealIdByTable(int32_t virtualId, const std::string &tableName);
138      EXPORT static std::string GetVirtualUriFromRealUri(const std::string &uri, const std::string &extrUri = "");
139      EXPORT static std::string GetRealUriFromVirtualUri(const std::string &uri);
140      EXPORT static bool StartsWith(const std::string &str, const std::string &prefix);
141      EXPORT static void ReplaceAll(std::string &str, const std::string &from, const std::string &to);
142      EXPORT static void UriAppendKeyValue(std::string &uri, const std::string &key, std::string value = "10");
143      EXPORT static std::string GetExtraUri(const std::string &displayName, const std::string &path,
144          const bool isNeedEncode = true);
145      EXPORT static std::string GetUriByExtrConditions(const std::string &prefix, const std::string &fileId,
146          const std::string &suffix = "");
147      EXPORT static std::string Encode(const std::string &uri);
148      EXPORT static bool CheckDisplayLevel(const int32_t &displayLevel);
149      EXPORT static std::string GetHighlightPath(const std::string &uri);
150      EXPORT static std::string GetHighlightVideoPath(const std::string &uri);
151      EXPORT static std::string GetTableNameByDisplayName(const std::string &displayName);
152  #ifdef MEDIALIBRARY_COMPATIBILITY
153      EXPORT static std::string GetTableFromVirtualUri(const std::string &uri);
154  #endif
155      EXPORT static bool IsUriV10(const std::string &mediaType);
156      EXPORT static bool IsFileTablePath(const std::string &path);
157      EXPORT static bool IsPhotoTablePath(const std::string &path);
158      EXPORT static std::string StrCreateTime(const std::string &format, int64_t time);
159      EXPORT static std::string StrCreateTimeByMilliseconds(const std::string &format, int64_t time);
160      EXPORT static std::string AddDocsToRelativePath(const std::string &relativePath);
161      EXPORT static std::string RemoveDocsFromRelativePath(const std::string &relativePath);
162      EXPORT static int64_t Timespec2Millisecond(const struct timespec &time);
163      EXPORT static std::string GetMovingPhotoVideoPath(const std::string &imagePath);
164      EXPORT static bool CheckMovingPhotoExtension(const std::string &extension);
165      EXPORT static bool IsMovingPhotoMimeType(const std::string &mimeType);
166      EXPORT static bool CheckMovingPhotoVideoExtension(const std::string &extension);
167      EXPORT static bool CheckMovingPhotoImage(const std::string &path);
168      EXPORT static bool CheckMovingPhotoVideo(const std::string &path);
169      EXPORT static bool CheckMovingPhotoVideo(const UniqueFd &uniqueFd);
170      EXPORT static bool CheckMovingPhotoVideoDuration(int32_t duration);
171      EXPORT static bool CheckMovingPhotoEffectMode(int32_t effectMode);
172      EXPORT static bool GetFileSize(const std::string &filePath, size_t &size);
173      EXPORT static bool SplitMovingPhotoUri(const std::string &uri, std::vector<std::string> &ret);
174      EXPORT static bool IsMediaLibraryUri(const std::string &uri);
175      EXPORT static void PrintStatInformation(const std::string& path);
176      EXPORT static void MediaFileDeletionRecord();
177      EXPORT static void SetDeletionRecord(int fd, const std::string &fileName);
178      EXPORT static void BackupPhotoDir();
179      EXPORT static void RecoverMediaTempDir();
180      EXPORT static std::string DesensitizePath(const std::string &path);
181      EXPORT static void CheckDirStatus(const std::unordered_set<std::string> &dirCheckSet, const std::string &dir);
182      EXPORT static std::string GetUriWithoutDisplayname(const std::string &uri);
183      EXPORT static int32_t CreateDirectoryAndCopyFiles(const std::string &srcDir, const std::string &dstDir);
184      EXPORT static void ModifyFile(const std::string path, int64_t modifiedTime);
185      EXPORT static int32_t CopyDirectory(const std::string &srcDir, const std::string &dstDir);
186      EXPORT static bool GenerateKvStoreKey(const std::string &fileId, const std::string &dateKey, std::string &key);
187      EXPORT static bool IsCalledBySelf();
188      EXPORT static bool CheckSupportedWatermarkType(int32_t watermarkType);
189  
190  private:
191      static bool Mkdir(const std::string &subStr, std::shared_ptr<int> errCodePtr);
192      static int32_t RemoveDirectory(const std::string &path);
193      static int32_t CheckStringSize(const std::string &str, const size_t max);
194  };
195  } // namespace OHOS::Media
196  
197  #endif // FRAMEWORKS_INNERKITSIMPL_MEDIA_LIBRARY_INCLUDE_MEDIA_FILE_UTILS_H_
198