1
1
#include " pch.h"
2
2
#include " RNFetchBlob.h"
3
-
4
3
#include < windows.h>
5
4
#include < winrt/Windows.Security.Cryptography.h>
6
5
#include < winrt/Windows.Security.Cryptography.Core.h>
226
225
{
227
226
data.push_back (var.AsInt8 ());
228
227
}
229
-
230
228
Streams::IBuffer buffer{ CryptographicBuffer::CreateFromByteArray (data) };
231
229
232
230
winrt::hstring destDirectoryPath, destFileName;
@@ -259,11 +257,77 @@ catch (...)
259
257
260
258
261
259
// writeStream
260
+ winrt::fire_and_forget RNFetchBlob::writeStream (
261
+ std::string path,
262
+ std::string encoding,
263
+ bool append,
264
+ std::function<void (std::string, std::string, std::string)> callback) noexcept
265
+ try
266
+ {
267
+ winrt::hstring directoryPath, fileName;
268
+ splitPath (path, directoryPath, fileName);
269
+ StorageFolder folder{ co_await StorageFolder::GetFolderFromPathAsync (directoryPath) };
270
+ StorageFile file{ co_await folder.GetFileAsync (fileName) };
271
+
272
+ Streams::IRandomAccessStream stream{ co_await file.OpenAsync (FileAccessMode::ReadWrite) };
273
+ if (append)
274
+ {
275
+ stream.Seek (UINT64_MAX);
276
+ }
277
+ EncodingOptions encodingOption;
278
+ if (encoding.compare (" utf8" ))
279
+ {
280
+ encodingOption = EncodingOptions::UTF8;
281
+ }
282
+ else if (encoding.compare (" base64" ))
283
+ {
284
+ encodingOption = EncodingOptions::BASE64;
285
+ }
286
+ else if (encoding.compare (" ascii" ))
287
+ {
288
+ encodingOption = EncodingOptions::ASCII;
289
+ }
290
+ else
291
+ {
292
+ co_return ;
293
+ }
294
+
295
+ // Define the length, in bytes, of the buffer.
296
+ uint32_t length = 128 ;
297
+ // Generate random data and copy it to a buffer.
298
+ IBuffer buffer = Cryptography::CryptographicBuffer::GenerateRandom (length);
299
+ // Encode the buffer to a hexadecimal string (for display).
300
+ std::string stringId = winrt::to_string (Cryptography::CryptographicBuffer::EncodeToHexString (buffer));
262
301
302
+ RNFetchBlobStream streamInstance{ stream, append, encodingOption };
263
303
264
304
305
+
306
+ }
307
+ catch (const hresult_error& ex)
308
+ {
309
+ callback (" EUNSPECIFIED" , " Failed to create write stream at path `" + path + " `; " + winrt::to_string (ex.message ().c_str ()), " " );
310
+ }
311
+
312
+
313
+ // writeChunk
314
+ void RNFetchBlob::writeChunk (
315
+ std::string streamId,
316
+ std::string data,
317
+ std::function<void (std::string)> callback) noexcept
318
+ {
319
+ }
320
+
265
321
// readStream
322
+ void RNFetchBlob::readStream (
323
+ std::string path,
324
+ std::string encoding,
325
+ int bufferSize,
326
+ int tick,
327
+ const std::string streamId) noexcept
328
+ {
266
329
330
+ }
267
331
268
332
269
333
// mkdir - Implemented, not tested
@@ -638,7 +702,6 @@ catch (const hresult_error& ex)
638
702
}
639
703
640
704
641
-
642
705
// df - Implemented, not tested
643
706
winrt::fire_and_forget RNFetchBlob::df (
644
707
std::function<void (std::string, winrt::Microsoft::ReactNative::JSValueObject&)> callback) noexcept
@@ -658,11 +721,131 @@ catch (...)
658
721
callback (" Failed to get storage usage." , emptyObject);
659
722
}
660
723
724
+
725
+ winrt::fire_and_forget RNFetchBlob::slice (
726
+ std::string src,
727
+ std::string dest,
728
+ uint32_t start,
729
+ uint32_t end,
730
+ winrt::Microsoft::ReactNative::ReactPromise<std::string> promise) noexcept
731
+ try
732
+ {
733
+ winrt::hstring srcDirectoryPath, srcFileName, destDirectoryPath, destFileName;
734
+ splitPath (src, srcDirectoryPath, srcFileName);
735
+ splitPath (src, destDirectoryPath, destFileName);
736
+
737
+ StorageFolder srcFolder{ co_await StorageFolder::GetFolderFromPathAsync (srcDirectoryPath) };
738
+ StorageFolder destFolder{ co_await StorageFolder::GetFolderFromPathAsync (destDirectoryPath) };
739
+
740
+ StorageFile srcFile{ co_await srcFolder.GetFileAsync (srcFileName) };
741
+ StorageFile destFile{ co_await destFolder.CreateFileAsync (destFileName, CreationCollisionOption::OpenIfExists) };
742
+
743
+ uint32_t length{ end - start };
744
+ Streams::IBuffer buffer;
745
+ Streams::IRandomAccessStream stream{ co_await srcFile.OpenAsync (FileAccessMode::Read) };
746
+ stream.Seek (start);
747
+ stream.ReadAsync (buffer, length, Streams::InputStreamOptions::None);
748
+ co_await FileIO::WriteBufferAsync (destFile, buffer);
749
+
750
+ promise.Resolve (dest);
751
+ }
752
+ catch (...)
753
+ {
754
+ promise.Reject (" Unable to slice file" );
755
+ }
756
+
757
+
758
+ void RNFetchBlob::fetchBlob (
759
+ winrt::Microsoft::ReactNative::JSValueObject options,
760
+ std::string taskId,
761
+ std::string method,
762
+ std::string url,
763
+ winrt::Microsoft::ReactNative::JSValueObject headers,
764
+ std::string body,
765
+ std::function<void (std::string)> callback) noexcept
766
+ {
767
+
768
+ }
769
+
770
+ void RNFetchBlob::fetchBlobForm (
771
+ winrt::Microsoft::ReactNative::JSValueObject options,
772
+ std::string taskId,
773
+ std::string method,
774
+ std::string url,
775
+ winrt::Microsoft::ReactNative::JSValueObject headers,
776
+ winrt::Microsoft::ReactNative::JSValueArray body,
777
+ std::function<void (std::string)> callback) noexcept
778
+ {
779
+
780
+ }
781
+
782
+ void RNFetchBlob::enableProgressReport (
783
+ std::string taskId,
784
+ int interval,
785
+ int count) noexcept
786
+ {
787
+ }
788
+
789
+ // enableUploadProgressReport
790
+ void RNFetchBlob::enableUploadProgressReport (
791
+ std::string taskId,
792
+ int interval,
793
+ int count) noexcept
794
+ {
795
+ }
796
+
797
+ // cancelRequest
798
+ void RNFetchBlob::cancelRequest (
799
+ std::string taskId,
800
+ std::function<void (std::string)> callback) noexcept
801
+ {
802
+ }
803
+
804
+ void RNFetchBlob::removeSession (
805
+ winrt::Microsoft::ReactNative::JSValueObject paths,
806
+ std::function<void (std::string)> callback) noexcept
807
+ {
808
+
809
+ }
810
+
811
+ void RNFetchBlob::closeStream (
812
+ std::string streamId,
813
+ std::function<void (std::string)> callback) noexcept
814
+ {
815
+
816
+ }
817
+
818
+
661
819
void RNFetchBlob::splitPath (const std::string& fullPath, winrt::hstring& directoryPath, winrt::hstring& fileName) noexcept
662
820
{
663
- std::filesystem::path path ( fullPath) ;
821
+ std::filesystem::path path{ fullPath } ;
664
822
path.make_preferred ();
665
823
666
824
directoryPath = path.has_parent_path () ? winrt::to_hstring (path.parent_path ().c_str ()) : L" " ;
667
825
fileName = path.has_filename () ? winrt::to_hstring (path.filename ().c_str ()) : L" " ;
668
826
}
827
+
828
+
829
+
830
+ RNFetchBlobStream::RNFetchBlobStream (Streams::IRandomAccessStream & _streamInstance, bool _append, EncodingOptions _encoding) noexcept
831
+ : streamInstance{ std::move (_streamInstance) }
832
+ , append{ _append }
833
+ , encoding{ _encoding }
834
+ {
835
+ }
836
+
837
+
838
+ void RNFetchBlobStreamMap::Add (StreamId streamId, RNFetchBlobStream streamContainer) noexcept
839
+ {
840
+ m_streamMap.try_emplace (streamId, streamContainer);
841
+ }
842
+ RNFetchBlobStream& RNFetchBlobStreamMap::Get (StreamId streamId) noexcept
843
+ {
844
+ auto iter{ m_streamMap.find (streamId) };
845
+ return iter->second ;
846
+ }
847
+ void RNFetchBlobStreamMap::Remove (StreamId streamId) noexcept
848
+ {
849
+ m_streamMap.extract (streamId);
850
+ }
851
+
0 commit comments