@@ -27,6 +27,8 @@ License along with this library; if not, write to the Free Software
27
27
#include " yandex_rest_client.h"
28
28
#include " httplib.h"
29
29
30
+ #define FILE_LIMIT 1000
31
+
30
32
YandexRestClient::YandexRestClient (){
31
33
m_client_id = " bc2f272cc37349b7a1320b9ac7826ebf" ;
32
34
m_http_client = new httplib::SSLClient (" cloud-api.yandex.net" );
@@ -68,66 +70,73 @@ void YandexRestClient::set_oauth_token(const char *token)
68
70
m_headers = { {" Authorization" , header_token} };
69
71
}
70
72
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 ;
79
79
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
+ }
82
94
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);
87
105
}
106
+
107
+ return pRes;
88
108
}
89
109
90
- pResources YandexRestClient::prepare_folder_result (json json, BOOL isRoot)
110
+ void YandexRestClient::prepare_folder_result (json json, BOOL isRoot, pResources pRes, int & total )
91
111
{
92
112
if (json[" _embedded" ][" total" ].is_null () || json[" _embedded" ][" items" ].is_null ())
93
113
throw std::runtime_error (" Wrong Json format" );
94
114
95
- int total = json[" _embedded" ][" total" ].get <int >();
115
+ total = json[" _embedded" ][" total" ].get <int >();
96
116
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);
100
118
101
- int i=0 ;
102
119
for (auto & item: json[" _embedded" ][" items" ]){
120
+ WIN32_FIND_DATAW file;
103
121
wcharstring wName = UTF8toUTF16 (item[" name" ].get <std::string>());
104
122
105
123
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);
107
125
108
126
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 ;
112
130
} 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 ;
116
134
}
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>());
122
137
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);
128
139
}
129
-
130
- return pRes;
131
140
}
132
141
133
142
void YandexRestClient::makeFolder (std::string utf8Path)
@@ -307,7 +316,6 @@ void YandexRestClient::move(std::string from, std::string to, BOOL overwrite)
307
316
308
317
void YandexRestClient::copy (std::string from, std::string to, BOOL overwrite)
309
318
{
310
- // TODO - max 'to' path is the 32760 symbols
311
319
std::string url (" /v1/disk/resources/copy?from=" );
312
320
url += url_encode (from) + " &path=" + url_encode (to);
313
321
url += " &overwrite=" ;
0 commit comments