fully working backend image posters

This commit is contained in:
Christophe Fajardo
2022-01-20 15:55:06 +01:00
parent 4a50fcafd8
commit 38dcdcba2f
6 changed files with 28 additions and 29 deletions

View File

@@ -13,7 +13,8 @@
"name":"Steam BigPicture",
"output":"steam.txt",
"detached":["setsid steam steam://open/bigpicture"]
"detached":["setsid steam steam://open/bigpicture"],
"image-path":"./asset/steam.png"
}
]
}

View File

@@ -8,7 +8,7 @@
"output":"steam.txt",
"detached":["steam steam://open/bigpicture"],
"image":"steam.png"
"image_path":"./asset/steam.png"
}
]
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 46 KiB

View File

@@ -761,29 +761,14 @@ void cancel(resp_https_t response, req_https_t request) {
}
}
#define CHECK_EXPECTED_EXTENTIONS(extention) (extention == "png" || extention == "jpg" || extention == "jpeg")
void appasset(resp_https_t response, req_https_t request) {
print_req<SimpleWeb::HTTPS>(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);
if (app_image.empty()) {
app_image = "box.png";
}
auto app_image = proc::proc.get_app_image(util::from_view(args.at("appid")));
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)) {
response->write(SimpleWeb::StatusCode::client_error_not_found);
return;
}
std::ifstream in(file_path, std::ios::binary);
std::ifstream in(app_image, std::ios::binary);
response->write(SimpleWeb::StatusCode::success_ok, in);
response->close_connection_after_response = true;
}

View File

@@ -8,6 +8,7 @@
#include <string>
#include <vector>
#include <filesystem>
#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/ptree.hpp>
@@ -189,19 +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) {
if(app_id < 0 || app_id >= _apps.size()) {
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 "box.png";
return SUNSHINE_ASSETS_DIR "/box.png";
}
auto app_image = _apps[app_id].image;
if (app_image.empty()) {
return "box.png";
auto app_image_path = _apps[app_index].image_path;
if (app_image_path.empty()) {
return SUNSHINE_ASSETS_DIR "/box.png";
}
return app_image;
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";
}
return app_image_path;
}
proc_t::~proc_t() {
@@ -294,7 +307,7 @@ std::optional<proc::proc_t> parse(const std::string &file_name) {
auto output = app_node.get_optional<std::string>("output"s);
auto name = parse_env_val(this_env, app_node.get<std::string>("name"s));
auto cmd = app_node.get_optional<std::string>("cmd"s);
auto image = app_node.get_optional<std::string>("image"s);
auto image_path = app_node.get_optional<std::string>("image-path"s);
auto working_dir = app_node.get_optional<std::string>("working-dir"s);
std::vector<proc::cmd_t> prep_cmds;
@@ -337,8 +350,8 @@ std::optional<proc::proc_t> parse(const std::string &file_name) {
ctx.working_dir = parse_env_val(this_env, *working_dir);
}
if (image) {
ctx.image = parse_env_val(this_env, *image);
if (image_path) {
ctx.image_path = parse_env_val(this_env, *image_path);
}
ctx.name = std::move(name);

View File

@@ -55,7 +55,7 @@ struct ctx_t {
std::string cmd;
std::string working_dir;
std::string output;
std::string image;
std::string image_path;
};
class proc_t {