Skip to content

Commit cae5833

Browse files
committed
Implement ds, unlink, exists, and mkdir
1 parent 342fda0 commit cae5833

File tree

2 files changed

+69
-39
lines changed

2 files changed

+69
-39
lines changed

RNFetchBlobWin/windows/RNFetchBlobWin/RNFetchBlob.cpp

Lines changed: 64 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ try
5353
catch (...)
5454
{
5555
promise.Reject("EEXIST: File already exists."); // TODO: Include filepath
56+
//promise.Reject()
5657
co_return;
5758
}
5859
promise.Resolve(path);
@@ -108,11 +109,29 @@ winrt::fire_and_forget RNFetchBlob::writeFileArray(
108109

109110

110111
// mkdir
111-
winrt::fire_and_forget RNFetchBlob::mkdir(
112+
void RNFetchBlob::mkdir(
112113
std::string path,
113114
winrt::Microsoft::ReactNative::ReactPromise<bool> promise) noexcept
115+
try
114116
{
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()) });
116135
}
117136

118137

@@ -166,29 +185,44 @@ winrt::fire_and_forget RNFetchBlob::cp(
166185

167186

168187
// exists
169-
winrt::fire_and_forget RNFetchBlob::exists(
188+
void RNFetchBlob::exists(
170189
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
181191
{
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);
183197
}
184198

185199

186200
// unlink
187201
winrt::fire_and_forget RNFetchBlob::unlink(
188202
std::string path,
189-
winrt::Microsoft::ReactNative::ReactPromise<void> promise) noexcept
203+
std::function<void(std::string, bool)> callback) noexcept
204+
try
190205
{
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);
192226
}
193227

194228

@@ -219,9 +253,21 @@ winrt::fire_and_forget RNFetchBlob::asset(
219253

220254
// df
221255
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
223258
{
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);
225271
}
226272

227273
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
232278
directoryPath = path.has_parent_path() ? winrt::to_hstring(path.parent_path().c_str()) : L"";
233279
fileName = path.has_filename() ? winrt::to_hstring(path.filename().c_str()) : L"";
234280
}
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-
}

RNFetchBlobWin/windows/RNFetchBlobWin/RNFetchBlob.h

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ struct RNFetchBlob
4747

4848
// mkdir
4949
REACT_METHOD(mkdir);
50-
winrt::fire_and_forget mkdir(
50+
void mkdir(
5151
std::string path,
5252
winrt::Microsoft::ReactNative::ReactPromise<bool> promise) noexcept;
5353

@@ -93,23 +93,16 @@ struct RNFetchBlob
9393

9494
// exists
9595
REACT_METHOD(exists);
96-
winrt::fire_and_forget exists(
96+
void exists(
9797
std::string path,
98-
winrt::Microsoft::ReactNative::ReactPromise<bool> promise) noexcept;
99-
100-
101-
// isDir
102-
REACT_METHOD(isDir);
103-
winrt::fire_and_forget isDir(
104-
std::string path,
105-
winrt::Microsoft::ReactNative::ReactPromise<bool> promise) noexcept;
98+
std::function<void(bool, bool)> callback) noexcept;
10699

107100

108101
// unlink
109102
REACT_METHOD(unlink);
110103
winrt::fire_and_forget unlink(
111104
std::string path,
112-
winrt::Microsoft::ReactNative::ReactPromise<void> promise) noexcept;
105+
std::function<void(std::string, bool)> callback) noexcept;
113106

114107

115108
// lstat
@@ -136,7 +129,7 @@ struct RNFetchBlob
136129
// df
137130
REACT_METHOD(df);
138131
winrt::fire_and_forget df(
139-
winrt::Microsoft::ReactNative::ReactPromise<winrt::Microsoft::ReactNative::JSValueObject> promise) noexcept;
132+
std::function<void(std::string, winrt::Microsoft::ReactNative::JSValueObject&)> callback) noexcept;
140133

141134

142135
// Helper methods

0 commit comments

Comments
 (0)