From 6d076f03cd1156cd349ababac0d3b8b2438cb411 Mon Sep 17 00:00:00 2001 From: Vladislav Ostapov Date: Thu, 14 Nov 2024 15:42:05 +0300 Subject: [PATCH] =?UTF-8?q?=D1=81=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=20=D0=B7?= =?UTF-8?q?=D0=B0=D0=B3=D1=80=D1=83=D0=B7=D0=BA=D1=83=20=D0=BE=D0=B1=D0=BD?= =?UTF-8?q?=D0=BE=D0=B2=D0=BB=D0=B5=D0=BD=D0=BD=D0=BE=D0=B9=20=D0=BF=D1=80?= =?UTF-8?q?=D0=BE=D1=88=D0=B8=D0=B2=D0=BA=D0=B8=20=D0=B8=20=D1=81=D0=B0?= =?UTF-8?q?=D0=BC=D0=BE=20=D0=BE=D0=B1=D0=BD=D0=BE=D0=B2=D0=BB=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D1=80=D0=B0=D0=B7=D0=B4=D0=B5=D0=BB=D1=8C=D0=BD?= =?UTF-8?q?=D1=8B=D0=BC=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.cpp | 22 ++++++++++++++++++---- static/main.html | 30 ++++++++++++++++-------------- 2 files changed, 34 insertions(+), 18 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index be6b59b..066ff9e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -81,19 +81,21 @@ class ServerResources { std::unique_ptr api; http::auth::AuthProvider auth{}; - void doTerminaFwUpdate(const http::server::Request& req) { + static void onUploadFirmware(const http::server::Request& req) { std::ofstream f("/tmp/firmware.zip", std::ios::binary); if (f.is_open()) { f.write(req.payload.data(), static_cast(req.payload.size())); f.close(); - - system("do-terminal-update.sh"); } else { throw std::runtime_error("File is not open"); } } + static void doTerminalUpgrade() { + system("do-terminal-update.sh"); + } + public: static constexpr const char* INDEX_HTML = "static/main.html"; static constexpr const char* LOGIN_HTML = "static/login.html"; @@ -409,7 +411,7 @@ public: if (req.method != "PUT") { http::server::stockReply(http::server::bad_request, rep); } - this->doTerminaFwUpdate(req); + onUploadFirmware(req); rep.status = http::server::ok; rep.headers.clear(); @@ -421,6 +423,18 @@ public: result += "\"}"; rep.content.insert(rep.content.end(), result.c_str(), result.c_str() + result.size()); })); + + s.resources.emplace_back(std::make_unique("/api/doFirmwareUpgrade", this->auth, http::auth::User::UPDATE_FIRMWARE, [this](const auto& req, auto& rep) { + if (req.method != "POST") { + http::server::stockReply(http::server::bad_request, rep); + } + doTerminalUpgrade(); + rep.status = http::server::ok; + rep.headers.clear(); + rep.headers.push_back({.name = "Content-Type", .value = toString(mime_types::json)}); + const auto result = api->loadFirmwareVersion(); + rep.content.insert(rep.content.end(), result.c_str(), result.c_str() + result.size()); + })); } ~ServerResources() = default; diff --git a/static/main.html b/static/main.html index 9b50db1..865e54f 100644 --- a/static/main.html +++ b/static/main.html @@ -629,7 +629,8 @@ SHA256: {{ uploadFw.sha256 }} - + +