minor refactoring and Content-Type header

This commit is contained in:
Christophe Fajardo
2022-01-20 18:10:30 +01:00
parent a9bbadf8ad
commit abf2a5ea4e
3 changed files with 16 additions and 12 deletions

View File

@@ -766,10 +766,12 @@ void appasset(resp_https_t response, req_https_t request) {
print_req<SimpleWeb::HTTPS>(request);
auto args = request->parse_query_string();
auto app_image = proc::proc.get_app_image(util::from_view(args.at("appid")));
auto [ app_image, image_content_type ] = proc::proc.get_app_image(util::from_view(args.at("appid")));
std::ifstream in(app_image, std::ios::binary);
response->write(SimpleWeb::StatusCode::success_ok, in);
SimpleWeb::CaseInsensitiveMultimap headers;
headers.emplace("Content-Type", image_content_type);
response->write(SimpleWeb::StatusCode::success_ok, in, headers);
response->close_connection_after_response = true;
}

View File

@@ -190,31 +190,31 @@ std::vector<ctx_t> &proc_t::get_apps() {
return _apps;
}
#define CHECK_EXPECTED_EXTENTIONS(extention) (extention == "png" || extention == "jpg" || extention == "jpeg")
/// Gets application image from application list.
/// Returns default image if image configuration is not set.
std::string proc_t::get_app_image(int app_id) {
/// returns http content-type header compatible image type
std::tuple<std::string, std::string> proc_t::get_app_image(int app_id) {
auto app_index = app_id -1;
if(app_index < 0 || app_index >= _apps.size()) {
BOOST_LOG(error) << "Couldn't find app with ID ["sv << app_id << ']';
return SUNSHINE_ASSETS_DIR "/box.png";
return { SUNSHINE_ASSETS_DIR "/box.png", "png" };
}
auto app_image_path = _apps[app_index].image_path;
if (app_image_path.empty()) {
return SUNSHINE_ASSETS_DIR "/box.png";
return { SUNSHINE_ASSETS_DIR "/box.png", "png" };
}
auto image_extention = std::filesystem::path(app_image_path).extension().string();
image_extention = image_extention.substr(1, image_extention.length() - 1);
std::error_code code;
if (!std::filesystem::exists(app_image_path, code) || !CHECK_EXPECTED_EXTENTIONS(image_extention)) {
return SUNSHINE_ASSETS_DIR "/box.png";
if (!std::filesystem::exists(app_image_path, code) || !CHECK_EXPECTED_PICTURE_EXTENTIONS(image_extention)) {
return { SUNSHINE_ASSETS_DIR "/box.png", "png" };
}
return app_image_path;
// return only "content-type" http header compatible image type.
return { app_image_path, image_extention == "jpg" ? "jpeg" : image_extention };
}
proc_t::~proc_t() {

View File

@@ -16,6 +16,8 @@
#include "utility.h"
#define CHECK_EXPECTED_PICTURE_EXTENTIONS(extention) (extention == "png" || extention == "jpg" || extention == "jpeg")
namespace proc {
using file_t = util::safe_ptr_v2<FILE, int, fclose>;
@@ -79,7 +81,7 @@ public:
const std::vector<ctx_t> &get_apps() const;
std::vector<ctx_t> &get_apps();
std::string get_app_image(int app_id);
std::tuple<std::string, std::string> get_app_image(int app_id);
void terminate();