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