@@ -24,7 +24,7 @@ void add_lock(const HttpRequestPtr &req, std::function<void(const HttpResponsePt
24
24
}
25
25
std::string shell_commons (const char *cmd)
26
26
{
27
- char buffer[128 ];
27
+ char buffer[1280 ];
28
28
std::string result = " " ;
29
29
FILE *pipe = popen (cmd, " r" );
30
30
if (!pipe)
@@ -45,44 +45,110 @@ std::string shell_commons(const char *cmd)
45
45
return result;
46
46
}
47
47
48
- void genTree (const HttpRequestPtr &req, std::function<void (const HttpResponsePtr &)> &&callback)
48
+ void commondsCtrl (const HttpRequestPtr &req, std::function<void (const HttpResponsePtr &)> &&callback)
49
49
{
50
- auto res = HttpResponse::newHttpResponse ();
51
- if (jwtVerify (req))
50
+
51
+ std::cout << " commondsCtrl" << std::endl;
52
+ enum Command {
53
+ tree,
54
+ cp,
55
+ mv,
56
+ rm,
57
+ mkdir,
58
+ touch,
59
+ cat
60
+ }command;
61
+ char *pathvar;
62
+ 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 ();
67
+ std::string result;
68
+ switch (command)
52
69
{
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" );
70
+ case tree:
71
+ result = shell_commons ((" cd " + std::string (pathvar) + " /.. " + " &&" + " tree -J root" ).c_str ());
72
+ break ;
73
+ case cp:
74
+ result = shell_commons ((" 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" ;
79
+ break ;
80
+ case mv:
81
+ result = shell_commons ((" 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" ;
86
+ break ;
87
+ case rm:
88
+ if (params1.find (" .." ) != std::string::npos){
89
+ result = " error:result in wrong directory" ;
90
+ break ;
91
+ }
92
+ result = shell_commons ((" rm -rf -v " + std::string (pathvar) + " /../root/" + params1).c_str ());
93
+ if (result!=" " )
94
+ result = " success" ;
95
+ else
96
+ result = " error: in rm" ;
97
+ break ;
98
+ case mkdir:
99
+ result = shell_commons ((" mkdir " + std::string (pathvar) + " /../root/" + params1).c_str ());
100
+ if (result!=" " )
101
+ result = " success" ;
102
+ else
103
+ result = " error: in mkdir" ;
104
+ break ;
105
+ case touch:
106
+ if (" " == shell_commons ((" ls -l " + std::string (pathvar) + " /../root/" + params1 + " grep ^- " ).c_str ()))
107
+ {
108
+ result = shell_commons ((" touch " + std::string (pathvar) + " /../root/" + params1).c_str ());
109
+ result = " success" ;
110
+ }
111
+
112
+ else
113
+ {
114
+ result = " error:file already exists" ;
115
+ }
116
+ break ;
117
+ case cat:
118
+ result = shell_commons ((" cat " + std::string (pathvar) + " /../root/" + params1).c_str ());
119
+ break ;
120
+ default :
121
+ result = " error:command not found" ;
122
+ break ;
62
123
}
63
- }
64
-
65
- void catFile (const HttpRequestPtr &req, std::function<void (const HttpResponsePtr &)> &&callback)
66
- {
67
-
68
124
auto res = HttpResponse::newHttpResponse ();
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 ());
125
+ res->addHeader (" Access-Control-Allow-Origin" , " *" );
126
+ res->setBody (result);
127
+ callback (res);
75
128
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
- }
85
129
}
130
+ // void genTree(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
131
+ // {
132
+ // char *pathvar;
133
+ // pathvar = getenv("PWD");
134
+ // std::string result = shell_commons(("cd " + std::string(pathvar) + "/.. " + "&&" + "tree -J root").c_str());
135
+ // auto res = HttpResponse::newHttpResponse();
136
+ // res->addHeader("Access-Control-Allow-Origin", "*");
137
+ // res->setBody(result);
138
+ // callback(res);
139
+ // }
140
+ // void catFile(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
141
+ // {
142
+ // char *pathvar;
143
+ // pathvar = getenv("PWD");
144
+ // std::string path = req->getParameter("path");
145
+ // std::string result = shell_commons(("cat " + std::string(pathvar) + "/../root/" + path).c_str());
146
+ // auto res = HttpResponse::newHttpResponse();
147
+ // res->addHeader("Access-Control-Allow-Origin", "*");
148
+ // res->setBody(result);
149
+ // callback(res);
150
+ // }
151
+
86
152
87
153
void saveFile (const HttpRequestPtr &req, std::function<void (const HttpResponsePtr &)> &&callback)
88
154
{
@@ -155,6 +221,7 @@ void getPicture(const HttpRequestPtr &req, std::function<void(const HttpResponse
155
221
{
156
222
std::string filename = req->getParameter (" filename" );
157
223
auto resp = HttpResponse::newFileResponse (" ./uploads/" + filename);
224
+
158
225
if (jwtVerify (req))
159
226
{
160
227
resp->addHeader (" Access-Control-Allow-Origin" , " *" );
@@ -164,4 +231,5 @@ void getPicture(const HttpRequestPtr &req, std::function<void(const HttpResponse
164
231
{
165
232
resp->setBody (" No Authorization" );
166
233
}
167
- }
234
+ }
235
+
0 commit comments