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
8 changed files with 69 additions and 5 deletions
+8 -2
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;
}
+34
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);
+2
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();