93 lines
2.3 KiB
C++
93 lines
2.3 KiB
C++
#include <siaapi.h>
|
|
#include <regex>
|
|
#include <filepath.h>
|
|
|
|
using namespace Sia::Api;
|
|
|
|
CSiaApi::_CSiaFileTree::_CSiaFileTree(const CSiaCurl& siaCurl, CSiaDriveConfig* siaDriveConfig) :
|
|
CSiaBase(siaCurl, siaDriveConfig)
|
|
{
|
|
|
|
}
|
|
|
|
CSiaApi::_CSiaFileTree::~_CSiaFileTree()
|
|
{
|
|
|
|
}
|
|
|
|
void CSiaApi::_CSiaFileTree::BuildTree(const json& result)
|
|
{
|
|
CSiaFileCollectionPtr fileList(new CSiaFileCollection());
|
|
for (const auto& file : result["files"])
|
|
{
|
|
fileList->push_back(CSiaFilePtr(new CSiaFile(GetSiaCurl(), &GetSiaDriveConfig(), file)));
|
|
}
|
|
|
|
_fileList = fileList;
|
|
}
|
|
|
|
bool CSiaApi::_CSiaFileTree::FileExists(const SString& siaPath) const
|
|
{
|
|
auto fileList = GetFileList();
|
|
auto result = std::find_if(fileList->begin(), fileList->end(), [&](const CSiaFilePtr& item)->bool
|
|
{
|
|
return (item->GetSiaPath() == siaPath);
|
|
});
|
|
|
|
return (result != fileList->end());
|
|
}
|
|
|
|
CSiaFileCollectionPtr CSiaApi::_CSiaFileTree::GetFileList() const
|
|
{
|
|
return _fileList;
|
|
}
|
|
|
|
CSiaFilePtr CSiaApi::_CSiaFileTree::GetFile(const SString& siaPath) const
|
|
{
|
|
auto fileList = GetFileList();
|
|
auto result = std::find_if(fileList->begin(), fileList->end(), [&](const CSiaFilePtr& item)->bool
|
|
{
|
|
return (item->GetSiaPath() == siaPath);
|
|
});
|
|
|
|
return ((result != fileList->end()) ? *result : nullptr);
|
|
}
|
|
|
|
CSiaFileCollection CSiaApi::_CSiaFileTree::Query(SString query) const
|
|
{
|
|
auto fileList = GetFileList();
|
|
query = CSiaApi::FormatToSiaPath(query);
|
|
query.Replace(".", "\\.").Replace("*", "[^/]+").Replace("?", "[^/]?");
|
|
std::wregex r(query.str());
|
|
|
|
CSiaFileCollection ret;
|
|
std::copy_if(fileList->begin(), fileList->end(), std::back_inserter(ret), [&](const CSiaFilePtr& v) -> bool
|
|
{
|
|
return std::regex_match(v->GetSiaPath().str(), r);
|
|
});
|
|
|
|
return std::move(ret);
|
|
}
|
|
|
|
std::vector<SString> CSiaApi::_CSiaFileTree::QueryDirectories(SString rootFolder) const
|
|
{
|
|
auto fileList = GetFileList();
|
|
CSiaFileCollection col;
|
|
rootFolder.Replace("/", "\\");
|
|
|
|
std::vector<SString> ret;
|
|
std::for_each(fileList->begin(), fileList->end(), [&](const CSiaFilePtr& v)
|
|
{
|
|
SString path = ("\\" + FilePath(v->GetSiaPath()).RemoveFileName()).Replace("/", "\\");
|
|
if (path.BeginsWith(rootFolder))
|
|
{
|
|
path = path.SubString(1, rootFolder.Length());
|
|
if (path.Length())
|
|
{
|
|
|
|
}
|
|
}
|
|
});
|
|
|
|
return std::move(ret);
|
|
} |