Skip to content

Commit bee46a2

Browse files
committed
Merge remote-tracking branch 'lj/main'
2 parents 5741263 + c0f1067 commit bee46a2

20 files changed

+529
-354
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ build/
66
### C ###
77
# Prerequisites
88
*.d
9-
9+
.DS_Store
1010
# Object files
1111
*.o
1212
*.ko

README.md

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33
</p>
44
<h2 align="center">Flypen</h2>
55
<p align="center">
6-
<!-- <a href="https://travis-ci.com/github/ybygjylj/habits-apriori/"><img src="https://travis-ci.com/ybygjylj/habits-apriori.svg?branch=master" alt="Build Status"></a> -->
7-
<a href="https://raw.githubusercontent.com/ybygjylj/habits-apriori/master/LICENSE"><img src="https://img.shields.io/badge/license-MIT-blue.svg" alt="LICENSE"></a>
6+
<a href="https://raw.githubusercontent.com/stepbystepcode/flypen/main/LICENSE"><img src="https://img.shields.io/badge/license-MIT-blue.svg" alt="LICENSE"></a>
87
<a href="https://github.com/topics/cpp"><img src="https://img.shields.io/badge/language-c++-blue.svg" alt="languange"></a>
9-
<!-- <a href="https://gitter.im/habits-apriori/community"><img src="https://badges.gitter.im/Join%20Chat.svg" alt="chat"></a> -->
108
</p>
119

1210
*Read this in other languages: [English](README.md), [简体中文](README.zh_Hans.md).*
@@ -15,21 +13,35 @@
1513
### An Office Collab Backend
1614
This is the Flypen C++ backend service for the Office Collab web application.
1715

16+
Frontend Project for Flypen is [Here](https://github.com/stepbystepcode/flypen-frontend)
17+
1818
## Features
1919
- Real-time collaborative editing of documents
2020
- Chat and notifications
2121
- User management and access control
2222
- Document management and storage (markdown)
23+
2324
## Architecture
2425
- Server application built with C++ drogon famework
2526
- MariaDB database for storage
2627
- REST APIs for client communication
2728
- Use MySQL Connector/C++ 8.1.0 connect database
28-
## Getting Started
2929

30-
### Installing
30+
## Getting Started
3131

32+
### Installation (Automatic)
33+
```bash
34+
sudo apt install git gcc g++ cmake libjsoncpp-dev uuid-dev zlib1g-dev openssl libssl-dev
35+
git clone https://github.com/stepbystepcode/flypen.git --recursive
36+
cd flypen
37+
./run.sh
3238
```
39+
40+
### Installation (Manual)
41+
<details>
42+
<summary>Not Recommended</summary>
43+
<br><br>
44+
<pre>
3345
git clone https://github.com/stepbystepcode/flypen.git
3446
cd flypen
3547
git submodule update --init
@@ -54,17 +66,17 @@ tar zxvf mysql-connector-c++-8.1.0-linux-glibc2.28-x86-64bit.tar.gz
5466
mv mysql-connector-c++-8.1.0-linux-glibc2.28-x86-64bit mysql-connector
5567
rm mysql-connector-c++-8.1.0-linux-glibc2.28-x86-64bit.tar.gz
5668
mkdir build
57-
./run.sh
58-
sudo rm -rf /*
59-
```
69+
./run.sh
70+
</pre>
71+
</details>
72+
73+
74+
6075
## API Documentation
6176
See [API.md](API.md) for details on APIs for authentication, user management, documents, etc.
6277

63-
## Contributing
64-
See CONTRIBUTING.md for contribution guidelines.
65-
6678
## License
6779
This project is licensed under the MIT License. See [LICENSE](LICENSE) for details.
6880

6981
## Contact
70-
For any questions, please reach out at [email protected]
82+
For any questions, please reach out at [email protected] or [email protected]

config.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@
105105
"session_timeout": 0,
106106
//string value of SameSite attribute of the Set-Cookie HTTP respone header
107107
//valid value is either 'Null' (default), 'Lax', 'Strict' or 'None'
108-
"session_same_site" : "Null",
108+
"session_same_site": "Null",
109109
//document_root: Root path of HTTP document, defaut path is ./
110110
"document_root": "./",
111111
//home_page: Set the HTML file of the home page, the default value is "index.html"
@@ -317,4 +317,4 @@
317317
],
318318
//custom_config: custom configuration for users. This object can be get by the app().getCustomConfig() method.
319319
"custom_config": {}
320-
}
320+
}

file_controller.cc

Lines changed: 96 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
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 )
9+
std::string return_status(std::string result, const std::string& command,Json::Value &res_json)
1110
{
12-
if (result != "")
13-
result = "success";
11+
if (!result.empty()){
12+
result = command + " success";
13+
res_json["code"] = 200;
14+
}
1415
else
15-
result = " error in :"+command;
16+
{
17+
result = " Error in :" + command;
18+
res_json["code"] = 400;
19+
}
20+
1621
return result;
1722
}
1823
void add_lock(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
@@ -36,13 +41,13 @@ void add_lock(const HttpRequestPtr &req, std::function<void(const HttpResponsePt
3641
std::string shell_commands(const char *cmd)
3742
{
3843
char buffer[1280];
39-
std::string result = "";
44+
std::string result;
4045
FILE *pipe = popen(cmd, "r");
4146
if (!pipe)
4247
throw std::runtime_error("popen() failed!");
4348
try
4449
{
45-
while (fgets(buffer, sizeof buffer, pipe) != NULL)
50+
while (fgets(buffer, sizeof buffer, pipe) != nullptr)
4651
{
4752
result += buffer;
4853
}
@@ -58,74 +63,91 @@ std::string shell_commands(const char *cmd)
5863

5964
void commandsCtrl(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
6065
{
61-
62-
std::cout << "commandsCtrl" << std::endl;
63-
enum Command
64-
{
65-
tree,
66-
cp,
67-
mv,
68-
rm,
69-
mkdir,
70-
touch,
71-
cat
72-
} command;
73-
char *pathvar;
74-
pathvar = getenv("PWD");
75-
76-
command = static_cast<Command>(stoi(req->getJsonObject()->get("command", "").asString()));
77-
std::string params1 = req->getJsonObject()->get("params", "")[0].asString();
78-
std::string params2 = req->getJsonObject()->get("params", "")[1].asString();
66+
auto res = HttpResponse::newHttpResponse();
7967
std::string result;
80-
switch (command)
81-
{
82-
case tree:
83-
result = shell_commands(("cd " + std::string(pathvar) + "/.. " + "&&" + "tree -J root").c_str());
84-
break;
85-
case cp:
86-
result = shell_commands(("cp -v " + std::string(pathvar) + "/../root/" + params1 + " " + std::string(pathvar) + "/../root/" + params2).c_str());
87-
result = return_status(result,"cp");
68+
Json::Value res_json;
69+
Json::FastWriter writer;
70+
res->addHeader("Access-Control-Allow-Origin", "*");
71+
if (jwtVerify(req)){
72+
enum Command
73+
{
74+
tree,
75+
cp,
76+
mv,
77+
rm,
78+
mkdir,
79+
touch,
80+
cat
81+
} command;
82+
char *pathvar;
83+
pathvar = getenv("PWD");
8884

89-
break;
90-
case mv:
91-
result = shell_commands(("mv -v " + std::string(pathvar) + "/../root/" + params1 + " " + std::string(pathvar) + "/../root/" + params2).c_str());
92-
result = return_status(result,"mv");
93-
break;
94-
case rm:
95-
if (params1.find("..") != std::string::npos)
85+
command = static_cast<Command>(stoi(req->getJsonObject()->get("command", "").asString()));
86+
std::string params1 = req->getJsonObject()->get("params", "")[0].asString();
87+
std::string params2 = req->getJsonObject()->get("params", "")[1].asString();
88+
switch (command)
9689
{
97-
result = "error:result in wrong directory";
90+
case tree:
91+
result = shell_commands(("cd " + std::string(pathvar) + "/.. " + "&&" + "tree -J root").c_str());
9892
break;
99-
}
100-
result = shell_commands(("rm -rf -v " + std::string(pathvar) + "/../root/" + params1).c_str());
101-
result = return_status(result,"rm");
102-
break;
103-
case mkdir:
104-
result = shell_commands(("mkdir -v " + std::string(pathvar) + "/../root/" + params1).c_str());
105-
result = return_status(result,"mkdir");
106-
break;
107-
case touch:
108-
if ("" == shell_commands(("ls -l " + std::string(pathvar) + "/../root/" + params1 + " grep ^- ").c_str()))
109-
{
110-
result = shell_commands(("touch " + std::string(pathvar) + "/../root/" + params1).c_str());
111-
result = "success";
112-
}
93+
case cp:
94+
result = shell_commands(("cp -v " + std::string(pathvar) + "/../root/" + params1 + " " + std::string(pathvar) + "/../root/" + params2).c_str());
95+
result = return_status(result, "cp",res_json);
11396

114-
else
115-
{
116-
result = "error:file already exists";
97+
break;
98+
case mv:
99+
result = shell_commands(("mv -v " + std::string(pathvar) + "/../root/" + params1 + " " + std::string(pathvar) + "/../root/" + params2).c_str());
100+
result = return_status(result, "mv",res_json);
101+
break;
102+
case rm:
103+
if (params1.find("..") != std::string::npos)
104+
{
105+
result = "error:result in wrong directory";
106+
res_json["code"] = 400;
107+
break;
108+
}
109+
result = shell_commands(("rm -rf -v " + std::string(pathvar) + "/../root/" + params1).c_str());
110+
result = return_status(result, "rm",res_json);
111+
break;
112+
case mkdir:
113+
result = shell_commands(("mkdir -v " + std::string(pathvar) + "/../root/" + params1).c_str());
114+
result = return_status(result, "mkdir",res_json);
115+
break;
116+
case touch:
117+
if (shell_commands(("ls -l " + std::string(pathvar) + "/../root/" + params1 + " grep ^- ").c_str()).empty())
118+
{
119+
shell_commands(("touch " + std::string(pathvar) + "/../root/" + params1).c_str());
120+
//result = shell_commands(("touch " + std::string(pathvar) + "/../root/" + params1).c_str());
121+
result = "success";
122+
res_json["code"] = 200;
123+
}
124+
125+
else
126+
{
127+
result = "error:file already exists";
128+
res_json["code"] = 400;
129+
}
130+
break;
131+
case cat:
132+
result = shell_commands(("cat " + std::string(pathvar) + "/../root/" + params1).c_str());
133+
res_json["code"] = 200;
134+
break;
135+
default:
136+
result = "error:command not found";
137+
res_json["code"] = 400;
138+
break;
117139
}
118-
break;
119-
case cat:
120-
result = shell_commands(("cat " + std::string(pathvar) + "/../root/" + params1).c_str());
121-
break;
122-
default:
123-
result = "error:command not found";
124-
break;
125140
}
126-
auto res = HttpResponse::newHttpResponse();
141+
else
142+
{
143+
result = "No Authorization";
144+
res_json["code"] = 401;
145+
}
146+
res_json["message"] = result;
147+
127148
res->addHeader("Access-Control-Allow-Origin", "*");
128-
res->setBody(result);
149+
auto output = writer.write(res_json);
150+
res->setBody(output);
129151
callback(res);
130152
}
131153
// void genTree(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
@@ -153,6 +175,7 @@ void commandsCtrl(const HttpRequestPtr &req, std::function<void(const HttpRespon
153175
void saveFile(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
154176
{
155177
auto res = HttpResponse::newHttpResponse();
178+
res->addHeader("Access-Control-Allow-Origin", "*");
156179
if (jwtVerify(req))
157180
{
158181
auto body = req->getBody();
@@ -173,7 +196,7 @@ void saveFile(const HttpRequestPtr &req, std::function<void(const HttpResponsePt
173196

174197
sql_unlocked(filename);
175198

176-
res->addHeader("Access-Control-Allow-Origin", "*");
199+
177200
res->setBody("success");
178201
callback(res);
179202
}
@@ -186,12 +209,12 @@ void saveFile(const HttpRequestPtr &req, std::function<void(const HttpResponsePt
186209
void imageUpload(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
187210
{
188211
auto resp = HttpResponse::newHttpResponse();
212+
resp->addHeader("Access-Control-Allow-Origin", "*");
189213
if (jwtVerify(req))
190214
{
191215
MultiPartParser fileUpload;
192216
if (fileUpload.parse(req) != 0 || fileUpload.getFiles().size() != 1)
193217
{
194-
auto resp = HttpResponse::newHttpResponse();
195218
resp->setBody("Must only be one file");
196219
resp->setStatusCode(k403Forbidden);
197220
callback(resp);
@@ -202,33 +225,24 @@ void imageUpload(const HttpRequestPtr &req, std::function<void(const HttpRespons
202225
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count();
203226
std::string timestamp = std::to_string(ms) + '.' + std::string(file.getFileExtension());
204227

205-
resp->addHeader("Access-Control-Allow-Origin", "*");
206228
resp->setBody(timestamp);
207229
file.save();
208230
shell_commands(("mv ./uploads/" + file.getFileName() + " ./uploads/" + timestamp).c_str());
209231

210232
LOG_INFO << "The uploaded file has been saved to the ./uploads "
211233
"directory";
212-
callback(resp);
213234
}
214235
else
215236
{
216237
resp->setBody("No Authorization");
217238
}
239+
callback(resp);
218240
}
219241

220242
void getPicture(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
221243
{
222244
std::string filename = req->getParameter("filename");
223245
auto resp = HttpResponse::newFileResponse("./uploads/" + filename);
224-
225-
if (jwtVerify(req))
226-
{
227-
resp->addHeader("Access-Control-Allow-Origin", "*");
228-
callback(resp);
229-
}
230-
else
231-
{
232-
resp->setBody("No Authorization");
233-
}
246+
resp->addHeader("Access-Control-Allow-Origin", "*");
247+
callback(resp);
234248
}

file_controller.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ void getPicture(const HttpRequestPtr &req, std::function<void(const HttpResponse
1313
void commandsCtrl(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback);
1414

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

0 commit comments

Comments
 (0)