Skip to content

Commit 552f5b4

Browse files
Merge pull request #29 from zzc20001/main
addlock and findexist
2 parents da645d0 + 2db1288 commit 552f5b4

File tree

6 files changed

+121
-30
lines changed

6 files changed

+121
-30
lines changed

file_controller.cc

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,26 @@
22
#include "jwt_controller.h"
33
#include <json/json.h>
44
#include <stdio.h>
5-
#include "mysql.h"
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];
@@ -50,6 +64,7 @@ void genTree(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr
5064

5165
void catFile(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
5266
{
67+
5368
auto res = HttpResponse::newHttpResponse();
5469
if (jwtVerify(req))
5570
{

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: 58 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,22 @@
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();
@@ -29,73 +32,99 @@ void chat(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)
2932
std::string msg = req_json["content"].asString();
3033
auto output = writer.write(res_json);
3134
res->setBody(output);
32-
} else {
35+
}
36+
else
37+
{
3338
res->setBody("No Authorization");
3439
}
3540
callback(res);
3641
}
3742
// get message history
38-
void check(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback) {
43+
void check(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
44+
{
3945
Json::Value res_json;
4046
Json::Reader reader;
4147
std::string me;
4248
Json::FastWriter writer;
4349
auto res = HttpResponse::newHttpResponse();
4450
res->addHeader("Access-Control-Allow-Origin", "*");
45-
if (jwtVerify(req)) {
51+
if (jwtVerify(req))
52+
{
4653
me = jwtDecrypt(req->getHeader("Authorization").substr(7));
4754
auto output = writer.write(sql_find_my_msg(me));
4855
res->setBody(output);
49-
} else {
56+
}
57+
else
58+
{
5059
res->setBody("No Authorization");
5160
}
5261
callback(res);
5362
}
5463
// request new friend or cancel request
55-
void friend_operation(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback) {
64+
void friend_operation(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
65+
{
5666
auto res = HttpResponse::newHttpResponse();
5767
res->addHeader("Access-Control-Allow-Origin", "*");
58-
if (jwtVerify(req)) {
68+
if (jwtVerify(req))
69+
{
5970
std::string sender = jwtDecrypt(req->getHeader("Authorization").substr(7));
6071
std::string receiver = req->getParameter("username");
6172
std::string operation = req->getParameter("operation");
6273

6374
if (operation == "add")
64-
sql_addrequest(sender, receiver);
75+
{
76+
if (sql_findexist(receiver))
77+
{
78+
sql_addrequest(sender, receiver);
79+
res->setBody("Success");
80+
}
81+
else
82+
res->setBody("No this body");
83+
}
6584
else
85+
{
6686
sql_delete_operation(sender, receiver);
6787

68-
res->setBody("Success");
69-
} else {
88+
res->setBody("Success");
89+
}
90+
}
91+
else
92+
{
7093
res->setBody("No Authorization");
7194
}
7295
callback(res);
7396
}
7497
// handle new friend request
75-
void request_processing(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback) {
98+
void request_processing(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
99+
{
76100
auto res = HttpResponse::newHttpResponse();
77101
res->addHeader("Access-Control-Allow-Origin", "*");
78102

79-
if (jwtVerify(req)) {
80-
103+
if (jwtVerify(req))
104+
{
105+
81106
std::string receiver = jwtDecrypt(req->getHeader("Authorization").substr(7));
82107
std::string sender = req->getParameter("username");
83108
std::string attitude = req->getParameter("info");
84109
sql_process_request(sender, receiver, attitude);
85110
res->setBody("Success");
86-
} else {
111+
}
112+
else
113+
{
87114
res->setBody("No Authorization");
88115
}
89116

90117
callback(res);
91118
}
92119
// get chat info
93-
void info(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback) {
120+
void info(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
121+
{
94122
auto body = req->getBody();
95123
Json::Value req_json, res_json;
96124
Json::Reader reader;
97125
std::string bodyStr(body);
98-
if (!reader.parse(bodyStr, req_json)) {
126+
if (!reader.parse(bodyStr, req_json))
127+
{
99128
callback(HttpResponse::newHttpResponse());
100129
return;
101130
}
@@ -104,15 +133,22 @@ void info(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)
104133
auto res = HttpResponse::newHttpResponse();
105134
res->addHeader("Access-Control-Allow-Origin", "*");
106135

107-
if (jwtVerify(req)) {
136+
if (jwtVerify(req))
137+
{
138+
108139
me = jwtDecrypt(req->getHeader("Authorization").substr(7));
109-
if (req_json["person"].asString() == "") {
140+
if (req_json["person"].asString() == "")
141+
{
110142
res->setBody(writer.write(get_chat_info(me, "")));
111-
} else {
143+
}
144+
else
145+
{
112146
who_send_me = req_json["person"].asString();
113147
res->setBody(writer.write(get_chat_info(me, who_send_me)));
114148
}
115-
} else {
149+
}
150+
else
151+
{
116152
res->setBody("No Authorization");
117153
}
118154

mysql.cc

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,44 @@ void sql_unlocked(std::string DeleteName)
3131
}
3232

3333
// relate to chat
34-
void process(sql::PreparedStatement *readDatament, std::vector<std::string> s, sql::Connection *con)
34+
35+
int sql_findexist(std::string receiver){
36+
sql::mysql::MySQL_Driver *driver = sql::mysql::get_mysql_driver_instance();
37+
sql::Connection *con = driver->connect("tcp://8.130.48.157:3306", "root", "abc.123");
38+
con->setSchema("flypen");
39+
std::string readdata ="SELECT username FROM users";
40+
sql::PreparedStatement * readdatament = con->prepareStatement(readdata);
41+
sql::ResultSet *resultSet =readdatament->executeQuery();
42+
std::string usernamelist;
43+
if(resultSet->next()){
44+
usernamelist = resultSet->getString("username");
45+
}
46+
int pos = usernamelist.find(receiver);
47+
if(pos!=std::string::npos)return 1;
48+
else return 0;
49+
}
50+
int lockcheck(std::string filename){
51+
sql::mysql::MySQL_Driver *driver = sql::mysql::get_mysql_driver_instance();
52+
sql::Connection *con = driver->connect("tcp://8.130.48.157:3306", "root", "abc.123");
53+
con->setSchema("flypen");
54+
std::string readdata ="SELECT filename FROM file";
55+
sql::PreparedStatement *readdatament =con->prepareStatement(readdata);
56+
sql::ResultSet *resultSet =readdatament->executeQuery();
57+
std::string filenamelist;
58+
if(resultSet->next()){
59+
filenamelist =resultSet->getString("filename");
60+
}
61+
int pos =filenamelist.find(filename);
62+
if(pos!=std::string::npos)return 1;
63+
else {
64+
std::string changestate ="INSERT INTO file(filename) VALUES (?)";
65+
sql::PreparedStatement *changestatement =con->prepareStatement(changestate);
66+
changestatement->setString(1,filename);
67+
changestatement->executeUpdate();
68+
return 0;
69+
}
70+
}
71+
void process(sql::PreparedStatement *readdatament, std::vector<std::string> s, sql::Connection *con)
3572
{
3673
for (int i = 0; i < 2; i++)
3774
{
@@ -197,6 +234,7 @@ void sql_addrequest(std::string sender, std::string receiver)
197234
delete con;
198235
}
199236

237+
200238
void sql_addconnect(std::string connectptr)
201239
{
202240
try

mysql.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@ void sql_addconnect(std::string connectptr);
1313
void sql_addrequest(std::string send,std::string receiver);
1414
void sql_process_request(std::string ,std::string,std::string);
1515
Json::Value sql_find_my_msg(std::string);
16-
16+
int lockcheck(std::string filename);
1717
Json::Value get_chat_info(std::string,std::string="");
18-
1918
void sql_delete_operation(std::string,std::string);
2019
void set_avatar(std::string person, int avatar);
21-
20+
int sql_findexist(std::string receiver);
2221
#endif

0 commit comments

Comments
 (0)