Skip to content

Commit b2ab1cd

Browse files
committed
Implemented ls and mv
1 parent cae5833 commit b2ab1cd

File tree

4 files changed

+84
-14
lines changed

4 files changed

+84
-14
lines changed

RNFetchBlobWin/windows/RNFetchBlobWin/RNFetchBlob.cpp

Lines changed: 75 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,6 @@ try
129129
}
130130
catch (const hresult_error& ex)
131131
{
132-
// "Unexpected error while making directory."
133-
promise.Reject(winrt::to_string(ex.message()).c_str());
134132
promise.Reject(winrt::Microsoft::ReactNative::ReactError{ "EUNSPECIFIED", "Error creating folder " + path + ", error: " + winrt::to_string(ex.message().c_str()) });
135133
}
136134

@@ -158,19 +156,69 @@ winrt::fire_and_forget RNFetchBlob::hash(
158156
// ls
159157
winrt::fire_and_forget RNFetchBlob::ls(
160158
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
162161
{
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+
}
164186
}
165187

166188

167189
// mv
168190
winrt::fire_and_forget RNFetchBlob::mv(
169191
std::string src, // from
170192
std::string dest, // to
171-
winrt::Microsoft::ReactNative::ReactPromise<bool> promise) noexcept
193+
std::function<void(std::string)> callback) noexcept
172194
{
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+
}
174222
}
175223

176224

@@ -179,8 +227,26 @@ winrt::fire_and_forget RNFetchBlob::cp(
179227
std::string src, // from
180228
std::string dest, // to
181229
winrt::Microsoft::ReactNative::ReactPromise<bool> promise) noexcept
230+
try
182231
{
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());
184250
}
185251

186252

@@ -218,7 +284,7 @@ try
218284
auto target{ co_await folder.GetItemAsync(fileName) };
219285
co_await target.DeleteAsync();
220286
}
221-
callback(nullptr, true);
287+
callback("", true);
222288
}
223289
catch (const hresult_error& ex)
224290
{
@@ -262,7 +328,7 @@ try
262328
winrt::Microsoft::ReactNative::JSValueObject result;
263329
result["freeSpace"] = unbox_value<uint64_t>(properties.Lookup(L"System.FreeSpace"));
264330
result["totalSpace"] = unbox_value<uint64_t>(properties.Lookup(L"System.Capacity"));
265-
callback(nullptr, result);
331+
callback("", result);
266332
}
267333
catch (...)
268334
{

RNFetchBlobWin/windows/RNFetchBlobWin/RNFetchBlob.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,15 @@ struct RNFetchBlob
7272
REACT_METHOD(ls);
7373
winrt::fire_and_forget ls(
7474
std::string path,
75-
winrt::Microsoft::ReactNative::ReactPromise<winrt::Microsoft::ReactNative::JSValueArray> promise) noexcept;
75+
winrt::Microsoft::ReactNative::ReactPromise<std::vector<std::string>> promise) noexcept;
7676

7777

7878
// mv
7979
REACT_METHOD(mv);
8080
winrt::fire_and_forget mv(
8181
std::string src, // from
8282
std::string dest, // to
83-
winrt::Microsoft::ReactNative::ReactPromise<bool> promise) noexcept;
83+
std::function<void(std::string)> callback) noexcept;
8484

8585

8686
// cp
@@ -134,12 +134,14 @@ struct RNFetchBlob
134134

135135
// Helper methods
136136
private:
137+
constexpr static int64_t UNIX_EPOCH_IN_WINRT_SECONDS = 11644473600;
138+
137139
void splitPath(const std::string& fullPath,
138140
winrt::hstring& directoryPath,
139141
winrt::hstring& fileName) noexcept;
140142

141-
void splitPath(const winrt::hstring& fullPath,
142-
winrt::hstring& directoryPath,
143-
winrt::hstring& folderName) noexcept;
143+
//void splitPath(const winrt::hstring& fullPath,
144+
// winrt::hstring& directoryPath,
145+
// winrt::hstring& folderName) noexcept;
144146

145147
};

RNFetchBlobWin/windows/RNFetchBlobWin/RNFetchBlobWin.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@
116116
<DependentUpon>App.xaml</DependentUpon>
117117
</ClInclude>
118118
<ClInclude Include="RNFetchBlob.h" />
119+
<ClInclude Include="RNFetchBlobConst.h" />
119120
</ItemGroup>
120121
<ItemGroup>
121122
<ApplicationDefinition Include="App.xaml">

RNFetchBlobWin/windows/RNFetchBlobWin/RNFetchBlobWin.vcxproj.filters

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<ClInclude Include="ReactPackageProvider.h" />
2121
<ClInclude Include="AutolinkedNativeModules.g.h" />
2222
<ClInclude Include="RNFetchBlob.h" />
23+
<ClInclude Include="RNFetchBlobConst.h" />
2324
</ItemGroup>
2425
<ItemGroup>
2526
<Image Include="Assets\Wide310x150Logo.scale-200.png">

0 commit comments

Comments
 (0)