• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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_DIR_ASSET_PROVIDER_H
17 #define FOUNDATION_ACE_ADAPTER_PREVIEW_ENTRANCE_DIR_ASSET_PROVIDER_H
18 
19 #include <memory>
20 #include <string>
21 
22 #ifdef WINDOWS_PLATFORM
23 #include <windows.h>
24 #else
25 #include <dirent.h>
26 #include <sys/types.h>
27 #endif
28 
29 #include "flutter/assets/directory_asset_bundle.h"
30 
31 #include "core/common/flutter/flutter_asset_manager.h"
32 #include "base/resource/asset_manager.h"
33 #include "base/utils/macros.h"
34 
35 namespace OHOS::Ace {
36 
37 class ACE_EXPORT DirAssetProvider : public FlutterAssetProvider {
38     DECLARE_ACE_TYPE(DirAssetProvider, FlutterAssetProvider);
39 
40 public:
DirAssetProvider(const std::string & basePath,std::unique_ptr<flutter::DirectoryAssetBundle> provider)41     DirAssetProvider(const std::string& basePath, std::unique_ptr<flutter::DirectoryAssetBundle> provider)
42         : basePath_(basePath), assetProvider_(std::move(provider))
43     {}
44     ~DirAssetProvider() override = default;
45 
GetAsMapping(const std::string & assetName)46     std::unique_ptr<fml::Mapping> GetAsMapping(const std::string& assetName) const override
47     {
48         if (!assetProvider_) {
49             return nullptr;
50         }
51         return assetProvider_->GetAsMapping(assetName);
52     }
53 
IsValid()54     bool IsValid() const override
55     {
56         if (!assetProvider_) {
57             return false;
58         }
59         return assetProvider_->IsValid();
60     }
61 
GetAssetPath(const std::string & assetName)62     std::string GetAssetPath(const std::string& assetName) override
63     {
64         std::string fileName = basePath_ + assetName;
65         std::FILE* fp = std::fopen(fileName.c_str(), "r");
66         if (fp == nullptr) {
67             return "";
68         }
69         std::fclose(fp);
70         return basePath_;
71     }
72 
GetAssetList(const std::string & path,std::vector<std::string> & assetList)73     void GetAssetList(const std::string& path, std::vector<std::string>& assetList) override
74     {
75 #if defined(WINDOWS_PLATFORM)
76         std::string dirPath = basePath_ + "\\" + path;
77         WIN32_FIND_DATA fileInfo;
78         HANDLE hFind;
79         if ((hFind = FindFirstFile(dirPath.append("\\*").c_str(), &fileInfo)) != INVALID_HANDLE_VALUE) {
80             do {
81                 if (strcmp(fileInfo.cFileName, ".") != 0 && strcmp(fileInfo.cFileName, "..") != 0) {
82                     assetList.push_back(fileInfo.cFileName);
83                 }
84             } while (FindNextFile(hFind, &fileInfo) != 0);
85             FindClose(hFind);
86         }
87 #elif defined(MAC_PLATFORM) || defined(LINUX_PLATFORM)
88         std::string dirPath = basePath_ + "/" + path;
89         DIR* dp = nullptr;
90         if (nullptr == (dp = opendir(dirPath.c_str()))) {
91             return;
92         }
93         struct dirent* dptr = nullptr;
94         while ((dptr = readdir(dp)) != nullptr) {
95             if (strcmp(dptr->d_name, ".") != 0 && strcmp(dptr->d_name, "..") != 0) {
96                 assetList.push_back(dptr->d_name);
97             }
98         }
99         closedir(dp);
100 #endif
101     }
102 
103 private:
104     std::string basePath_;
105     std::unique_ptr<flutter::AssetResolver> assetProvider_;
106 };
107 
108 } // namespace OHOS::Ace
109 
110 #endif // FOUNDATION_ACE_ADAPTER_PREVIEW_ENTRANCE_DIR_ASSET_PROVIDER_H
111