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 "base/resource/asset_manager.h"
34 #include "base/utils/macros.h"
35 #include "base/utils/utils.h"
36 #include "core/common/asset_manager_impl.h"
37 #include "core/common/rosen/rosen_asset_manager.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         return true;
54     }
55 
GetAsset(const std::string & assetName)56     RefPtr<Asset> GetAsset(const std::string& assetName) const override
57     {
58         errno = 0;
59         LOGI("GetAsset: %{public}s, %{private}s", assetName.c_str(), basePath_.c_str());
60         std::string fileName = basePath_ + assetName;
61         char realPath[PATH_MAX] = { 0x00 };
62         if (!RealPath(fileName, realPath)) {
63             return nullptr;
64         }
65         auto fp = std::fopen(realPath, "rb");
66         if (!fp) {
67             return nullptr;
68         }
69 
70         if (std::fseek(fp, 0, SEEK_END) != 0) {
71             std::fclose(fp);
72             return nullptr;
73         }
74 
75         size_t size = std::ftell(fp);
76         if (size < 0) {
77             std::fclose(fp);
78             return nullptr;
79         }
80 
81         auto data = std::make_unique<char[]>(size);
82         if (data == nullptr) {
83             std::fclose(fp);
84             return nullptr;
85         }
86 
87         if (std::fseek(fp, 0, SEEK_SET) != 0) {
88             std::fclose(fp);
89             return nullptr;
90         }
91 
92         auto rsize = std::fread(data.get(), 1, size, fp);
93         if (rsize <= 0) {
94             std::fclose(fp);
95             return nullptr;
96         }
97         std::fclose(fp);
98         LOGI("[%{public}s] length: %{public}zu/%{public}zu success", assetName.c_str(), rsize, size);
99         return AceType::MakeRefPtr<RSAsset>(std::move(data), rsize);
100     }
101 
GetAssetPath(const std::string & assetName,bool isAddHapPath)102     std::string GetAssetPath(const std::string& assetName, bool isAddHapPath) override
103     {
104         std::string fileName = basePath_ + assetName;
105         char realPath[PATH_MAX] = { 0x00 };
106         if (!RealPath(fileName, realPath)) {
107             return nullptr;
108         }
109         std::FILE* fp = std::fopen(realPath, "r");
110         if (fp == nullptr) {
111             return "";
112         }
113         std::fclose(fp);
114         return basePath_;
115     }
116 
GetAssetList(const std::string & path,std::vector<std::string> & assetList)117     void GetAssetList(const std::string& path, std::vector<std::string>& assetList) override
118     {
119 #if defined(WINDOWS_PLATFORM)
120         std::string dirPath = basePath_ + "\\" + path;
121         WIN32_FIND_DATA fileInfo;
122         HANDLE hFind;
123         if ((hFind = FindFirstFile(dirPath.append("\\*").c_str(), &fileInfo)) != INVALID_HANDLE_VALUE) {
124             do {
125                 if (strcmp(fileInfo.cFileName, ".") != 0 && strcmp(fileInfo.cFileName, "..") != 0) {
126                     assetList.push_back(fileInfo.cFileName);
127                 }
128             } while (FindNextFile(hFind, &fileInfo) != 0);
129             FindClose(hFind);
130         }
131 #elif defined(MAC_PLATFORM)
132         std::string dirPath = basePath_ + "/" + path;
133         DIR* dp = nullptr;
134         if (nullptr == (dp = opendir(dirPath.c_str()))) {
135             return;
136         }
137         struct dirent* dptr = nullptr;
138         while ((dptr = readdir(dp)) != nullptr) {
139             if (strcmp(dptr->d_name, ".") != 0 && strcmp(dptr->d_name, "..") != 0) {
140                 assetList.push_back(dptr->d_name);
141             }
142         }
143         closedir(dp);
144 #endif
145     }
146 
147 private:
148     std::string basePath_;
149 };
150 
151 } // namespace OHOS::Ace
152 
153 #endif // FOUNDATION_ACE_ADAPTER_PREVIEW_ENTRANCE_RS_DIR_ASSET_PROVIDER_H
154