1 /*
2 * Copyright (c) 2022 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 #include <thread>
17 #include <cstdio>
18 #include "upload_task.h"
19
20 using namespace OHOS::AppExecFwk;
21 namespace OHOS::Request::Upload {
~ObtainFile()22 ObtainFile::~ObtainFile()
23 {
24 }
25
GetFile(FILE ** file,std::string & fileUri,unsigned int & fileSize,std::shared_ptr<OHOS::AbilityRuntime::Context> & context)26 uint32_t ObtainFile::GetFile(FILE **file, std::string &fileUri,
27 unsigned int& fileSize, std::shared_ptr<OHOS::AbilityRuntime::Context> &context)
28 {
29 uint32_t ret = UPLOAD_OK;
30 std::string dataAbilityHead("dataability");
31 std::string internalHead("internal");
32
33 // file type check
34 if (fileUri.compare(0, dataAbilityHead.size(), dataAbilityHead) == 0) {
35 UPLOAD_HILOGD(UPLOAD_MODULE_FRAMEWORK, "GetDataAbilityFile");
36 ret = GetDataAbilityFile(file, fileUri, fileSize, context);
37 } else if (fileUri.compare(0, internalHead.size(), internalHead) == 0) {
38 UPLOAD_HILOGD(UPLOAD_MODULE_FRAMEWORK, "GetInternalFile");
39 ret = GetInternalFile(file, fileUri, fileSize, context);
40 } else {
41 UPLOAD_HILOGE(UPLOAD_MODULE_FRAMEWORK, "wrong path");
42 ret = UPLOAD_ERRORCODE_UNSUPPORT_URI;
43 *file = nullptr;
44 fileSize = 0;
45 }
46
47 UPLOAD_HILOGD(UPLOAD_MODULE_FRAMEWORK,
48 "ObtainFile::GetFile, ret : %{public}d, size : %{public}d, pf : %{public}p", ret, fileSize, *file);
49 return ret;
50 }
51
GetDataAbilityFile(FILE ** file,std::string & fileUri,uint32_t & fileSize,std::shared_ptr<OHOS::AbilityRuntime::Context> & context)52 uint32_t ObtainFile::GetDataAbilityFile(FILE **file, std::string &fileUri,
53 uint32_t& fileSize, std::shared_ptr<OHOS::AbilityRuntime::Context> &context)
54 {
55 uint32_t ret = UPLOAD_OK;
56 FILE *filePtr = nullptr;
57 int32_t fileLength = 0;
58
59 std::shared_ptr<Uri> uri = std::make_shared<Uri>(fileUri);
60 std::shared_ptr<DataAbilityHelper> dataAbilityHelper = DataAbilityHelper::Creator(context, uri);
61
62 do {
63 int32_t fd = dataAbilityHelper->OpenFile(*uri, "r");
64 if (fd == -1) {
65 UPLOAD_HILOGE(UPLOAD_MODULE_FRAMEWORK, "ObtainFile::GetDataAbilityFile, open file error.");
66 ret = UPLOAD_ERRORCODE_GET_FILE_ERROR;
67 break;
68 }
69
70 filePtr = fdopen(fd, "r");
71 if (filePtr == nullptr) {
72 UPLOAD_HILOGE(UPLOAD_MODULE_FRAMEWORK, "ObtainFile::GetDataAbilityFile, fdopen error.");
73 ret = UPLOAD_ERRORCODE_GET_FILE_ERROR;
74 break;
75 }
76
77 (void)fseek(filePtr, 0, SEEK_END);
78 fileLength = ftell(filePtr);
79 (void)fseek(filePtr, 0, SEEK_SET);
80 } while (0);
81
82 *file = filePtr;
83 fileSize = fileLength;
84 return ret;
85 }
86
IsValidPath(const std::string & filePath)87 bool ObtainFile::IsValidPath(const std::string &filePath)
88 {
89 char resolvedPath[PATH_MAX + 1] = { 0 };
90 if (filePath.length() > PATH_MAX || realpath(filePath.c_str(), resolvedPath) == nullptr ||
91 strncmp(resolvedPath, filePath.c_str(), filePath.length()) != 0) {
92 UPLOAD_HILOGE(UPLOAD_MODULE_FRAMEWORK, "filePath error");
93 return false;
94 }
95 return true;
96 }
97
SplitPath(const std::string & fileUri,std::string & fileName)98 bool ObtainFile::SplitPath(const std::string &fileUri, std::string &fileName)
99 {
100 std::string pattern = "internal://cache/";
101 size_t pos = fileUri.find(pattern);
102 if (pos != 0) {
103 UPLOAD_HILOGE(UPLOAD_MODULE_FRAMEWORK, "internal path is invalid");
104 return false;
105 }
106 fileName = fileUri.substr(pattern.size(), fileUri.size());
107 return true;
108 }
109
GetInternalFile(FILE ** file,const std::string & fileUri,uint32_t & fileSize,std::shared_ptr<OHOS::AbilityRuntime::Context> & context)110 uint32_t ObtainFile::GetInternalFile(FILE **file, const std::string &fileUri, uint32_t &fileSize,
111 std::shared_ptr<OHOS::AbilityRuntime::Context> &context)
112 {
113 std::string fileName;
114 if (!SplitPath(fileUri, fileName)) {
115 return UPLOAD_ERRORCODE_UNSUPPORT_URI;
116 }
117 std::string filePath = context->GetCacheDir();
118 if (filePath.empty()) {
119 UPLOAD_HILOGE(UPLOAD_MODULE_FRAMEWORK, "ObtainFile::GetInternalFile, internal to cache error");
120 return UPLOAD_ERRORCODE_GET_FILE_ERROR;
121 }
122 filePath += "/" + fileName;
123 if (!IsValidPath(filePath)) {
124 return UPLOAD_ERRORCODE_GET_FILE_ERROR;
125 }
126 FILE *filePtr = fopen(filePath.c_str(), "r");
127 if (filePtr == nullptr) {
128 UPLOAD_HILOGE(UPLOAD_MODULE_FRAMEWORK, "open file error, error info : %{public}d.", errno);
129 return UPLOAD_ERRORCODE_GET_FILE_ERROR;
130 }
131 (void)fseek(filePtr, 0, SEEK_END);
132 int32_t fileLength = ftell(filePtr);
133 (void)fseek(filePtr, 0, SEEK_SET);
134
135 *file = filePtr;
136 fileSize = fileLength;
137 return UPLOAD_OK;
138 }
139 } // end of OHOS::Request::Upload