180922886Sopenharmony_ci/* 280922886Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd. 380922886Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 480922886Sopenharmony_ci * you may not use this file except in compliance with the License. 580922886Sopenharmony_ci * You may obtain a copy of the License at 680922886Sopenharmony_ci * 780922886Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 880922886Sopenharmony_ci * 980922886Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1080922886Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1180922886Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1280922886Sopenharmony_ci * See the License for the specific language governing permissions and 1380922886Sopenharmony_ci * limitations under the License. 1480922886Sopenharmony_ci */ 1580922886Sopenharmony_ci 1680922886Sopenharmony_ci#ifndef OHOS_AVSESSION_UTILS_H 1780922886Sopenharmony_ci#define OHOS_AVSESSION_UTILS_H 1880922886Sopenharmony_ci 1980922886Sopenharmony_ci#include <cstdio> 2080922886Sopenharmony_ci#include <fstream> 2180922886Sopenharmony_ci#include <string> 2280922886Sopenharmony_ci#include <vector> 2380922886Sopenharmony_ci#include <regex> 2480922886Sopenharmony_ci 2580922886Sopenharmony_ci#include "avsession_log.h" 2680922886Sopenharmony_ci#include "directory_ex.h" 2780922886Sopenharmony_ci 2880922886Sopenharmony_cinamespace OHOS::AVSession { 2980922886Sopenharmony_ciclass AVSessionUtils { 3080922886Sopenharmony_cipublic: 3180922886Sopenharmony_ci static constexpr const int32_t MAX_FILE_SIZE = 4 * 1024 * 1024; 3280922886Sopenharmony_ci 3380922886Sopenharmony_ci static void WriteImageToFile(const std::shared_ptr<AVSessionPixelMap>& innerPixelMap, 3480922886Sopenharmony_ci const std::string& fileDir, const std::string& fileName) 3580922886Sopenharmony_ci { 3680922886Sopenharmony_ci if (innerPixelMap == nullptr) { 3780922886Sopenharmony_ci SLOGE("innerPixelMap is nullptr"); 3880922886Sopenharmony_ci return; 3980922886Sopenharmony_ci } 4080922886Sopenharmony_ci 4180922886Sopenharmony_ci char realPath[PATH_MAX] = { 0x00 }; 4280922886Sopenharmony_ci if (realpath(fileDir.c_str(), realPath) == nullptr 4380922886Sopenharmony_ci && !OHOS::ForceCreateDirectory(fileDir)) { 4480922886Sopenharmony_ci SLOGE("WriteImageToFile check and create path failed %{public}s", fileDir.c_str()); 4580922886Sopenharmony_ci return; 4680922886Sopenharmony_ci } 4780922886Sopenharmony_ci std::string filePath = fileDir + fileName; 4880922886Sopenharmony_ci 4980922886Sopenharmony_ci std::vector<uint8_t> tempBuffer = innerPixelMap->GetInnerImgBuffer(); 5080922886Sopenharmony_ci size_t imgBufferSize = tempBuffer.size(); 5180922886Sopenharmony_ci SLOGI("write img to file with imgBufferSize=%{public}zu", imgBufferSize); 5280922886Sopenharmony_ci if (imgBufferSize > MAX_FILE_SIZE || imgBufferSize <= 0) { 5380922886Sopenharmony_ci SLOGE("error, dataSize larger than %{public}d or invalid", MAX_FILE_SIZE); 5480922886Sopenharmony_ci return; 5580922886Sopenharmony_ci } 5680922886Sopenharmony_ci 5780922886Sopenharmony_ci std::ofstream ofile(filePath.c_str(), std::ios::binary | std::ios::out | std::ios::trunc); 5880922886Sopenharmony_ci if (!ofile.is_open()) { 5980922886Sopenharmony_ci SLOGE("open file error, filePath=%{public}s", filePath.c_str()); 6080922886Sopenharmony_ci return; 6180922886Sopenharmony_ci } 6280922886Sopenharmony_ci 6380922886Sopenharmony_ci ofile.write((char*)&imgBufferSize, sizeof(size_t)); 6480922886Sopenharmony_ci SLOGI("write imgBuffer after write size %{public}zu", imgBufferSize); 6580922886Sopenharmony_ci ofile.write((char*)(&(tempBuffer[0])), imgBufferSize); 6680922886Sopenharmony_ci ofile.close(); 6780922886Sopenharmony_ci } 6880922886Sopenharmony_ci 6980922886Sopenharmony_ci static void ReadImageFromFile(std::shared_ptr<AVSessionPixelMap>& innerPixelMap, 7080922886Sopenharmony_ci const std::string& fileDir, const std::string& fileName) 7180922886Sopenharmony_ci { 7280922886Sopenharmony_ci if (innerPixelMap == nullptr) { 7380922886Sopenharmony_ci SLOGE("innerPixelMap is nullptr"); 7480922886Sopenharmony_ci return; 7580922886Sopenharmony_ci } 7680922886Sopenharmony_ci 7780922886Sopenharmony_ci char realPath[PATH_MAX] = { 0x00 }; 7880922886Sopenharmony_ci if (realpath(fileDir.c_str(), realPath) == nullptr) { 7980922886Sopenharmony_ci SLOGE("ReadImageFromFile check path failed %{public}s", fileDir.c_str()); 8080922886Sopenharmony_ci return; 8180922886Sopenharmony_ci } 8280922886Sopenharmony_ci std::string filePath = fileDir + fileName; 8380922886Sopenharmony_ci 8480922886Sopenharmony_ci std::ifstream ifile(filePath.c_str(), std::ios::binary | std::ios::in); 8580922886Sopenharmony_ci if (!ifile.is_open()) { 8680922886Sopenharmony_ci SLOGE("open file error, filePath=%{public}s", filePath.c_str()); 8780922886Sopenharmony_ci return; 8880922886Sopenharmony_ci } 8980922886Sopenharmony_ci 9080922886Sopenharmony_ci size_t imgBufferSize; 9180922886Sopenharmony_ci ifile.read((char*)&imgBufferSize, sizeof(size_t)); 9280922886Sopenharmony_ci SLOGI("imgBufferSize=%{public}zu", imgBufferSize); 9380922886Sopenharmony_ci if (imgBufferSize > MAX_FILE_SIZE || imgBufferSize <= 0) { 9480922886Sopenharmony_ci SLOGE("error, dataSize larger than %{public}d or invalid", MAX_FILE_SIZE); 9580922886Sopenharmony_ci ifile.close(); 9680922886Sopenharmony_ci return; 9780922886Sopenharmony_ci } 9880922886Sopenharmony_ci std::vector<std::uint8_t> imgBuffer(imgBufferSize); 9980922886Sopenharmony_ci ifile.read((char*)&imgBuffer[0], imgBufferSize); 10080922886Sopenharmony_ci SLOGD("imgBuffer prepare set"); 10180922886Sopenharmony_ci innerPixelMap->SetInnerImgBuffer(imgBuffer); 10280922886Sopenharmony_ci SLOGI("imgBuffer SetInnerImgBuffer done"); 10380922886Sopenharmony_ci ifile.close(); 10480922886Sopenharmony_ci } 10580922886Sopenharmony_ci 10680922886Sopenharmony_ci static void DeleteFile(const std::string& filePath) 10780922886Sopenharmony_ci { 10880922886Sopenharmony_ci if (OHOS::RemoveFile(filePath)) { 10980922886Sopenharmony_ci SLOGI("remove .image.dat file success filePath=%{public}s", filePath.c_str()); 11080922886Sopenharmony_ci } else { 11180922886Sopenharmony_ci SLOGE("remove .image.dat file fail filePath=%{public}s", filePath.c_str()); 11280922886Sopenharmony_ci } 11380922886Sopenharmony_ci } 11480922886Sopenharmony_ci 11580922886Sopenharmony_ci static void DeleteCacheFiles(const std::string& path) 11680922886Sopenharmony_ci { 11780922886Sopenharmony_ci std::vector<std::string> fileList; 11880922886Sopenharmony_ci OHOS::GetDirFiles(path, fileList); 11980922886Sopenharmony_ci for (const auto& file : fileList) { 12080922886Sopenharmony_ci if (file.find(AVSessionUtils::GetFileSuffix()) != std::string::npos) { 12180922886Sopenharmony_ci DeleteFile(file); 12280922886Sopenharmony_ci } 12380922886Sopenharmony_ci } 12480922886Sopenharmony_ci } 12580922886Sopenharmony_ci 12680922886Sopenharmony_ci static std::string GetCachePathName() 12780922886Sopenharmony_ci { 12880922886Sopenharmony_ci return std::string(DATA_PATH_NAME) + PUBLIC_PATH_NAME + CACHE_PATH_NAME; 12980922886Sopenharmony_ci } 13080922886Sopenharmony_ci 13180922886Sopenharmony_ci static std::string GetCachePathName(int32_t userId) 13280922886Sopenharmony_ci { 13380922886Sopenharmony_ci return std::string(DATA_PATH_NAME) + std::to_string(userId) + CACHE_PATH_NAME; 13480922886Sopenharmony_ci } 13580922886Sopenharmony_ci 13680922886Sopenharmony_ci static std::string GetFixedPathName() 13780922886Sopenharmony_ci { 13880922886Sopenharmony_ci return std::string(DATA_PATH_NAME) + PUBLIC_PATH_NAME + FIXED_PATH_NAME; 13980922886Sopenharmony_ci } 14080922886Sopenharmony_ci 14180922886Sopenharmony_ci static std::string GetFixedPathName(int32_t userId) 14280922886Sopenharmony_ci { 14380922886Sopenharmony_ci return std::string(DATA_PATH_NAME) + std::to_string(userId) + FIXED_PATH_NAME; 14480922886Sopenharmony_ci } 14580922886Sopenharmony_ci 14680922886Sopenharmony_ci static const char* GetFileSuffix() 14780922886Sopenharmony_ci { 14880922886Sopenharmony_ci return FILE_SUFFIX; 14980922886Sopenharmony_ci } 15080922886Sopenharmony_ci 15180922886Sopenharmony_ci static std::string GetAnonySessionId(const std::string& sessionId) 15280922886Sopenharmony_ci { 15380922886Sopenharmony_ci constexpr size_t PRE_LEN = 3; 15480922886Sopenharmony_ci constexpr size_t MAX_LEN = 100; 15580922886Sopenharmony_ci std::string res; 15680922886Sopenharmony_ci std::string tmpStr("******"); 15780922886Sopenharmony_ci size_t len = sessionId.length(); 15880922886Sopenharmony_ci 15980922886Sopenharmony_ci std::regex nameRegex("[\\w]*"); 16080922886Sopenharmony_ci if (len < PRE_LEN || len > MAX_LEN) { 16180922886Sopenharmony_ci SLOGE("GetAnonySessionId err length %{public}d", static_cast<int>(len)); 16280922886Sopenharmony_ci return "ERROR_LENGTH"; 16380922886Sopenharmony_ci } 16480922886Sopenharmony_ci if (!std::regex_match(sessionId, nameRegex)) { 16580922886Sopenharmony_ci SLOGE("GetAnonySessionId err content"); 16680922886Sopenharmony_ci return "ERROR_CONTENT"; 16780922886Sopenharmony_ci } 16880922886Sopenharmony_ci res.append(sessionId, 0, PRE_LEN).append(tmpStr).append(sessionId, len - PRE_LEN, PRE_LEN); 16980922886Sopenharmony_ci return res; 17080922886Sopenharmony_ci } 17180922886Sopenharmony_ci 17280922886Sopenharmony_ciprivate: 17380922886Sopenharmony_ci static constexpr const char* DATA_PATH_NAME = "/data/service/el2/"; 17480922886Sopenharmony_ci static constexpr const char* CACHE_PATH_NAME = "/av_session/cache/"; 17580922886Sopenharmony_ci static constexpr const char* FIXED_PATH_NAME = "/av_session/"; 17680922886Sopenharmony_ci static constexpr const char* PUBLIC_PATH_NAME = "public"; 17780922886Sopenharmony_ci static constexpr const char* FILE_SUFFIX = ".image.dat"; 17880922886Sopenharmony_ci}; 17980922886Sopenharmony_ci} // namespace OHOS::AVSession 18080922886Sopenharmony_ci#endif // OHOS_AVSESSION_UTILS_H