Skip to content

Commit 5cad800

Browse files
committed
addlock and findexist
1 parent a314773 commit 5cad800

File tree

6 files changed

+136
-49
lines changed

6 files changed

+136
-49
lines changed

file_controller.cc

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,26 @@
22

33
#include <json/json.h>
44
#include <stdio.h>
5-
5+
#include <mysql.h>
66
#include <chrono>
77
#include <iostream>
88
#include <stdexcept>
99
#include <string>
10-
10+
void add_lock(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback){
11+
std::string filename = req->getParameter("filename");
12+
if(lockcheck(filename)){
13+
auto res = HttpResponse::newHttpResponse();
14+
res->addHeader("Access-Control-Allow-Origin", "*");
15+
res->setBody("This file has been occupied");
16+
callback(res);
17+
}
18+
else {
19+
auto res = HttpResponse::newHttpResponse();
20+
res->addHeader("Access-Control-Allow-Origin", "*");
21+
res->setBody("This file is yours");
22+
callback(res);
23+
}
24+
}
1125
std::string shell_commons(const char *cmd)
1226
{
1327
char buffer[128];
@@ -46,9 +60,9 @@ void catFile(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr
4660
char *pathvar;
4761
pathvar = getenv("PWD");
4862
std::string path = req->getParameter("path");
49-
std::string result = shell_commons(("cat " + std::string(pathvar) + "/../root/" + path).c_str());
5063
auto res = HttpResponse::newHttpResponse();
5164
res->addHeader("Access-Control-Allow-Origin", "*");
65+
std::string result = shell_commons(("cat " + std::string(pathvar) + "/../root/" + path).c_str());
5266
res->setBody(result);
5367
callback(res);
5468
}

file_controller.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ void catFile(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr
99
void saveFile(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback);
1010
void imageUpload(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback);
1111
void getPicture(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback);
12+
void add_lock(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback);
1213
// std::string
1314
#endif

main.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ int main()
2525
{ friend_operation(req, std::move(callback)); });
2626
drogon::app().registerHandler("/api/info", [](const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
2727
{ info(req, std::move(callback)); });
28-
2928
drogon::app().registerHandler("/api/file/tree", [](const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
3029
{ genTree(req, std::move(callback)); });
3130
drogon::app().registerHandler("/api/file/cat", [](const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
@@ -37,7 +36,10 @@ int main()
3736
drogon::app().registerHandler("/api/upload", [](const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
3837
{ imageUpload(req, std::move(callback)); });
3938
drogon::app().registerHandler("/api/file/get", [](const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
40-
{ getPicture(req, std::move(callback)); }, {Get});
39+
{ getPicture(req, std::move(callback)); },
40+
{Get});
41+
drogon::app().registerHandler("/api/file/lock", [](const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
42+
{add_lock(req, std::move(callback));});
4143
drogon::app().setUploadPath("./uploads").run();
4244
return 0;
4345
}

msg_controller.cc

Lines changed: 56 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,104 +7,139 @@
77
using namespace drogon;
88

99
// send a message
10-
void chat(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback) {
10+
void chat(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
11+
{
1112
auto body = req->getBody();
1213
Json::Value req_json, res_json;
1314
Json::Reader reader;
1415
std::string bodyStr(body);
15-
if (!reader.parse(bodyStr, req_json)) {
16+
if (!reader.parse(bodyStr, req_json))
17+
{
1618
callback(HttpResponse::newHttpResponse());
1719
return;
1820
}
1921
Json::FastWriter writer;
2022
auto res = HttpResponse::newHttpResponse();
2123
res->addHeader("Access-Control-Allow-Origin", "*");
22-
if (jwtVerify(req)) {
24+
if (jwtVerify(req))
25+
{
2326
std::string sender = jwtDecrypt(req->getHeader("Authorization").substr(7));
2427
std::string content = req_json["content"].asString();
2528
std::string receiver = req_json["receiver"].asString();
2629
sql_addhistory(sender, receiver, content, "0");
2730
std::string msg = req_json["content"].asString();
2831
auto output = writer.write(res_json);
2932
res->setBody(output);
30-
} else {
33+
}
34+
else
35+
{
3136
res->setBody("No Authorization");
3237
}
3338
callback(res);
3439
}
3540
// get message history
36-
void check(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback) {
41+
void check(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
42+
{
3743
Json::Value res_json;
3844
Json::Reader reader;
3945
std::string me;
4046
Json::FastWriter writer;
4147
auto res = HttpResponse::newHttpResponse();
4248
res->addHeader("Access-Control-Allow-Origin", "*");
43-
if (jwtVerify(req)) {
49+
if (jwtVerify(req))
50+
{
4451
me = jwtDecrypt(req->getHeader("Authorization").substr(7));
4552
auto output = writer.write(sql_find_my_msg(me));
4653
res->setBody(output);
47-
} else {
54+
}
55+
else
56+
{
4857
res->setBody("No Authorization");
4958
}
5059
callback(res);
5160
}
5261
// request new friend or cancel request
53-
void friend_operation(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback) {
62+
void friend_operation(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
63+
{
5464
auto res = HttpResponse::newHttpResponse();
5565
res->addHeader("Access-Control-Allow-Origin", "*");
56-
if (jwtVerify(req)) {
66+
if (jwtVerify(req))
67+
{
5768
std::string sender = jwtDecrypt(req->getHeader("Authorization").substr(7));
5869
std::string receiver = req->getParameter("username");
5970
std::string operation = req->getParameter("operation");
6071
if (operation == "add")
61-
sql_addrequest(sender, receiver);
72+
{
73+
if (sql_findexist(receiver))
74+
{
75+
sql_addrequest(sender, receiver);
76+
res->setBody("Success");
77+
}
78+
else
79+
res->setBody("No this body");
80+
}
6281
else
82+
{
6383
sql_delete_operation(sender, receiver);
64-
res->setBody("Success");
65-
} else {
84+
res->setBody("Success");
85+
}
86+
}
87+
else
88+
{
6689
res->setBody("No Authorization");
6790
}
6891
callback(res);
6992
}
7093
// handle new friend request
71-
void request_processing(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback) {
94+
void request_processing(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
95+
{
7296
auto res = HttpResponse::newHttpResponse();
7397
res->addHeader("Access-Control-Allow-Origin", "*");
74-
if (jwtVerify(req)) {
98+
if (jwtVerify(req))
99+
{
75100
std::string receiver = jwtDecrypt(req->getHeader("Authorization").substr(7));
76101
std::string sender = req->getParameter("username");
77102
std::string attitude = req->getParameter("info");
78103
sql_process_request(sender, receiver, attitude);
79104
res->setBody("Success");
80-
} else {
105+
}
106+
else
107+
{
81108
res->setBody("No Authorization");
82109
}
83110
callback(res);
84111
}
85112
// get chat info
86-
void info(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback) {
113+
void info(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
114+
{
87115
auto body = req->getBody();
88116
Json::Value req_json, res_json;
89117
Json::Reader reader;
90118
std::string bodyStr(body);
91-
if (!reader.parse(bodyStr, req_json)) {
119+
if (!reader.parse(bodyStr, req_json))
120+
{
92121
callback(HttpResponse::newHttpResponse());
93122
return;
94123
}
95124
std::string me, who_send_me;
96125
Json::FastWriter writer;
97126
auto res = HttpResponse::newHttpResponse();
98127
res->addHeader("Access-Control-Allow-Origin", "*");
99-
if (jwtVerify(req)) {
128+
if (jwtVerify(req))
129+
{
100130
me = jwtDecrypt(req->getHeader("Authorization").substr(7));
101-
if (req_json["person"].asString() == "") {
131+
if (req_json["person"].asString() == "")
132+
{
102133
res->setBody(writer.write(get_chat_info(me, "")));
103-
} else {
134+
}
135+
else
136+
{
104137
who_send_me = req_json["person"].asString();
105138
res->setBody(writer.write(get_chat_info(me, who_send_me)));
106139
}
107-
} else {
140+
}
141+
else
142+
{
108143
res->setBody("No Authorization");
109144
}
110145
callback(res);

mysql.cc

Lines changed: 56 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,42 @@
44
#include "jdbc/mysql_driver.h"
55
#include "json/json.h"
66
// relate to chat
7+
int sql_findexist(std::string receiver){
8+
sql::mysql::MySQL_Driver *driver = sql::mysql::get_mysql_driver_instance();
9+
sql::Connection *con = driver->connect("tcp://8.130.48.157:3306", "root", "abc.123");
10+
con->setSchema("flypen");
11+
std::string readdata ="SELECT username FROM users";
12+
sql::PreparedStatement * readdatament = con->prepareStatement(readdata);
13+
sql::ResultSet *resultSet =readdatament->executeQuery();
14+
std::string usernamelist;
15+
if(resultSet->next()){
16+
usernamelist = resultSet->getString("username");
17+
}
18+
int pos = usernamelist.find(receiver);
19+
if(pos!=std::string::npos)return 1;
20+
else return 0;
21+
}
22+
int lockcheck(std::string filename){
23+
sql::mysql::MySQL_Driver *driver = sql::mysql::get_mysql_driver_instance();
24+
sql::Connection *con = driver->connect("tcp://8.130.48.157:3306", "root", "abc.123");
25+
con->setSchema("flypen");
26+
std::string readdata ="SELECT filename FROM file";
27+
sql::PreparedStatement *readdatament =con->prepareStatement(readdata);
28+
sql::ResultSet *resultSet =readdatament->executeQuery();
29+
std::string filenamelist;
30+
if(resultSet->next()){
31+
filenamelist =resultSet->getString("filename");
32+
}
33+
int pos =filenamelist.find(filename);
34+
if(pos!=std::string::npos)return 1;
35+
else {
36+
std::string changestate ="INSERT INTO file(filename) VALUES (?)";
37+
sql::PreparedStatement *changestatement =con->prepareStatement(changestate);
38+
changestatement->setString(1,filename);
39+
changestatement->executeUpdate();
40+
return 0;
41+
}
42+
}
743
void process(sql::PreparedStatement *readdatament, std::vector<std::string> s, sql::Connection *con)
844
{
945
for (int i = 0; i < 2; i++)
@@ -161,26 +197,26 @@ void sql_addrequest(std::string sender, std::string receiver)
161197
delete tool;
162198
delete con;
163199
}
164-
void sql_addconnect(std::string connectptr)
165-
{
166-
try
167-
{
168-
sql::mysql::MySQL_Driver *driver = sql::mysql::get_mysql_driver_instance();
169-
sql::Connection *con = driver->connect("tcp://8.130.48.157:3306", "root", "abc.123");
170-
con->setSchema("flypen");
171-
172-
std::string classMysql = "INSERT INTO users(username, password, time, connection) VALUES (?, ?, NOW(), ?)";
173-
sql::PreparedStatement *insertConnect = con->prepareStatement(classMysql);
174-
insertConnect->setString(4, connectptr);
175-
176-
delete insertConnect;
177-
delete con;
178-
}
179-
catch (sql::SQLException &e)
180-
{
181-
std::cerr << "SQL Exception: " << e.what() << std::endl;
182-
}
183-
}
200+
// void sql_addconnect(std::string connectptr)
201+
// {
202+
// try
203+
// {
204+
// sql::mysql::MySQL_Driver *driver = sql::mysql::get_mysql_driver_instance();
205+
// sql::Connection *con = driver->connect("tcp://8.130.48.157:3306", "root", "abc.123");
206+
// con->setSchema("flypen");
207+
208+
// std::string classMysql = "INSERT INTO users(username, password, time, connection) VALUES (?, ?, NOW(), ?)";
209+
// sql::PreparedStatement *insertConnect = con->prepareStatement(classMysql);
210+
// insertConnect->setString(4, connectptr);
211+
212+
// delete insertConnect;
213+
// delete con;
214+
// }
215+
// catch (sql::SQLException &e)
216+
// {
217+
// std::cerr << "SQL Exception: " << e.what() << std::endl;
218+
// }
219+
// }
184220
void sql_addhistory(std::string sender, std::string receiver, std::string message, std::string isread)
185221
{
186222
try

mysql.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@ void sql_addconnect(std::string connectptr);
1212
void sql_addrequest(std::string send,std::string receiver);
1313
void sql_process_request(std::string ,std::string,std::string);
1414
Json::Value sql_find_my_msg(std::string);
15-
15+
int lockcheck(std::string filename);
1616
Json::Value get_chat_info(std::string,std::string="");
17-
1817
void sql_delete_operation(std::string,std::string);
1918
void set_avatar(std::string person, int avatar);
20-
19+
int sql_findexist(std::string receiver);
2120
#endif

0 commit comments

Comments
 (0)