Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getMimeType from brewtils #33

Merged
merged 2 commits into from
Aug 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions include/expresso/core/response.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ class Response {
void sendFileInChunks(const std::string &path);
void sendToClient();

static std::string getMimeType(const std::string &path);

public:
Response(int clientSocket);
~Response();
Expand Down
2 changes: 1 addition & 1 deletion lib/brewtils
33 changes: 2 additions & 31 deletions src/core/response.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ void expresso::core::Response::sendFile(const std::string &path) {
std::string fileName = path.substr(path.find_last_of('/') + 1);
this->headers.erase("Content-Length");
this->set("Transfer-Encoding", "chunked");
this->set("Content-Type", this->getMimeType(fileName));
this->set("Content-Type", brewtils::os::file::getMimeType(fileName));
this->set("Content-Disposition", "inline; filename=\"" + fileName + "\"");
this->set("Accept-Ranges", "bytes");
std::string headers = "HTTP/1.1 " + std::to_string(this->statusCode) + "\r\n";
Expand Down Expand Up @@ -130,7 +130,7 @@ void expresso::core::Response::sendFiles(const std::set<std::string> &paths,
const std::string &zipFileName) {
this->headers.erase("Content-Length");
this->set("Transfer-Encoding", "chunked");
this->set("Content-Type", this->getMimeType(zipFileName));
this->set("Content-Type", brewtils::os::file::getMimeType(zipFileName));
this->set("Content-Disposition", "inline; filename=\"" + zipFileName + "\"");
this->set("Accept-Ranges", "bytes");
std::string headers = "HTTP/1.1 " + std::to_string(this->statusCode) + "\r\n";
Expand Down Expand Up @@ -293,32 +293,3 @@ expresso::core::Response::getAvailableFile(const std::string &path) {

return "";
}

std::string expresso::core::Response::getMimeType(const std::string &path) {
std::string extension = path.substr(path.find_last_of('.') + 1);
extension = brewtils::string::lower(extension);

// Default MIME types
for (const std::pair<const std::string, std::set<std::string>> &it :
expresso::core::Response::MIME_TYPES) {
if (it.second.find(extension) != it.second.end()) {
return it.first + "/" + extension;
}
}

// Special cases handling
if (extension == "js") {
return "text/javascript";
} else if (extension == "svg") {
return "image/svg+xml";
} else if (extension == "ico") {
return "image/x-icon";
} else if (extension == "txt") {
return "text/plain";
} else if (extension == "mp3") {
return "audio/mpeg";
}

// Default MIME type -> download
return "application/octet-stream";
}
Loading