Skip to content

Commit

Permalink
Set content-type for generic files
Browse files Browse the repository at this point in the history
  • Loading branch information
oglimmer committed May 25, 2022
1 parent a4e0e32 commit 159bf26
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions src/oatpp-swagger/Controller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,29 @@
#include "oatpp/core/macro/codegen.hpp"
#include "oatpp/core/macro/component.hpp"


bool hasEnding (std::string fullString, std::string const &ending) {
std::transform(fullString.begin(), fullString.end(), fullString.begin(),
[](unsigned char c){ return std::tolower(c); });
if (fullString.length() >= ending.length()) {
return (0 == fullString.compare (fullString.length() - ending.length(), ending.length(), ending));
} else {
return false;
}
}

std::string getMimeType(const std::string &filename) {
if (hasEnding(filename, ".html")) return "text/html";
if (hasEnding(filename, ".jpg")) return "image/jpeg";
if (hasEnding(filename, ".jpeg")) return "image/jpeg";
if (hasEnding(filename, ".png")) return "image/png";
if (hasEnding(filename, ".gif")) return "image/gif";
if (hasEnding(filename, ".css")) return "text/css";
if (hasEnding(filename, ".js")) return "text/javascript";
if (hasEnding(filename, ".xml")) return "text/xml";
return "text/plain";
}

namespace oatpp { namespace swagger {

/**
Expand Down Expand Up @@ -116,9 +139,13 @@ class Controller : public oatpp::web::server::api::ApiController {
auto body = std::make_shared<oatpp::web::protocol::http::outgoing::StreamingBody>(
m_resources->getResourceStream(filename->c_str())
);
return OutgoingResponse::createShared(Status::CODE_200, body);
auto resp = OutgoingResponse::createShared(Status::CODE_200, body);
resp->putHeader("Content-Type", getMimeType(filename));
return resp;
}
return createResponse(Status::CODE_200, m_resources->getResource(filename->c_str()));
auto resp = createResponse(Status::CODE_200, m_resources->getResource(filename->c_str()));
resp->putHeader("Content-Type", getMimeType(filename));
return resp;
}

#include OATPP_CODEGEN_END(ApiController)
Expand Down

0 comments on commit 159bf26

Please sign in to comment.