Skip to content

Commit 2db1288

Browse files
Merge branch 'main' into main
2 parents 5cad800 + da645d0 commit 2db1288

File tree

6 files changed

+218
-193
lines changed

6 files changed

+218
-193
lines changed

file_controller.cc

Lines changed: 96 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include "file_controller.h"
2-
2+
#include "jwt_controller.h"
33
#include <json/json.h>
44
#include <stdio.h>
55
#include <mysql.h>
@@ -47,92 +47,121 @@ std::string shell_commons(const char *cmd)
4747

4848
void genTree(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
4949
{
50-
char *pathvar;
51-
pathvar = getenv("PWD");
52-
std::string result = shell_commons(("cd " + std::string(pathvar) + "/.. " + "&&" + "tree -J root").c_str());
5350
auto res = HttpResponse::newHttpResponse();
54-
res->addHeader("Access-Control-Allow-Origin", "*");
55-
res->setBody(result);
56-
callback(res);
51+
if (jwtVerify(req))
52+
{
53+
char *pathvar;
54+
pathvar = getenv("PWD");
55+
std::string result = shell_commons(("cd " + std::string(pathvar) + "/.. " + "&&" + "tree -J root").c_str());
56+
res->addHeader("Access-Control-Allow-Origin", "*");
57+
res->setBody(result);
58+
callback(res);
59+
}
60+
else{
61+
res->setBody("No Authorization");
62+
}
5763
}
64+
5865
void catFile(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
5966
{
60-
char *pathvar;
61-
pathvar = getenv("PWD");
62-
std::string path = req->getParameter("path");
67+
6368
auto res = HttpResponse::newHttpResponse();
64-
res->addHeader("Access-Control-Allow-Origin", "*");
65-
std::string result = shell_commons(("cat " + std::string(pathvar) + "/../root/" + path).c_str());
66-
res->setBody(result);
67-
callback(res);
69+
if (jwtVerify(req))
70+
{
71+
char *pathvar;
72+
pathvar = getenv("PWD");
73+
std::string path = req->getParameter("path");
74+
std::string result = shell_commons(("cat " + std::string(pathvar) + "/../root/" + path).c_str());
75+
76+
res->addHeader("Access-Control-Allow-Origin", "*");
77+
res->setBody(result);
78+
79+
callback(res);
80+
}
81+
else
82+
{
83+
res->setBody("No Authorization");
84+
}
6885
}
86+
6987
void saveFile(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
7088
{
71-
auto body = req->getBody();
72-
Json::Value req_json;
73-
Json::Reader reader;
74-
std::string bodyStr(body);
75-
if (!reader.parse(bodyStr, req_json))
89+
auto res = HttpResponse::newHttpResponse();
90+
if (jwtVerify(req))
91+
{
92+
auto body = req->getBody();
93+
Json::Value req_json;
94+
Json::Reader reader;
95+
std::string bodyStr(body);
96+
if (!reader.parse(bodyStr, req_json))
97+
{
98+
std::cout << "parse failed" << std::endl;
99+
callback(HttpResponse::newHttpResponse());
100+
return;
101+
}
102+
char *pathvar;
103+
pathvar = getenv("PWD");
104+
std::string filename = req_json["filename"].asString();
105+
std::string content = req_json["content"].asString();
106+
std::string result = shell_commons(("echo '" + content + "'>" + std::string(pathvar) + "/../root/" + filename).c_str());
107+
108+
sql_unlocked(filename);
109+
110+
res->addHeader("Access-Control-Allow-Origin", "*");
111+
res->setBody("success");
112+
callback(res);
113+
}
114+
else
76115
{
77-
std::cout << "parse failed" << std::endl;
78-
callback(HttpResponse::newHttpResponse());
79-
return;
116+
res->setBody("No Authorization");
80117
}
81-
char *pathvar;
82-
pathvar = getenv("PWD");
83-
std::string filename = req_json["filename"].asString();
84-
std::string content = req_json["content"].asString();
85-
std::string result = shell_commons(("echo '" + content + "'>" + std::string(pathvar) + "/../root/" + filename).c_str());
86-
auto res = HttpResponse::newHttpResponse();
87-
res->addHeader("Access-Control-Allow-Origin", "*");
88-
res->setBody("success");
89-
callback(res);
90118
}
91119

92120
void imageUpload(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
93121
{
94-
MultiPartParser fileUpload;
95-
if (fileUpload.parse(req) != 0 || fileUpload.getFiles().size() != 1)
96-
{
97-
auto resp = HttpResponse::newHttpResponse();
98-
resp->setBody("Must only be one file");
99-
resp->setStatusCode(k403Forbidden);
100-
callback(resp);
101-
return;
102-
}
103-
auto &file = fileUpload.getFiles()[0];
104-
auto now = std::chrono::system_clock::now();
105-
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count();
106-
std::string timestamp = std::to_string(ms) + '.' + std::string(file.getFileExtension());
107122
auto resp = HttpResponse::newHttpResponse();
108-
resp->addHeader("Access-Control-Allow-Origin", "*");
109-
resp->setBody(timestamp);
110-
file.save();
111-
shell_commons(("mv ./uploads/" + file.getFileName() + " ./uploads/" + timestamp).c_str());
112-
113-
LOG_INFO << "The uploaded file has been saved to the ./uploads "
114-
"directory";
115-
callback(resp);
116-
// auto now = std::chrono::high_resolution_clock::now();
117-
// auto ms = std::chrono::time_point_cast<std::chrono::milliseconds>(now);
118-
// auto timestamp = std::chrono::duration_cast<std::chrono::milliseconds>(ms.time_since_epoch());
123+
if (jwtVerify(req))
124+
{
125+
MultiPartParser fileUpload;
126+
if (fileUpload.parse(req) != 0 || fileUpload.getFiles().size() != 1)
127+
{
128+
auto resp = HttpResponse::newHttpResponse();
129+
resp->setBody("Must only be one file");
130+
resp->setStatusCode(k403Forbidden);
131+
callback(resp);
132+
return;
133+
}
134+
auto &file = fileUpload.getFiles()[0];
135+
auto now = std::chrono::system_clock::now();
136+
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count();
137+
std::string timestamp = std::to_string(ms) + '.' + std::string(file.getFileExtension());
119138

120-
// std::stringstream ss;
121-
// ss << timestamp.count();
122-
// std::string fileName = ss.str();
123-
// // Save image to /root folder
124-
// auto imagePath = "/root/" + fileName;
125-
// imageFile->second.save(imagePath);
139+
resp->addHeader("Access-Control-Allow-Origin", "*");
140+
resp->setBody(timestamp);
141+
file.save();
142+
shell_commons(("mv ./uploads/" + file.getFileName() + " ./uploads/" + timestamp).c_str());
126143

127-
// // Send back filename in response
128-
// Json::Value resp;
129-
// resp["filename"] = fileName;
130-
// callback(HttpResponse::newHttpJsonResponse(resp));
144+
LOG_INFO << "The uploaded file has been saved to the ./uploads "
145+
"directory";
146+
callback(resp);
147+
}
148+
else
149+
{
150+
resp->setBody("No Authorization");
151+
}
131152
}
153+
132154
void getPicture(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
133155
{
134156
std::string filename = req->getParameter("filename");
135157
auto resp = HttpResponse::newFileResponse("./uploads/" + filename);
136-
resp->addHeader("Access-Control-Allow-Origin", "*");
137-
callback(resp);
158+
if (jwtVerify(req))
159+
{
160+
resp->addHeader("Access-Control-Allow-Origin", "*");
161+
callback(resp);
162+
}
163+
else
164+
{
165+
resp->setBody("No Authorization");
166+
}
138167
}

msg_controller.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ void chat(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)
2626
std::string sender = jwtDecrypt(req->getHeader("Authorization").substr(7));
2727
std::string content = req_json["content"].asString();
2828
std::string receiver = req_json["receiver"].asString();
29+
2930
sql_addhistory(sender, receiver, content, "0");
31+
3032
std::string msg = req_json["content"].asString();
3133
auto output = writer.write(res_json);
3234
res->setBody(output);
@@ -68,6 +70,7 @@ void friend_operation(const HttpRequestPtr &req, std::function<void(const HttpRe
6870
std::string sender = jwtDecrypt(req->getHeader("Authorization").substr(7));
6971
std::string receiver = req->getParameter("username");
7072
std::string operation = req->getParameter("operation");
73+
7174
if (operation == "add")
7275
{
7376
if (sql_findexist(receiver))
@@ -81,6 +84,7 @@ void friend_operation(const HttpRequestPtr &req, std::function<void(const HttpRe
8184
else
8285
{
8386
sql_delete_operation(sender, receiver);
87+
8488
res->setBody("Success");
8589
}
8690
}
@@ -95,8 +99,10 @@ void request_processing(const HttpRequestPtr &req, std::function<void(const Http
9599
{
96100
auto res = HttpResponse::newHttpResponse();
97101
res->addHeader("Access-Control-Allow-Origin", "*");
102+
98103
if (jwtVerify(req))
99104
{
105+
100106
std::string receiver = jwtDecrypt(req->getHeader("Authorization").substr(7));
101107
std::string sender = req->getParameter("username");
102108
std::string attitude = req->getParameter("info");
@@ -107,6 +113,7 @@ void request_processing(const HttpRequestPtr &req, std::function<void(const Http
107113
{
108114
res->setBody("No Authorization");
109115
}
116+
110117
callback(res);
111118
}
112119
// get chat info
@@ -125,8 +132,10 @@ void info(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)
125132
Json::FastWriter writer;
126133
auto res = HttpResponse::newHttpResponse();
127134
res->addHeader("Access-Control-Allow-Origin", "*");
135+
128136
if (jwtVerify(req))
129137
{
138+
130139
me = jwtDecrypt(req->getHeader("Authorization").substr(7));
131140
if (req_json["person"].asString() == "")
132141
{
@@ -142,5 +151,6 @@ void info(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)
142151
{
143152
res->setBody("No Authorization");
144153
}
154+
145155
callback(res);
146156
}

0 commit comments

Comments
 (0)