Skip to content

Commit 97c99e9

Browse files
committed
Allow storage of non-text files
1 parent b6e8b88 commit 97c99e9

File tree

2 files changed

+81
-55
lines changed

2 files changed

+81
-55
lines changed

examples/RNFetchBlobWin/App.js

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -359,21 +359,8 @@ const App: () => React$Node = () => {
359359
fileCache : true,
360360
})
361361
.fetch(
362-
'POST',
363-
'https://enb954aqyumba.x.pipedream.net/',
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-
}
362+
'GET',
363+
'https://upload.wikimedia.org/wikipedia/commons/c/c4/Change-5.png',
377364
)
378365
.then((res) => {
379366
// the temp file path
@@ -395,7 +382,7 @@ const App: () => React$Node = () => {
395382
// here's the body you're going to send, should be a BASE64 encoded string
396383
// (you can use "base64"(refer to the library 'mathiasbynens/base64') APIs to make one).
397384
// The data will be converted to "byte array"(say, blob) before request sent.
398-
}, RNFetchBlob.wrap(RNFetchBlob.fs.dirs.DocumentDir + '/temp1.txt'))
385+
}, RNFetchBlob.wrap(RNFetchBlob.fs.dirs.DocumentDir + '\\ImageToUpload.jpg'))
399386
.then((res) => {
400387
console.log(res.text());
401388
})
@@ -472,7 +459,7 @@ const App: () => React$Node = () => {
472459
// custom content type
473460
{ name : 'avatar-png', filename : 'avatar-png.png', type:'image/png', data: "whaddup my pickles"},
474461
// part file from storage
475-
{ name : 'avatar-foo', filename : 'avatar-foo.png', type:'image/foo', data: RNFetchBlob.wrap(RNFetchBlob.fs.dirs.DocumentDir + '/temp1.txt')},
462+
{ name : 'avatar-foo', filename : 'avatar-foo.png', type:'image/foo', data: RNFetchBlob.wrap(RNFetchBlob.fs.dirs.DocumentDir + '\\ImageToUpload.jpg')},
476463
// elements without property `filename` will be sent as plain text
477464
{ name : 'name', data : 'user'},
478465
{ name : 'info', data : JSON.stringify({
@@ -900,12 +887,12 @@ const App: () => React$Node = () => {
900887
onPress={fetchCall}
901888
/>
902889
<Button
903-
title="Attempt Storage Call Fetch"
890+
title="Upload File from Storage"
904891
color="#9a73ef"
905892
onPress={uploadFromStorageCall}
906893
/>
907894
<Button
908-
title="Upload Text From Call"
895+
title="Upload Text From Storage"
909896
color="#9a73ef"
910897
onPress={uploadTextFromCall}
911898
/>

windows/RNFetchBlob/RNFetchBlob.cpp

Lines changed: 75 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,29 +1120,47 @@ winrt::fire_and_forget RNFetchBlob::fetchBlob(
11201120

11211121
winrt::Windows::Web::Http::HttpRequestMessage requestMessage{ httpMethod, Uri{url} };
11221122
bool pathToFile{ body.rfind(prefix, 0) == 0 };
1123-
winrt::hstring fileContent;
11241123
if (pathToFile)
11251124
{
11261125
std::string contentPath{ body.substr(prefix.length()) };
1127-
std::filesystem::path path{ contentPath };
1128-
path.make_preferred();
1126+
size_t fileLength = contentPath.length();
1127+
bool hasTrailingSlash{ contentPath[fileLength - 1] == '\\' || contentPath[fileLength - 1] == '/' };
1128+
winrt::hstring directoryPath, fileName;
1129+
splitPath(hasTrailingSlash ? contentPath.substr(0, fileLength - 1) : contentPath, directoryPath, fileName);
1130+
StorageFolder folder{ co_await StorageFolder::GetFolderFromPathAsync(directoryPath) };
1131+
StorageFile storageFile{ co_await folder.CreateFileAsync(fileName, CreationCollisionOption::OpenIfExists) };
1132+
IBuffer requestBuffer{ co_await FileIO::ReadBufferAsync(storageFile) };
11291133

1130-
StorageFile storageFile{ co_await StorageFile::GetFileFromPathAsync(winrt::to_hstring(path.c_str())) };
1131-
fileContent = co_await FileIO::ReadTextAsync(storageFile);
1132-
}
1134+
winrt::Windows::Web::Http::HttpBufferContent requestContent{ requestBuffer };
11331135

1134-
winrt::Windows::Web::Http::HttpStringContent requestContent{ pathToFile ? fileContent : winrt::to_hstring(body) };
1136+
for (auto const& entry : headers)
1137+
{
1138+
if (!requestMessage.Headers().TryAppendWithoutValidation(winrt::to_hstring(entry.first), winrt::to_hstring(entry.second.AsString())))
1139+
{
1140+
requestContent.Headers().TryAppendWithoutValidation(winrt::to_hstring(entry.first), winrt::to_hstring(entry.second.AsString()));
1141+
}
1142+
}
1143+
requestMessage.Content(requestContent);
1144+
}
1145+
else if (!body.empty()) {
1146+
winrt::Windows::Web::Http::HttpStringContent requestString{ winrt::to_hstring(body) };
11351147

1136-
for (auto const& entry : headers)
1137-
{
1138-
if (!requestMessage.Headers().TryAppendWithoutValidation(winrt::to_hstring(entry.first), winrt::to_hstring(entry.second.AsString())))
1148+
for (auto const& entry : headers)
11391149
{
1140-
requestContent.Headers().TryAppendWithoutValidation(winrt::to_hstring(entry.first), winrt::to_hstring(entry.second.AsString()));
1150+
if (!requestMessage.Headers().TryAppendWithoutValidation(winrt::to_hstring(entry.first), winrt::to_hstring(entry.second.AsString())))
1151+
{
1152+
requestString.Headers().TryAppendWithoutValidation(winrt::to_hstring(entry.first), winrt::to_hstring(entry.second.AsString()));
1153+
}
1154+
}
1155+
requestMessage.Content(requestString);
1156+
}
1157+
else {
1158+
for (auto const& entry : headers)
1159+
{
1160+
requestMessage.Headers().TryAppendWithoutValidation(winrt::to_hstring(entry.first), winrt::to_hstring(entry.second.AsString()));
11411161
}
11421162
}
11431163

1144-
requestMessage.Content(requestContent);
1145-
11461164
auto exists{ uploadProgressMap.find(taskId) };
11471165
if (exists != uploadProgressMap.end()) {
11481166
auto progress{ uploadProgressMap[taskId] };
@@ -1280,35 +1298,55 @@ winrt::fire_and_forget RNFetchBlob::fetchBlobForm(
12801298

12811299
auto data{ items["data"].AsString() };
12821300
bool pathToFile{ data.rfind(prefix, 0) == 0 };
1283-
winrt::hstring fileContent;
12841301
if (pathToFile)
12851302
{
12861303
std::string contentPath{ data.substr(prefix.length()) };
1287-
std::filesystem::path path{ contentPath };
1288-
path.make_preferred();
1289-
1290-
StorageFile storageFile{ co_await StorageFile::GetFileFromPathAsync(winrt::to_hstring(path.c_str())) };
1291-
fileContent = co_await FileIO::ReadTextAsync(storageFile);
1292-
1293-
}
1294-
winrt::Windows::Web::Http::HttpStringContent dataContents{ pathToFile ? fileContent : winrt::to_hstring(data) };
1295-
if (!items["type"].IsNull()) {
1296-
dataContents.Headers().TryAppendWithoutValidation(L"content-type", winrt::to_hstring(items["type"].AsString()));
1297-
}
1304+
size_t fileLength = contentPath.length();
1305+
bool hasTrailingSlash{ contentPath[fileLength - 1] == '\\' || contentPath[fileLength - 1] == '/' };
1306+
winrt::hstring directoryPath, fileName;
1307+
splitPath(hasTrailingSlash ? contentPath.substr(0, fileLength - 1) : contentPath, directoryPath, fileName);
1308+
StorageFolder folder{ co_await StorageFolder::GetFolderFromPathAsync(directoryPath) };
1309+
StorageFile storageFile = co_await folder.CreateFileAsync(fileName, CreationCollisionOption::OpenIfExists);
1310+
IBuffer requestBuffer{ co_await FileIO::ReadBufferAsync(storageFile) };
1311+
1312+
winrt::Windows::Web::Http::HttpBufferContent requestBufferContent{ requestBuffer };
1313+
1314+
if (!items["type"].IsNull()) {
1315+
requestBufferContent.Headers().TryAppendWithoutValidation(L"content-type", winrt::to_hstring(items["type"].AsString()));
1316+
}
12981317

1299-
auto name{ items["name"].IsNull() ? L"" : winrt::to_hstring(items["name"].AsString()) };
1300-
if (name.size() <= 0) {
1301-
requestContent.Add(dataContents);
1302-
continue;
1303-
}
1304-
auto filename{ items["filename"].IsNull() ? L"" : winrt::to_hstring(items["filename"].AsString()) };
1305-
if (filename.size() <= 0) {
1306-
requestContent.Add(dataContents, name);
1318+
auto name{ items["name"].IsNull() ? L"" : winrt::to_hstring(items["name"].AsString()) };
1319+
if (name.size() <= 0) {
1320+
requestContent.Add(requestBufferContent);
1321+
continue;
1322+
}
1323+
auto filename{ items["filename"].IsNull() ? L"" : winrt::to_hstring(items["filename"].AsString()) };
1324+
if (filename.size() <= 0) {
1325+
requestContent.Add(requestBufferContent, name);
1326+
}
1327+
else {
1328+
requestContent.Add(requestBufferContent, name, filename);
1329+
}
13071330
}
13081331
else {
1309-
requestContent.Add(dataContents, name, filename);
1310-
}
1332+
winrt::Windows::Web::Http::HttpStringContent dataContents{ winrt::to_hstring(data) };
1333+
if (!items["type"].IsNull()) {
1334+
dataContents.Headers().TryAppendWithoutValidation(L"content-type", winrt::to_hstring(items["type"].AsString()));
1335+
}
13111336

1337+
auto name{ items["name"].IsNull() ? L"" : winrt::to_hstring(items["name"].AsString()) };
1338+
if (name.size() <= 0) {
1339+
requestContent.Add(dataContents);
1340+
continue;
1341+
}
1342+
auto filename{ items["filename"].IsNull() ? L"" : winrt::to_hstring(items["filename"].AsString()) };
1343+
if (filename.size() <= 0) {
1344+
requestContent.Add(dataContents, name);
1345+
}
1346+
else {
1347+
requestContent.Add(dataContents, name, filename);
1348+
}
1349+
}
13121350
}
13131351
requestMessage.Content(requestContent);
13141352

@@ -1530,7 +1568,8 @@ try
15301568
for (;;)
15311569
{
15321570
buffer.Length(0);
1533-
auto readBuffer = co_await contentStream.ReadAsync(buffer, buffer.Capacity(), InputStreamOptions::None);
1571+
auto readBuffer{ co_await contentStream.ReadAsync(buffer, buffer.Capacity(), InputStreamOptions::None) };
1572+
//readBuffer.
15341573
readContents = winrt::to_string(CryptographicBuffer::ConvertBinaryToString(BinaryStringEncoding::Utf8, readBuffer));
15351574
read += readBuffer.Length();
15361575
totalRead += read;

0 commit comments

Comments
 (0)