feat/fix: fixed storage error, added async stuff

This commit is contained in:
CT5
2026-03-14 14:32:19 +11:00
parent c42c4c36c5
commit f6df2bf149
21 changed files with 719 additions and 36 deletions

View File

@@ -268,4 +268,253 @@ bool wd::impl::winrt::interfaces::storage::WinRTDirectory::fileExists(std::strin
return false;
}
return false;
}
}
concurrencpp::result<bool> wd::impl::winrt::interfaces::storage::WinRTDirectory::openAsync()
{
if (dir != nullptr)
{
co_return false;
}
try
{
StorageFolder sf = ApplicationData::Current().LocalFolder();
for (const auto &part : path)
{
hstring partStr = hstring(part.wstring());
sf = co_await sf.GetFolderAsync(partStr);
}
if (sf)
{
dir = sf;
co_return true;
}
}
catch (const hresult_error &ex)
{
co_return false;
}
co_return false;
}
concurrencpp::result<std::shared_ptr<wd::common::interfaces::storage::File>> wd::impl::winrt::interfaces::storage::WinRTDirectory::CreateFileAsync(std::filesystem::path targetPath)
{
if (dir == nullptr)
{
co_return nullptr;
}
try
{
auto file = co_await dir.CreateFileAsync(hstring(targetPath.filename().wstring()), CreationCollisionOption::OpenIfExists);
std::shared_ptr<wd::impl::winrt::interfaces::storage::WinRTFile> fileRT =
std::make_shared<wd::impl::winrt::interfaces::storage::WinRTFile>(path / targetPath.filename());
co_return fileRT;
}
catch (const hresult_error &ex)
{
co_return nullptr;
}
}
concurrencpp::result<std::shared_ptr<wd::common::interfaces::storage::Directory>> wd::impl::winrt::interfaces::storage::WinRTDirectory::CreateFolderAsync(std::filesystem::path targetPath)
{
if (dir == nullptr)
{
co_return nullptr;
}
try
{
auto file = co_await dir.CreateFolderAsync(hstring(targetPath.filename().wstring()), CreationCollisionOption::OpenIfExists);
std::shared_ptr<wd::impl::winrt::interfaces::storage::WinRTDirectory> folderRT =
std::make_shared<wd::impl::winrt::interfaces::storage::WinRTDirectory>(path / targetPath.filename());
co_return folderRT;
}
catch (const hresult_error &ex)
{
co_return nullptr;
}
}
concurrencpp::result<std::filesystem::path> wd::impl::winrt::interfaces::storage::WinRTDirectory::dirpathAsync()
{
co_return path;
}
concurrencpp::result<bool> wd::impl::winrt::interfaces::storage::WinRTDirectory::renameAsync(std::string newName)
{
if (dir == nullptr)
{
co_return false;
}
try
{
co_await dir.RenameAsync(to_hstring(newName));
path.replace_filename(newName);
co_return true;
}
catch (const hresult_error &ex)
{
co_return false;
}
co_return false;
}
concurrencpp::result<bool> wd::impl::winrt::interfaces::storage::WinRTDirectory::removeAsync()
{
if (dir == nullptr)
{
co_return false;
}
try
{
co_await dir.DeleteAsync();
co_return true;
}
catch (const hresult_error &ex)
{
co_return false;
}
co_return false;
}
concurrencpp::result<bool> MoveFolderAsync(StorageFolder source, StorageFolder destinationContainer)
{
try
{
StorageFolder destinationFolder = co_await destinationContainer.CreateFolderAsync(source.Name(), CreationCollisionOption::OpenIfExists);
for (auto file : co_await source.GetFilesAsync())
{
co_await file.MoveAsync(destinationFolder, file.Name(), NameCollisionOption::GenerateUniqueName);
}
for (auto folder : co_await source.GetFoldersAsync())
{
co_await MoveFolderAsync(folder, destinationFolder);
}
co_return true;
}
catch (const hresult_error &ex)
{
co_return false;
}
}
concurrencpp::result<bool> wd::impl::winrt::interfaces::storage::WinRTDirectory::moveAsync(std::filesystem::path targetPath)
{
if (dir == nullptr)
{
co_return false;
}
try
{
StorageFolder target = ApplicationData::Current().LocalFolder();
for (const auto &part : targetPath)
{
hstring partStr = hstring(part.wstring());
target = co_await target.GetFolderAsync(partStr);
}
if (co_await MoveFolderAsync(dir, target))
{
path = targetPath;
co_return true;
}
}
catch (const hresult_error &ex)
{
co_return false;
}
co_return false;
}
concurrencpp::result<bool> CopyFolderAsync(StorageFolder source, StorageFolder destinationContainer)
{
try
{
StorageFolder destinationFolder = co_await destinationContainer.CreateFolderAsync(source.Name(), CreationCollisionOption::OpenIfExists);
for (auto file : co_await source.GetFilesAsync())
{
co_await file.CopyAsync(destinationFolder, file.Name(), NameCollisionOption::GenerateUniqueName);
}
for (auto folder : co_await source.GetFoldersAsync())
{
co_await CopyFolderAsync(folder, destinationFolder);
}
co_return true;
}
catch (const hresult_error &ex)
{
co_return false;
}
}
concurrencpp::result<bool> wd::impl::winrt::interfaces::storage::WinRTDirectory::copyAsync(std::filesystem::path targetPath)
{
if (dir == nullptr)
{
co_return false;
}
try
{
StorageFolder target = ApplicationData::Current().LocalFolder();
for (const auto &part : targetPath)
{
hstring partStr = hstring(part.wstring());
target = co_await target.GetFolderAsync(partStr);
}
if (co_await CopyFolderAsync(dir, target))
{
path = targetPath;
co_return true;
}
}
catch (const hresult_error &ex)
{
co_return false;
}
co_return false;
}
concurrencpp::result<bool> wd::impl::winrt::interfaces::storage::WinRTDirectory::dirExistsAsync(std::string name)
{
if (dir == nullptr)
{
co_return false;
}
try
{
auto item = co_await dir.GetItemAsync(to_hstring(name));
co_return item && item.IsOfType(StorageItemTypes::Folder);
}
catch (const hresult_error &ex)
{
co_return false;
}
co_return false;
}
concurrencpp::result<bool> wd::impl::winrt::interfaces::storage::WinRTDirectory::fileExistsAsync(std::string name)
{
if (dir == nullptr)
{
co_return false;
}
try
{
auto item = co_await dir.GetItemAsync(to_hstring(name));
co_return item && item.IsOfType(StorageItemTypes::File);
}
catch (const hresult_error &ex)
{
co_return false;
}
co_return false;
}