1 | /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
---|
2 | /* |
---|
3 | * Copyright (C) 2015 HUBzero Foundation, LLC |
---|
4 | * |
---|
5 | * Author: Leif Delgass <ldelgass@purdue.edu> |
---|
6 | */ |
---|
7 | #include <string> |
---|
8 | #include <iostream> |
---|
9 | #include <sstream> |
---|
10 | #include <cstring> |
---|
11 | #include <cstdlib> |
---|
12 | #include <cstddef> |
---|
13 | |
---|
14 | #include <curl/curl.h> |
---|
15 | #include <osgEarth/JsonUtils> |
---|
16 | |
---|
17 | #include "IData.h" |
---|
18 | #include "Trace.h" |
---|
19 | |
---|
20 | static const char *commandNames[] = { |
---|
21 | "list", |
---|
22 | "get", |
---|
23 | "contents", |
---|
24 | "setglobalmeta", |
---|
25 | "delete", |
---|
26 | "upload", |
---|
27 | "finishupload", |
---|
28 | "checksum" |
---|
29 | }; |
---|
30 | #if 0 |
---|
31 | static const char *sharingValues[] = { |
---|
32 | "no", |
---|
33 | "read-only", |
---|
34 | "read-write" |
---|
35 | }; |
---|
36 | #endif |
---|
37 | |
---|
38 | static const char *defaultHubURL = "https://mygeohub.org"; |
---|
39 | static const std::string servicePath = "/api/idata/collection/"; |
---|
40 | static std::string serviceURL; |
---|
41 | static std::string iDataUsername; |
---|
42 | |
---|
43 | using namespace IData; |
---|
44 | using namespace osgEarth; |
---|
45 | |
---|
46 | void IData::iDataInit(const char *username, const char *hubURL) |
---|
47 | { |
---|
48 | TRACE("user: %s hub: %s", username, hubURL); |
---|
49 | static bool first = true; |
---|
50 | std::ostringstream oss; |
---|
51 | std::string hubHost; |
---|
52 | if (hubURL != NULL) { |
---|
53 | hubHost = hubURL; |
---|
54 | } else { |
---|
55 | hubHost = defaultHubURL; |
---|
56 | } |
---|
57 | oss << hubHost << servicePath; |
---|
58 | serviceURL = oss.str(); |
---|
59 | iDataUsername = username; |
---|
60 | if (first) { |
---|
61 | curl_global_init(CURL_GLOBAL_DEFAULT); |
---|
62 | first = false; |
---|
63 | } |
---|
64 | } |
---|
65 | |
---|
66 | void IData::iDataCleanup() |
---|
67 | { |
---|
68 | curl_global_cleanup(); |
---|
69 | } |
---|
70 | |
---|
71 | static std::string urlEncode(const std::string& str) |
---|
72 | { |
---|
73 | std::string retStr; |
---|
74 | CURL *curl = curl_easy_init(); |
---|
75 | if (curl) { |
---|
76 | char *encoded = curl_easy_escape(curl, str.c_str(), str.length()); |
---|
77 | retStr = encoded; |
---|
78 | curl_free(encoded); |
---|
79 | curl_easy_cleanup(curl); |
---|
80 | } |
---|
81 | return retStr; |
---|
82 | } |
---|
83 | |
---|
84 | static std::string formatQuery(Command cmd, std::vector<std::string> ¶ms) |
---|
85 | { |
---|
86 | std::ostringstream oss; |
---|
87 | oss << serviceURL << commandNames[cmd] << "?username=" << iDataUsername; |
---|
88 | for (std::vector<std::string>::iterator itr = params.begin(); |
---|
89 | itr != params.end(); ++itr) { |
---|
90 | oss << "&" << *itr << "="; |
---|
91 | itr++; |
---|
92 | oss << urlEncode(*itr); |
---|
93 | } |
---|
94 | |
---|
95 | return oss.str(); |
---|
96 | } |
---|
97 | |
---|
98 | static size_t writeData(void *buffer, size_t size, size_t nmemb, void *userp) |
---|
99 | { |
---|
100 | size_t numBytes = size * nmemb; |
---|
101 | Buffer *out = static_cast<Buffer *>(userp); |
---|
102 | out->data = realloc(out->data, out->size + numBytes); |
---|
103 | memcpy((char *)out->data + out->size, buffer, numBytes); |
---|
104 | out->size += numBytes; |
---|
105 | return numBytes; |
---|
106 | } |
---|
107 | |
---|
108 | void IData::getCollectionList(std::vector<Collection>& list) |
---|
109 | { |
---|
110 | std::vector<std::string> params; |
---|
111 | std::string query = formatQuery(COMMAND_LIST, params); |
---|
112 | CURL *curl = curl_easy_init(); |
---|
113 | if (curl) { |
---|
114 | curl_easy_setopt(curl, CURLOPT_URL, query.c_str()); |
---|
115 | curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeData); |
---|
116 | Buffer buff; |
---|
117 | curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buff); |
---|
118 | CURLcode ret = curl_easy_perform(curl); |
---|
119 | if (ret == CURLE_OK) { |
---|
120 | //std::cout.write((char *)buff.data, buff.size); |
---|
121 | Json::Reader reader; |
---|
122 | Json::Value result; |
---|
123 | const char *begin = (const char *)buff.data; |
---|
124 | const char *end = begin + buff.size; |
---|
125 | reader.parse(begin, end, result, false); |
---|
126 | for (size_t i = 0; i < result["collections"].size(); i++) { |
---|
127 | Collection collection; |
---|
128 | Json::Value& val = result["collections"][i]; |
---|
129 | collection.name = val["name"].asString(); |
---|
130 | collection.shared = val["shared"].asString(); |
---|
131 | collection.id = atoi(val["id"].asCString()); |
---|
132 | list.push_back(collection); |
---|
133 | } |
---|
134 | } |
---|
135 | curl_easy_cleanup(curl); |
---|
136 | } |
---|
137 | } |
---|
138 | |
---|
139 | void IData::getContents(int collectionID, const char *path, |
---|
140 | std::vector<DirectoryItem>& contents) |
---|
141 | { |
---|
142 | std::vector<std::string> params; |
---|
143 | { |
---|
144 | std::ostringstream oss; |
---|
145 | oss << collectionID; |
---|
146 | params.push_back("id"); |
---|
147 | params.push_back(oss.str()); |
---|
148 | } |
---|
149 | if (path) { |
---|
150 | params.push_back("path"); |
---|
151 | params.push_back(path); |
---|
152 | } |
---|
153 | std::string query = formatQuery(COMMAND_CONTENTS, params); |
---|
154 | CURL *curl = curl_easy_init(); |
---|
155 | if (curl) { |
---|
156 | curl_easy_setopt(curl, CURLOPT_URL, query.c_str()); |
---|
157 | curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeData); |
---|
158 | Buffer buff; |
---|
159 | curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buff); |
---|
160 | CURLcode ret = curl_easy_perform(curl); |
---|
161 | if (ret == CURLE_OK) { |
---|
162 | std::cout.write((char *)buff.data, buff.size); |
---|
163 | Json::Reader reader; |
---|
164 | Json::Value result; |
---|
165 | const char *begin = (const char *)buff.data; |
---|
166 | const char *end = begin + buff.size; |
---|
167 | reader.parse(begin, end, result, false); |
---|
168 | for (size_t i = 0; i < result["contents"].size(); i++) { |
---|
169 | DirectoryItem dir; |
---|
170 | Json::Value& val = result["contents"][i]; |
---|
171 | dir.name = val["name"].asString(); |
---|
172 | dir.isDir = (val["dir-or-file"].asInt() == TYPE_DIRECTORY); |
---|
173 | if (!dir.isDir) { |
---|
174 | dir.type = val["type"].asString(); |
---|
175 | dir.size = atol(val["size"].asCString()); |
---|
176 | dir.ctime = val["ctime"].asString(); |
---|
177 | dir.doi = val["doi"].asString(); |
---|
178 | } |
---|
179 | contents.push_back(dir); |
---|
180 | } |
---|
181 | } |
---|
182 | curl_easy_cleanup(curl); |
---|
183 | } |
---|
184 | } |
---|
185 | |
---|
186 | void IData::getFile(int collectionID, const char *doi, Buffer *buff) |
---|
187 | { |
---|
188 | std::vector<std::string> params; |
---|
189 | { |
---|
190 | std::ostringstream oss; |
---|
191 | oss << collectionID; |
---|
192 | params.push_back("id"); |
---|
193 | params.push_back(oss.str()); |
---|
194 | } |
---|
195 | if (doi) { |
---|
196 | params.push_back("doi"); |
---|
197 | params.push_back(doi); |
---|
198 | } |
---|
199 | std::string query = formatQuery(COMMAND_GET, params); |
---|
200 | CURL *curl = curl_easy_init(); |
---|
201 | if (curl) { |
---|
202 | curl_easy_setopt(curl, CURLOPT_URL, query.c_str()); |
---|
203 | curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeData); |
---|
204 | curl_easy_setopt(curl, CURLOPT_WRITEDATA, buff); |
---|
205 | CURLcode ret = curl_easy_perform(curl); |
---|
206 | if (ret == CURLE_OK) { |
---|
207 | //std::cout.write((char *)buff->data, buff->size); |
---|
208 | } |
---|
209 | curl_easy_cleanup(curl); |
---|
210 | } |
---|
211 | } |
---|
212 | |
---|
213 | void setFileMetadata(int collectionID, const char *doi, const char *name, const char *value) |
---|
214 | { |
---|
215 | std::vector<std::string> params; |
---|
216 | { |
---|
217 | std::ostringstream oss; |
---|
218 | oss << collectionID; |
---|
219 | params.push_back("id"); |
---|
220 | params.push_back(oss.str()); |
---|
221 | } |
---|
222 | if (doi) { |
---|
223 | params.push_back("doi"); |
---|
224 | params.push_back(doi); |
---|
225 | } |
---|
226 | if (name) { |
---|
227 | params.push_back(name); |
---|
228 | params.push_back(value); |
---|
229 | } |
---|
230 | std::string query = formatQuery(COMMAND_SETGLOBALMETA, params); |
---|
231 | CURL *curl = curl_easy_init(); |
---|
232 | if (curl) { |
---|
233 | curl_easy_setopt(curl, CURLOPT_URL, query.c_str()); |
---|
234 | curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeData); |
---|
235 | Buffer buff; |
---|
236 | curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buff); |
---|
237 | CURLcode ret = curl_easy_perform(curl); |
---|
238 | if (ret == CURLE_OK) { |
---|
239 | //std::cout.write((char *)buff->data, buff->size); |
---|
240 | } |
---|
241 | curl_easy_cleanup(curl); |
---|
242 | } |
---|
243 | } |
---|
244 | |
---|
245 | void deleteFile(int collectionID, const char *path, const char *filename) |
---|
246 | { |
---|
247 | std::vector<std::string> params; |
---|
248 | { |
---|
249 | std::ostringstream oss; |
---|
250 | oss << collectionID; |
---|
251 | params.push_back("id"); |
---|
252 | params.push_back(oss.str()); |
---|
253 | } |
---|
254 | if (path) { |
---|
255 | params.push_back("path"); |
---|
256 | params.push_back(path); |
---|
257 | } |
---|
258 | if (filename) { |
---|
259 | params.push_back("filename"); |
---|
260 | params.push_back(filename); |
---|
261 | } |
---|
262 | std::string query = formatQuery(COMMAND_DELETE, params); |
---|
263 | CURL *curl = curl_easy_init(); |
---|
264 | if (curl) { |
---|
265 | curl_easy_setopt(curl, CURLOPT_URL, query.c_str()); |
---|
266 | curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeData); |
---|
267 | Buffer buff; |
---|
268 | curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buff); |
---|
269 | CURLcode ret = curl_easy_perform(curl); |
---|
270 | if (ret == CURLE_OK) { |
---|
271 | //std::cout.write((char *)buff->data, buff->size); |
---|
272 | } |
---|
273 | curl_easy_cleanup(curl); |
---|
274 | } |
---|
275 | } |
---|