129
129
}
130
130
catch (const hresult_error& ex)
131
131
{
132
- // "Unexpected error while making directory."
133
- promise.Reject (winrt::to_string (ex.message ()).c_str ());
134
132
promise.Reject (winrt::Microsoft::ReactNative::ReactError{ " EUNSPECIFIED" , " Error creating folder " + path + " , error: " + winrt::to_string (ex.message ().c_str ()) });
135
133
}
136
134
@@ -158,19 +156,69 @@ winrt::fire_and_forget RNFetchBlob::hash(
158
156
// ls
159
157
winrt::fire_and_forget RNFetchBlob::ls (
160
158
std::string path,
161
- winrt::Microsoft::ReactNative::ReactPromise<winrt::Microsoft::ReactNative::JSValueArray> promise) noexcept
159
+ winrt::Microsoft::ReactNative::ReactPromise<std::vector<std::string>> promise) noexcept
160
+ try
162
161
{
163
- co_return ;
162
+ winrt::hstring directoryPath, fileName;
163
+ splitPath (path, directoryPath, fileName);
164
+
165
+ StorageFolder targetDirectory{ co_await StorageFolder::GetFolderFromPathAsync (directoryPath) };
166
+
167
+ std::vector<std::string> results;
168
+ auto items{ co_await targetDirectory.GetItemsAsync () };
169
+ for (auto item : items)
170
+ {
171
+ results.push_back (to_string (item.Name ()));
172
+ }
173
+ promise.Resolve (results);
174
+ }
175
+ catch (const hresult_error& ex)
176
+ {
177
+ hresult result{ ex.code () };
178
+ if (result == 0x80070002 ) // FileNotFoundException
179
+ {
180
+ promise.Reject (winrt::Microsoft::ReactNative::ReactError{ " ENOTDIR" , " Not a directory '" + path + " '" });
181
+ }
182
+ else
183
+ {
184
+ promise.Reject (winrt::Microsoft::ReactNative::ReactError{ " EUNSPECIFIED" , winrt::to_string (ex.message ()).c_str () });
185
+ }
164
186
}
165
187
166
188
167
189
// mv
168
190
winrt::fire_and_forget RNFetchBlob::mv (
169
191
std::string src, // from
170
192
std::string dest, // to
171
- winrt::Microsoft::ReactNative::ReactPromise< bool > promise ) noexcept
193
+ std::function< void (std::string)> callback ) noexcept
172
194
{
173
- co_return ;
195
+ try
196
+ {
197
+ winrt::hstring srcDirectoryPath, srcFileName;
198
+ splitPath (src, srcDirectoryPath, srcFileName);
199
+
200
+ winrt::hstring destDirectoryPath, destFileName;
201
+ splitPath (dest, destDirectoryPath, destFileName);
202
+
203
+ StorageFolder srcFolder{ co_await StorageFolder::GetFolderFromPathAsync (srcDirectoryPath) };
204
+ StorageFolder destFolder{ co_await StorageFolder::GetFolderFromPathAsync (destDirectoryPath) };
205
+ StorageFile file{ co_await srcFolder.GetFileAsync (srcFileName) };
206
+
207
+ co_await file.MoveAsync (destFolder, destFileName, NameCollisionOption::ReplaceExisting);
208
+ callback (" " );
209
+ }
210
+ catch (const hresult_error& ex)
211
+ {
212
+ hresult result{ ex.code () };
213
+ if (result == 0x80070002 ) // FileNotFoundException
214
+ {
215
+ callback (" Source file not found." );
216
+ }
217
+ else
218
+ {
219
+ callback (winrt::to_string (ex.message ()).c_str ());
220
+ }
221
+ }
174
222
}
175
223
176
224
@@ -179,8 +227,26 @@ winrt::fire_and_forget RNFetchBlob::cp(
179
227
std::string src, // from
180
228
std::string dest, // to
181
229
winrt::Microsoft::ReactNative::ReactPromise<bool > promise) noexcept
230
+ try
182
231
{
183
- co_return ;
232
+ winrt::hstring srcDirectoryPath, srcFileName;
233
+ splitPath (src, srcDirectoryPath, srcFileName);
234
+
235
+ winrt::hstring destDirectoryPath, destFileName;
236
+ splitPath (dest, destDirectoryPath, destFileName);
237
+
238
+ StorageFolder srcFolder{ co_await StorageFolder::GetFolderFromPathAsync (srcDirectoryPath) };
239
+ StorageFolder destFolder{ co_await StorageFolder::GetFolderFromPathAsync (destDirectoryPath) };
240
+ StorageFile file{ co_await srcFolder.GetFileAsync (srcFileName) };
241
+
242
+ co_await file.CopyAsync (destFolder, destFileName, NameCollisionOption::ReplaceExisting);
243
+
244
+ promise.Resolve (true );
245
+ }
246
+ catch (const hresult_error& ex)
247
+ {
248
+ // "Failed to copy file."
249
+ promise.Reject (winrt::to_string (ex.message ()).c_str ());
184
250
}
185
251
186
252
218
284
auto target{ co_await folder.GetItemAsync (fileName) };
219
285
co_await target.DeleteAsync ();
220
286
}
221
- callback (nullptr , true );
287
+ callback (" " , true );
222
288
}
223
289
catch (const hresult_error& ex)
224
290
{
262
328
winrt::Microsoft::ReactNative::JSValueObject result;
263
329
result[" freeSpace" ] = unbox_value<uint64_t >(properties.Lookup (L" System.FreeSpace" ));
264
330
result[" totalSpace" ] = unbox_value<uint64_t >(properties.Lookup (L" System.Capacity" ));
265
- callback (nullptr , result);
331
+ callback (" " , result);
266
332
}
267
333
catch (...)
268
334
{
0 commit comments