-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathserver.cpp
More file actions
87 lines (72 loc) · 2.61 KB
/
server.cpp
File metadata and controls
87 lines (72 loc) · 2.61 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
#include <odr/archive.hpp>
#include <odr/exceptions.hpp>
#include <odr/file.hpp>
#include <odr/filesystem.hpp>
#include <odr/html.hpp>
#include <odr/http_server.hpp>
#include <string>
using namespace odr;
int main(const int argc, char **argv) {
const std::shared_ptr logger =
Logger::create_stdio("odr-server", LogLevel::verbose);
std::string input{argv[1]};
std::optional<std::string> password;
if (argc >= 3) {
password = argv[2];
}
DecodePreference decode_preference;
decode_preference.engine_priority = {
DecoderEngine::poppler, DecoderEngine::wvware, DecoderEngine::odr};
decode_preference.as_file_type = FileType::zip;
DecodedFile decoded_file{input, decode_preference, *logger};
if (decoded_file.password_encrypted() && !password) {
ODR_FATAL(*logger, "document encrypted but no password given");
return 2;
}
if (decoded_file.password_encrypted()) {
try {
decoded_file = decoded_file.decrypt(*password);
} catch (const WrongPasswordError &) {
ODR_FATAL(*logger, "wrong password");
return 1;
}
}
HttpServer::Config server_config;
HttpServer server(server_config);
HtmlConfig html_config;
html_config.embed_images = false;
html_config.embed_shipped_resources = true;
html_config.relative_resource_paths = false;
html_config.text_document_margin = true;
html_config.editable = true;
{
const std::string prefix = "file";
const HtmlViews views =
server.serve_file(decoded_file, prefix, html_config);
ODR_INFO(*logger, "hosted decoded file with id: " << prefix);
for (const auto &view : views) {
ODR_INFO(*logger,
"http://localhost:8080/file/" << prefix << "/" << view.path());
}
}
if (decoded_file.is_document_file() || decoded_file.is_archive_file()) {
std::optional<Filesystem> filesystem;
if (decoded_file.is_document_file()) {
filesystem = decoded_file.as_document_file().document().as_filesystem();
} else if (decoded_file.is_archive_file()) {
filesystem = decoded_file.as_archive_file().archive().as_filesystem();
}
const std::string prefix = "filesystem";
const HtmlService filesystem_service = html::translate(
filesystem.value(), server_config.cache_path + "/" + prefix,
html_config, logger);
server.connect_service(filesystem_service, prefix);
ODR_INFO(*logger, "hosted filesystem with id: " << prefix);
for (const auto &view : filesystem_service.list_views()) {
ODR_INFO(*logger,
"http://localhost:8080/file/" << prefix << "/" << view.path());
}
}
server.listen("localhost", 8080);
return 0;
}