Skip to content

Commit 7757962

Browse files
Merge pull request #66 from 0WAQ/main
add code
2 parents a42a047 + d92a344 commit 7757962

File tree

5 files changed

+105
-68
lines changed

5 files changed

+105
-68
lines changed

file_controller.cc

Lines changed: 100 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
#include <iostream>
77
#include <stdexcept>
88
#include <string>
9-
std::string return_status(std::string result, const std::string& command,Json::Value &res_json)
9+
10+
std::string return_status(std::string result, const std::string &command, Json::Value &res_json)
1011
{
11-
if (!result.empty()){
12+
if (!result.empty())
13+
{
1214
result = command + " success";
1315
res_json["code"] = 200;
1416
}
@@ -17,32 +19,49 @@ std::string return_status(std::string result, const std::string& command,Json::V
1719
result = " Error in :" + command;
1820
res_json["code"] = 400;
1921
}
20-
22+
2123
return result;
2224
}
25+
2326
void add_lock(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
2427
{
28+
Json::Value res_json;
29+
Json::FastWriter writer;
30+
2531
std::string filename = req->getParameter("filename");
26-
if (lockcheck(filename))
32+
auto res = HttpResponse::newHttpResponse();
33+
34+
if (jwtVerify(req))
2735
{
28-
auto res = HttpResponse::newHttpResponse();
2936
res->addHeader("Access-Control-Allow-Origin", "*");
30-
res->setBody("This file has been occupied");
31-
callback(res);
37+
if (lockcheck(filename))
38+
{
39+
res_json["code"] = 402;
40+
res_json["message"] = "This file has been occupied";
41+
}
42+
else
43+
{
44+
res_json["code"] = 200;
45+
res_json["message"] = "Success";
46+
}
3247
}
3348
else
3449
{
35-
auto res = HttpResponse::newHttpResponse();
36-
res->addHeader("Access-Control-Allow-Origin", "*");
37-
res->setBody("This file is yours");
38-
callback(res);
50+
res_json["code"] = 401;
51+
res_json["message"] = "No Authorization";
3952
}
53+
54+
auto output = writer.write(res_json);
55+
res->setBody(output);
56+
callback(res);
4057
}
58+
4159
std::string shell_commands(const char *cmd)
4260
{
4361
char buffer[1280];
4462
std::string result;
4563
FILE *pipe = popen(cmd, "r");
64+
4665
if (!pipe)
4766
throw std::runtime_error("popen() failed!");
4867
try
@@ -58,17 +77,22 @@ std::string shell_commands(const char *cmd)
5877
throw;
5978
}
6079
pclose(pipe);
80+
6181
return result;
6282
}
6383

6484
void commandsCtrl(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
6585
{
6686
auto res = HttpResponse::newHttpResponse();
6787
std::string result;
88+
6889
Json::Value res_json;
6990
Json::FastWriter writer;
91+
7092
res->addHeader("Access-Control-Allow-Origin", "*");
71-
if (jwtVerify(req)){
93+
94+
if (jwtVerify(req))
95+
{
7296
enum Command
7397
{
7498
tree,
@@ -85,9 +109,10 @@ void commandsCtrl(const HttpRequestPtr &req, std::function<void(const HttpRespon
85109
command = static_cast<Command>(stoi(req->getJsonObject()->get("command", "").asString()));
86110
std::string params1 = req->getJsonObject()->get("params", "")[0].asString();
87111
std::string params2 = req->getJsonObject()->get("params", "")[1].asString();
112+
88113
if ((params1.find("..") != std::string::npos) || (params2.find("..") != std::string::npos))
89114
{
90-
result = "error:result in wrong directory";
115+
result = "Error:result in wrong directory";
91116
res_json["code"] = 400;
92117
}
93118
else
@@ -99,12 +124,11 @@ void commandsCtrl(const HttpRequestPtr &req, std::function<void(const HttpRespon
99124
break;
100125
case cp:
101126
result = shell_commands(("cp -v " + std::string(pathvar) + "/../root/" + params1 + " " + std::string(pathvar) + "/../root/" + params2).c_str());
102-
result = return_status(result, "cp",res_json);
103-
127+
result = return_status(result, "cp", res_json);
104128
break;
105129
case mv:
106130
result = shell_commands(("mv -v " + std::string(pathvar) + "/../root/" + params1 + " " + std::string(pathvar) + "/../root/" + params2).c_str());
107-
result = return_status(result, "mv",res_json);
131+
result = return_status(result, "mv", res_json);
108132
break;
109133
case rm:
110134
if (params1.find("..") != std::string::npos)
@@ -114,21 +138,19 @@ void commandsCtrl(const HttpRequestPtr &req, std::function<void(const HttpRespon
114138
break;
115139
}
116140
result = shell_commands(("rm -rf -v " + std::string(pathvar) + "/../root/" + params1).c_str());
117-
result = return_status(result, "rm",res_json);
141+
result = return_status(result, "rm", res_json);
118142
break;
119143
case mkdir:
120144
result = shell_commands(("mkdir -v " + std::string(pathvar) + "/../root/" + params1).c_str());
121-
result = return_status(result, "mkdir",res_json);
145+
result = return_status(result, "mkdir", res_json);
122146
break;
123147
case touch:
124148
if (shell_commands(("ls -l " + std::string(pathvar) + "/../root/" + params1 + " grep ^- ").c_str()).empty())
125149
{
126150
shell_commands(("touch " + std::string(pathvar) + "/../root/" + params1).c_str());
127-
//result = shell_commands(("touch " + std::string(pathvar) + "/../root/" + params1).c_str());
128151
result = "success";
129152
res_json["code"] = 200;
130153
}
131-
132154
else
133155
{
134156
result = "error:file already exists";
@@ -151,39 +173,22 @@ void commandsCtrl(const HttpRequestPtr &req, std::function<void(const HttpRespon
151173
result = "No Authorization";
152174
res_json["code"] = 401;
153175
}
176+
154177
res_json["message"] = result;
155-
156-
res->addHeader("Access-Control-Allow-Origin", "*");
178+
157179
auto output = writer.write(res_json);
158180
res->setBody(output);
159181
callback(res);
160182
}
161-
// void genTree(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
162-
// {
163-
// char *pathvar;
164-
// pathvar = getenv("PWD");
165-
// std::string result = shell_commands(("cd " + std::string(pathvar) + "/.. " + "&&" + "tree -J root").c_str());
166-
// auto res = HttpResponse::newHttpResponse();
167-
// res->addHeader("Access-Control-Allow-Origin", "*");
168-
// res->setBody(result);
169-
// callback(res);
170-
// }
171-
// void catFile(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
172-
// {
173-
// char *pathvar;
174-
// pathvar = getenv("PWD");
175-
// std::string path = req->getParameter("path");
176-
// std::string result = shell_commands(("cat " + std::string(pathvar) + "/../root/" + path).c_str());
177-
// auto res = HttpResponse::newHttpResponse();
178-
// res->addHeader("Access-Control-Allow-Origin", "*");
179-
// res->setBody(result);
180-
// callback(res);
181-
// }
182183

183184
void saveFile(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
184185
{
185186
auto res = HttpResponse::newHttpResponse();
186187
res->addHeader("Access-Control-Allow-Origin", "*");
188+
189+
Json::Value res_json;
190+
Json::FastWriter writer;
191+
187192
if (jwtVerify(req))
188193
{
189194
auto body = req->getBody();
@@ -204,53 +209,87 @@ void saveFile(const HttpRequestPtr &req, std::function<void(const HttpResponsePt
204209

205210
sql_unlocked(filename);
206211

207-
208-
res->setBody("success");
209-
callback(res);
212+
res_json["code"] = 200;
213+
res_json["message"] = "File save Success";
210214
}
211215
else
212216
{
213-
res->setBody("No Authorization");
217+
res_json["code"] = 401;
218+
res_json["message"] = "No Authorization";
214219
}
220+
221+
auto output = writer.write(res_json);
222+
res->setBody(output);
223+
callback(res);
215224
}
216225

217226
void imageUpload(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
218227
{
228+
// used to return the frontend (json value)
229+
Json::Value res_json;
230+
Json::FastWriter writer;
231+
219232
auto resp = HttpResponse::newHttpResponse();
220233
resp->addHeader("Access-Control-Allow-Origin", "*");
221234
if (jwtVerify(req))
222235
{
223236
MultiPartParser fileUpload;
224237
if (fileUpload.parse(req) != 0 || fileUpload.getFiles().size() != 1)
225238
{
226-
resp->setBody("Must only be one file");
227-
resp->setStatusCode(k403Forbidden);
228-
callback(resp);
229-
return;
239+
// resp->setStatusCode(k403Forbidden);
240+
res_json["code"] = 403;
241+
res_json["message"] = "Must only be one file";
230242
}
231-
auto &file = fileUpload.getFiles()[0];
232-
auto now = std::chrono::system_clock::now();
233-
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count();
234-
std::string timestamp = std::to_string(ms) + '.' + std::string(file.getFileExtension());
243+
else
244+
{
245+
auto &file = fileUpload.getFiles()[0];
246+
auto now = std::chrono::system_clock::now();
247+
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count();
248+
std::string timestamp = std::to_string(ms) + '.' + std::string(file.getFileExtension());
249+
250+
resp->setBody(timestamp);
251+
file.save();
252+
shell_commands(("mv ./uploads/" + file.getFileName() + " ./uploads/" + timestamp).c_str());
235253

236-
resp->setBody(timestamp);
237-
file.save();
238-
shell_commands(("mv ./uploads/" + file.getFileName() + " ./uploads/" + timestamp).c_str());
254+
LOG_INFO << "The uploaded file has been saved to the ./uploads "
255+
"directory";
239256

240-
LOG_INFO << "The uploaded file has been saved to the ./uploads "
241-
"directory";
257+
res_json["code"] = 200;
258+
res_json["message"] = "Upload Success";
259+
}
242260
}
243261
else
244262
{
245-
resp->setBody("No Authorization");
263+
res_json["code"] = 401;
264+
res_json["messsage"] = "No Authorization";
246265
}
266+
267+
auto output = writer.write(res_json);
268+
resp->setBody(output);
247269
callback(resp);
248270
}
249271

250272
void getPicture(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
251273
{
274+
Json::Value res_json;
275+
Json::FastWriter writer;
276+
252277
std::string filename = req->getParameter("filename");
253278
auto resp = HttpResponse::newFileResponse("./uploads/" + filename);
254279
resp->addHeader("Access-Control-Allow-Origin", "*");
280+
281+
if (jwtVerify(req))
282+
{
283+
res_json["code"] = 200;
284+
res_json["message"] = "Picture get success!";
285+
}
286+
else
287+
{
288+
res_json["code"] = 404;
289+
res_json["message"] = "PICTURE NOT FOUND!!!";
290+
}
291+
292+
auto output = writer.write(res_json);
293+
resp->setBody(output);
255294
callback(resp);
256295
}

file_controller.h

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,12 @@
33
#include <string>
44
#include <drogon/drogon.h>
55
using namespace drogon;
6-
std::string shell_commons(const char *cmd);
7-
// void genTree(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback);
8-
// void catFile(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback);
6+
97
void saveFile(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback);
108
void imageUpload(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback);
119
void getPicture(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback);
12-
1310
void commandsCtrl(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback);
14-
1511
void add_lock(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback);
16-
//void return_status(std::string result);
1712
std::string return_status(std::string result, const std::string& command,Json::Value &res_json);
18-
// std::string
13+
1914
#endif

root/123

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bwb

root/bwb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bwb

root/rubbish_lj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
TM压力我是吧

0 commit comments

Comments
 (0)