10
10
11
11
std::string shell_commons (const char *cmd)
12
12
{
13
- char buffer[128 ];
13
+ char buffer[1280 ];
14
14
std::string result = " " ;
15
15
FILE *pipe = popen (cmd, " r" );
16
16
if (!pipe)
@@ -31,27 +31,108 @@ std::string shell_commons(const char *cmd)
31
31
return result;
32
32
}
33
33
34
- void genTree (const HttpRequestPtr &req, std::function<void (const HttpResponsePtr &)> &&callback)
34
+ void commondsCtrl (const HttpRequestPtr &req, std::function<void (const HttpResponsePtr &)> &&callback)
35
35
{
36
+ std::cout << " commondsCtrl" << std::endl;
37
+ enum Command {
38
+ tree,
39
+ cp,
40
+ mv,
41
+ rm,
42
+ mkdir,
43
+ touch,
44
+ cat
45
+ }command;
36
46
char *pathvar;
37
47
pathvar = getenv (" PWD" );
38
- std::string result = shell_commons ((" cd " + std::string (pathvar) + " /.. " + " &&" + " tree -J root" ).c_str ());
39
- auto res = HttpResponse::newHttpResponse ();
40
- res->addHeader (" Access-Control-Allow-Origin" , " *" );
41
- res->setBody (result);
42
- callback (res);
43
- }
44
- void catFile (const HttpRequestPtr &req, std::function<void (const HttpResponsePtr &)> &&callback)
45
- {
46
- char *pathvar;
47
- pathvar = getenv (" PWD" );
48
- std::string path = req->getParameter (" path" );
49
- std::string result = shell_commons ((" cat " + std::string (pathvar) + " /../root/" + path).c_str ());
48
+
49
+ command = static_cast <Command>(stoi (req->getJsonObject ()->get (" command" ," " ).asString ()));
50
+ std::string params1 = req->getJsonObject ()->get (" params" ," " )[0 ].asString ();
51
+ std::string params2 = req->getJsonObject ()->get (" params" ," " )[1 ].asString ();
52
+ std::string result;
53
+ switch (command)
54
+ {
55
+ case tree:
56
+ result = shell_commons ((" cd " + std::string (pathvar) + " /.. " + " &&" + " tree -J root" ).c_str ());
57
+ break ;
58
+ case cp:
59
+ result = shell_commons ((" cp -v " + std::string (pathvar) + " /../root/" + params1 + " " + std::string (pathvar) + " /../root/" + params2).c_str ());
60
+ if (result!=" " )
61
+ result = " success" ;
62
+ else
63
+ result = " error: in cp" ;
64
+ break ;
65
+ case mv:
66
+ result = shell_commons ((" mv -v " + std::string (pathvar) + " /../root/" + params1 + " " + std::string (pathvar) + " /../root/" + params2).c_str ());
67
+ if (result!=" " )
68
+ result = " success" ;
69
+ else
70
+ result = " error: in mv" ;
71
+ break ;
72
+ case rm:
73
+ if (params1.find (" .." ) != std::string::npos){
74
+ result = " error:result in wrong directory" ;
75
+ break ;
76
+ }
77
+ result = shell_commons ((" rm -rf -v " + std::string (pathvar) + " /../root/" + params1).c_str ());
78
+ if (result!=" " )
79
+ result = " success" ;
80
+ else
81
+ result = " error: in rm" ;
82
+ break ;
83
+ case mkdir:
84
+ result = shell_commons ((" mkdir " + std::string (pathvar) + " /../root/" + params1).c_str ());
85
+ if (result!=" " )
86
+ result = " success" ;
87
+ else
88
+ result = " error: in mkdir" ;
89
+ break ;
90
+ case touch:
91
+ if (" " == shell_commons ((" ls -l " + std::string (pathvar) + " /../root/" + params1 + " grep ^- " ).c_str ()))
92
+ {
93
+ result = shell_commons ((" touch " + std::string (pathvar) + " /../root/" + params1).c_str ());
94
+ result = " success" ;
95
+ }
96
+
97
+ else
98
+ {
99
+ result = " error:file already exists" ;
100
+ }
101
+ break ;
102
+ case cat:
103
+ result = shell_commons ((" cat " + std::string (pathvar) + " /../root/" + params1).c_str ());
104
+ break ;
105
+ default :
106
+ result = " error:command not found" ;
107
+ break ;
108
+ }
50
109
auto res = HttpResponse::newHttpResponse ();
51
110
res->addHeader (" Access-Control-Allow-Origin" , " *" );
52
111
res->setBody (result);
53
112
callback (res);
113
+
54
114
}
115
+ // void genTree(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
116
+ // {
117
+ // char *pathvar;
118
+ // pathvar = getenv("PWD");
119
+ // std::string result = shell_commons(("cd " + std::string(pathvar) + "/.. " + "&&" + "tree -J root").c_str());
120
+ // auto res = HttpResponse::newHttpResponse();
121
+ // res->addHeader("Access-Control-Allow-Origin", "*");
122
+ // res->setBody(result);
123
+ // callback(res);
124
+ // }
125
+ // void catFile(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
126
+ // {
127
+ // char *pathvar;
128
+ // pathvar = getenv("PWD");
129
+ // std::string path = req->getParameter("path");
130
+ // std::string result = shell_commons(("cat " + std::string(pathvar) + "/../root/" + path).c_str());
131
+ // auto res = HttpResponse::newHttpResponse();
132
+ // res->addHeader("Access-Control-Allow-Origin", "*");
133
+ // res->setBody(result);
134
+ // callback(res);
135
+ // }
55
136
void saveFile (const HttpRequestPtr &req, std::function<void (const HttpResponsePtr &)> &&callback)
56
137
{
57
138
auto body = req->getBody ();
@@ -121,4 +202,4 @@ void getPicture(const HttpRequestPtr &req, std::function<void(const HttpResponse
121
202
auto resp = HttpResponse::newFileResponse (" ./uploads/" + filename);
122
203
resp->addHeader (" Access-Control-Allow-Origin" , " *" );
123
204
callback (resp);
124
- }
205
+ }
0 commit comments