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 exifRotate, bool isLcd); 83 static int32_t IsCloneCloudSyncSwitchOn(int32_t sceneCode); 84 static bool IsValidFile(const std::string &path); 85 static bool IsMovingPhotoExist(const std::string &path); 86 static bool HasOrientationOrExifRotate(const FileInfo &info); 87 88 private: 89 static const std::string IMAGE_FORMAT; 90 static const std::string LCD_FILE_NAME; 91 static const std::string THM_FILE_NAME; 92 static const uint8_t IMAGE_QUALITY; 93 static const uint32_t IMAGE_NUMBER_HINT; 94 static const int32_t IMAGE_MIN_BUF_SIZE; 95 96 static std::shared_ptr<DataShare::DataShareHelper> sDataShareHelper_; 97 static int32_t GetFileMetadata(std::unique_ptr<Metadata> &data); 98 static int32_t CreateAssetRealName(int32_t fileId, int32_t mediaType, const std::string &extension, 99 std::string &name); 100 static std::shared_ptr<FileAccessHelper> fileAccessHelper_; 101 102 static unique_ptr<ImageSource> LoadImageSource(const std::string &file, uint32_t &err); 103 static bool HandleHdrImage(std::unique_ptr<ImageSource> imageSource, 104 const std::string &targetPath, int32_t exifRotate, bool isLcd); 105 static bool EncodePicture(Picture &picture, const std::string &outFile); 106 static bool HandleSdrImage(std::unique_ptr<ImageSource> imageSource, 107 const std::string &targetPath, int32_t exifRotate, bool isLcd); 108 static bool EncodePixelMap(PixelMap &pixelMap, const std::string &outFile); 109 static bool ScalePixelMap(PixelMap &pixelMap, ImageSource &imageSource, const std::string &outFile); 110 }; 111 } // namespace Media 112 } // namespace OHOS 113 114 #endif // BACKUP_FILE_UTILS_H