~aleteoryx/muditaos

ref: bff70a4ab3b0cc02bd10d5914b91752155a0be91 muditaos/module-services/service-desktop/endpoints/developerMode/fs/FS_Helper.cpp -rw-r--r-- 3.4 KiB
bff70a4a — Przemyslaw Brudny [BH-1029] Refactored Time widgets with colon and content change 4 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
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
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include <endpoints/developerMode/fs/FS_Helper.hpp>

#include <log/log.hpp>
#include <service-desktop/Constants.hpp>
#include <service-desktop/DeveloperModeMessage.hpp>
#include <endpoints/message/Sender.hpp>

#include <filesystem>
#include <system_error>

namespace sdesktop::endpoints
{
    using sender::putToSendQueue;

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

    auto FS_Helper::processGet(Context &context) -> ProcessResult
    {
        const auto &body = context.getBody();
        if (body[json::fs::listDir].is_string()) {
            const auto &dir = body[json::fs::listDir].string_value();
            auto response   = requestListDir(dir);
            return {sent::no, std::move(response)};
        }
        else {
            return {sent::no, ResponseContext{.status = http::Code::BadRequest}};
        }
    }

    auto FS_Helper::processPut(Context &context) -> ProcessResult
    {
        auto body = context.getBody();
        auto code = http::Code::BadRequest;
        if (body[json::fs::removeFile].is_string()) {
            const auto &fileName = body[json::fs::removeFile].string_value();
            try {
                code = requestFileRemoval(fileName) ? http::Code::NoContent : http::Code::NotFound;
            }
            catch (const std::filesystem::filesystem_error &) {
                LOG_WARN("Can't remove requested file");
                code = http::Code::InternalServerError;
            }
        }
        else if (body[json::fs::renameFile].is_string()) {
            const auto &fileName = body[json::fs::renameFile].string_value();
            if (body[json::fs::destFileName].is_string()) {
                const auto &destFileName = body[json::fs::destFileName].string_value();
                code = requestFileRename(fileName, destFileName) ? http::Code::NoContent : http::Code::NotFound;
            }
            else {
                code = http::Code::NotFound;
            }
        }
        else {
            context.setResponseStatus(http::Code::BadRequest);
            putToSendQueue(context.createSimpleResponse());
        }

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

    bool FS_Helper::requestFileRemoval(const std::string &fileName)
    {
        return std::filesystem::remove(fileName);
    }

    bool FS_Helper::requestFileRename(const std::string &fileName, const std::string &destFileName) noexcept
    {
        std::error_code ec;
        std::filesystem::rename(fileName, destFileName, ec);
        return (!ec) ? true : false;
    }

    ResponseContext FS_Helper::requestListDir(const std::string &directory)
    {
        std::vector<std::string> filesInDir;
        for (const auto &entry : std::filesystem::directory_iterator{directory}) {
            filesInDir.push_back(entry.path());
        }
        json11::Json::array jsonArr;
        jsonArr.reserve(filesInDir.size());
        for (const auto &path : filesInDir) {
            jsonArr.push_back(json11::Json::object{{json::fs::path, path}});
        }
        json11::Json::object response({{directory, jsonArr}});
        return ResponseContext{.status = http::Code::OK, .body = response};
    }

} // namespace sdesktop::endpoints