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