• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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 BACKUP_FILE_UTILS_H
17 #define BACKUP_FILE_UTILS_H
18 
19 #include <memory>
20 #include <string>
21 
22 #include "backup_const.h"
23 #include "datashare_helper.h"
24 #include "image_packer.h"
25 #include "metadata.h"
26 
27 namespace OHOS {
28 namespace Media {
29 class FileAccessHelper {
30 public:
31     bool GetValidPath(std::string &filePath);
32 
33 private:
34     bool ConvertCurrentPath(std::string &curPath, std::string &resultPath);
35     std::map<std::string, std::string> pathMap = {};
36     std::mutex mapMutex;
37 };
38 class BackupFileUtils {
39 public:
40     static int32_t FillMetadata(std::unique_ptr<Metadata> &data);
41     static std::string GarbleFilePath(const std::string &filePath, int32_t sceneCode, std::string cloneFilePath = "");
42     static std::string GarbleFileName(const std::string &fileName);
43     static int32_t CreateAssetPathById(int32_t fileId, int32_t mediaType, const std::string &extension,
44         std::string &filePath);
45     static std::string GetFullPathByPrefixType(PrefixType prefixType, const std::string &relativePath);
46     static int32_t CreatePath(int32_t mediaType, const std::string &displayName, std::string &path);
47     static int32_t PreparePath(const std::string &path);
48     static int32_t MoveFile(const string &oldPath, const string &newPath, int32_t sceneCode);
49     static std::string GetReplacedPathByPrefixType(PrefixType srcPrefixType, PrefixType dstPrefixType,
50         const std::string &path);
51     static void ModifyFile(const std::string path, int64_t modifiedTime);
52     static std::string GetFileNameFromPath(const string &path);
53     static std::string GetFileTitle(const string &displayName);
54     static int32_t IsFileValid(std::string &filePath, int32_t sceneCode,
55         string relativePath = "", bool hasLowQualityImage = false);
56     static std::string GetDetailsPath(int32_t sceneCode, const std::string &type,
57         const std::unordered_map<std::string, FailedFileInfo> &failedFiles, size_t limit);
58     static std::string GetFailedFilesStr(int32_t sceneCode,
59         const std::unordered_map<std::string, FailedFileInfo> &failedFiles, size_t limit);
60     static std::vector<std::string> GetFailedFilesList(int32_t sceneCode,
61         const std::unordered_map<std::string, FailedFileInfo> &failedFiles, size_t limit);
62     static std::string GetFailedFile(int32_t sceneCode, const std::string &failedFilePath,
63         const FailedFileInfo &failedFileInfo);
64     static void CreateDataShareHelper(const sptr<IRemoteObject> &token);
65     static void GenerateThumbnailsAfterRestore(int32_t restoreAstcCount);
66     static bool GetPathPosByPrefixLevel(int32_t sceneCode, const std::string &path, int32_t prefixLevel, size_t &pos);
67     static bool ShouldIncludeSd(const std::string &prefix);
68     static void DeleteSdDatabase(const std::string &prefix);
69     static bool IsLivePhoto(const FileInfo &fileInfo);
70     static bool ConvertToMovingPhoto(FileInfo &fileInfo);
71     static string ConvertLowQualityPath(int32_t sceneCode, const std::string &filePath, const string &relativePath);
72     static void ParseResolution(const std::string &resolution, int32_t &width, int32_t &height);
73     static int32_t IsLowQualityImage(std::string &filePath, int32_t sceneCode,
74         string relativePath, bool hasLowQualityImage);
75     static size_t GetLastSlashPosFromPath(const std::string &path);
76     static std::string GetFileFolderFromPath(const std::string &path, bool shouldStartWithSlash = true);
77     static std::string GetExtraPrefixForRealPath(int32_t sceneCode, const std::string &path);
78     static bool IsAppTwinData(const std::string &path);
79     static int32_t GetUserId(const std::string &path);
80 
81     static bool HandleRotateImage(const std::string &sourceFile, const std::string &targetPath,
82         int32_t degrees, bool isLcd);
83 
84 private:
85     static const std::string IMAGE_FORMAT;
86     static const std::string LCD_FILE_NAME;
87     static const std::string THM_FILE_NAME;
88     static const uint8_t IMAGE_QUALITY;
89     static const uint32_t IMAGE_NUMBER_HINT;
90     static const int32_t IMAGE_MIN_BUF_SIZE;
91 
92     static std::shared_ptr<DataShare::DataShareHelper> sDataShareHelper_;
93     static int32_t GetFileMetadata(std::unique_ptr<Metadata> &data);
94     static int32_t CreateAssetRealName(int32_t fileId, int32_t mediaType, const std::string &extension,
95         std::string &name);
96     static std::shared_ptr<FileAccessHelper> fileAccessHelper_;
97 
98     static unique_ptr<ImageSource> LoadImageSource(const std::string &file, uint32_t &err);
99     static bool HandleHdrImage(std::unique_ptr<ImageSource> imageSource,
100         const std::string &targetPath, int32_t degrees, bool isLcd);
101     static bool EncodePicture(Picture &picture, const std::string &outFile);
102     static bool HandleSdrImage(std::unique_ptr<ImageSource> imageSource,
103         const std::string &targetPath, int32_t degrees, bool isLcd);
104     static bool EncodePixelMap(PixelMap &pixelMap, const std::string &outFile);
105     static bool ScalePixelMap(PixelMap &pixelMap, ImageSource &imageSource, const std::string &outFile);
106 };
107 } // namespace Media
108 } // namespace OHOS
109 
110 #endif  // BACKUP_FILE_UTILS_H