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