-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.cpp
More file actions
135 lines (121 loc) · 4.02 KB
/
Copy pathconfig.cpp
File metadata and controls
135 lines (121 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <fstream>
#include <QUrlQuery>
#include "config.h"
#include "util.hpp"
using std::map;
using std::string;
Config::Config() {
try {
std::ifstream file("config.json");
defer(file.close());
auto jsonConfig = nlohmann::json::parse(file);
user = jsonConfig.value("user", "");
auth = jsonConfig.value("auth", "");
apiHost = jsonConfig.value("apiHost", "").c_str();
this->remindMe = jsonConfig.value("remindMe", false);
} catch (std::exception e) {
this->WriteNull();
}
}
void Config::Write(bool status) {
if (!status) {
this->WriteNull();
return;
}
std::ofstream file("config.json");
defer(file.close());
nlohmann::json jsonTxt;
jsonTxt["user"] = this->user;
jsonTxt["auth"] = this->auth;
jsonTxt["apiHost"] = this->apiHost;
jsonTxt["remindMe"] = this->remindMe;
if (file.is_open()) {
file << std::setw(4) << jsonTxt;
}
}
// https://raw.githubusercontent.com/i-curve/filesystem-client/master/version.json
QNetworkRequest *Config::upgrade() {
return new QNetworkRequest(QUrl("https://raw.githubusercontent.com/i-curve/filesystem-client/master/version.json"));
}
QNetworkRequest *Config::getVersion(bool flag) {
// return this->concat(flag, "/version");
auto req = this->concatUrl("/version");
if (flag)
this->authRequest(req);
return req;
}
QNetworkRequest *Config::getBucket() {
// return this->concat(true, "/bucket");
auto req = this->concatUrl("/bucket");
this->authRequest(req);
return req;
}
QNetworkRequest *Config::getCatalog(std::string path) {
std::map<std::string, std::string> mp = {{"path", path}};
auto req = this->concatUrl("/file/catalog", mp);
this->authRequest(req);
return req;
}
QNetworkRequest *Config::getDownload(std::string bucket, std::string key) {
if (bucket.size() > 0 && bucket[0] == '/')
bucket = bucket.substr(1, bucket.size() - 1);
if (key.size() > 0 && key[0] == '/')
key = key.substr(1, key.size() - 1);
std::map<std::string, std::string> mp = {{"bucket", bucket}, {"key", key}};
auto req = this->concatUrl("/file/download", mp);
this->authRequest(req);
return req;
}
QNetworkRequest *Config::postFile() {
auto req = this->concatUrl("/file/upload");
this->authRequest(req);
return req;
}
QNetworkRequest *Config::addBucket() {
auto req = this->concatUrl("/bucket");
this->authRequest(req);
req->setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
return req;
}
QNetworkRequest *Config::deleteBucket(std::string bucket) {
if (!bucket.empty() && bucket[0] == '/')
bucket = bucket.substr(1, bucket.size() - 1);
map<string, string> mp = {{"name", bucket}};
auto req = this->concatUrl("/bucket", mp);
this->authRequest(req);
return req;
}
QNetworkRequest *Config::deleteFile(std::string bucket, std::string key) {
if (bucket.size() > 0 && bucket[0] == '/')
bucket = bucket.substr(1, bucket.size() - 1);
if (key.size() > 0 && key[0] == '/')
key = key.substr(1, key.size() - 1);
std::map<std::string, std::string> mp = {{"bucket", bucket}, {"key", key}};
auto req = this->concatUrl("/file/delete", mp);
this->authRequest(req);
return req;
}
void Config::WriteNull() {
std::ofstream file("config.json");
defer(file.close());
if (file.is_open()) {
file << "{}";
}
}
QNetworkRequest *Config::authRequest(QNetworkRequest *request) {
request->setRawHeader("user", this->user.c_str());
request->setRawHeader("auth", this->auth.c_str());
return request;
}
QNetworkRequest *Config::concatUrl(const std::string path, std::map<std::string, std::string> query, std::string data) {
QUrl url = QUrl(apiHost.c_str());
url.setPath(path.c_str());
QUrlQuery qquery;
for (auto [key, value] : query) {
qquery.addQueryItem(key.c_str(), value.c_str());
}
if (!query.empty())
url.setQuery(qquery);
QNetworkRequest *req = new QNetworkRequest(url);
return req;
}