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