Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
namchuai committed Dec 20, 2024
1 parent 65f9790 commit 667713e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 25 deletions.
6 changes: 2 additions & 4 deletions engine/controllers/engines.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,8 @@ void Engines::TestJinja(
callback(resp);
return;
}
// TODO: namh recheck all the api using this. because we have an issue with Germany locale before.
auto resp = HttpResponse::newHttpResponse();
resp->setBody(rendered_data.value());
resp->setContentTypeCode(drogon::CT_TEXT_PLAIN);

auto resp = cortex_utils::CreateTextPlainResponse(rendered_data.value());
callback(resp);
}

Expand Down
17 changes: 5 additions & 12 deletions engine/controllers/files.cc
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,8 @@ void Files::RetrieveFileContent(
return;
}

auto [buffer, size] = std::move(res.value());
auto resp = HttpResponse::newHttpResponse();
resp->setBody(std::string(buffer.get(), size));
resp->setContentTypeCode(CT_APPLICATION_OCTET_STREAM);
auto resp =
cortex_utils::CreateCortexContentResponse(std::move(res.value()));
callback(resp);
} else {
if (!msg_res->rel_path.has_value()) {
Expand All @@ -243,10 +241,8 @@ void Files::RetrieveFileContent(
return;
}

auto [buffer, size] = std::move(content_res.value());
auto resp = HttpResponse::newHttpResponse();
resp->setBody(std::string(buffer.get(), size));
resp->setContentTypeCode(CT_APPLICATION_OCTET_STREAM);
auto resp = cortex_utils::CreateCortexContentResponse(
std::move(content_res.value()));
callback(resp);
}
}
Expand All @@ -261,9 +257,6 @@ void Files::RetrieveFileContent(
return;
}

auto [buffer, size] = std::move(res.value());
auto resp = HttpResponse::newHttpResponse();
resp->setBody(std::string(buffer.get(), size));
resp->setContentTypeCode(CT_APPLICATION_OCTET_STREAM);
auto resp = cortex_utils::CreateCortexContentResponse(std::move(res.value()));
callback(resp);
}
34 changes: 25 additions & 9 deletions engine/utils/cortex_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,10 @@
#include <drogon/HttpClient.h>
#include <drogon/HttpResponse.h>
#include <sys/stat.h>
#include <algorithm>
#include <ctime>
#include <filesystem>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <ostream>
#include <random>
#include <regex>
#include <string>
#include <utility>
#if defined(__linux__)
#include <limits.h>
#include <unistd.h>
Expand Down Expand Up @@ -69,6 +63,30 @@ inline drogon::HttpResponsePtr CreateCortexHttpJsonResponse(
return res;
};

inline drogon::HttpResponsePtr CreateCortexContentResponse(
std::pair<std::unique_ptr<char[]>, size_t> content) {
auto [buffer, size] = std::move(content);
auto resp = drogon::HttpResponse::newHttpResponse();
resp->setBody(std::string(buffer.get(), size));
resp->setContentTypeCode(drogon::CT_APPLICATION_OCTET_STREAM);

#if defined(_WIN32)
resp->addHeader("date", GetDateRFC1123());
#endif
return resp;
}

inline drogon::HttpResponsePtr CreateTextPlainResponse(
const std::string& text) {
auto resp = drogon::HttpResponse::newHttpResponse();
resp->setBody(text);
resp->setContentTypeCode(drogon::CT_TEXT_PLAIN);
#if defined(_WIN32)
resp->addHeader("date", GetDateRFC1123());
#endif
return resp;
}

inline drogon::HttpResponsePtr CreateCortexStreamResponse(
const std::function<std::size_t(char*, std::size_t)>& callback,
const std::string& attachmentFileName = "") {
Expand All @@ -80,8 +98,6 @@ inline drogon::HttpResponsePtr CreateCortexStreamResponse(
return res;
}



#if defined(_WIN32)
inline std::string GetCurrentPath() {
char path[MAX_PATH];
Expand Down

0 comments on commit 667713e

Please sign in to comment.