Merge pull request #36 from cfajardo/add-application-image

Add application image
This commit is contained in:
ReenigneArcher 2022-02-06 16:44:43 -05:00 committed by GitHub
commit 067214a9f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 69 additions and 5 deletions

3
NOTICE Normal file
View File

@ -0,0 +1,3 @@
©2018 Valve Corporation. Steam and the Steam logo are trademarks and/or
registered trademarks of Valve Corporation in the U.S. and/or other countries. All
rights reserved.

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":"./assets/steam.png"
}
]
}

View File

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

BIN
assets/steam.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

View File

@ -166,6 +166,21 @@
If not set, Sunshine will default to the parent directory of the command
</div>
</div>
<!-- Image path -->
<div class="mb-3">
<label for="appImagePath" class="form-label">Image</label>
<input
type="text"
class="form-control monospace"
id="appImagePath"
aria-describedby="appImagePathHelp"
v-model="editForm['image-path']"
/>
<div id="appImagePathHelp" class="form-text">
Application icon/picture/image path that will be sent to client. Image must be a PNG file.
If not set, Sunshine will send default box image.
</div>
</div>
<!--buttons-->
<div class="d-flex">
<button @click="showEditForm = false" class="btn btn-secondary m-2">
@ -208,6 +223,7 @@
index: -1,
"prep-cmd": [],
detached: [],
"image-path": ""
};
this.editForm.index = -1;
this.showEditForm = true;
@ -238,6 +254,7 @@
});
},
save() {
this.editForm["image-path"] = this.editForm["image-path"].toString().replace(/"/g, '');
fetch("/api/apps", {
method: "POST",
body: JSON.stringify(this.editForm),
@ -257,4 +274,4 @@
.monospace {
font-family: monospace;
}
</style>
</style>

View File

@ -761,11 +761,17 @@ void cancel(resp_https_t response, req_https_t request) {
}
}
void appasset(resp_https_t response, req_https_t request) {
print_req<SimpleWeb::HTTPS>(request);
std::ifstream in(SUNSHINE_ASSETS_DIR "/box.png");
response->write(SimpleWeb::StatusCode::success_ok, in);
auto args = request->parse_query_string();
auto app_image = proc::proc.get_app_image(util::from_view(args.at("appid")));
std::ifstream in(app_image, std::ios::binary);
SimpleWeb::CaseInsensitiveMultimap headers;
headers.emplace("Content-Type", "image/png");
response->write(SimpleWeb::StatusCode::success_ok, in, headers);
response->close_connection_after_response = true;
}

View File

@ -8,10 +8,12 @@
#include <string>
#include <vector>
#include <filesystem>
#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/filesystem.hpp>
#include <boost/algorithm/string.hpp>
#include "main.h"
#include "utility.h"
@ -189,6 +191,33 @@ 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.
/// returns http content-type header compatible image type
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";
}
auto app_image_path = _apps[app_index].image_path;
if (app_image_path.empty()) {
return SUNSHINE_ASSETS_DIR "/box.png";
}
auto image_extension = std::filesystem::path(app_image_path).extension().string();
boost::to_lower(image_extension);
std::error_code code;
if (!std::filesystem::exists(app_image_path, code) || image_extension != ".png") {
return SUNSHINE_ASSETS_DIR "/box.png";
}
// return only "content-type" http header compatible image type.
return app_image_path;
}
proc_t::~proc_t() {
terminate();
}
@ -279,6 +308,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_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;
@ -321,6 +351,10 @@ std::optional<proc::proc_t> parse(const std::string &file_name) {
ctx.working_dir = parse_env_val(this_env, *working_dir);
}
if (image_path) {
ctx.image_path = parse_env_val(this_env, *image_path);
}
ctx.name = std::move(name);
ctx.prep_cmds = std::move(prep_cmds);
ctx.detached = std::move(detached);

View File

@ -55,6 +55,7 @@ struct ctx_t {
std::string cmd;
std::string working_dir;
std::string output;
std::string image_path;
};
class proc_t {
@ -78,6 +79,7 @@ public:
const std::vector<ctx_t> &get_apps() const;
std::vector<ctx_t> &get_apps();
std::string get_app_image(int app_id);
void terminate();