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 <string>
20 
21 #include "backup_const.h"
22 #include "datashare_helper.h"
23 #include "metadata.h"
24 
25 namespace OHOS {
26 namespace Media {
27 class FileAccessHelper {
28 public:
29     bool GetValidPath(std::string &filePath);
30 
31 private:
32     bool ConvertCurrentPath(std::string &curPath, std::string &resultPath);
33     std::map<std::string, std::string> pathMap = {};
34     std::mutex mapMutex;
35 };
36 class BackupFileUtils {
37 public:
38     static int32_t FillMetadata(std::unique_ptr<Metadata> &data);
39     static std::string GarbleFilePath(const std::string &filePath, int32_t sceneCode, std::string cloneFilePath = "");
40     static std::string GarbleFileName(const std::string &fileName);
41     static int32_t CreateAssetPathById(int32_t fileId, int32_t mediaType, const std::string &extension,
42         std::string &filePath);
43     static std::string GetFullPathByPrefixType(PrefixType prefixType, const std::string &relativePath);
44     static int32_t CreatePath(int32_t mediaType, const std::string &displayName, std::string &path);
45     static int32_t PreparePath(const std::string &path);
46     static std::string GetReplacedPathByPrefixType(PrefixType srcPrefixType, PrefixType dstPrefixType,
47         const std::string &path);
48     static int32_t MoveFile(const string &oldPath, const string &newPath, int32_t sceneCode);
49     static void ModifyFile(const std::string path, int64_t modifiedTime);
50     static bool IsFileValid(const std::string &filePath, int32_t sceneCode);
51     static std::string GetFileNameFromPath(const string &path);
52     static std::string GetFileTitle(const string &displayName);
53     static int32_t IsFileValid(std::string &filePath, int32_t sceneCode,
54         string relativePath = "", bool hasLowQualityImage = false);
55     static std::string GetDetailsPath(int32_t sceneCode, const std::string &type,
56         const std::unordered_map<std::string, FailedFileInfo> &failedFiles, size_t limit);
57     static std::string GetFailedFilesStr(int32_t sceneCode,
58         const std::unordered_map<std::string, FailedFileInfo> &failedFiles, size_t limit);
59     static std::vector<std::string> GetFailedFilesList(int32_t sceneCode,
60         const std::unordered_map<std::string, FailedFileInfo> &failedFiles, size_t limit);
61     static std::string GetFailedFile(int32_t sceneCode, const std::string &failedFilePath,
62         const FailedFileInfo &failedFileInfo);
63     static void CreateDataShareHelper(const sptr<IRemoteObject> &token);
64     static void GenerateThumbnailsAfterRestore();
65     static bool GetPathPosByPrefixLevel(int32_t sceneCode, const std::string &path, int32_t prefixLevel, size_t &pos);
66     static bool ShouldIncludeSd(const std::string &prefix);
67     static void DeleteSdDatabase(const std::string &prefix);
68     static string ConvertLowQualityPath(int32_t sceneCode, const std::string &filePath, const string &relativePath);
69     static int32_t IsLowQualityImage(std::string &filePath, int32_t sceneCode,
70         string relativePath, bool hasLowQualityImage);
71     static bool IsLivePhoto(const FileInfo &fileInfo);
72     static bool ConvertToMovingPhoto(FileInfo &fileInfo);
73     static size_t GetLastSlashPosFromPath(const std::string &path);
74     static std::string GetFileFolderFromPath(const std::string &path, bool shouldStartWithSlash = true);
75     static std::string GetExtraPrefixForRealPath(int32_t sceneCode, const std::string &path);
76     static bool IsAppTwinData(const std::string &path);
77     static int32_t GetUserId(const std::string &path);
78 
79 private:
80     static std::shared_ptr<DataShare::DataShareHelper> sDataShareHelper_;
81     static int32_t GetFileMetadata(std::unique_ptr<Metadata> &data);
82     static int32_t CreateAssetRealName(int32_t fileId, int32_t mediaType, const std::string &extension,
83         std::string &name);
84     static std::shared_ptr<FileAccessHelper> fileAccessHelper_;
85 };
86 } // namespace Media
87 } // namespace OHOS
88 
89 #endif  // BACKUP_FILE_UTILS_H