1 /* 2 * Copyright (c) 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 OHOS_AVSESSION_UTILS_H 17 #define OHOS_AVSESSION_UTILS_H 18 19 #include <cstdio> 20 #include <fstream> 21 #include <string> 22 #include <vector> 23 #include <regex> 24 25 #include "avsession_log.h" 26 #include "directory_ex.h" 27 28 namespace OHOS::AVSession { 29 class AVSessionUtils { 30 public: 31 static constexpr const int32_t MAX_FILE_SIZE = 4 * 1024 * 1024; 32 WriteImageToFile(const std::shared_ptr<AVSessionPixelMap> & innerPixelMap,const std::string & fileDir,const std::string & fileName)33 static void WriteImageToFile(const std::shared_ptr<AVSessionPixelMap>& innerPixelMap, 34 const std::string& fileDir, const std::string& fileName) 35 { 36 if (innerPixelMap == nullptr) { 37 SLOGE("innerPixelMap is nullptr"); 38 return; 39 } 40 41 char realPath[PATH_MAX] = { 0x00 }; 42 if (realpath(fileDir.c_str(), realPath) == nullptr 43 && !OHOS::ForceCreateDirectory(fileDir)) { 44 SLOGE("WriteImageToFile check and create path failed %{public}s", fileDir.c_str()); 45 return; 46 } 47 std::string filePath = fileDir + fileName; 48 49 std::vector<uint8_t> tempBuffer = innerPixelMap->GetInnerImgBuffer(); 50 size_t imgBufferSize = tempBuffer.size(); 51 SLOGI("write img to file with imgBufferSize=%{public}zu", imgBufferSize); 52 if (imgBufferSize > MAX_FILE_SIZE || imgBufferSize <= 0) { 53 SLOGE("error, dataSize larger than %{public}d or invalid", MAX_FILE_SIZE); 54 return; 55 } 56 57 std::ofstream ofile(filePath.c_str(), std::ios::binary | std::ios::out | std::ios::trunc); 58 if (!ofile.is_open()) { 59 SLOGE("open file error, filePath=%{public}s", filePath.c_str()); 60 return; 61 } 62 63 ofile.write((char*)&imgBufferSize, sizeof(size_t)); 64 SLOGI("write imgBuffer after write size %{public}zu", imgBufferSize); 65 ofile.write((char*)(&(tempBuffer[0])), imgBufferSize); 66 ofile.close(); 67 } 68 ReadImageFromFile(std::shared_ptr<AVSessionPixelMap> & innerPixelMap,const std::string & fileDir,const std::string & fileName)69 static void ReadImageFromFile(std::shared_ptr<AVSessionPixelMap>& innerPixelMap, 70 const std::string& fileDir, const std::string& fileName) 71 { 72 if (innerPixelMap == nullptr) { 73 SLOGE("innerPixelMap is nullptr"); 74 return; 75 } 76 77 char realPath[PATH_MAX] = { 0x00 }; 78 if (realpath(fileDir.c_str(), realPath) == nullptr) { 79 SLOGE("ReadImageFromFile check path failed %{public}s", fileDir.c_str()); 80 return; 81 } 82 std::string filePath = fileDir + fileName; 83 84 std::ifstream ifile(filePath.c_str(), std::ios::binary | std::ios::in); 85 if (!ifile.is_open()) { 86 SLOGE("open file error, filePath=%{public}s", filePath.c_str()); 87 return; 88 } 89 90 size_t imgBufferSize; 91 ifile.read((char*)&imgBufferSize, sizeof(size_t)); 92 SLOGI("imgBufferSize=%{public}zu", imgBufferSize); 93 if (imgBufferSize > MAX_FILE_SIZE || imgBufferSize <= 0) { 94 SLOGE("error, dataSize larger than %{public}d or invalid", MAX_FILE_SIZE); 95 ifile.close(); 96 return; 97 } 98 std::vector<std::uint8_t> imgBuffer(imgBufferSize); 99 ifile.read((char*)&imgBuffer[0], imgBufferSize); 100 SLOGD("imgBuffer prepare set"); 101 innerPixelMap->SetInnerImgBuffer(imgBuffer); 102 SLOGI("imgBuffer SetInnerImgBuffer done"); 103 ifile.close(); 104 } 105 DeleteFile(const std::string & filePath)106 static void DeleteFile(const std::string& filePath) 107 { 108 if (OHOS::RemoveFile(filePath)) { 109 SLOGI("remove .image.dat file success filePath=%{public}s", filePath.c_str()); 110 } else { 111 SLOGE("remove .image.dat file fail filePath=%{public}s", filePath.c_str()); 112 } 113 } 114 DeleteCacheFiles(const std::string & path)115 static void DeleteCacheFiles(const std::string& path) 116 { 117 std::vector<std::string> fileList; 118 OHOS::GetDirFiles(path, fileList); 119 for (const auto& file : fileList) { 120 if (file.find(AVSessionUtils::GetFileSuffix()) != std::string::npos) { 121 DeleteFile(file); 122 } 123 } 124 } 125 GetCachePathName()126 static std::string GetCachePathName() 127 { 128 return std::string(DATA_PATH_NAME) + PUBLIC_PATH_NAME + CACHE_PATH_NAME; 129 } 130 GetCachePathName(int32_t userId)131 static std::string GetCachePathName(int32_t userId) 132 { 133 return std::string(DATA_PATH_NAME) + std::to_string(userId) + CACHE_PATH_NAME; 134 } 135 GetFixedPathName()136 static std::string GetFixedPathName() 137 { 138 return std::string(DATA_PATH_NAME) + PUBLIC_PATH_NAME + FIXED_PATH_NAME; 139 } 140 GetFixedPathName(int32_t userId)141 static std::string GetFixedPathName(int32_t userId) 142 { 143 return std::string(DATA_PATH_NAME) + std::to_string(userId) + FIXED_PATH_NAME; 144 } 145 GetFileSuffix()146 static const char* GetFileSuffix() 147 { 148 return FILE_SUFFIX; 149 } 150 GetAnonySessionId(const std::string & sessionId)151 static std::string GetAnonySessionId(const std::string& sessionId) 152 { 153 constexpr size_t PRE_LEN = 3; 154 constexpr size_t MAX_LEN = 100; 155 std::string res; 156 std::string tmpStr("******"); 157 size_t len = sessionId.length(); 158 159 std::regex nameRegex("[\\w]*"); 160 if (len < PRE_LEN || len > MAX_LEN) { 161 SLOGE("GetAnonySessionId err length %{public}d", static_cast<int>(len)); 162 return "ERROR_LENGTH"; 163 } 164 if (!std::regex_match(sessionId, nameRegex)) { 165 SLOGE("GetAnonySessionId err content"); 166 return "ERROR_CONTENT"; 167 } 168 res.append(sessionId, 0, PRE_LEN).append(tmpStr).append(sessionId, len - PRE_LEN, PRE_LEN); 169 return res; 170 } 171 172 private: 173 static constexpr const char* DATA_PATH_NAME = "/data/service/el2/"; 174 static constexpr const char* CACHE_PATH_NAME = "/av_session/cache/"; 175 static constexpr const char* FIXED_PATH_NAME = "/av_session/"; 176 static constexpr const char* PUBLIC_PATH_NAME = "public"; 177 static constexpr const char* FILE_SUFFIX = ".image.dat"; 178 }; 179 } // namespace OHOS::AVSession 180 #endif // OHOS_AVSESSION_UTILS_H