сделал загрузку обновленной прошивки и само обновление раздельными
This commit is contained in:
22
src/main.cpp
22
src/main.cpp
@@ -81,19 +81,21 @@ class ServerResources {
|
||||
std::unique_ptr<api_driver::ApiDriver> 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<long>(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<http::auth::AuthRequiredResource>("/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;
|
||||
|
Reference in New Issue
Block a user