53
53
catch (...)
54
54
{
55
55
promise.Reject (" EEXIST: File already exists." ); // TODO: Include filepath
56
+ // promise.Reject()
56
57
co_return ;
57
58
}
58
59
promise.Resolve (path);
@@ -108,11 +109,29 @@ winrt::fire_and_forget RNFetchBlob::writeFileArray(
108
109
109
110
110
111
// mkdir
111
- winrt::fire_and_forget RNFetchBlob::mkdir (
112
+ void RNFetchBlob::mkdir (
112
113
std::string path,
113
114
winrt::Microsoft::ReactNative::ReactPromise<bool > promise) noexcept
115
+ try
114
116
{
115
- co_return ;
117
+ std::filesystem::path dirPath (path);
118
+ dirPath.make_preferred ();
119
+
120
+ // Consistent with Apple's createDirectoryAtPath method and result, but not with Android's
121
+ if (std::filesystem::create_directories (dirPath) == false )
122
+ {
123
+ promise.Reject (winrt::Microsoft::ReactNative::ReactError{ " ENOENT" , " ENOENT: no such file or directory, open " + path });
124
+ }
125
+ else
126
+ {
127
+ promise.Resolve (true );
128
+ }
129
+ }
130
+ catch (const hresult_error& ex)
131
+ {
132
+ // "Unexpected error while making directory."
133
+ promise.Reject (winrt::to_string (ex.message ()).c_str ());
134
+ promise.Reject (winrt::Microsoft::ReactNative::ReactError{ " EUNSPECIFIED" , " Error creating folder " + path + " , error: " + winrt::to_string (ex.message ().c_str ()) });
116
135
}
117
136
118
137
@@ -166,29 +185,44 @@ winrt::fire_and_forget RNFetchBlob::cp(
166
185
167
186
168
187
// exists
169
- winrt::fire_and_forget RNFetchBlob::exists (
188
+ void RNFetchBlob::exists (
170
189
std::string path,
171
- winrt::Microsoft::ReactNative::ReactPromise<bool > promise) noexcept
172
- {
173
- co_return ;
174
- }
175
-
176
-
177
- // isDir
178
- winrt::fire_and_forget RNFetchBlob::isDir (
179
- std::string path,
180
- winrt::Microsoft::ReactNative::ReactPromise<bool > promise) noexcept
190
+ std::function<void (bool , bool )> callback) noexcept
181
191
{
182
- co_return ;
192
+ std::filesystem::path fsPath (path);
193
+ bool doesExist{ std::filesystem::exists (fsPath) };
194
+ bool isDirectory{ doesExist ? std::filesystem::is_directory (fsPath) : false };
195
+
196
+ callback (doesExist, isDirectory);
183
197
}
184
198
185
199
186
200
// unlink
187
201
winrt::fire_and_forget RNFetchBlob::unlink (
188
202
std::string path,
189
- winrt::Microsoft::ReactNative::ReactPromise<void > promise) noexcept
203
+ std::function<void (std::string, bool )> callback) noexcept
204
+ try
190
205
{
191
- co_return ;
206
+ if (std::filesystem::is_directory (path))
207
+ {
208
+ std::filesystem::path path (path);
209
+ path.make_preferred ();
210
+ StorageFolder folder{ co_await StorageFolder::GetFolderFromPathAsync (winrt::to_hstring (path.c_str ())) };
211
+ co_await folder.DeleteAsync ();
212
+ }
213
+ else
214
+ {
215
+ winrt::hstring directoryPath, fileName;
216
+ splitPath (path, directoryPath, fileName);
217
+ StorageFolder folder{ co_await StorageFolder::GetFolderFromPathAsync (directoryPath) };
218
+ auto target{ co_await folder.GetItemAsync (fileName) };
219
+ co_await target.DeleteAsync ();
220
+ }
221
+ callback (nullptr , true );
222
+ }
223
+ catch (const hresult_error& ex)
224
+ {
225
+ callback (winrt::to_string (ex.message ()), false );
192
226
}
193
227
194
228
@@ -219,9 +253,21 @@ winrt::fire_and_forget RNFetchBlob::asset(
219
253
220
254
// df
221
255
winrt::fire_and_forget RNFetchBlob::df (
222
- winrt::Microsoft::ReactNative::ReactPromise<winrt::Microsoft::ReactNative::JSValueObject> promise) noexcept
256
+ std::function<void (std::string, winrt::Microsoft::ReactNative::JSValueObject&)> callback) noexcept
257
+ try
223
258
{
224
- co_return ;
259
+ auto localFolder{ Windows::Storage::ApplicationData::Current ().LocalFolder () };
260
+ auto properties{ co_await localFolder.Properties ().RetrievePropertiesAsync ({L" System.FreeSpace" , L" System.Capacity" }) };
261
+
262
+ winrt::Microsoft::ReactNative::JSValueObject result;
263
+ result[" freeSpace" ] = unbox_value<uint64_t >(properties.Lookup (L" System.FreeSpace" ));
264
+ result[" totalSpace" ] = unbox_value<uint64_t >(properties.Lookup (L" System.Capacity" ));
265
+ callback (nullptr , result);
266
+ }
267
+ catch (...)
268
+ {
269
+ winrt::Microsoft::ReactNative::JSValueObject emptyObject;
270
+ callback (" Failed to get storage usage." , emptyObject);
225
271
}
226
272
227
273
void RNFetchBlob::splitPath (const std::string& fullPath, winrt::hstring& directoryPath, winrt::hstring& fileName) noexcept
@@ -232,12 +278,3 @@ void RNFetchBlob::splitPath(const std::string& fullPath, winrt::hstring& directo
232
278
directoryPath = path.has_parent_path () ? winrt::to_hstring (path.parent_path ().c_str ()) : L" " ;
233
279
fileName = path.has_filename () ? winrt::to_hstring (path.filename ().c_str ()) : L" " ;
234
280
}
235
-
236
- void RNFetchBlob::splitPath (const winrt::hstring& fullPath, winrt::hstring& directoryPath, winrt::hstring& folderName) noexcept
237
- {
238
- std::filesystem::path path (winrt::to_string (fullPath));
239
- path.make_preferred ();
240
-
241
- directoryPath = path.has_parent_path () ? winrt::to_hstring (path.parent_path ().c_str ()) : L" " ;
242
- folderName = path.has_filename () ? winrt::to_hstring (path.filename ().c_str ()) : L" " ;
243
- }
0 commit comments