-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviews_media.cpp
More file actions
243 lines (237 loc) · 9.91 KB
/
Copy pathviews_media.cpp
File metadata and controls
243 lines (237 loc) · 9.91 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// FUZE.page 2026
// The following code is not to be used for AI training. For humans, the MIT license applies.
#include "views.hpp"
#include "FuzeHttp.hpp"
#include "PermissionObject.hpp"
#include "shared_state.hpp"
#include <boost/beast/http/status.hpp>
// #include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>
#ifdef WITH_MAGICK
#include <Magick++.h>
#endif
#include <fstream>
#include <iostream>
#include <print>
FuzeHttp::Response uploadFile(shared_state* state, FuzeHttp::Request req) {
std::optional<Client> client = state->getClientIfExists(req);
// TODO have UPLOAD_FILE permission modifiable for threads and boards.
if (!state->clientHasPermission(client, PERMISSION::UPLOAD_FILE))
return FuzeHttp::Response{.status = http::status::forbidden, .error_message = "You lack permission to upload files."};
std::istringstream req_stream(req.body());
std::string req_line;
std::getline(req_stream, req_line);
std::string req_terminator = req_line.substr(0, req_line.length()-1) + "--\r";
// std::cout << "Request ID: " << req_terminator << std::endl;
std::string out_filename;
bool empty_line = false;
while (!empty_line) {
std::getline(req_stream, req_line, '\n');
if (req_line == "\r") {
empty_line = true;
}
else if (req_line.substr(0, 19) == "Content-Disposition") {
int filename_i;
filename_i = req_line.find("filename", 20) + 10;
if (filename_i != std::string::npos) {
int filename_end_i;
if ((filename_end_i = req_line.find(";", filename_i)) == std::string::npos) {
filename_end_i = req_line.length() - filename_i - 2;
}
std::cout << filename_end_i << std::endl;
out_filename = req_line.substr(filename_i, filename_end_i);
std::cout << "out_filename: " << out_filename << std::endl;
}
}
// else if (req_line.substr(0, 13) == "Content-Type") {
// int boundary_i;
// if ((boundary_i = req_line.find("boundary", 13)) != std::string::npos) {
// std::cout << req_line.substr(boundary_i+1, req_line.length()) << std::endl;
// }
// }
// std::cout << "line: " << i++ << std::endl << req_line << std::endl;
}
if (out_filename.empty()) {
return FuzeHttp::Response{.status = http::status::bad_request, .error_message = "File name not found in POST header"};
}
else if (out_filename.length() > static_cast<int>(MESSAGE_FIELDS::MAX_FILE_NAME)) {
return FuzeHttp::Response{.status = http::status::bad_request, .error_message = std::format("File name length exceeds the server-defined limit of {}", static_cast<int>(MESSAGE_FIELDS::MAX_FILE_NAME))};
}
FuzeHttp::sanitiseFileName(out_filename);
std::cout << "Sanitised out_filename: " << out_filename << std::endl;
// Add UUID to filename
boost::uuids::uuid u = boost::uuids::random_generator()();
std::string uuid_str = boost::uuids::to_string(u);
int filename_uuid_index;
if ((filename_uuid_index = out_filename.rfind(".")) == -1) {
filename_uuid_index = out_filename.size();
}
out_filename.insert(filename_uuid_index, uuid_str);
const std::filesystem::path out_file_path = state->getMediaLocation() / out_filename;
// Write to the file
std::ofstream outfile(out_file_path.string(), std::ios::binary);
bool is_initial_line = true;
bool previous_line_ends_with_carriage_return = false;
while (std::getline(req_stream, req_line)) {
// std::cout << req_line << std::endl;
// std::cout << req_line.length() << ", " << req_terminator.length() << std::endl;
if (req_line != req_terminator) {
if (previous_line_ends_with_carriage_return) {
previous_line_ends_with_carriage_return = false;
outfile << "\r";
}
if (!is_initial_line)
outfile << "\n";
if (req_line.length() == 0)
continue;
else if (req_line.back() == '\r') {
previous_line_ends_with_carriage_return = true;
outfile << req_line.substr(0, req_line.length() - 1);
}
else
outfile << req_line;
is_initial_line = false;
}
else {
break;
}
}
std::cout << "Finished reading data" << std::endl;
try {
outfile.exceptions(outfile.failbit);
outfile.close();
}
catch (const std::ios_base::failure& exception) {
std::stringstream error_message;
error_message
<< "Reason: " << exception.what() << '\n'
<< "Error code: " << exception.code() << "\n";
std::cerr << "Exception thrown when attempting to save uploaded file.\n" << error_message.str();
return FuzeHttp::Response{.status = http::status::internal_server_error, .error_message = error_message.str()};
}
std::cout << "END OF FILE" << std::endl;
FuzeHttp::Response response{
.status = http::status::accepted,
.headers = {{
{"File-Name", out_filename}
}}
};
// Create thumbnail if applicable
#ifdef WITH_MAGICK
const std::string_view file_mime_type = FuzeHttp::getMimeType(out_filename);
std::print("MIME type: {}", file_mime_type);
bool create_thumbnail_for_image = state->canCreateThumbnailForImageFormat(file_mime_type);
bool create_thumbnail_for_video = state->canCreateThumbnailForVideoFormat(file_mime_type);
std::println(" - create thumbnail? {}", create_thumbnail_for_image || create_thumbnail_for_video);
unsigned int image_width = 100, image_height = 100;
bool uploaded_file_has_thumbnail = false;
if (create_thumbnail_for_image || create_thumbnail_for_video) {
try {
Magick::Image thumbnail;
if (create_thumbnail_for_image) {
thumbnail.read(out_file_path.string());
if (file_mime_type != "image/svg+xml") { // stripping image messes up svg files
Magick::Image image;
image.read(out_file_path.string());
if (state->config.strip_metadata) {
image.autoOrient();
image.strip(); // Removes metadata
}
if (state->config.convert_heic_to_jpg && file_mime_type == "image/heic") {
image.quality(80);
image.write(out_file_path.string().substr(0, out_file_path.string().rfind('.'))+".jpg");
std::println("out_filename was first {}", out_filename);
out_filename = out_filename.substr(0, out_filename.rfind('.'))+".jpg";
std::println("out_filename is now {}", out_filename);
}
else if (state->config.strip_metadata)
image.write(out_file_path.string());
}
}
else {
thumbnail.read(std::format("{}[0]", out_file_path.string())); // read the first frame into ImageMagick ffmpeg delegate
}
const Magick::Geometry size = thumbnail.size();
image_width = size.width();
image_height = size.height();
Magick::Geometry thumbnail_dimensions;
if (size.width() < size.height()) {
thumbnail_dimensions.width(size.width() < size.height()>>1 ? std::ceil(state->config.thumbnail_size / 2) : std::ceil(state->config.thumbnail_size * (size.width()/size.height())));
thumbnail_dimensions.height(state->config.thumbnail_size);
}
else if (size.height() < size.width()) {
thumbnail_dimensions.width(state->config.thumbnail_size);
thumbnail_dimensions.height(size.height() < size.width()>>1 ? std::ceil(state->config.thumbnail_size / 2) : std::ceil(state->config.thumbnail_size * (size.height()/size.width())));
}
else {
thumbnail_dimensions.width(state->config.thumbnail_size);
thumbnail_dimensions.height(state->config.thumbnail_size);
}
thumbnail.resize(thumbnail_dimensions);
thumbnail.quality(60);
thumbnail.write(std::format("{}/thumbnails/THUMBNAIL_{}.{}", state->getMediaLocation().string(), out_filename, state->config.thumbnail_file_extension));
uploaded_file_has_thumbnail = true;
if (state->config.convert_heic_to_jpg && file_mime_type == "image/heic") {
std::filesystem::remove(out_file_path);
}
}
catch( Magick::Warning& magick_warning ) {
std::cerr << "[Magick++] WARNING: " << magick_warning.what() << std::endl << "Image might not be made." << std::endl;
}
catch (Magick::Error& magick_error) {
std::cerr << "[Magick++] ERROR: " << magick_error.what() << std::endl << "Image will therefore not be made." << std::endl;
}
}
if (create_thumbnail_for_image || create_thumbnail_for_video) {
if (create_thumbnail_for_image || (create_thumbnail_for_video && uploaded_file_has_thumbnail)) {
response.headers.value().emplace("Image-Width", std::to_string(image_width));
response.headers.value().emplace("Image-Height", std::to_string(image_height));
}
if (uploaded_file_has_thumbnail)
response.headers.value().emplace("Thumbnail-File-Extension", state->config.thumbnail_file_extension);
}
#endif
return response;
}
// TODO find a way to handle multiple directories under one view
FuzeHttp::Response getMedia(shared_state* state, FuzeHttp::Request req, std::string location) {
std::cout << "Showing thru getMedia" << std::endl;
std::string file_name = location;
int filename_extension_index;
if ((filename_extension_index = file_name.rfind(".")) == -1) {
filename_extension_index = file_name.size();
}
if (filename_extension_index >= 36) {
file_name.erase(filename_extension_index - 36, 36);
}
std::filesystem::path file_path = state->getMediaLocation() / location;
if (!std::filesystem::exists(file_path))
return FuzeHttp::Response{http::status::not_found};
if (!std::filesystem::is_regular_file(file_path))
return FuzeHttp::Response{.status = http::status::bad_request, .error_message = "Is a directory"};
return FuzeHttp::Response{
.status = http::status::ok,
.headers = {{
{"Content-Disposition", std::format("inline; filename=\"{}\"", file_name)}
// Would conflict with Cache-Control set in FuzeHttp.hpp
// ,{"Cache-Control", "max-age=31536000, immutable"}
}},
.file = file_path.string()
};
}
FuzeHttp::Response getThumbnail(shared_state* state, FuzeHttp::Request req, std::string file_path) {
std::cout << "Showing thru getMedia" << std::endl;
std::string file_name = file_path;
int filename_extension_index;
if ((filename_extension_index = file_name.rfind(".")) == -1) {
filename_extension_index = file_name.size();
}
if (filename_extension_index >= 36) {
file_name.erase(filename_extension_index - 36, 36);
}
return FuzeHttp::Response{
.status = http::status::ok,
.file = std::format("{}/thumbnails/{}", state->getMediaLocation().string(), file_path)
};
}