-
Notifications
You must be signed in to change notification settings - Fork 1
/
Handle.cpp
89 lines (70 loc) · 2.82 KB
/
Handle.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include "Handle.h"
#include "Render.h"
#include "FileAction.h"
#include "default_index.h"
#include <iostream>
Handle::Handle(httplib::Server* svr):m_pServer(svr)
{
}
Handle::~Handle()
{
}
int Handle::registerCallBack()
{
m_pServer->set_mount_point("/ResDir", "./ResDir");
m_pServer->Get("/",std::bind(&Handle::IndexView,this,std::placeholders::_1,std::placeholders::_2));
//m_pServer->Get(R"(/ResDir/((\w+)(/*))*\.\w+)",std::bind(&Handle::loadFile,this,std::placeholders::_1,std::placeholders::_2));
m_pServer->Get(R"(/ResDir/((\w+)(/*))*)",std::bind(&Handle::ListFile,this,std::placeholders::_1,std::placeholders::_2));
std::function<void(const Request&, Response&)> upF= std::bind(&Handle::UpLoadFile, this, std::placeholders::_1, std::placeholders::_2);
m_pServer->Post("/post", upF);
return 0;
}
void Handle::IndexView(const Request & request, Response & response)
{
FileAction fileaction;
std::vector<FileInfo> filelist = fileaction.listOfDir("./ResDir", false);
Json listData;
for (size_t i = 0; i < filelist.size(); i++)
{
Json data;
data["fileName"] = filelist[i].fileName;
data["fullPath"] = filelist[i].fullPath.substr(1, filelist[i].fullPath.size() - 1);
data["isDir"] = filelist[i].isDir;
listData["filelist"].push_back(data);
}
if(!fileaction.fileExists("./ResDir/index.html"))
response.set_content(Render::render_string(strDefaultIndex,listData),"text/html");
else
response.set_content(Render::render_file("index.html",listData),"text/html");
}
void Handle::ListFile(const Request & request, Response & response)
{
FileAction fileaction;
std::vector<FileInfo> filelist = fileaction.listOfDir("." + request.path, false);
Json listData;
for (size_t i = 0; i < filelist.size(); i++)
{
Json data;
data["fileName"] = filelist[i].fileName;
data["fullPath"] = filelist[i].fullPath.substr(1, filelist[i].fullPath.size() - 1);
data["isDir"] = filelist[i].isDir;
listData["filelist"].push_back(data);
}
if(!fileaction.fileExists("./ResDir/index.html"))
response.set_content(Render::render_string(strDefaultIndex,listData),"text/html");
else
response.set_content(Render::render_file("index.html",listData),"text/html");
}
void Handle::UpLoadFile(const Request& request, Response& response)
{
const std::string resDir = "./ResDir";
FileAction fileaction;
if(!fileaction.dirExists(resDir))
fileaction.makeDir(resDir);
auto file = request.get_file_value("file");
std::cout << "file length: " << file.content.length() << std::endl
<< "file name: " << file.filename << std::endl;
std::ofstream ofs(resDir + "/"+ file.filename, std::ios::binary);
ofs << file.content;
//response.set_redirect("/");
}