-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
65 lines (53 loc) · 2.4 KB
/
main.cpp
File metadata and controls
65 lines (53 loc) · 2.4 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
#include <iostream>
#include <nsfwcpp.h>
#include <drogon/HttpController.h>
using namespace drogon;
class NSFWController : public HttpController<NSFWController> {
public:
METHOD_LIST_BEGIN
ADD_METHOD_TO(NSFWController::nsfw_route, "/nsfw", Post);
METHOD_LIST_END
void nsfw_route(const HttpRequestPtr &req, std::function<void (const HttpResponsePtr &)> &&callback) {
try {
MultiPartParser fileUpload;
if (fileUpload.parse(req) != 0 || fileUpload.getFiles().size() != 1) {
std::cout << "Invalid file upload: " << fileUpload.getFiles().size() << " files" << std::endl;
Json::Value jsonResponse;
jsonResponse["status"] = k400BadRequest;
jsonResponse["title"] = "must be only 1 file";
auto errorResponse = drogon::HttpResponse::newHttpJsonResponse(jsonResponse);
errorResponse->setStatusCode(k400BadRequest);
callback(errorResponse);
return;
}
const auto& file = fileUpload.getFiles()[0];
const std::string_view& fileData = file.fileContent();
std::vector<uchar> image_data(fileData.begin(), fileData.end());
auto result = nsfw(image_data);
Json::Value responseJson;
for (auto param : result) {
responseJson[param.first] = param.second;
}
auto response = drogon::HttpResponse::newHttpJsonResponse(responseJson);
callback(response);
} catch(const std::exception& e) {
std::cout << "exception: " << e.what() << std::endl;
Json::Value jsonResponse;
jsonResponse["status"] = k500InternalServerError;
jsonResponse["title"] = e.what();
auto errorResponse = drogon::HttpResponse::newHttpJsonResponse(jsonResponse);
errorResponse->setStatusCode(k500InternalServerError);
callback(errorResponse);
}
};
NSFWController() {
LOG_DEBUG << "NSFW route /nsfw";
}
};
int main() {
std::cout << "started nsfw detector server" << std::endl;
drogon::app()
.loadConfigFile("drogon_config.json")
.run();
return 0;
}