send image as a binary stream

This commit is contained in:
Christophe Fajardo
2022-01-20 14:31:16 +01:00
parent 67f35dfc5f
commit 4a50fcafd8
2 changed files with 15 additions and 16 deletions

View File

@@ -769,31 +769,23 @@ void appasset(resp_https_t response, req_https_t request) {
auto args = request->parse_query_string();
auto appid = util::from_view(args.at("appid")) - 1;
auto app_image = proc::proc.get_app_image(appid);
BOOST_LOG(debug) << "/appasset: ["sv << app_image << "] -- image"sv;
if (app_image.empty()) {
app_image = "steam.png";
app_image = "box.png";
}
auto file_path = SUNSHINE_ASSETS_DIR "/" + app_image;
BOOST_LOG(debug) << "/appasset: ["sv << file_path << "] -- image path"sv;
// check if files exists
std::error_code code;
auto file_path = SUNSHINE_ASSETS_DIR "/" + app_image;
auto image_extention = std::filesystem::path(file_path).extension().string();
image_extention = image_extention.substr(1, image_extention.length() - 1);
std::error_code code;
if (!std::filesystem::exists(file_path, code) || !CHECK_EXPECTED_EXTENTIONS(image_extention)) {
BOOST_LOG(debug) << "/appasset: ["sv << file_path << "] [extention="sv << image_extention << "] -- file not found"sv;
response->write(SimpleWeb::StatusCode::client_error_not_found);
return;
}
SimpleWeb::CaseInsensitiveMultimap header;
header.emplace("Content-Type", "image/" + image_extention);
std::ifstream in(file_path);
response->write(SimpleWeb::StatusCode::success_ok, in, header);
std::ifstream in(file_path, std::ios::binary);
response->write(SimpleWeb::StatusCode::success_ok, in);
response->close_connection_after_response = true;
BOOST_LOG(debug) << "/appasset: ["sv << file_path << "] [extention="sv << image_extention << "] -- sent"sv;
}
void start() {

View File

@@ -189,12 +189,19 @@ std::vector<ctx_t> &proc_t::get_apps() {
return _apps;
}
/// 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) {
if(app_id < 0 || app_id >= _apps.size()) {
BOOST_LOG(error) << "Couldn't find app with ID ["sv << app_id << ']';
return {};
return "box.png";
}
return _apps[app_id].image;
auto app_image = _apps[app_id].image;
if (app_image.empty()) {
return "box.png";
}
return app_image;
}
proc_t::~proc_t() {