-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathimage_file.cpp
More file actions
152 lines (121 loc) · 4.26 KB
/
Copy pathimage_file.cpp
File metadata and controls
152 lines (121 loc) · 4.26 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
#include <odr/internal/html/image_file.hpp>
#include <odr/exceptions.hpp>
#include <odr/file.hpp>
#include <odr/html.hpp>
#include <odr/internal/common/file.hpp>
#include <odr/internal/html/common.hpp>
#include <odr/internal/html/html_service.hpp>
#include <odr/internal/html/html_writer.hpp>
#include <odr/internal/svm/svm_file.hpp>
#include <odr/internal/svm/svm_to_svg.hpp>
#include <sstream>
namespace odr::internal::html {
namespace {
class HtmlServiceImpl final : public HtmlService {
public:
HtmlServiceImpl(ImageFile image_file, HtmlConfig config,
std::shared_ptr<Logger> logger)
: HtmlService(std::move(config), std::move(logger)),
m_image_file{std::move(image_file)} {
m_views.emplace_back(
std::make_shared<HtmlView>(*this, "image", 0, "image.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 == "image.html") {
return true;
}
return false;
}
[[nodiscard]] std::string mimetype(const std::string &path) const override {
if (path == "image.html") {
return "text/html";
}
throw FileNotFound("Unknown path: " + path);
}
void write(const std::string &path, std::ostream &out) const override {
if (path == "image.html") {
HtmlWriter writer(out, config());
write_image(writer);
return;
}
throw FileNotFound("Unknown path: " + path);
}
HtmlResources write_html(const std::string &path,
HtmlWriter &out) const override {
if (path == "image.html") {
return write_image(out);
}
throw FileNotFound("Unknown path: " + path);
}
HtmlResources write_image(HtmlWriter &out) const {
HtmlResources resources;
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_end();
out.write_body_begin();
{
out.write_new_line();
out.out() << "<img";
out.out() << " alt=\"Error: image not found or unsupported\"";
out.out() << " src=\"";
translate_image_src(m_image_file, out.out(), config());
out.out() << "\">";
}
out.write_body_end();
out.write_end();
return resources;
}
protected:
ImageFile m_image_file;
HtmlViews m_views;
};
} // namespace
} // namespace odr::internal::html
namespace odr::internal {
void html::translate_image_src(const File &file, std::ostream &out,
const HtmlConfig &config) {
try {
translate_image_src(DecodedFile(file).as_image_file(), out, config);
} catch (...) {
// TODO hacky - `image/jpg` works for all common image types in chrome
// TODO use stream
out << file_to_url(*file.stream(), "image/jpg");
}
}
void html::translate_image_src(const ImageFile &image_file, std::ostream &out,
const HtmlConfig & /*config*/) {
// try svm
try {
// TODO `image_file` is already an `SvmFile`
// TODO `impl()` might be a bit dirty
const std::shared_ptr<abstract::File> image_file_impl =
image_file.file().impl();
// TODO memory file might not be necessary; other istreams didn't support
// `tellg`
const svm::SvmFile svm_file(std::make_shared<MemoryFile>(*image_file_impl));
std::ostringstream svg_out;
svm::Translator::svg(svm_file, svg_out);
// TODO use stream
out << file_to_url(svg_out.str(), "image/svg+xml");
} catch (...) {
// else we guess that it is a usual image
// TODO hacky - `image/jpg` works for all common image types in chrome
// TODO use stream
out << file_to_url(*image_file.stream(), "image/jpg");
}
}
HtmlService html::create_image_service(const ImageFile &image_file,
const std::string & /*cache_path*/,
HtmlConfig config,
std::shared_ptr<Logger> logger) {
return odr::HtmlService(std::make_unique<HtmlServiceImpl>(
image_file, std::move(config), std::move(logger)));
}
} // namespace odr::internal