• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 FOUNDATION_ACE_ADAPTER_PREVIEW_ENTRANCE_RS_DIR_ASSET_PROVIDER_H
17 #define FOUNDATION_ACE_ADAPTER_PREVIEW_ENTRANCE_RS_DIR_ASSET_PROVIDER_H
18 
19 #include <cerrno>
20 #include <cstdio>
21 #include <cstring>
22 #include <memory>
23 #include <string>
24 
25 #ifdef WINDOWS_PLATFORM
26 #include <shlwapi.h>
27 #include <windows.h>
28 #else
29 #include <dirent.h>
30 #include <sys/types.h>
31 #endif
32 
33 #include "flutter/assets/directory_asset_bundle.h"
34 
35 #include "core/common/flutter/flutter_asset_manager.h"
36 #include "core/common/rosen/rosen_asset_manager.h"
37 #include "base/resource/asset_manager.h"
38 #include "base/utils/macros.h"
39 #include "base/utils/utils.h"
40 
41 namespace OHOS::Ace {
42 class ACE_EXPORT RSDirAssetProvider : public RSAssetProvider {
43     DECLARE_ACE_TYPE(RSDirAssetProvider, RSAssetProvider);
44 
45 public:
46 #ifdef WINDOWS_PLATFORM
RSDirAssetProvider(const std::string & basePath)47     RSDirAssetProvider(const std::string& basePath) : basePath_(basePath + "\\") {}
48 #else
49     RSDirAssetProvider(const std::string& basePath) : basePath_(basePath + "/") {}
50 #endif
51     ~RSDirAssetProvider() override = default;
52 
IsValid()53     bool IsValid() const override
54     {
55         return true;
56     }
57 
GetAsset(const std::string & assetName)58     RefPtr<Asset> GetAsset(const std::string& assetName) const override
59     {
60         errno = 0;
61         LOGI("GetAsset: %{private}s, %{private}s", assetName.c_str(), basePath_.c_str());
62         std::string fileName = basePath_ + assetName;
63         char realPath[PATH_MAX] = { 0x00 };
64         if (!RealPath(fileName, realPath)) {
65             return nullptr;
66         }
67         auto fp = std::fopen(realPath, "rb");
68         if (!fp) {
69             LOGE("[%{private}s] open file error %{public}s", fileName.c_str(), strerror(errno));
70             return nullptr;
71         }
72 
73         if (std::fseek(fp, 0, SEEK_END) != 0) {
74             LOGE("[%{private}s] seek file tail error %{public}s", fileName.c_str(), strerror(errno));
75             std::fclose(fp);
76             return nullptr;
77         }
78 
79         size_t size = std::ftell(fp);
80         if (size < 0) {
81             LOGE("[%{private}s] tell file error %{public}s", fileName.c_str(), strerror(errno));
82             std::fclose(fp);
83             return nullptr;
84         }
85 
86         auto data = std::make_unique<char[]>(size);
87         if (data == nullptr) {
88             LOGE("[%{private}s] new uint8_t array failed", fileName.c_str());
89             std::fclose(fp);
90             return nullptr;
91         }
92 
93         if (std::fseek(fp, 0, SEEK_SET) != 0) {
94             LOGE("[%{private}s] seek file begin error %{public}s", fileName.c_str(), strerror(errno));
95             std::fclose(fp);
96             return nullptr;
97         }
98 
99         auto rsize = std::fread(data.get(), 1, size, fp);
100         if (rsize <= 0) {
101             LOGE("[%{private}s] read file failed, %{public}s", fileName.c_str(), strerror(errno));
102             std::fclose(fp);
103             return nullptr;
104         }
105         std::fclose(fp);
106         LOGI("[%{private}s] length: %{public}zu/%{public}zu success", fileName.c_str(), rsize, size);
107         return AceType::MakeRefPtr<RSAsset>(std::move(data), rsize);
108     }
109 
GetAssetPath(const std::string & assetName,bool isAddHapPath)110     std::string GetAssetPath(const std::string& assetName, bool isAddHapPath) override
111     {
112         std::string fileName = basePath_ + assetName;
113         char realPath[PATH_MAX] = { 0x00 };
114         if (!RealPath(fileName, realPath)) {
115             return nullptr;
116         }
117         std::FILE* fp = std::fopen(realPath, "r");
118         if (fp == nullptr) {
119             return "";
120         }
121         std::fclose(fp);
122         return basePath_;
123     }
124 
GetAssetList(const std::string & path,std::vector<std::string> & assetList)125     void GetAssetList(const std::string& path, std::vector<std::string>& assetList) override
126     {
127 #if defined(WINDOWS_PLATFORM)
128         std::string dirPath = basePath_ + "\\" + path;
129         WIN32_FIND_DATA fileInfo;
130         HANDLE hFind;
131         if ((hFind = FindFirstFile(dirPath.append("\\*").c_str(), &fileInfo)) != INVALID_HANDLE_VALUE) {
132             do {
133                 if (strcmp(fileInfo.cFileName, ".") != 0 && strcmp(fileInfo.cFileName, "..") != 0) {
134                     assetList.push_back(fileInfo.cFileName);
135                 }
136             } while (FindNextFile(hFind, &fileInfo) != 0);
137             FindClose(hFind);
138         }
139 #elif defined(MAC_PLATFORM)
140         std::string dirPath = basePath_ + "/" + path;
141         DIR* dp = nullptr;
142         if (nullptr == (dp = opendir(dirPath.c_str()))) {
143             return;
144         }
145         struct dirent* dptr = nullptr;
146         while ((dptr = readdir(dp)) != nullptr) {
147             if (strcmp(dptr->d_name, ".") != 0 && strcmp(dptr->d_name, "..") != 0) {
148                 assetList.push_back(dptr->d_name);
149             }
150         }
151         closedir(dp);
152 #endif
153     }
154 
155 private:
156     std::string basePath_;
157 };
158 
159 } // namespace OHOS::Ace
160 
161 #endif // FOUNDATION_ACE_ADAPTER_PREVIEW_ENTRANCE_RS_DIR_ASSET_PROVIDER_H
162