Skip to content

Commit bceca51

Browse files
committed
yandex client - check resource limits
1 parent 9e89cc4 commit bceca51

File tree

2 files changed

+49
-41
lines changed

2 files changed

+49
-41
lines changed

service_clients/yandex_rest_client.cpp

Lines changed: 48 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ License along with this library; if not, write to the Free Software
2727
#include "yandex_rest_client.h"
2828
#include "httplib.h"
2929

30+
#define FILE_LIMIT 1000
31+
3032
YandexRestClient::YandexRestClient(){
3133
m_client_id = "bc2f272cc37349b7a1320b9ac7826ebf";
3234
m_http_client = new httplib::SSLClient("cloud-api.yandex.net");
@@ -68,66 +70,73 @@ void YandexRestClient::set_oauth_token(const char *token)
6870
m_headers = { {"Authorization", header_token} };
6971
}
7072

71-
pResources YandexRestClient::get_resources(std::string path, BOOL isTrash) {
72-
//TODO turn off unnesesary fields
73-
std::string url("/v1/disk/");
74-
if(isTrash)
75-
url += "trash/";
76-
url += "resources?limit=1000&path=";
77-
url += url_encode(path);
78-
auto r = m_http_client->Get(url.c_str(), m_headers);
73+
pResources YandexRestClient::get_resources(std::string path, BOOL isTrash)
74+
{
75+
int offset = 0;
76+
int total = 0;
77+
pResources pRes = new tResources;
78+
pRes->nCount = 0;
7979

80-
if(r.get() && r->status==200){
81-
const auto json = json::parse(r->body);
80+
do{
81+
std::string url("/v1/disk/");
82+
if(isTrash)
83+
url += "trash/";
84+
url += "resources?limit=" + std::to_string(FILE_LIMIT) + "&offset=" + std::to_string(offset) + "&path=";
85+
url += url_encode(path);
86+
auto r = m_http_client->Get(url.c_str(), m_headers);
87+
88+
if(r.get() && r->status==200){
89+
const auto json = json::parse(r->body);
90+
prepare_folder_result(json, (path == "/"), pRes, total);
91+
} else {
92+
throw_response_error(r.get());
93+
}
8294

83-
//TODO check if total > limit
84-
return prepare_folder_result(json, (path == "/") );
85-
} else {
86-
throw_response_error(r.get());
95+
offset += FILE_LIMIT;
96+
} while(offset < total);
97+
98+
if(path == "/"){ // is root
99+
WIN32_FIND_DATAW file;
100+
memcpy(file.cFileName, u".Trash", sizeof(WCHAR) * 7);
101+
file.dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY;
102+
file.nFileSizeLow = 0;
103+
file.nFileSizeHigh = 0;
104+
pRes->resource_array.push_back(file);
87105
}
106+
107+
return pRes;
88108
}
89109

90-
pResources YandexRestClient::prepare_folder_result(json json, BOOL isRoot)
110+
void YandexRestClient::prepare_folder_result(json json, BOOL isRoot, pResources pRes, int& total)
91111
{
92112
if(json["_embedded"]["total"].is_null() || json["_embedded"]["items"].is_null())
93113
throw std::runtime_error("Wrong Json format");
94114

95-
int total = json["_embedded"]["total"].get<int>();
115+
total = json["_embedded"]["total"].get<int>();
96116

97-
pResources pRes = new tResources;
98-
pRes->nCount = 0;
99-
pRes->resource_array.resize(isRoot ? total+1: total);
117+
pRes->resource_array.reserve(isRoot ? total+1: total);
100118

101-
int i=0;
102119
for(auto& item: json["_embedded"]["items"]){
120+
WIN32_FIND_DATAW file;
103121
wcharstring wName = UTF8toUTF16(item["name"].get<std::string>());
104122

105123
size_t str_size = (MAX_PATH > wName.size()+1)? (wName.size()+1): MAX_PATH;
106-
memcpy(pRes->resource_array[i].cFileName, wName.data(), sizeof(WCHAR) * str_size);
124+
memcpy(file.cFileName, wName.data(), sizeof(WCHAR) * str_size);
107125

108126
if(item["type"].get<std::string>() == "file") {
109-
pRes->resource_array[i].dwFileAttributes = 0;
110-
pRes->resource_array[i].nFileSizeLow = (DWORD) item["size"].get<int>();
111-
pRes->resource_array[i].nFileSizeHigh = 0;
127+
file.dwFileAttributes = 0;
128+
file.nFileSizeLow = (DWORD) item["size"].get<int>();
129+
file.nFileSizeHigh = 0;
112130
} else {
113-
pRes->resource_array[i].dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY;
114-
pRes->resource_array[i].nFileSizeLow = 0;
115-
pRes->resource_array[i].nFileSizeHigh = 0;
131+
file.dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY;
132+
file.nFileSizeLow = 0;
133+
file.nFileSizeHigh = 0;
116134
}
117-
pRes->resource_array[i].ftCreationTime = parse_iso_time(item["created"].get<std::string>());
118-
pRes->resource_array[i].ftLastWriteTime = parse_iso_time(item["modified"].get<std::string>());
119-
120-
i++;
121-
}
135+
file.ftCreationTime = parse_iso_time(item["created"].get<std::string>());
136+
file.ftLastWriteTime = parse_iso_time(item["modified"].get<std::string>());
122137

123-
if(isRoot){
124-
memcpy(pRes->resource_array[total].cFileName, u".Trash", sizeof(WCHAR) * 7);
125-
pRes->resource_array[total].dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY;
126-
pRes->resource_array[total].nFileSizeLow = 0;
127-
pRes->resource_array[total].nFileSizeHigh = 0;
138+
pRes->resource_array.push_back(file);
128139
}
129-
130-
return pRes;
131140
}
132141

133142
void YandexRestClient::makeFolder(std::string utf8Path)
@@ -307,7 +316,6 @@ void YandexRestClient::move(std::string from, std::string to, BOOL overwrite)
307316

308317
void YandexRestClient::copy(std::string from, std::string to, BOOL overwrite)
309318
{
310-
//TODO - max 'to' path is the 32760 symbols
311319
std::string url("/v1/disk/resources/copy?from=");
312320
url += url_encode(from) + "&path=" + url_encode(to);
313321
url += "&overwrite=";

service_clients/yandex_rest_client.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class YandexRestClient: public ServiceClient {
7070
void throw_response_error(httplib::Response* resp);
7171
void wait_success_operation(std::string &body);
7272
void _do_download(std::string url, std::ofstream &ofstream);
73-
pResources prepare_folder_result(json json, BOOL isRoot);
73+
void prepare_folder_result(json json, BOOL isRoot, pResources pRes, int& total);
7474

7575
};
7676

0 commit comments

Comments
 (0)