~aleteoryx/muditaos

ref: 3887c43ca51732e2c32b2ce35215e2bba11645d4 muditaos/module-services/service-desktop/endpoints/update/UpdateHelper.cpp -rw-r--r-- 1.9 KiB
3887c43c — Lefucjusz [CP-1734] Create factory reset reboot UI 3 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include <endpoints/Context.hpp>
#include <endpoints/JsonKeyNames.hpp>
#include <endpoints/update/UpdateHelper.hpp>
#include <log/log.hpp>
#include <SystemManager/SystemManagerCommon.hpp>
#include <purefs/filesystem_paths.hpp>

#include <filesystem>

namespace sdesktop::endpoints
{

    auto constexpr updatePackageFile = "update.tar";

    void UpdateHelper::preProcess(http::Method method, Context &context)
    {
        LOG_INFO("In UpdateHelper - requesting %d", static_cast<int>(method));
    }

    auto UpdateHelper::processPost(Context &context) -> ProcessResult
    {
        const auto &body = context.getBody();

        if (!(body[json::update] == true && body[json::reboot] == true)) {
            return {sent::no, ResponseContext{.status = http::Code::BadRequest}};
        }

        if (!std::filesystem::exists(purefs::dir::getUserDiskPath() / updatePackageFile)) {
            return {sent::no, ResponseContext{.status = http::Code::NotFound}};
        }

        if (sys::SystemManagerCommon::RebootToRecovery(owner, sys::RecoveryReason::Update)) {
            return {sent::no, ResponseContext{.status = http::Code::NoContent}};
        }

        return {sent::no, ResponseContext{.status = http::Code::InternalServerError}};
    }

    auto UpdateHelper::processPut(Context &context) -> ProcessResult
    {
        const auto &body = context.getBody();
        auto code        = http::Code::BadRequest;
        if (body[json::rebootMode] == json::usbMscMode) {
            code = sys::SystemManagerCommon::RebootToUsbMscMode(owner) ? http::Code::NoContent
                                                                       : http::Code::InternalServerError;
        }

        return {sent::no, ResponseContext{.status = code}};
    }

} // namespace sdesktop::endpoints