Skip to content

Commit b5bc7d7

Browse files
committed
Fixed 0x80072EFF issue
1 parent 49fba2b commit b5bc7d7

File tree

2 files changed

+162
-31
lines changed

2 files changed

+162
-31
lines changed

RNFetchBlobWin/App.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -358,9 +358,23 @@ const App: () => React$Node = () => {
358358
// this is much more performant.
359359
fileCache : true,
360360
})
361-
.fetch('GET', 'https://bit.ly/2TtutbS', {
362-
'Content-Type' : 'multipart/form-data'
363-
}, "Hello World!")
361+
.fetch(
362+
'POST',
363+
'https://content.dropboxapi.com/2/files/upload',
364+
{
365+
Authorization : "Bearer access-token...",
366+
'Dropbox-API-Arg': JSON.stringify({
367+
path : '/img-from-react-native.png',
368+
mode : 'add',
369+
autorename : true,
370+
mute : false
371+
}),
372+
'Content-Type' : 'application/octet-stream',
373+
// here's the body you're going to send, should be a BASE64 encoded string
374+
// (you can use "base64"(refer to the library 'mathiasbynens/base64') APIs to make one).
375+
// The data will be converted to "byte array"(say, blob) before request sent.
376+
}
377+
)
364378
.then((res) => {
365379
// the temp file path
366380
console.log('The file saved to ', res.path())

RNFetchBlobWin/windows/RNFetchBlobWin/RNFetchBlob.cpp

Lines changed: 145 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,9 +1051,126 @@ winrt::fire_and_forget RNFetchBlob::fetchBlob(
10511051
std::string body,
10521052
std::function<void(std::string, std::string, std::string)> callback) noexcept
10531053
{
1054-
winrt::Microsoft::ReactNative::JSValueArray emptyArray;
1055-
createBlobForm(options, taskId, method, url, headers, body, emptyArray, callback);
1056-
co_return;
1054+
winrt::Windows::Web::Http::Filters::HttpBaseProtocolFilter filter;
1055+
1056+
RNFetchBlobConfig config;
1057+
if (options["appendExt"].IsNull() == true)
1058+
{
1059+
config.appendExt = "";
1060+
}
1061+
else
1062+
{
1063+
std::filesystem::path path(options["appendExt"].AsString());
1064+
path.make_preferred();
1065+
config.appendExt = winrt::to_string(path.c_str());
1066+
}
1067+
config.taskId = taskId;
1068+
config.appendExt = options["appendExt"].IsNull() ? "" : options["appendExt"].AsString();
1069+
config.fileCache = options["fileCache"].AsBoolean();
1070+
config.followRedirect = options["followRedirect"].AsBoolean();
1071+
config.overwrite = options["overwrite"].AsBoolean();
1072+
if (options["path"].IsNull() == true)
1073+
{
1074+
config.path = "";
1075+
}
1076+
else
1077+
{
1078+
std::filesystem::path path{ options["path"].AsString() };
1079+
path.make_preferred();
1080+
config.path = winrt::to_string(path.c_str());
1081+
}
1082+
config.timeout = options["timeout"].AsInt32();
1083+
1084+
config.trusty = options["trusty"].AsBoolean();
1085+
if (config.followRedirect == true)
1086+
{
1087+
filter.AllowAutoRedirect(true);
1088+
}
1089+
else
1090+
{
1091+
filter.AllowAutoRedirect(false);
1092+
}
1093+
1094+
if (config.timeout > 0)
1095+
{
1096+
// TODO: find winrt config property
1097+
}
1098+
1099+
if (config.trusty)
1100+
{
1101+
filter.IgnorableServerCertificateErrors().Append(Cryptography::Certificates::ChainValidationResult::Untrusted);
1102+
}
1103+
1104+
winrt::Windows::Web::Http::HttpClient httpClient{ filter };
1105+
1106+
winrt::Windows::Web::Http::HttpMethod httpMethod{ winrt::Windows::Web::Http::HttpMethod::Post() };
1107+
std::string methodUpperCase{ method };
1108+
for (auto& c : methodUpperCase)
1109+
{
1110+
toupper(c);
1111+
}
1112+
1113+
// Delete, Patch, Post, Put, Get, Options, Head
1114+
if (methodUpperCase.compare("DELETE") == 0)
1115+
{
1116+
httpMethod = winrt::Windows::Web::Http::HttpMethod::Delete();
1117+
}
1118+
else if (methodUpperCase.compare("PATCH") == 0)
1119+
{
1120+
httpMethod = winrt::Windows::Web::Http::HttpMethod::Patch();
1121+
}
1122+
else if (methodUpperCase.compare("PUT") == 0)
1123+
{
1124+
httpMethod = winrt::Windows::Web::Http::HttpMethod::Put();
1125+
}
1126+
else if (methodUpperCase.compare("GET") == 0)
1127+
{
1128+
httpMethod = winrt::Windows::Web::Http::HttpMethod::Get();
1129+
}
1130+
else if (methodUpperCase.compare("OPTIONS") == 0)
1131+
{
1132+
httpMethod = winrt::Windows::Web::Http::HttpMethod::Options();
1133+
}
1134+
else if (methodUpperCase.compare("HEAD") == 0)
1135+
{
1136+
httpMethod = winrt::Windows::Web::Http::HttpMethod::Head();
1137+
}
1138+
else if (methodUpperCase.compare("POST") != 0)
1139+
{
1140+
// Method not supported by winrt
1141+
co_return;
1142+
}
1143+
1144+
winrt::Windows::Web::Http::HttpRequestMessage requestMessage{ httpMethod, Uri{url} };
1145+
1146+
std::string prefix{ "RNFetchBlob-file://" };
1147+
bool pathToFile{ body.rfind(prefix, 0) == 0 };
1148+
winrt::hstring fileContent;
1149+
if (pathToFile)
1150+
{
1151+
std::string contentPath{ body.substr(prefix.length()) };
1152+
std::filesystem::path path{ contentPath };
1153+
path.make_preferred();
1154+
1155+
StorageFile storageFile{ co_await StorageFile::GetFileFromPathAsync(winrt::to_hstring(path.c_str())) };
1156+
fileContent = co_await FileIO::ReadTextAsync(storageFile);
1157+
1158+
}
1159+
1160+
winrt::Windows::Web::Http::HttpStringContent requestContent{ pathToFile ? fileContent : winrt::to_hstring(body) };
1161+
1162+
for (auto const& entry : headers)
1163+
{
1164+
if (!requestMessage.Headers().TryAppendWithoutValidation(winrt::to_hstring(entry.first), winrt::to_hstring(entry.second.AsString())))
1165+
{
1166+
requestContent.Headers().TryAppendWithoutValidation(winrt::to_hstring(entry.first), winrt::to_hstring(entry.second.AsString()));
1167+
}
1168+
}
1169+
1170+
requestMessage.Content(requestContent);
1171+
1172+
co_await m_tasks.Add(taskId, ProcessRequestAsync(filter, requestMessage, config, callback));
1173+
10571174
}
10581175

10591176
winrt::fire_and_forget RNFetchBlob::fetchBlobForm(
@@ -1065,21 +1182,8 @@ winrt::fire_and_forget RNFetchBlob::fetchBlobForm(
10651182
winrt::Microsoft::ReactNative::JSValueArray body,
10661183
std::function<void(std::string, std::string, std::string)> callback) noexcept
10671184
{
1068-
createBlobForm(options, taskId, method, url, headers, "", body, callback);
1069-
co_return;
1070-
}
1071-
1072-
winrt::fire_and_forget RNFetchBlob::createBlobForm(
1073-
const winrt::Microsoft::ReactNative::JSValueObject& options,
1074-
const std::string& taskId,
1075-
const std::string& method,
1076-
const std::wstring& url,
1077-
const winrt::Microsoft::ReactNative::JSValueObject& headers,
1078-
const std::string& bodyString,
1079-
const winrt::Microsoft::ReactNative::JSValueArray& bodyArray,
1080-
std::function<void(std::string, std::string, std::string)> callback) noexcept
1081-
{
1082-
1185+
//createBlobForm(options, taskId, method, url, headers, "", body, callback);
1186+
//co_return;
10831187
winrt::Windows::Web::Http::Filters::HttpBaseProtocolFilter filter;
10841188

10851189
RNFetchBlobConfig config;
@@ -1125,7 +1229,7 @@ winrt::fire_and_forget RNFetchBlob::createBlobForm(
11251229
// TODO: find winrt config property
11261230
}
11271231

1128-
if (!config.trusty)
1232+
if (config.trusty)
11291233
{
11301234
filter.IgnorableServerCertificateErrors().Append(Cryptography::Certificates::ChainValidationResult::Untrusted);
11311235
}
@@ -1183,18 +1287,32 @@ winrt::fire_and_forget RNFetchBlob::createBlobForm(
11831287
}
11841288
}
11851289

1186-
if (bodyString.length() > 0) {
1187-
winrt::Windows::Web::Http::HttpBufferContent content{ CryptographicBuffer::ConvertStringToBinary(winrt::to_hstring(bodyString), BinaryStringEncoding::Utf8) };
1188-
requestMessage.Content(content);
1189-
}
1190-
else if (!bodyArray.empty())
1191-
{
1192-
// TODO: Add in multipart aspects
1193-
}
1290+
//if (bodyString.length() > 0) {
1291+
// winrt::Windows::Web::Http::HttpBufferContent content{ CryptographicBuffer::ConvertStringToBinary(winrt::to_hstring(bodyString), BinaryStringEncoding::Utf8) };
1292+
// requestMessage.Content(content);
1293+
//}
1294+
//else if (!bodyArray.empty())
1295+
//{
1296+
// // TODO: Add in multipart aspects
1297+
//}
11941298

11951299
co_await m_tasks.Add(taskId, ProcessRequestAsync(filter, requestMessage, config, callback));
11961300
}
11971301

1302+
winrt::fire_and_forget RNFetchBlob::createBlobForm(
1303+
const winrt::Microsoft::ReactNative::JSValueObject& options,
1304+
const std::string& taskId,
1305+
const std::string& method,
1306+
const std::wstring& url,
1307+
const winrt::Microsoft::ReactNative::JSValueObject& headers,
1308+
const std::string& bodyString,
1309+
const winrt::Microsoft::ReactNative::JSValueArray& bodyArray,
1310+
std::function<void(std::string, std::string, std::string)> callback) noexcept
1311+
{
1312+
1313+
co_return;
1314+
}
1315+
11981316
void RNFetchBlob::enableProgressReport(
11991317
std::string taskId,
12001318
int interval,
@@ -1314,7 +1432,6 @@ try
13141432
for (;;)
13151433
{
13161434
buffer.Length(0);
1317-
// TODO: fix 0x80072EFF
13181435
auto readBuffer = contentStream.ReadAsync(buffer, buffer.Capacity(), InputStreamOptions::None).get();
13191436
if (readBuffer.Length() == 0)
13201437
{

0 commit comments

Comments
 (0)