Skip to content

Commit a41c0fe

Browse files
Merge pull request #32 from lglglglgy/main
🎨 Reduce code duplication and shit content
2 parents 951a151 + d34a86b commit a41c0fe

File tree

2 files changed

+35
-35
lines changed

2 files changed

+35
-35
lines changed

file_controller.cc

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,26 @@
77
#include <iostream>
88
#include <stdexcept>
99
#include <string>
10-
void add_lock(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback){
10+
std:: string return_status(std::string result,std::string command )
11+
{
12+
if (result != "")
13+
result = "success";
14+
else
15+
result = " error in :"+command;
16+
return result;
17+
}
18+
void add_lock(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
19+
{
1120
std::string filename = req->getParameter("filename");
12-
if(lockcheck(filename)){
21+
if (lockcheck(filename))
22+
{
1323
auto res = HttpResponse::newHttpResponse();
1424
res->addHeader("Access-Control-Allow-Origin", "*");
1525
res->setBody("This file has been occupied");
1626
callback(res);
1727
}
18-
else {
28+
else
29+
{
1930
auto res = HttpResponse::newHttpResponse();
2031
res->addHeader("Access-Control-Allow-Origin", "*");
2132
res->setBody("This file is yours");
@@ -49,21 +60,22 @@ void commandsCtrl(const HttpRequestPtr &req, std::function<void(const HttpRespon
4960
{
5061

5162
std::cout << "commandsCtrl" << std::endl;
52-
enum Command {
63+
enum Command
64+
{
5365
tree,
5466
cp,
5567
mv,
5668
rm,
5769
mkdir,
5870
touch,
5971
cat
60-
}command;
72+
} command;
6173
char *pathvar;
6274
pathvar = getenv("PWD");
63-
64-
command = static_cast<Command>(stoi(req->getJsonObject()->get("command","").asString()));
65-
std::string params1 = req->getJsonObject()->get("params","")[0].asString();
66-
std::string params2 = req->getJsonObject()->get("params","")[1].asString();
75+
76+
command = static_cast<Command>(stoi(req->getJsonObject()->get("command", "").asString()));
77+
std::string params1 = req->getJsonObject()->get("params", "")[0].asString();
78+
std::string params2 = req->getJsonObject()->get("params", "")[1].asString();
6779
std::string result;
6880
switch (command)
6981
{
@@ -72,51 +84,41 @@ void commandsCtrl(const HttpRequestPtr &req, std::function<void(const HttpRespon
7284
break;
7385
case cp:
7486
result = shell_commands(("cp -v " + std::string(pathvar) + "/../root/" + params1 + " " + std::string(pathvar) + "/../root/" + params2).c_str());
75-
if(result!="")
76-
result = "success";
77-
else
78-
result = "error: in cp" ;
87+
result = return_status(result,"cp");
88+
7989
break;
8090
case mv:
8191
result = shell_commands(("mv -v " + std::string(pathvar) + "/../root/" + params1 + " " + std::string(pathvar) + "/../root/" + params2).c_str());
82-
if(result!="")
83-
result = "success";
84-
else
85-
result = "error: in mv" ;
92+
result = return_status(result,"mv");
8693
break;
8794
case rm:
88-
if(params1.find("..") != std::string::npos){
95+
if (params1.find("..") != std::string::npos)
96+
{
8997
result = "error:result in wrong directory";
9098
break;
9199
}
92100
result = shell_commands(("rm -rf -v " + std::string(pathvar) + "/../root/" + params1).c_str());
93-
if(result!="")
94-
result = "success";
95-
else
96-
result = "error: in rm" ;
101+
result = return_status(result,"rm");
97102
break;
98103
case mkdir:
99104
result = shell_commands(("mkdir -v " + std::string(pathvar) + "/../root/" + params1).c_str());
100-
if(result!="")
101-
result = "success";
102-
else
103-
result = "error: in mkdir" ;
105+
result = return_status(result,"mkdir");
104106
break;
105107
case touch:
106-
if("" == shell_commands(("ls -l " + std::string(pathvar) + "/../root/" + params1 + " grep ^- ").c_str()))
108+
if ("" == shell_commands(("ls -l " + std::string(pathvar) + "/../root/" + params1 + " grep ^- ").c_str()))
107109
{
108110
result = shell_commands(("touch " + std::string(pathvar) + "/../root/" + params1).c_str());
109-
result = "success";
111+
result = "success";
110112
}
111-
113+
112114
else
113115
{
114116
result = "error:file already exists";
115117
}
116118
break;
117119
case cat:
118120
result = shell_commands(("cat " + std::string(pathvar) + "/../root/" + params1).c_str());
119-
break;
121+
break;
120122
default:
121123
result = "error:command not found";
122124
break;
@@ -125,7 +127,6 @@ void commandsCtrl(const HttpRequestPtr &req, std::function<void(const HttpRespon
125127
res->addHeader("Access-Control-Allow-Origin", "*");
126128
res->setBody(result);
127129
callback(res);
128-
129130
}
130131
// void genTree(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
131132
// {
@@ -149,7 +150,6 @@ void commandsCtrl(const HttpRequestPtr &req, std::function<void(const HttpRespon
149150
// callback(res);
150151
// }
151152

152-
153153
void saveFile(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
154154
{
155155
auto res = HttpResponse::newHttpResponse();
@@ -170,9 +170,9 @@ void saveFile(const HttpRequestPtr &req, std::function<void(const HttpResponsePt
170170
std::string filename = req_json["filename"].asString();
171171
std::string content = req_json["content"].asString();
172172
std::string result = shell_commands(("echo '" + content + "'>" + std::string(pathvar) + "/../root/" + filename).c_str());
173-
173+
174174
sql_unlocked(filename);
175-
175+
176176
res->addHeader("Access-Control-Allow-Origin", "*");
177177
res->setBody("success");
178178
callback(res);
@@ -232,4 +232,3 @@ void getPicture(const HttpRequestPtr &req, std::function<void(const HttpResponse
232232
resp->setBody("No Authorization");
233233
}
234234
}
235-

file_controller.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ void getPicture(const HttpRequestPtr &req, std::function<void(const HttpResponse
1313
void commandsCtrl(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback);
1414

1515
void add_lock(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback);
16+
void return_status(std::string result);
1617

1718
// std::string
1819
#endif

0 commit comments

Comments
 (0)