Skip to content

Commit

Permalink
Merge pull request oatpp#71 from oglimmer/master
Browse files Browse the repository at this point in the history
Set content-type for generic files
  • Loading branch information
lganzzzo authored Sep 1, 2022
2 parents 2bc804e + 40e2d63 commit ed5251c
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
9 changes: 7 additions & 2 deletions src/oatpp-swagger/AsyncController.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,14 @@ class AsyncController : public oatpp::web::server::api::ApiController {
auto body = std::make_shared<oatpp::web::protocol::http::outgoing::StreamingBody>(
controller->m_resources->getResourceStream(filename->c_str())
);
return _return(OutgoingResponse::createShared(Status::CODE_200, body));
auto resp = OutgoingResponse::createShared(Status::CODE_200, body);
resp->putHeader("Content-Type", controller->m_resources->getMimeType(filename));
return _return(resp);
}
return _return(controller->createResponse(Status::CODE_200, controller->m_resources->getResource(filename->c_str())));
auto resp = controller->createResponse(Status::CODE_200,
controller->m_resources->getResource(filename->c_str()));
resp->putHeader("Content-Type", controller->m_resources->getMimeType(filename));
return _return(resp);
}

};
Expand Down
8 changes: 6 additions & 2 deletions src/oatpp-swagger/Controller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,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", m_resources->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", m_resources->getMimeType(filename));
return resp;
}

#include OATPP_CODEGEN_END(ApiController)
Expand Down
22 changes: 22 additions & 0 deletions src/oatpp-swagger/Resources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,26 @@ v_io_size Resources::ReadCallback::read(void *buffer, v_buff_size count, async::
return m_stream.read(buffer, count, action);
}

bool Resources::hasEnding(std::string fullString, std::string const &ending) const {
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 Resources::getMimeType(const std::string &filename) const {
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";
}

}}
8 changes: 8 additions & 0 deletions src/oatpp-swagger/Resources.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class Resources {
private:
oatpp::String loadFromFile(const char* fileName);
void cacheResource(const char* fileName);
bool hasEnding(std::string fullString, std::string const &ending) const;

class ReadCallback : public oatpp::data::stream::ReadCallback {
private:
Expand Down Expand Up @@ -126,6 +127,13 @@ class Resources {
bool isStreaming() {
return m_streaming;
}

/**
* Returns the MIME type for a given filename
* @param filename to return the MIME type
* @return a MIME type
*/
std::string getMimeType(const std::string &filename) const;
};

}}
Expand Down

0 comments on commit ed5251c

Please sign in to comment.