-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathfilesystem.cpp
More file actions
159 lines (120 loc) · 4.37 KB
/
Copy pathfilesystem.cpp
File metadata and controls
159 lines (120 loc) · 4.37 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <odr/internal/html/filesystem.hpp>
#include <odr/exceptions.hpp>
#include <odr/filesystem.hpp>
#include <odr/html.hpp>
#include <odr/internal/abstract/file.hpp>
#include <odr/internal/common/path.hpp>
#include <odr/internal/html/common.hpp>
#include <odr/internal/html/html_service.hpp>
#include <odr/internal/html/html_writer.hpp>
namespace odr::internal::html {
namespace {
class HtmlServiceImpl final : public HtmlService {
public:
HtmlServiceImpl(Filesystem filesystem, HtmlConfig config,
std::shared_ptr<Logger> logger)
: HtmlService(std::move(config), std::move(logger)),
m_filesystem{std::move(filesystem)} {
m_views.emplace_back(
std::make_shared<HtmlView>(*this, "files", 0, "files.html"));
}
void warmup() const override {}
[[nodiscard]] const HtmlViews &list_views() const override { return m_views; }
[[nodiscard]] bool exists(const std::string &path) const override {
if (path == "files.html") {
return true;
}
return false;
}
[[nodiscard]] std::string mimetype(const std::string &path) const override {
if (path == "files.html") {
return "text/html";
}
throw FileNotFound("Unknown path: " + path);
}
void write(const std::string &path, std::ostream &out) const override {
if (path == "files.html") {
HtmlWriter writer(out, config());
write_filesystem(writer);
return;
}
throw FileNotFound("Unknown path: " + path);
}
HtmlResources write_html(const std::string &path,
HtmlWriter &out) const override {
if (path == "files.html") {
return write_filesystem(out);
}
throw FileNotFound("Unknown path: " + path);
}
HtmlResources write_filesystem(HtmlWriter &out) const {
HtmlResources resources;
const FileWalker file_walker = m_filesystem.file_walker("/");
out.write_begin();
out.write_header_begin();
out.write_header_charset("UTF-8");
out.write_header_target("_blank");
out.write_header_title("odr");
out.write_header_viewport(
"width=device-width,initial-scale=1.0,user-scalable=yes");
out.write_header_style_begin();
out.write_raw("*{font-family:monospace;}");
out.write_header_style_end();
out.write_header_end();
out.write_body_begin();
for (; !file_walker.end(); file_walker.next()) {
Path file_path(file_walker.path());
const bool is_file = file_walker.is_file();
out.write_element_begin("p");
out.write_element_begin("span");
out.write_raw(file_path.string());
out.write_element_end("span");
out.write_element_begin("span");
out.write_raw(" ");
out.write_element_end("span");
out.write_element_begin("span");
out.write_raw(file_walker.is_file() ? "file" : "directory");
out.write_element_end("span");
if (is_file) {
out.write_element_begin("span");
out.write_raw(" ");
out.write_element_end("span");
File file = m_filesystem.open(file_path.string());
out.write_element_begin("span");
out.write_raw(std::to_string(file.size()));
out.write_element_end("span");
if (const std::unique_ptr<std::istream> stream = file.stream();
stream != nullptr) {
out.write_element_begin("span");
out.write_raw(" ");
out.write_element_end("span");
out.write_element_begin(
"a",
HtmlElementOptions().set_attributes(HtmlAttributesVector{
{"href", file_to_url(*stream, "application/octet-stream")},
{"download", file_path.basename()}}));
out.write_raw("download");
out.write_element_end("a");
}
}
out.write_element_end("p");
}
out.write_body_end();
out.write_end();
return resources;
}
protected:
Filesystem m_filesystem;
HtmlViews m_views;
};
} // namespace
} // namespace odr::internal::html
namespace odr::internal {
HtmlService html::create_filesystem_service(const Filesystem &filesystem,
const std::string & /*cache_path*/,
HtmlConfig config,
std::shared_ptr<Logger> logger) {
return odr::HtmlService(std::make_unique<HtmlServiceImpl>(
filesystem, std::move(config), std::move(logger)));
}
} // namespace odr::internal