@@ -249,7 +249,9 @@ catch (const hresult_error& ex)
249249 }
250250 else
251251 {
252- promise.Reject (" EUNSPECIFIED: Failed to write to file." );
252+ // EUNSPECIFIED: Failed to write to file
253+ auto errorMessage{ L" EUNSPECIFIED: " + ex.message () };
254+ promise.Reject (errorMessage.c_str ());
253255 }
254256}
255257
@@ -271,23 +273,12 @@ winrt::fire_and_forget RNFetchBlob::createFileASCII(
271273 winrt::hstring directoryPath, fileName;
272274 splitPath (path, directoryPath, fileName);
273275
274- bool shouldExit{ false };
275276 StorageFolder folder{ co_await StorageFolder::GetFolderFromPathAsync (directoryPath) };
276- try
277- {
278- StorageFile file{ co_await folder.CreateFileAsync (fileName, CreationCollisionOption::FailIfExists) };
279- Streams::IRandomAccessStream stream{ co_await file.OpenAsync (FileAccessMode::ReadWrite) };
280- co_await stream.WriteAsync (buffer);
281- }
282- catch (...)
283- {
284- promise.Reject (" EEXIST: File already exists." ); // TODO: Include filepath
285- shouldExit = true ;
286- }
287- if (shouldExit)
288- {
289- co_return ;
290- }
277+
278+ StorageFile file{ co_await folder.CreateFileAsync (fileName, CreationCollisionOption::FailIfExists) };
279+ Streams::IRandomAccessStream stream{ co_await file.OpenAsync (FileAccessMode::ReadWrite) };
280+ co_await stream.WriteAsync (buffer);
281+
291282 promise.Resolve (path);
292283}
293284catch (const hresult_error& ex)
@@ -297,9 +288,14 @@ catch (const hresult_error& ex)
297288 {
298289 promise.Reject (" ENOENT: File does not exist and could not be created" ); // TODO: Include filepath
299290 }
300- else
291+ else if (result == 0x80070050 )
301292 {
302- promise.Reject (" EUNSPECIFIED: Failed to write to file." );
293+ promise.Reject (" EEXIST: File already exists." );
294+ }
295+ else
296+ {
297+ auto errorMessage{ L" EUNSPECIFIED: " + ex.message () };
298+ promise.Reject (errorMessage.c_str ());
303299 }
304300}
305301
332328 }
333329 else
334330 {
335- promise.Reject (" Invalid encoding" );
331+ auto errorMessage{ " Invalid encoding: " + encoding };
332+ promise.Reject (errorMessage.c_str ());
336333 }
337334
338335 winrt::hstring destDirectoryPath, destFileName;
@@ -963,54 +960,30 @@ catch (...)
963960winrt::fire_and_forget RNFetchBlob::stat (
964961 std::string path,
965962 std::function<void (std::string, winrt::Microsoft::ReactNative::JSValueObject&)> callback) noexcept
966- try
963+ try
967964{
968965 std::filesystem::path givenPath (path);
969966 givenPath.make_preferred ();
967+ bool isDirectory{ std::filesystem::is_directory (path) };
970968
971- std::string resultPath{ winrt::to_string (givenPath.c_str ()) };
972- auto potentialPath{ winrt::to_hstring (resultPath) };
973- bool isFile{ false };
974- uint64_t mtime{ 0 };
975- uint64_t size{ 0 };
969+ // std::string resultPath{ winrt::to_string(givenPath.c_str()) };
970+ auto resultPath{ winrt::to_hstring (givenPath.c_str ()) };
976971
977972 // Try to open as folder
978- try
979- {
980- StorageFolder folder{ co_await StorageFolder::GetFolderFromPathAsync (potentialPath) };
981- auto properties{ co_await folder.GetBasicPropertiesAsync () };
982- mtime = properties.DateModified ().time_since_epoch () / std::chrono::seconds (1 ) - UNIX_EPOCH_IN_WINRT_SECONDS;
983- size = properties.Size ();
984- }
985- catch (...)
986- {
987- isFile = true ;
988- auto lastCharIndex{ path.length () - 1 };
989- if (resultPath[lastCharIndex] == ' \\ ' )
990- {
991- givenPath = std::filesystem::path (resultPath.substr (0 , lastCharIndex));
992- }
973+ IStorageItem item;
974+ if (isDirectory) {
975+ item = co_await StorageFolder::GetFolderFromPathAsync (resultPath);
993976 }
994-
995- // Try to open as file
996- if (isFile)
997- {
998- auto directoryPath{ givenPath.has_parent_path () ? winrt::to_hstring (givenPath.parent_path ().c_str ()) : L" " };
999- auto fileName{ givenPath.has_filename () ? winrt::to_hstring (givenPath.filename ().c_str ()) : L" " };
1000-
1001- StorageFolder folder{ co_await StorageFolder::GetFolderFromPathAsync (directoryPath) };
1002- auto properties{ (co_await folder.GetFileAsync (fileName)).Properties () };
1003- auto propertyMap{ co_await properties.RetrievePropertiesAsync ({ L" System.DateCreated" , L" System.DateModified" , L" System.Size" }) };
1004- mtime = winrt::unbox_value<DateTime>(propertyMap.Lookup (L" System.DateModified" )).time_since_epoch () / std::chrono::seconds (1 ) - UNIX_EPOCH_IN_WINRT_SECONDS;
1005- size = winrt::unbox_value<uint64_t >(propertyMap.Lookup (L" System.Size" ));
977+ else {
978+ item = co_await StorageFile::GetFileFromPathAsync (resultPath);
1006979 }
1007-
980+ auto properties{ co_await item. GetBasicPropertiesAsync () };
1008981 winrt::Microsoft::ReactNative::JSValueObject fileInfo;
1009- fileInfo[" size" ] = size ;
982+ fileInfo[" size" ] = properties. Size () ;
1010983 fileInfo[" filename" ] = givenPath.filename ().string ();
1011984 fileInfo[" path" ] = givenPath.string ();
1012- fileInfo[" lastModified" ] = mtime ;
1013- fileInfo[" type" ] = isFile ? " file " : " directory " ;
985+ fileInfo[" lastModified" ] = winrt::clock::to_time_t (properties. DateModified ()); ;
986+ fileInfo[" type" ] = isDirectory ? " directory " : " file " ;
1014987
1015988 callback (" " , fileInfo);
1016989}
@@ -1115,7 +1088,7 @@ winrt::fire_and_forget RNFetchBlob::fetchBlob(
11151088 {
11161089 httpMethod = winrt::Windows::Web::Http::HttpMethod::Get ();
11171090 }
1118- else if (method. compare ( " POST " ) != 0 && method. compare ( " post " ) != 0 )
1091+ else
11191092 {
11201093 callback (" Method not supported" , " error" , " " );
11211094 co_return ;
0 commit comments