Skip to content

Commit a6b924f

Browse files
🚀 Code Optimization with Jetbrains Clion
1 parent c46916a commit a6b924f

File tree

9 files changed

+54
-71
lines changed

9 files changed

+54
-71
lines changed

file_controller.cc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
#include "file_controller.h"
22
#include "jwt_controller.h"
33
#include <json/json.h>
4-
#include <stdio.h>
4+
#include <cstdio>
55
#include <mysql.h>
6-
#include <chrono>
76
#include <iostream>
87
#include <stdexcept>
98
#include <string>
10-
std::string return_status(std::string result, std::string command,Json::Value &res_json)
9+
std::string return_status(std::string result, const std::string& command,Json::Value &res_json)
1110
{
12-
if (result != ""){
11+
if (!result.empty()){
1312
result = command + " success";
1413
res_json["code"] = 200;
1514
}
@@ -42,13 +41,13 @@ void add_lock(const HttpRequestPtr &req, std::function<void(const HttpResponsePt
4241
std::string shell_commands(const char *cmd)
4342
{
4443
char buffer[1280];
45-
std::string result = "";
44+
std::string result;
4645
FILE *pipe = popen(cmd, "r");
4746
if (!pipe)
4847
throw std::runtime_error("popen() failed!");
4948
try
5049
{
51-
while (fgets(buffer, sizeof buffer, pipe) != NULL)
50+
while (fgets(buffer, sizeof buffer, pipe) != nullptr)
5251
{
5352
result += buffer;
5453
}
@@ -115,9 +114,10 @@ void commandsCtrl(const HttpRequestPtr &req, std::function<void(const HttpRespon
115114
result = return_status(result, "mkdir",res_json);
116115
break;
117116
case touch:
118-
if ("" == shell_commands(("ls -l " + std::string(pathvar) + "/../root/" + params1 + " grep ^- ").c_str()))
117+
if (shell_commands(("ls -l " + std::string(pathvar) + "/../root/" + params1 + " grep ^- ").c_str()).empty())
119118
{
120-
result = shell_commands(("touch " + std::string(pathvar) + "/../root/" + params1).c_str());
119+
shell_commands(("touch " + std::string(pathvar) + "/../root/" + params1).c_str());
120+
//result = shell_commands(("touch " + std::string(pathvar) + "/../root/" + params1).c_str());
121121
result = "success";
122122
res_json["code"] = 200;
123123
}

file_controller.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ void commandsCtrl(const HttpRequestPtr &req, std::function<void(const HttpRespon
1414

1515
void add_lock(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback);
1616
//void return_status(std::string result);
17-
std::string return_status(std::string result, std::string command,Json::Value &res_json);
17+
std::string return_status(std::string result, const std::string& command,Json::Value &res_json);
1818
// std::string
1919
#endif

main.cc

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11

2-
#include <drogon/drogon.h>
32
#include "msg_controller.h"
43
#include "user_controller.h"
54
#include "file_controller.h"
6-
#include <drogon/WebSocketController.h>
75

86
using namespace drogon;
9-
std::unordered_map<std::string, WebSocketConnectionPtr> clientTable;
107
int main()
118
{
129
drogon::app().addListener("0.0.0.0", 8081);

msg_controller.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#include <drogon/drogon.h>
21
#include <json/json.h>
32
#include "jwt_controller.h"
43
#include "msg_controller.h"

mysql.cc

Lines changed: 16 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,15 @@
44
#include "jdbc/mysql_driver.h"
55
#include "json/json.h"
66

7-
void sql_unlocked(std::string DeleteName)
7+
void sql_unlocked(const std::string& DeleteName)
88
{
99
sql::mysql::MySQL_Driver *driver = sql::mysql::get_mysql_driver_instance();
1010
sql::Connection *con = driver->connect("tcp://8.130.48.157:3306", "root", "abc.123");
1111
con->setSchema("flypen");
1212
sql::Statement *stmt = con->createStatement();
1313
try
1414
{
15-
std::string filename = DeleteName;
1615
std::string DeleteQuery = "DELETE FROM file WHERE filename = " + DeleteName;
17-
int rowsDelete = stmt->executeUpdate(DeleteQuery);
1816
}
1917
catch (sql::SQLException &e)
2018
{
@@ -26,7 +24,7 @@ void sql_unlocked(std::string DeleteName)
2624

2725
// relate to chat
2826

29-
int sql_findexist(std::string receiver)
27+
int sql_findexist(const std::string& receiver)
3028
{
3129
sql::mysql::MySQL_Driver *driver = sql::mysql::get_mysql_driver_instance();
3230
sql::Connection *con = driver->connect("tcp://8.130.48.157:3306", "root", "abc.123");
@@ -42,7 +40,7 @@ int sql_findexist(std::string receiver)
4240
}
4341
return 0;
4442
}
45-
int lockcheck(std::string filename)
43+
int lockcheck(const std::string& filename)
4644
{
4745
sql::mysql::MySQL_Driver *driver = sql::mysql::get_mysql_driver_instance();
4846
sql::Connection *con = driver->connect("tcp://8.130.48.157:3306", "root", "abc.123");
@@ -87,7 +85,7 @@ void process(sql::PreparedStatement *readDatament, std::vector<std::string> s, s
8785
}
8886
}
8987

90-
void sql_delete_operation(std::string sender, std::string receiver)
88+
void sql_delete_operation(const std::string& sender, const std::string& receiver)
9189
{
9290
sql::mysql::MySQL_Driver *driver = sql::mysql::get_mysql_driver_instance();
9391
sql::Connection *con = driver->connect("tcp://8.130.48.157:3306", "root", "abc.123");
@@ -102,7 +100,7 @@ void sql_delete_operation(std::string sender, std::string receiver)
102100
process(readDatament, s, con);
103101
}
104102

105-
void sql_process_request(std::string sender, std::string receiver, std::string attitude)
103+
void sql_process_request(const std::string& sender, const std::string& receiver, const std::string& attitude)
106104
{
107105
std::vector<std::string> usernamelist;
108106
sql::mysql::MySQL_Driver *driver = sql::mysql::get_mysql_driver_instance();
@@ -135,7 +133,7 @@ void sql_process_request(std::string sender, std::string receiver, std::string a
135133
if (attitude == "allow")
136134
{
137135
std::string readData = "SELECT friends FROM users WHERE username = ?";
138-
std::string *RS = new std::string[2]{receiver, sender}; // R, S
136+
auto *RS = new std::string[2]{receiver, sender}; // R, S
139137

140138
for (int i = 0; i < 2; i++)
141139
{
@@ -157,7 +155,7 @@ void sql_process_request(std::string sender, std::string receiver, std::string a
157155
std::string updateQuery = "UPDATE users SET friends = ? WHERE username = ?";
158156
sql::PreparedStatement *update = con->prepareStatement(updateQuery);
159157

160-
if (friendsList != "")
158+
if (!friendsList.empty())
161159
{
162160
friendsList += "," + RS[1 - i];
163161
update->setString(1, friendsList);
@@ -178,10 +176,9 @@ void sql_process_request(std::string sender, std::string receiver, std::string a
178176
delete resultSet;
179177
delete readDatament;
180178
delete con;
181-
return;
182179
}
183180

184-
void sql_addrequest(std::string sender, std::string receiver)
181+
void sql_addrequest(const std::string& sender, const std::string& receiver)
185182
{
186183
std::vector<std::string> usernamelist;
187184

@@ -208,7 +205,7 @@ void sql_addrequest(std::string sender, std::string receiver)
208205
std::string updateQuery = "UPDATE users SET req = ? WHERE username = ?";
209206
sql::PreparedStatement *updateStatement = con->prepareStatement(updateQuery);
210207

211-
if (req != "")
208+
if (!req.empty())
212209
{
213210
req += "," + sender;
214211
updateStatement->setString(1, req);
@@ -229,28 +226,7 @@ void sql_addrequest(std::string sender, std::string receiver)
229226
delete con;
230227
}
231228

232-
void sql_addconnect(std::string connectptr)
233-
{
234-
try
235-
{
236-
sql::mysql::MySQL_Driver *driver = sql::mysql::get_mysql_driver_instance();
237-
sql::Connection *con = driver->connect("tcp://8.130.48.157:3306", "root", "abc.123");
238-
con->setSchema("flypen");
239-
240-
std::string classMysql = "INSERT INTO users(username, password, time, connection) VALUES (?, ?, NOW(), ?)";
241-
sql::PreparedStatement *insertConnect = con->prepareStatement(classMysql);
242-
insertConnect->setString(4, connectptr);
243-
244-
delete insertConnect;
245-
delete con;
246-
}
247-
catch (sql::SQLException &e)
248-
{
249-
std::cerr << "SQL Exception: " << e.what() << std::endl;
250-
}
251-
}
252-
253-
void sql_addhistory(std::string sender, std::string receiver, std::string message, std::string isread)
229+
void sql_addhistory(const std::string& sender, const std::string& receiver, const std::string& message, const std::string& isread)
254230
{
255231
try
256232
{
@@ -278,7 +254,7 @@ void sql_addhistory(std::string sender, std::string receiver, std::string messag
278254
}
279255

280256
// relate to user
281-
void sql_add(std::string username, std::string passwd, int avatar)
257+
void sql_add(const std::string& username, const std::string& passwd, int avatar)
282258
{
283259
try
284260
{
@@ -305,7 +281,7 @@ void sql_add(std::string username, std::string passwd, int avatar)
305281
}
306282
}
307283

308-
Json::Value get_my_info(std::string me)
284+
Json::Value get_my_info(const std::string& me)
309285
{
310286
Json::Value json;
311287

@@ -386,7 +362,7 @@ Json::Value get_my_info(std::string me)
386362
return json;
387363
}
388364

389-
bool sql_check(std::string user, std::string passwd)
365+
bool sql_check(const std::string& user, const std::string& passwd)
390366
{
391367
bool result = false;
392368
try
@@ -410,7 +386,6 @@ bool sql_check(std::string user, std::string passwd)
410386
// 提取所有列的值
411387
std::string username = res->getString("username");
412388
std::string password = res->getString("password");
413-
int createtime = res->getInt("createtime");
414389

415390
if ((passwd != password) && (passwd != "@DEFAULT@"))
416391
result = false;
@@ -428,7 +403,7 @@ bool sql_check(std::string user, std::string passwd)
428403
// std::cout << result << std::endl;
429404
return result;
430405
}
431-
Json::Value sql_find_my_msg(std::string me, std::string connect_type)
406+
Json::Value sql_find_my_msg(const std::string& me, const std::string& connect_type)
432407

433408
{
434409
// std::cout << "login user: " << me << std::endl;
@@ -437,8 +412,7 @@ Json::Value sql_find_my_msg(std::string me, std::string connect_type)
437412
sql::mysql::MySQL_Driver *driver = sql::mysql::get_mysql_driver_instance();
438413
sql::Connection *con = driver->connect("tcp://8.130.48.157:3306", "root", "abc.123");
439414
con->setSchema("flypen");
440-
sql::PreparedStatement *prepStmt = nullptr;
441-
415+
sql::PreparedStatement *prepStmt;
442416
//=================== find begin
443417
// SELECT * FROM chat WHERE (sender = 'lglglglgy' OR receiver = 'lglglglgy') AND isread = 0
444418
std::string sqlFind_new_connect = "SELECT * FROM chat WHERE sender = ? OR receiver = ?";
@@ -534,7 +508,7 @@ Json::Value sql_find_my_msg(std::string me, std::string connect_type)
534508
}
535509
}
536510

537-
void set_avatar(std::string person, int avatar)
511+
void set_avatar(const std::string& person, int avatar)
538512
{
539513
sql::mysql::MySQL_Driver *driver = sql::mysql::get_mysql_driver_instance();
540514
sql::Connection *con = driver->connect("tcp://8.130.48.157:3306", "root", "abc.123");

mysql.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@
44
#include <jdbc/mysql_connection.h>
55
#include <json/json.h>
66

7-
void sql_unlocked(std::string DeleteName);
8-
void sql_add(std::string username, std::string passwd, int avatar);
9-
bool sql_check(std::string, std::string passwd = "@DEFAULT@");
10-
void sql_addhistory(std::string, std::string, std::string, std::string);
7+
void sql_unlocked(const std::string& DeleteName);
8+
void sql_add(const std::string& username, const std::string& passwd, int avatar);
9+
bool sql_check(const std::string&, const std::string& passwd = "@DEFAULT@");
10+
void sql_addhistory(const std::string&, const std::string&, const std::string&, const std::string&);
1111
void sql_addconnect(std::string connectptr);
1212

13-
void sql_addrequest(std::string send, std::string receiver);
14-
void sql_process_request(std::string, std::string, std::string);
15-
Json::Value sql_find_my_msg(std::string, std::string);
16-
int lockcheck(std::string filename);
17-
Json::Value get_my_info(std::string);
18-
void sql_delete_operation(std::string, std::string);
19-
void set_avatar(std::string person, int avatar);
20-
int sql_findexist(std::string receiver);
13+
void sql_addrequest(const std::string& send, const std::string& receiver);
14+
void sql_process_request(const std::string&, const std::string&, const std::string&);
15+
Json::Value sql_find_my_msg(const std::string&, const std::string&);
16+
int lockcheck(const std::string& filename);
17+
Json::Value get_my_info(const std::string&);
18+
void sql_delete_operation(const std::string&, const std::string&);
19+
void set_avatar(const std::string& person, int avatar);
20+
int sql_findexist(const std::string& receiver);
2121
#endif

root/lglglgly/lgylgy

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
### this is llggyy file~
2+
3+
```c++
4+
#include <iostream>
5+
int main()
6+
{
7+
std::cout<<"Hello, world!"<<std::endl;
8+
return 0;
9+
}
10+
```
11+
![](http://127.0.0.1:8081/api/file/get?filename=No Authorization)
12+
13+
![](http://127.0.0.1:8081/api/file/get?filename=1696040739895.png)
14+
File renamed without changes.

user_controller.cc

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#include <drogon/drogon.h>
21
#include <json/json.h>
32
#include "mysql.h"
43
#include <iomanip>
@@ -48,7 +47,7 @@ void Handle(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr
4847
callback(res);
4948
}
5049

51-
std::string sha256(const std::string str)
50+
std::string sha256(const std::string& str)
5251
{
5352
unsigned char hash[SHA256_DIGEST_LENGTH];
5453

@@ -60,8 +59,8 @@ std::string sha256(const std::string str)
6059

6160
std::stringstream ss;
6261

63-
for (int i = 0; i < SHA256_DIGEST_LENGTH; i++)
64-
ss << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(hash[i]);
62+
for (unsigned char i : hash)
63+
ss << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(i);
6564

6665
return ss.str();
6766
}

0 commit comments

Comments
 (0)