summaryrefslogtreecommitdiff
path: root/include/image_upload.hpp
blob: 2b84db8a334f953aadfa0d5cb5008d9eace824ee (plain)
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#pragma once

#include <crow/app.h>

#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <cstdio>
#include <dbus_singleton.hpp>
#include <fstream>
#include <memory>

namespace crow
{
namespace image_upload
{

std::unique_ptr<sdbusplus::bus::match::match> fwUpdateMatcher;

inline void uploadImageHandler(const crow::Request& req, crow::Response& res,
                               const std::string& filename)
{
    // Only allow one FW update at a time
    if (fwUpdateMatcher != nullptr)
    {
        res.addHeader("Retry-After", "30");
        res.result(boost::beast::http::status::service_unavailable);
        res.end();
        return;
    }
    // Make this const static so it survives outside this method
    static boost::asio::deadline_timer timeout(*req.ioService,
                                               boost::posix_time::seconds(5));

    timeout.expires_from_now(boost::posix_time::seconds(5));

    timeout.async_wait([&res](const boost::system::error_code& ec) {
        fwUpdateMatcher = nullptr;
        if (ec == asio::error::operation_aborted)
        {
            // expected, we were canceled before the timer completed.
            return;
        }
        BMCWEB_LOG_ERROR << "Timed out waiting for log event";

        if (ec)
        {
            BMCWEB_LOG_ERROR << "Async_wait failed " << ec;
            return;
        }

        res.result(boost::beast::http::status::internal_server_error);
        res.end();
    });

    std::function<void(sdbusplus::message::message&)> callback =
        [&res](sdbusplus::message::message& m) {
            BMCWEB_LOG_DEBUG << "Match fired";
            boost::system::error_code ec;
            timeout.cancel(ec);
            if (ec)
            {
                BMCWEB_LOG_ERROR << "error canceling timer " << ec;
            }
            std::string versionInfo;
            m.read(
                versionInfo); // Read in the object path that was just created

            std::size_t index = versionInfo.rfind('/');
            if (index != std::string::npos)
            {
                versionInfo.erase(0, index);
            }
            res.jsonValue = {{"data", std::move(versionInfo)},
                             {"message", "200 OK"},
                             {"status", "ok"}};
            BMCWEB_LOG_DEBUG << "ending response";
            res.end();
            fwUpdateMatcher = nullptr;
        };
    fwUpdateMatcher = std::make_unique<sdbusplus::bus::match::match>(
        *crow::connections::systemBus,
        "interface='org.freedesktop.DBus.ObjectManager',type='signal',"
        "member='InterfacesAdded',path='/xyz/openbmc_project/logging'",
        callback);

    std::string filepath(
        "/tmp/images/" +
        boost::uuids::to_string(boost::uuids::random_generator()()));
    BMCWEB_LOG_DEBUG << "Writing file to " << filepath;
    std::ofstream out(filepath, std::ofstream::out | std::ofstream::binary |
                                    std::ofstream::trunc);
    out << req.body;
    out.close();
}

template <typename... Middlewares> void requestRoutes(Crow<Middlewares...>& app)
{
    BMCWEB_ROUTE(app, "/upload/image/<str>")
        .methods("POST"_method,
                 "PUT"_method)([](const crow::Request& req, crow::Response& res,
                                  const std::string& filename) {
            uploadImageHandler(req, res, filename);
        });

    BMCWEB_ROUTE(app, "/upload/image")
        .methods("POST"_method, "PUT"_method)(
            [](const crow::Request& req, crow::Response& res) {
                uploadImageHandler(req, res, "");
            });
}
} // namespace image_upload
} // namespace crow