1 /*
2  * Copyright (C) 2024 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 OH_CJ_INITIALIZE_H
17 #define OH_CJ_INITIALIZE_H
18 
19 #include <vector>
20 #include "ability.h"
21 #include "directory_ex.h"
22 #include "constant.h"
23 #include "js_common.h"
24 #include "napi_base_context.h"
25 #include "cj_request_ffi.h"
26 
27 namespace OHOS::CJSystemapi::Request {
28 using OHOS::Request::ExceptionError;
29 using OHOS::Request::Config;
30 using OHOS::Request::FormItem;
31 using OHOS::Request::FileSpec;
32 using OHOS::Request::Action;
33 using OHOS::Request::Network;
34 using OHOS::Request::Mode;
35 using OHOS::AbilityRuntime::Context;
36 class CJInitialize {
37 public:
38     CJInitialize() = default;
39     ~CJInitialize() = default;
40 
41     static void StringSplit(const std::string &str, const char delim, std::vector<std::string> &elems);
42     static bool GetBaseDir(std::string &baseDir);
43 
44     static ExceptionError ParseConfig(OHOS::AbilityRuntime::Context *context, const CConfig *ffiConfig, Config &config);
45     static ExceptionError ParseBundleName(const std::shared_ptr<OHOS::AbilityRuntime::Context> &context,
46         std::string &config);
47     static bool ParseUrl(std::string &url);
48     static bool ParseCertsPath(std::string &url, std::vector<std::string> &certsPath);
49     static bool ParseFormItems(const CFormItemArr *cForms, std::vector<FormItem> &forms, std::vector<FileSpec> &files);
50     static bool ParseData(const CConfig *config, Config &out);
51     static bool Convert2FileSpec(const CFileSpec *cFile, const char *name, FileSpec &file);
52     static bool Convert2FileSpecs(const CFileSpecArr *cFiles, const char *name, std::vector<FileSpec> &files);
53     static bool ParseIndex(Config &config);
54     static int64_t ParseBegins(int64_t &begins);
55     static bool ParseTitle(Config &config);
56     static bool ParseToken(Config &config);
57     static bool ParseDescription(std::string &description);
58     static bool ParseSaveas(Config &config);
59     static void ParseMethod(Config &config);
60     static void ParseNetwork(Network &network);
61     static void ParseBackGround(Mode mode, bool &background);
62 
63     static ExceptionError CheckFileSpec(const std::shared_ptr<OHOS::AbilityRuntime::Context> &context, Config &config);
64     static ExceptionError CheckFilePath(const std::shared_ptr<OHOS::AbilityRuntime::Context> &context, Config &config);
65     static bool CheckPathBaseDir(const std::string &filepath, std::string &baseDir);
66     static bool CreateDirs(const std::vector<std::string> &pathDirs);
67     static bool InterceptData(const std::string &str, const std::string &in, std::string &out);
68     static bool GetInternalPath(const std::string &fileUri,
69         const std::shared_ptr<OHOS::AbilityRuntime::Context> &context, Config &config, std::string &filePath);
70     static ExceptionError GetFD(const std::string &path, const Config &config, int32_t &fd);
71     static bool FindDir(const std::string &pathDir);
72 private:
73     static bool CheckDownloadFilePath(
74         const std::shared_ptr<OHOS::AbilityRuntime::Context> &context, Config &config, std::string &errInfo);
75     static bool StandardizePath(
76         const std::shared_ptr<OHOS::AbilityRuntime::Context> &context, const Config &config, std::string &path);
77     static bool CacheToWhole(const std::shared_ptr<OHOS::AbilityRuntime::Context> &context, std::string &path);
78     static bool FileToWhole(const std::shared_ptr<OHOS::AbilityRuntime::Context> &context, const Config &config,
79         std::string &path);
80     static bool PathVecToNormal(const std::vector<std::string> &in, std::vector<std::string> &out);
81     static bool WholeToNormal(const std::string &wholePath, std::string &normalPath, std::vector<std::string> &out);
82     static ExceptionError CheckUploadBodyFiles(Config &config, const std::string &filePath);
83     static ExceptionError UploadBodyFileProc(std::string &fileName, Config &config);
84 };
85 } // OHOS::CJSystemapi::Request
86 #endif // CJ_INITIALIZE_H
87