Create management portal in Flutter (#40)
All checks were successful
BlockStorage/repertory/pipeline/head This commit looks good

Reviewed-on: #40
This commit is contained in:
Scott E. Graves 2025-03-03 19:56:56 -06:00
parent c59c846856
commit d9cd2aa88a
42 changed files with 2252 additions and 84 deletions

View File

@ -154,8 +154,10 @@ mtune
musl-libc
nana
ncrypt
nlohmann
nlohmann_json
nmakeprg
nohup
nominmax
ntstatus
nullptr

View File

@ -19,6 +19,8 @@ PROJECT_APP_LIST=(${PROJECT_NAME})
PROJECT_PRIVATE_KEY=${DEVELOPER_PRIVATE_KEY}
PROJECT_PUBLIC_KEY=${DEVELOPER_PUBLIC_KEY}
PROJECT_FLUTTER_BASE_HREF="/ui/"
PROJECT_ENABLE_WIN32_LONG_PATH_NAMES=OFF
PROJECT_ENABLE_BACKWARD_CPP=OFF

View File

@ -52,6 +52,8 @@ public:
[[nodiscard]] static auto get_provider_name(const provider_type &prov)
-> std::string;
[[nodiscard]] static auto get_root_data_directory() -> std::string;
public:
[[nodiscard]] static auto get_stop_requested() -> bool;

View File

@ -0,0 +1,82 @@
/*
Copyright <2018-2025> <scott.e.graves@protonmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef REPERTORY_INCLUDE_RPC_COMMON_HPP_
#define REPERTORY_INCLUDE_RPC_COMMON_HPP_
#include "utils/base64.hpp"
#include "utils/error_utils.hpp"
#include "utils/string.hpp"
namespace repertory::rpc {
[[nodiscard]] auto check_authorization(const auto &cfg,
const httplib::Request &req) -> bool {
REPERTORY_USES_FUNCTION_NAME();
if (cfg.get_api_auth().empty() || cfg.get_api_user().empty()) {
utils::error::raise_error(function_name,
"authorization user or password is not set");
return false;
}
auto authorization = req.get_header_value("Authorization");
if (authorization.empty()) {
utils::error::raise_error(function_name,
"'Authorization' header is not set");
return false;
}
auto auth_parts = utils::string::split(authorization, ' ', true);
if (auth_parts.empty()) {
utils::error::raise_error(function_name, "'Authorization' header is empty");
return false;
}
auto auth_type = auth_parts[0U];
if (auth_type != "Basic") {
utils::error::raise_error(function_name,
"authorization type is not 'Basic'");
return false;
}
auto data = macaron::Base64::Decode(authorization.substr(6U));
auto auth_str = std::string(data.begin(), data.end());
auto auth = utils::string::split(auth_str, ':', false);
if (auth.size() < 2U) {
utils::error::raise_error(function_name, "authorization data is not valid");
return false;
}
auto user = auth.at(0U);
auth.erase(auth.begin());
auto pwd = utils::string::join(auth, ':');
if ((user != cfg.get_api_user()) || (pwd != cfg.get_api_auth())) {
utils::error::raise_error(function_name, "authorization failed");
return false;
}
return true;
}
} // namespace repertory::rpc
#endif // REPERTORY_INCLUDE_RPC_COMMON_HPP_

View File

@ -40,8 +40,6 @@ private:
std::mutex start_stop_mutex_;
private:
[[nodiscard]] auto check_authorization(const httplib::Request &req) -> bool;
void handle_get_config(const httplib::Request &req, httplib::Response &res);
void handle_get_config_value_by_name(const httplib::Request &req,

View File

@ -38,6 +38,7 @@ constexpr const auto default_retry_read_count{6U};
constexpr const auto default_ring_buffer_file_size{512U};
constexpr const auto default_task_wait_ms{100U};
constexpr const auto default_timeout_ms{60000U};
constexpr const auto default_ui_mgmt_port{std::uint16_t{30000U}};
constexpr const auto max_ring_buffer_file_size{std::uint16_t(1024U)};
constexpr const auto max_s3_object_name_length{1024U};
constexpr const auto min_cache_size_bytes{
@ -280,6 +281,8 @@ enum class exit_code : std::int32_t {
pin_failed = -16,
unpin_failed = -17,
init_failed = -18,
ui_mount_failed = -19,
exception = -20,
};
enum http_error_codes : std::int32_t {
@ -304,6 +307,13 @@ enum class provider_type : std::size_t {
unknown,
};
[[nodiscard]] auto
provider_type_from_string(std::string_view type,
provider_type default_type = provider_type::unknown)
-> provider_type;
[[nodiscard]] auto provider_type_to_string(provider_type type) -> std::string;
#if defined(_WIN32)
struct open_file_data final {
PVOID directory_buffer{nullptr};
@ -487,6 +497,7 @@ inline constexpr const auto JSON_MAX_UPLOAD_COUNT{"MaxUploadCount"};
inline constexpr const auto JSON_MED_FREQ_INTERVAL_SECS{
"MedFreqIntervalSeconds"};
inline constexpr const auto JSON_META{"Meta"};
inline constexpr const auto JSON_MOUNT_LOCATIONS{"MountLocations"};
inline constexpr const auto JSON_ONLINE_CHECK_RETRY_SECS{
"OnlineCheckRetrySeconds"};
inline constexpr const auto JSON_PATH{"Path"};

View File

@ -49,6 +49,8 @@ static const option password_option = {"-pw", "--password"};
static const option remote_mount_option = {"-rm", "--remote_mount"};
static const option set_option = {"-set", "--set"};
static const option status_option = {"-status", "--status"};
static const option ui_option = {"-ui", "--ui"};
static const option ui_port_option = {"-up", "--ui_port"};
static const option unmount_option = {"-unmount", "--unmount"};
static const option unpin_file_option = {"-uf", "--unpin_file"};
static const option user_option = {"-us", "--user"};
@ -75,6 +77,8 @@ static const std::vector<option> option_list = {
remote_mount_option,
set_option,
status_option,
ui_option,
ui_port_option,
unmount_option,
unpin_file_option,
user_option,
@ -87,26 +91,27 @@ void get_api_authentication_data(std::string &user, std::string &password,
std::uint16_t &port, const provider_type &prov,
const std::string &data_directory);
[[nodiscard]] auto
get_provider_type_from_args(std::vector<const char *> args) -> provider_type;
[[nodiscard]] auto get_provider_type_from_args(std::vector<const char *> args)
-> provider_type;
[[nodiscard]] auto has_option(std::vector<const char *> args,
const std::string &option_name) -> bool;
[[nodiscard]] auto has_option(std::vector<const char *> args,
const option &opt) -> bool;
[[nodiscard]] auto has_option(std::vector<const char *> args, const option &opt)
-> bool;
[[nodiscard]] auto parse_option(std::vector<const char *> args,
const std::string &option_name,
std::uint8_t count) -> std::vector<std::string>;
[[nodiscard]] auto parse_string_option(std::vector<const char *> args,
const option &opt,
std::string &value) -> exit_code;
const option &opt, std::string &value)
-> exit_code;
[[nodiscard]] auto
parse_drive_options(std::vector<const char *> args, provider_type &prov,
std::string &data_directory) -> std::vector<std::string>;
[[nodiscard]] auto parse_drive_options(std::vector<const char *> args,
provider_type &prov,
std::string &data_directory)
-> std::vector<std::string>;
} // namespace repertory::utils::cli
#endif // REPERTORY_INCLUDE_UTILS_CLI_UTILS_HPP_

View File

@ -699,36 +699,37 @@ auto app_config::default_api_port(const provider_type &prov) -> std::uint16_t {
return PROVIDER_API_PORTS.at(static_cast<std::size_t>(prov));
}
auto app_config::default_data_directory(const provider_type &prov)
-> std::string {
auto app_config::get_root_data_directory() -> std::string {
#if defined(_WIN32)
auto data_directory =
utils::path::combine(utils::get_local_app_data_directory(),
{
REPERTORY_DATA_NAME,
app_config::get_provider_name(prov),
});
auto data_directory = utils::path::combine(
utils::get_local_app_data_directory(), {
REPERTORY_DATA_NAME,
});
#else // !defined(_WIN32)
#if defined(__APPLE__)
auto data_directory =
utils::path::combine("~", {
"Library",
"Application Support",
REPERTORY_DATA_NAME,
app_config::get_provider_name(prov),
});
auto data_directory = utils::path::combine("~", {
"Library",
"Application Support",
REPERTORY_DATA_NAME,
});
#else // !defined(__APPLE__)
auto data_directory =
utils::path::combine("~", {
".local",
REPERTORY_DATA_NAME,
app_config::get_provider_name(prov),
});
auto data_directory = utils::path::combine("~", {
".local",
REPERTORY_DATA_NAME,
});
#endif // defined(__APPLE__)
#endif // defined(_WIN32)
return data_directory;
}
auto app_config::default_data_directory(const provider_type &prov)
-> std::string {
return utils::path::combine(app_config::get_root_data_directory(),
{
app_config::get_provider_name(prov),
});
}
auto app_config::default_remote_api_port(const provider_type &prov)
-> std::uint16_t {
static const std::array<std::uint16_t,
@ -741,6 +742,7 @@ auto app_config::default_remote_api_port(const provider_type &prov)
};
return PROVIDER_REMOTE_PORTS.at(static_cast<std::size_t>(prov));
}
auto app_config::default_rpc_port(const provider_type &prov) -> std::uint16_t {
static const std::array<std::uint16_t,
static_cast<std::size_t>(provider_type::unknown)>

View File

@ -28,6 +28,7 @@
#include "events/types/service_stop_begin.hpp"
#include "events/types/service_stop_end.hpp"
#include "events/types/unmount_requested.hpp"
#include "rpc/common.hpp"
#include "utils/base64.hpp"
#include "utils/error_utils.hpp"
#include "utils/string.hpp"
@ -35,54 +36,6 @@
namespace repertory {
server::server(app_config &config) : config_(config) {}
auto server::check_authorization(const httplib::Request &req) -> bool {
REPERTORY_USES_FUNCTION_NAME();
if (config_.get_api_auth().empty() || config_.get_api_user().empty()) {
utils::error::raise_error(function_name,
"authorization user or password is not set");
return false;
}
auto authorization = req.get_header_value("Authorization");
if (authorization.empty()) {
utils::error::raise_error(function_name, "Authorization header is not set");
return false;
}
auto auth_parts = utils::string::split(authorization, ' ', true);
if (auth_parts.empty()) {
utils::error::raise_error(function_name, "Authorization header is empty");
return false;
}
auto auth_type = auth_parts[0U];
if (auth_type != "Basic") {
utils::error::raise_error(function_name, "Authorization is not Basic");
return false;
}
auto data = macaron::Base64::Decode(authorization.substr(6U));
auto auth_str = std::string(data.begin(), data.end());
auto auth = utils::string::split(auth_str, ':', false);
if (auth.size() < 2U) {
utils::error::raise_error(function_name, "Authorization is not valid");
return false;
}
auto user = auth.at(0U);
auth.erase(auth.begin());
auto pwd = utils::string::join(auth, ':');
if ((user != config_.get_api_user()) || (pwd != config_.get_api_auth())) {
utils::error::raise_error(function_name, "Authorization failed");
return false;
}
return true;
}
void server::handle_get_config(const httplib::Request & /*req*/,
httplib::Response &res) {
auto data = config_.get_json();
@ -173,7 +126,7 @@ void server::start() {
server_->set_pre_routing_handler(
[this](auto &&req, auto &&res) -> httplib::Server::HandlerResponse {
if (check_authorization(req)) {
if (rpc::check_authorization(config_, req)) {
return httplib::Server::HandlerResponse::Unhandled;
}

View File

@ -21,6 +21,7 @@
*/
#include "types/repertory.hpp"
#include "app_config.hpp"
#include "types/startup_exception.hpp"
#include "utils/string.hpp"
@ -191,4 +192,34 @@ auto api_error_to_string(const api_error &error) -> const std::string & {
return LOOKUP.at(error);
}
auto provider_type_from_string(std::string_view type,
provider_type default_type) -> provider_type {
auto type_lower = utils::string::to_lower(std::string{type});
if (type_lower == "encrypt") {
return provider_type::encrypt;
}
if (type_lower == "remote") {
return provider_type::remote;
}
if (type_lower == "s3") {
return provider_type::s3;
}
if (type_lower == "sia") {
return provider_type::sia;
}
if (type_lower == "unknown") {
return provider_type::unknown;
}
return default_type;
}
auto provider_type_to_string(provider_type type) -> std::string {
return app_config::get_provider_name(type);
}
} // namespace repertory

View File

@ -36,6 +36,7 @@
#include "cli/pinned_status.hpp"
#include "cli/set.hpp"
#include "cli/status.hpp"
#include "cli/ui.hpp"
#include "cli/unmount.hpp"
#include "cli/unpin_file.hpp"
#include "utils/cli_utils.hpp"
@ -70,6 +71,7 @@ static const std::unordered_map<utils::cli::option, action, option_hasher>
cli::actions::pinned_status},
{utils::cli::options::set_option, cli::actions::set},
{utils::cli::options::status_option, cli::actions::status},
{utils::cli::options::ui_option, cli::actions::ui},
{utils::cli::options::unmount_option, cli::actions::unmount},
{utils::cli::options::unpin_file_option, cli::actions::unpin_file},
};

View File

@ -79,6 +79,12 @@ template <typename drive> inline void help(std::vector<const char *> args) {
<< std::endl;
std::cout << " -status Display mount status"
<< std::endl;
std::cout
<< " -ui,--ui Run embedded management UI"
<< std::endl;
std::cout << " -up,--ui_port Custom port for embedded "
"management UI"
<< std::endl;
std::cout << " -unmount,--unmount Unmount and shutdown"
<< std::endl;
std::cout << " -uf,--unpin_file [API path] Unpin a file from cache "

View File

@ -0,0 +1,63 @@
/*
Copyright <2018-2025> <scott.e.graves@protonmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef REPERTORY_INCLUDE_CLI_UI_HPP_
#define REPERTORY_INCLUDE_CLI_UI_HPP_
#include "types/repertory.hpp"
#include "ui/handlers.hpp"
#include "ui/mgmt_app_config.hpp"
#include "utils/cli_utils.hpp"
#include "utils/file.hpp"
#include "utils/string.hpp"
namespace repertory::cli::actions {
[[nodiscard]] inline auto
ui(std::vector<const char *> args, const std::string & /*data_directory*/,
const provider_type & /* prov */, const std::string & /* unique_id */,
std::string /* user */, std::string /* password */) -> exit_code {
auto ui_port{default_ui_mgmt_port};
std::string data;
auto res = utils::cli::parse_string_option(
args, utils::cli::options::ui_port_option, data);
if (res == exit_code::success && not data.empty()) {
ui_port = utils::string::to_uint16(data);
}
if (not utils::file::change_to_process_directory()) {
return exit_code::ui_mount_failed;
}
httplib::Server server;
if (not server.set_mount_point("/ui", "./web")) {
return exit_code::ui_mount_failed;
}
ui::mgmt_app_config config{};
config.set_api_port(ui_port);
ui::handlers handlers(&config, &server);
return exit_code::success;
}
} // namespace repertory::cli::actions
#endif // REPERTORY_INCLUDE_CLI_UI_HPP_

View File

@ -0,0 +1,70 @@
/*
Copyright <2018-2025> <scott.e.graves@protonmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef REPERTORY_INCLUDE_UI_HANDLERS_HPP_
#define REPERTORY_INCLUDE_UI_HANDLERS_HPP_
#include "events/consumers/console_consumer.hpp"
namespace repertory::ui {
class mgmt_app_config;
class handlers final {
public:
handlers(mgmt_app_config *config, httplib::Server *server);
handlers() = delete;
handlers(const handlers &) = delete;
handlers(handlers &&) = delete;
~handlers();
auto operator=(const handlers &) -> handlers & = delete;
auto operator=(handlers &&) -> handlers & = delete;
private:
mgmt_app_config *config_;
std::string repertory_binary_;
httplib::Server *server_;
private:
console_consumer console;
private:
void handle_get_mount(auto &&req, auto &&res) const;
void handle_get_mount_list(auto &&res) const;
void handle_get_mount_location(auto &&req, auto &&res) const;
void handle_get_mount_status(auto &&req, auto &&res) const;
void handle_post_mount(auto &&req, auto &&res) const;
void handle_put_set_value_by_name(auto &&req, auto &&res);
auto launch_process(provider_type prov, std::string_view name,
std::string_view args, bool background = false) const
-> std::vector<std::string>;
};
} // namespace repertory::ui
#endif // REPERTORY_INCLUDE_UI_HANDLERS_HPP_

View File

@ -0,0 +1,62 @@
/*
Copyright <2018-2025> <scott.e.graves@protonmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef REPERTORY_INCLUDE_UI_MGMT_APP_CONFIG_HPP_
#define REPERTORY_INCLUDE_UI_MGMT_APP_CONFIG_HPP_
#include "types/repertory.hpp"
namespace repertory::ui {
class mgmt_app_config final {
public:
mgmt_app_config();
private:
atomic<std::string> api_auth_{"test"};
std::atomic<std::uint16_t> api_port_{default_ui_mgmt_port};
atomic<std::string> api_user_{"test"};
std::unordered_map<provider_type,
std::unordered_map<std::string, std::string>>
locations_;
mutable std::recursive_mutex mtx_;
private:
void save() const;
public:
[[nodiscard]] auto get_api_auth() const -> std::string { return api_auth_; }
[[nodiscard]] auto get_api_port() const -> std::uint16_t { return api_port_; }
[[nodiscard]] auto get_api_user() const -> std::string { return api_user_; }
[[nodiscard]] auto get_mount_location(provider_type prov,
std::string_view name) const
-> std::string;
void set_api_port(std::uint16_t api_port);
void set_mount_location(provider_type prov, std::string_view name,
std::string_view location);
};
} // namespace repertory::ui
#endif // REPERTORY_INCLUDE_UI_MGMT_APP_CONFIG_HPP_

View File

@ -146,10 +146,17 @@ auto main(int argc, char **argv) -> int {
(res == exit_code::option_not_found) &&
(idx < utils::cli::options::option_list.size());
idx++) {
res = cli::actions::perform_action(
utils::cli::options::option_list[idx], args, data_directory, prov,
unique_id, user, password);
try {
res = cli::actions::perform_action(
utils::cli::options::option_list[idx], args, data_directory, prov,
unique_id, user, password);
} catch (const std::exception &ex) {
res = exit_code::exception;
} catch (...) {
res = exit_code::exception;
}
}
if (res == exit_code::option_not_found) {
res = cli::actions::mount(args, data_directory, mount_result, prov,
remote_host, remote_port, unique_id);

View File

@ -0,0 +1,368 @@
/*
Copyright <2018-2025> <scott.e.graves@protonmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "ui/handlers.hpp"
#include "app_config.hpp"
#include "events/event_system.hpp"
#include "rpc/common.hpp"
#include "types/repertory.hpp"
#include "ui/mgmt_app_config.hpp"
#include "utils/error_utils.hpp"
#include "utils/file.hpp"
#include "utils/path.hpp"
#include "utils/string.hpp"
namespace {
[[nodiscard]] constexpr auto is_restricted(std::string_view data) -> bool {
constexpr std::string_view invalid_chars = "&;|><$()`{}!*?";
return data.find_first_of(invalid_chars) != std::string_view::npos;
}
} // namespace
namespace repertory::ui {
handlers::handlers(mgmt_app_config *config, httplib::Server *server)
: config_(config),
#if defined(_WIN32)
repertory_binary_(utils::path::combine(".", {"repertory.exe"})),
#else // !defined(_WIN32)
repertory_binary_(utils::path::combine(".", {"repertory"})),
#endif // defined(_WIN32)
server_(server) {
REPERTORY_USES_FUNCTION_NAME();
server_->set_pre_routing_handler(
[this](auto &&req, auto &&res) -> httplib::Server::HandlerResponse {
if (rpc::check_authorization(*config_, req)) {
return httplib::Server::HandlerResponse::Unhandled;
}
res.status = http_error_codes::unauthorized;
res.set_header(
"WWW-Authenticate",
R"(Basic realm="Repertory Management Portal", charset="UTF-8")");
return httplib::Server::HandlerResponse::Handled;
});
server_->set_exception_handler([](const httplib::Request &req,
httplib::Response &res,
std::exception_ptr ptr) {
json data{
{"path", req.path},
};
try {
std::rethrow_exception(ptr);
} catch (const std::exception &e) {
data["error"] = (e.what() == nullptr) ? "unknown error" : e.what();
utils::error::raise_error(function_name, e,
"failed request: " + req.path);
} catch (...) {
data["error"] = "unknown error";
utils::error::raise_error(function_name, "unknown error",
"failed request: " + req.path);
}
res.set_content(data.dump(), "application/json");
res.status = http_error_codes::internal_error;
});
server->Get("/api/v1/mount",
[this](auto &&req, auto &&res) { handle_get_mount(req, res); });
server->Get("/api/v1/mount_location", [this](auto &&req, auto &&res) {
handle_get_mount_location(req, res);
});
server->Get("/api/v1/mount_list", [this](auto && /* req */, auto &&res) {
handle_get_mount_list(res);
});
server->Get("/api/v1/mount_status",
[this](const httplib::Request &req, auto &&res) {
handle_get_mount_status(req, res);
});
server->Post("/api/v1/mount",
[this](auto &&req, auto &&res) { handle_post_mount(req, res); });
server->Put("/api/v1/set_value_by_name", [this](auto &&req, auto &&res) {
handle_put_set_value_by_name(req, res);
});
event_system::instance().start();
static std::atomic<httplib::Server *> this_server{server_};
static const auto quit_handler = [](int /* sig */) {
auto *ptr = this_server.load();
if (ptr == nullptr) {
return;
}
this_server = nullptr;
ptr->stop();
};
std::signal(SIGINT, quit_handler);
#if !defined(_WIN32)
std::signal(SIGQUIT, quit_handler);
#endif // !defined(_WIN32)
std::signal(SIGTERM, quit_handler);
#if defined(_WIN32)
system(fmt::format(
R"(start "Repertory Management Portal" "http://127.0.0.1:{}/ui")",
config_->get_api_port())
.c_str());
#elif defined(__linux__)
system(fmt::format(R"(xdg-open "http://127.0.0.1:{}/ui")",
config_->get_api_port())
.c_str());
#else // error
build fails here
#endif
server_->listen("127.0.0.1", config_->get_api_port());
this_server = nullptr;
}
handlers::~handlers() { event_system::instance().stop(); }
void handlers::handle_get_mount(auto &&req, auto &&res) const {
REPERTORY_USES_FUNCTION_NAME();
auto prov = provider_type_from_string(req.get_param_value("type"));
auto lines = launch_process(prov, req.get_param_value("name"), "-dc");
if (lines.at(0U) != "0") {
throw utils::error::create_exception(function_name, {
"command failed",
lines.at(0U),
});
}
lines.erase(lines.begin());
auto result = nlohmann::json::parse(utils::string::join(lines, '\n'));
res.set_content(result.dump(), "application/json");
res.status = http_error_codes::ok;
}
void handlers::handle_get_mount_list(auto &&res) const {
auto data_dir = utils::file::directory{app_config::get_root_data_directory()};
nlohmann::json result;
auto encrypt_dir = data_dir.get_directory("encrypt");
if (encrypt_dir && encrypt_dir->get_file("config.json")) {
result["encrypt"].emplace_back("encrypt");
}
const auto process_dir = [&data_dir, &result](std::string_view name) {
auto name_dir = data_dir.get_directory(name);
if (not name_dir) {
return;
}
for (const auto &dir : name_dir->get_directories()) {
if (not dir->get_file("config.json")) {
continue;
}
result[name].emplace_back(
utils::path::strip_to_file_name(dir->get_path()));
}
};
process_dir("remote");
process_dir("s3");
process_dir("sia");
res.set_content(result.dump(), "application/json");
res.status = http_error_codes::ok;
}
void handlers::handle_get_mount_location(auto &&req, auto &&res) const {
auto name = req.get_param_value("name");
auto prov = provider_type_from_string(req.get_param_value("type"));
res.set_content(
nlohmann::json({
{"Location", config_->get_mount_location(prov, name)},
})
.dump(),
"application/json");
res.status = http_error_codes::ok;
}
void handlers::handle_get_mount_status(auto &&req, auto &&res) const {
REPERTORY_USES_FUNCTION_NAME();
auto name = req.get_param_value("name");
auto prov = provider_type_from_string(req.get_param_value("type"));
auto status_name = app_config::get_provider_display_name(prov);
switch (prov) {
case provider_type::encrypt:
break;
case provider_type::remote: {
auto parts = utils::string::split(name, '_', false);
status_name = fmt::format("{}{}:{}", status_name, parts[0U], parts[1U]);
} break;
case provider_type::sia:
case provider_type::s3:
status_name = fmt::format("{}{}", status_name, name);
break;
default:
throw utils::error::create_exception(function_name,
{
"provider is not supported",
provider_type_to_string(prov),
name,
});
}
auto lines = launch_process(prov, name, "-status");
nlohmann::json result(
nlohmann::json::parse(utils::string::join(lines, '\n')).at(status_name));
if (result.at("Location").get<std::string>().empty()) {
result.at("Location") = config_->get_mount_location(prov, name);
} else if (result.at("Active").get<bool>()) {
config_->set_mount_location(prov, name,
result.at("Location").get<std::string>());
}
res.set_content(result.dump(), "application/json");
res.status = http_error_codes::ok;
}
void handlers::handle_post_mount(auto &&req, auto &&res) const {
auto location = utils::path::absolute(req.get_param_value("location"));
auto name = req.get_param_value("name");
auto prov = provider_type_from_string(req.get_param_value("type"));
auto unmount = utils::string::to_bool(req.get_param_value("unmount"));
if (unmount) {
launch_process(prov, name, "-unmount");
} else {
launch_process(prov, name, fmt::format(R"("{}")", location), true);
}
res.status = http_error_codes::ok;
}
void handlers::handle_put_set_value_by_name(auto &&req, auto &&res) {
auto key = req.get_param_value("key");
auto name = req.get_param_value("name");
auto prov = provider_type_from_string(req.get_param_value("type"));
auto value = req.get_param_value("value");
#if defined(_WIN32)
launch_process(prov, name, fmt::format(R"(-set {} "{}")", key, value));
#else //! defined(_WIN32)
launch_process(prov, name, fmt::format("-set {} '{}'", key, value));
#endif // defined(_WIN32)
res.status = http_error_codes::ok;
}
auto handlers::launch_process(provider_type prov, std::string_view name,
std::string_view args, bool background) const
-> std::vector<std::string> {
REPERTORY_USES_FUNCTION_NAME();
if (is_restricted(name) || is_restricted(args)) {
throw utils::error::create_exception(function_name,
{
"invalid data detected",
});
}
std::string str_type;
switch (prov) {
case provider_type::encrypt:
str_type = "-en";
break;
case provider_type::remote: {
auto parts = utils::string::split(name, '_', false);
str_type = fmt::format("-rm {}:{}", parts[0U], parts[1U]);
} break;
case provider_type::s3:
str_type = fmt::format("-s3 -na {}", name);
break;
case provider_type::sia:
str_type = fmt::format("-na {}", name);
break;
default:
throw utils::error::create_exception(function_name,
{
"provider is not supported",
provider_type_to_string(prov),
name,
});
}
auto cmd_line = fmt::format(R"({} {} {})", repertory_binary_, str_type, args);
if (background) {
#if defined(_WIN32)
system(fmt::format(R"(start "" /b {})", cmd_line).c_str());
#elif defined(__linux__) // defined(__linux__)
system(fmt::format("nohup {} 1>/dev/null 2>&1", cmd_line).c_str());
#else // !defined(__linux__) && !defined(_WIN32)
build fails here
#endif // defined(_WIN32)
return {};
}
auto *pipe = popen(cmd_line.c_str(), "r");
if (pipe == nullptr) {
throw utils::error::create_exception(function_name,
{
"failed to execute command",
provider_type_to_string(prov),
name,
});
}
std::string data;
std::array<char, 1024U> buffer{};
while (feof(pipe) == 0) {
while (fgets(buffer.data(), buffer.size(), pipe) != nullptr) {
data += buffer.data();
}
}
pclose(pipe);
return utils::string::split(utils::string::replace(data, "\r", ""), '\n',
false);
}
} // namespace repertory::ui

View File

@ -0,0 +1,170 @@
/*
Copyright <2018-2025> <scott.e.graves@protonmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "ui/mgmt_app_config.hpp"
#include "app_config.hpp"
#include "utils/error_utils.hpp"
#include "utils/file.hpp"
#include "utils/path.hpp"
#include "utils/unix.hpp"
#include "utils/windows.hpp"
namespace {
[[nodiscard]] auto from_json(const nlohmann::json &json)
-> std::unordered_map<repertory::provider_type,
std::unordered_map<std::string, std::string>> {
std::unordered_map<repertory::provider_type,
std::unordered_map<std::string, std::string>>
map_of_maps{
{repertory::provider_type::encrypt, {}},
{repertory::provider_type::remote, {}},
{repertory::provider_type::s3, {}},
{repertory::provider_type::sia, {}},
};
for (const auto &[prov, map] : map_of_maps) {
for (const auto &[key, value] :
json[repertory::provider_type_to_string(prov)].items()) {
if (value.is_null()) {
continue;
}
map_of_maps[prov][key] = value;
}
}
return map_of_maps;
}
[[nodiscard]] auto to_json(const auto &map_of_maps) -> nlohmann::json {
nlohmann::json json;
for (const auto &[prov, map] : map_of_maps) {
for (const auto &[key, value] : map) {
json[repertory::provider_type_to_string(prov)][key] = value;
}
}
return json;
}
} // namespace
namespace repertory::ui {
mgmt_app_config::mgmt_app_config() {
REPERTORY_USES_FUNCTION_NAME();
auto config_file =
utils::path::combine(app_config::get_root_data_directory(), {"ui.json"});
try {
nlohmann::json data;
if (utils::file::read_json_file(config_file, data)) {
api_auth_ = data.at(JSON_API_AUTH).get<std::string>();
api_port_ = data.at(JSON_API_PORT).get<std::uint16_t>();
api_user_ = data.at(JSON_API_USER).get<std::string>();
locations_ = from_json(data.at(JSON_MOUNT_LOCATIONS));
return;
}
utils::error::raise_error(
function_name, utils::get_last_error_code(),
fmt::format("failed to read file|{}", config_file));
} catch (const std::exception &ex) {
utils::error::raise_error(
function_name, ex, fmt::format("failed to read file|{}", config_file));
}
}
auto mgmt_app_config::get_mount_location(provider_type prov,
std::string_view name) const
-> std::string {
recur_mutex_lock lock(mtx_);
if (locations_.contains(prov) &&
locations_.at(prov).contains(std::string{name})) {
return locations_.at(prov).at(std::string{name});
}
return "";
}
void mgmt_app_config::save() const {
REPERTORY_USES_FUNCTION_NAME();
auto config_file =
utils::path::combine(app_config::get_root_data_directory(), {"ui.json"});
try {
if (not utils::file::directory{app_config::get_root_data_directory()}
.create_directory()) {
utils::error::raise_error(
function_name, fmt::format("failed to create directory|{}",
app_config::get_root_data_directory()));
return;
}
nlohmann::json data;
data[JSON_API_AUTH] = api_auth_;
data[JSON_API_PORT] = api_port_;
data[JSON_API_USER] = api_user_;
data[JSON_MOUNT_LOCATIONS] = to_json(locations_);
if (utils::file::write_json_file(config_file, data)) {
return;
}
utils::error::raise_error(
function_name, utils::get_last_error_code(),
fmt::format("failed to save file|{}", config_file));
} catch (const std::exception &ex) {
utils::error::raise_error(
function_name, ex, fmt::format("failed to save file|{}", config_file));
}
}
void mgmt_app_config::set_api_port(std::uint16_t api_port) {
if (api_port_ == api_port) {
return;
}
api_port_ = api_port;
save();
}
void mgmt_app_config::set_mount_location(provider_type prov,
std::string_view name,
std::string_view location) {
if (name.empty()) {
return;
}
if (location.empty()) {
return;
}
recur_mutex_lock lock(mtx_);
if (locations_[prov][std::string{name}] == std::string{location}) {
return;
}
locations_[prov][std::string{name}] = std::string{location};
save();
}
} // namespace repertory::ui

View File

@ -0,0 +1,4 @@
cupertino
cupertinoicons
fromargb
onetwothree

47
web/repertory/.gitignore vendored Normal file
View File

@ -0,0 +1,47 @@
# Miscellaneous
*.class
*.log
*.pyc
*.swp
.DS_Store
.atom/
.build/
.buildlog/
.history
.svn/
.swiftpm/
migrate_working_dir/
# IntelliJ related
*.iml
*.ipr
*.iws
.idea/
# The .vscode folder contains launch configuration and tasks you configure in
# VS Code which you may wish to be included in version control, so this line
# is commented out by default.
#.vscode/
# Flutter/Dart/Pub related
**/doc/api/
**/ios/Flutter/.last_build_id
.dart_tool/
.flutter-plugins
.flutter-plugins-dependencies
.pub-cache/
.pub/
/build/
# Symbolication related
app.*.symbols
# Obfuscation related
app.*.map.json
# Android Studio will place build artifacts here
/android/app/debug
/android/app/profile
/android/app/release
.flutter-companion
pubspec.lock

30
web/repertory/.metadata Normal file
View File

@ -0,0 +1,30 @@
# This file tracks properties of this Flutter project.
# Used by Flutter tool to assess capabilities and perform upgrades etc.
#
# This file should be version controlled and should not be manually edited.
version:
revision: "35c388afb57ef061d06a39b537336c87e0e3d1b1"
channel: "stable"
project_type: app
# Tracks metadata for the flutter migrate command
migration:
platforms:
- platform: root
create_revision: 35c388afb57ef061d06a39b537336c87e0e3d1b1
base_revision: 35c388afb57ef061d06a39b537336c87e0e3d1b1
- platform: web
create_revision: 35c388afb57ef061d06a39b537336c87e0e3d1b1
base_revision: 35c388afb57ef061d06a39b537336c87e0e3d1b1
# User provided section
# List of Local paths (relative to this file) that should be
# ignored by the migrate tool.
#
# Files that are not part of the templates will be ignored by default.
unmanaged_files:
- 'lib/main.dart'
- 'ios/Runner.xcodeproj/project.pbxproj'

16
web/repertory/README.md Normal file
View File

@ -0,0 +1,16 @@
# repertory
A new Flutter project.
## Getting Started
This project is a starting point for a Flutter application.
A few resources to get you started if this is your first Flutter project:
- [Lab: Write your first Flutter app](https://docs.flutter.dev/get-started/codelab)
- [Cookbook: Useful Flutter samples](https://docs.flutter.dev/cookbook)
For help getting started with Flutter development, view the
[online documentation](https://docs.flutter.dev/), which offers tutorials,
samples, guidance on mobile development, and a full API reference.

View File

@ -0,0 +1,28 @@
# This file configures the analyzer, which statically analyzes Dart code to
# check for errors, warnings, and lints.
#
# The issues identified by the analyzer are surfaced in the UI of Dart-enabled
# IDEs (https://dart.dev/tools#ides-and-editors). The analyzer can also be
# invoked from the command line by running `flutter analyze`.
# The following line activates a set of recommended lints for Flutter apps,
# packages, and plugins designed to encourage good coding practices.
include: package:flutter_lints/flutter.yaml
linter:
# The lint rules applied to this project can be customized in the
# section below to disable rules from the `package:flutter_lints/flutter.yaml`
# included above or to enable additional rules. A list of all available lints
# and their documentation is published at https://dart.dev/lints.
#
# Instead of disabling a lint rule for the entire project in the
# section below, it can also be suppressed for a single line of code
# or a specific dart file by using the `// ignore: name_of_lint` and
# `// ignore_for_file: name_of_lint` syntax on the line or in the file
# producing the lint.
rules:
# avoid_print: false # Uncomment to disable the `avoid_print` rule
# prefer_single_quotes: true # Uncomment to enable the `prefer_single_quotes` rule
# Additional information about this file can be found at
# https://dart.dev/guides/language/analysis-options

View File

@ -0,0 +1 @@
const String appTitle = "Repertory Management Portal";

View File

@ -0,0 +1,6 @@
class DuplicateMountException implements Exception {
final String _name;
const DuplicateMountException({required name}) : _name = name, super();
String get name => _name;
}

View File

@ -0,0 +1,18 @@
String formatMountName(String type, String name) {
if (type == "remote") {
return name.replaceAll("_", ":");
}
return name;
}
String initialCaps(String txt) {
if (txt.isEmpty) {
return txt;
}
if (txt.length == 1) {
return txt[0].toUpperCase();
}
return txt[0].toUpperCase() + txt.substring(1).toLowerCase();
}

View File

@ -0,0 +1,94 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:repertory/constants.dart' as constants;
import 'package:repertory/helpers.dart';
import 'package:repertory/models/mount.dart';
import 'package:repertory/models/mount_list.dart';
import 'package:repertory/widgets/mount_list_widget.dart';
import 'package:repertory/widgets/mount_settings.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: constants.appTitle,
theme: ThemeData(
brightness: Brightness.light,
colorScheme: ColorScheme.fromSeed(
seedColor: Colors.deepOrange,
brightness: Brightness.light,
),
),
themeMode: ThemeMode.dark,
darkTheme: ThemeData(
useMaterial3: true,
brightness: Brightness.dark,
colorScheme: ColorScheme.fromSeed(
brightness: Brightness.dark,
onSurface: Colors.white70,
seedColor: Colors.deepOrange,
surface: Color.fromARGB(255, 32, 33, 36),
surfaceContainerLow: Color.fromARGB(255, 41, 42, 45),
),
),
initialRoute: '/',
routes: {'/': (context) => const MyHomePage(title: constants.appTitle)},
onGenerateRoute: (settings) {
if (settings.name != '/settings') {
return null;
}
final mount = settings.arguments as Mount;
return MaterialPageRoute(
builder: (context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(
'${initialCaps(mount.type)} [${formatMountName(mount.type, mount.name)}] Settings',
),
),
body: MountSettingsWidget(mount: mount),
);
},
);
},
);
}
}
class MyHomePage extends StatefulWidget {
final String title;
const MyHomePage({super.key, required this.title});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
leading: const Icon(Icons.storage),
title: Text(widget.title),
),
body: ChangeNotifierProvider(
create: (context) => MountList(),
child: MountListWidget(),
),
floatingActionButton: FloatingActionButton(
onPressed: () {},
tooltip: 'Add',
child: const Icon(Icons.add),
),
);
}
}

View File

@ -0,0 +1,83 @@
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:repertory/types/mount_config.dart';
class Mount with ChangeNotifier {
final MountConfig mountConfig;
Mount(this.mountConfig) {
refresh();
}
String get name => mountConfig.name;
String get path => mountConfig.path;
IconData get state => mountConfig.state;
String get type => mountConfig.type;
Future<void> _fetch() async {
final response = await http.get(
Uri.parse(
Uri.encodeFull('${Uri.base.origin}/api/v1/mount?name=$name&type=$type'),
),
);
if (response.statusCode != 200) {
return;
}
mountConfig.updateSettings(jsonDecode(response.body));
notifyListeners();
}
Future<void> _fetchStatus() async {
final response = await http.get(
Uri.parse(
Uri.encodeFull(
'${Uri.base.origin}/api/v1/mount_status?name=$name&type=$type',
),
),
);
if (response.statusCode != 200) {
return;
}
mountConfig.updateStatus(jsonDecode(response.body));
notifyListeners();
}
Future<void> mount(bool unmount, {String? location}) async {
await http.post(
Uri.parse(
Uri.encodeFull(
'${Uri.base.origin}/api/v1/mount?unmount=$unmount&name=$name&type=$type&location=$location',
),
),
);
return refresh();
}
Future<void> refresh() async {
await _fetch();
return _fetchStatus();
}
Future<void> setValue(String key, String value) async {
await http.put(
Uri.parse(
Uri.encodeFull(
'${Uri.base.origin}/api/v1/set_value_by_name?name=$name&type=$type&key=$key&value=$value',
),
),
);
return refresh();
}
Future<String?> getMountLocation() async {
return "~/mnt/encrypt";
}
}

View File

@ -0,0 +1,67 @@
import 'dart:convert';
import 'package:collection/collection.dart';
import 'package:flutter/foundation.dart';
import 'package:http/http.dart' as http;
import 'package:repertory/errors/duplicate_mount_exception.dart';
import 'package:repertory/types/mount_config.dart';
class MountList with ChangeNotifier {
MountList() {
_fetch();
}
List<MountConfig> _mountList = [];
UnmodifiableListView get items => UnmodifiableListView(_mountList);
Future<void> _fetch() async {
final response = await http.get(
Uri.parse('${Uri.base.origin}/api/v1/mount_list'),
);
if (response.statusCode == 200) {
List<MountConfig> nextList = [];
jsonDecode(response.body).forEach((key, value) {
nextList.addAll(
value.map((name) => MountConfig.fromJson(key, name)).toList(),
);
});
_sort(nextList);
_mountList = nextList;
notifyListeners();
return;
}
}
void _sort(list) {
list.sort((a, b) {
final res = a.type.compareTo(b.type);
if (res != 0) {
return res;
}
return a.name.compareTo(b.name);
});
}
void add(MountConfig config) {
var item = _mountList.firstWhereOrNull((cfg) => cfg.name == config.name);
if (item != null) {
throw DuplicateMountException(name: config.name);
}
_mountList.add(config);
_sort(_mountList);
notifyListeners();
}
void remove(String name) {
_mountList.removeWhere((item) => item.name == name);
notifyListeners();
}
}

View File

@ -0,0 +1,31 @@
import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
class MountConfig {
final String _name;
String _path = "";
Map<String, dynamic> _settings = {};
IconData _state = Icons.toggle_off;
final String _type;
MountConfig({required name, required type}) : _name = name, _type = type;
String get name => _name;
String get path => _path;
UnmodifiableMapView<String, dynamic> get settings =>
UnmodifiableMapView<String, dynamic>(_settings);
IconData get state => _state;
String get type => _type;
factory MountConfig.fromJson(String type, String name) {
return MountConfig(name: name, type: type);
}
void updateSettings(Map<String, dynamic> settings) {
_settings = settings;
}
void updateStatus(Map<String, dynamic> status) {
_path = status["Location"] as String;
_state = status["Active"] as bool ? Icons.toggle_on : Icons.toggle_off;
}
}

View File

@ -0,0 +1,31 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:repertory/models/mount.dart';
import 'package:repertory/models/mount_list.dart';
import 'package:repertory/widgets/mount_widget.dart';
class MountListWidget extends StatefulWidget {
const MountListWidget({super.key});
@override
State<MountListWidget> createState() => _MountListWidgetState();
}
class _MountListWidgetState extends State<MountListWidget> {
@override
Widget build(BuildContext context) {
return Consumer<MountList>(
builder: (context, mountList, widget) {
return ListView.builder(
itemBuilder: (context, idx) {
return ChangeNotifierProvider(
create: (context) => Mount(mountList.items[idx]),
child: const MountWidget(),
);
},
itemCount: mountList.items.length,
);
},
);
}
}

View File

@ -0,0 +1,568 @@
import 'dart:convert';
import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:repertory/models/mount.dart';
import 'package:settings_ui/settings_ui.dart';
class MountSettingsWidget extends StatefulWidget {
final Mount mount;
const MountSettingsWidget({super.key, required this.mount});
@override
State<MountSettingsWidget> createState() => _MountSettingsWidgetState();
}
class _MountSettingsWidgetState extends State<MountSettingsWidget> {
Map<String, dynamic> _settings = {};
void _addBooleanSetting(list, root, key, value) {
list.add(
SettingsTile.switchTile(
leading: Icon(Icons.quiz),
title: Text(key),
initialValue: (value as bool),
onPressed: (_) {
setState(() {
root[key] = !value;
});
},
onToggle: (bool nextValue) {
setState(() {
root[key] = nextValue;
});
},
),
);
}
void _addIntSetting(list, root, key, value) {
list.add(
SettingsTile.navigation(
leading: Icon(Icons.onetwothree),
title: Text(key),
value: Text(value.toString()),
onPressed: (_) {
String updatedValue = value.toString();
showDialog(
context: context,
builder: (context) {
return AlertDialog(
actions: [
TextButton(
child: Text('Cancel'),
onPressed: () {
Navigator.of(context).pop();
},
),
TextButton(
child: Text('OK'),
onPressed: () {
setState(() {
root[key] = int.parse(updatedValue);
});
Navigator.of(context).pop();
},
),
],
content: TextField(
autofocus: true,
controller: TextEditingController(text: updatedValue),
inputFormatters: [FilteringTextInputFormatter.digitsOnly],
keyboardType: TextInputType.number,
onChanged: (nextValue) {
updatedValue = nextValue;
},
),
title: Text(key),
);
},
);
},
),
);
}
void _addIntListSetting(
list,
root,
key,
value,
List<String> valueList,
defaultValue,
icon,
) {
list.add(
SettingsTile.navigation(
title: Text(key),
leading: Icon(icon),
value: DropdownButton<String>(
value: value.toString(),
onChanged: (newValue) {
setState(() {
root[key] = int.parse(newValue ?? defaultValue.toString());
});
},
items:
valueList.map<DropdownMenuItem<String>>((item) {
return DropdownMenuItem<String>(value: item, child: Text(item));
}).toList(),
),
),
);
}
void _addListSetting(list, root, key, value, List<String> valueList, icon) {
list.add(
SettingsTile.navigation(
title: Text(key),
leading: Icon(icon),
value: DropdownButton<String>(
value: value,
onChanged: (newValue) {
setState(() {
root[key] = newValue;
});
},
items:
valueList.map<DropdownMenuItem<String>>((item) {
return DropdownMenuItem<String>(value: item, child: Text(item));
}).toList(),
),
),
);
}
void _addPasswordSetting(list, root, key, value) {
list.add(
SettingsTile.navigation(
leading: Icon(Icons.password),
title: Text(key),
value: Text('*' * (value as String).length),
),
);
}
void _addStringSetting(list, root, key, value, icon) {
list.add(
SettingsTile.navigation(
leading: Icon(icon),
title: Text(key),
value: Text(value),
onPressed: (_) {
String updatedValue = value;
showDialog(
context: context,
builder: (context) {
return AlertDialog(
actions: [
TextButton(
child: Text('Cancel'),
onPressed: () {
Navigator.of(context).pop();
},
),
TextButton(
child: Text('OK'),
onPressed: () {
setState(() {
root[key] = updatedValue;
});
Navigator.of(context).pop();
},
),
],
content: TextField(
autofocus: true,
controller: TextEditingController(text: updatedValue),
onChanged: (value) {
updatedValue = value;
},
),
title: Text(key),
);
},
);
},
),
);
}
@override
Widget build(BuildContext context) {
List<SettingsTile> commonSettings = [];
List<SettingsTile> encryptConfigSettings = [];
List<SettingsTile> hostConfigSettings = [];
List<SettingsTile> remoteConfigSettings = [];
List<SettingsTile> remoteMountSettings = [];
List<SettingsTile> s3ConfigSettings = [];
List<SettingsTile> siaConfigSettings = [];
_settings.forEach((key, value) {
if (key == "ApiAuth") {
_addPasswordSetting(commonSettings, _settings, key, value);
} else if (key == "ApiPort") {
_addIntSetting(commonSettings, _settings, key, value);
} else if (key == "ApiUser") {
_addStringSetting(commonSettings, _settings, key, value, Icons.person);
} else if (key == "DatabaseType") {
_addListSetting(commonSettings, _settings, key, value, [
"rocksdb",
"sqlite",
], Icons.dataset);
} else if (key == "DownloadTimeoutSeconds") {
_addIntSetting(commonSettings, _settings, key, value);
} else if (key == "EnableDownloadTimeout") {
_addBooleanSetting(commonSettings, _settings, key, value);
} else if (key == "EnableDriveEvents") {
_addBooleanSetting(commonSettings, _settings, key, value);
} else if (key == "EventLevel") {
_addListSetting(commonSettings, _settings, key, value, [
"critical",
"error",
"warn",
"info",
"debug",
"trace",
], Icons.event);
} else if (key == "EvictionDelayMinutes") {
_addIntSetting(commonSettings, _settings, key, value);
} else if (key == "EvictionUseAccessedTime") {
_addBooleanSetting(commonSettings, _settings, key, value);
} else if (key == "MaxCacheSizeBytes") {
_addIntSetting(commonSettings, _settings, key, value);
} else if (key == "MaxUploadCount") {
_addIntSetting(commonSettings, _settings, key, value);
} else if (key == "OnlineCheckRetrySeconds") {
_addIntSetting(commonSettings, _settings, key, value);
} else if (key == "PreferredDownloadType") {
_addListSetting(commonSettings, _settings, key, value, [
"default",
"direct",
"ring_buffer",
], Icons.download);
} else if (key == "RetryReadCount") {
_addIntSetting(commonSettings, _settings, key, value);
} else if (key == "RingBufferFileSize") {
_addIntListSetting(
commonSettings,
_settings,
key,
value,
["128", "256", "512", "1024", "2048"],
512,
Icons.animation,
);
} else if (key == "EncryptConfig") {
value.forEach((subKey, subValue) {
if (subKey == "EncryptionToken") {
_addPasswordSetting(
encryptConfigSettings,
_settings[key],
subKey,
subValue,
);
} else if (subKey == "Path") {
_addStringSetting(
encryptConfigSettings,
_settings[key],
subKey,
subValue,
Icons.folder,
);
}
});
} else if (key == "HostConfig") {
value.forEach((subKey, subValue) {
if (subKey == "AgentString") {
_addStringSetting(
hostConfigSettings,
_settings[key],
subKey,
subValue,
Icons.support_agent,
);
} else if (subKey == "ApiPassword") {
_addPasswordSetting(
hostConfigSettings,
_settings[key],
subKey,
subValue,
);
} else if (subKey == "ApiPort") {
_addIntSetting(
hostConfigSettings,
_settings[key],
subKey,
subValue,
);
} else if (subKey == "ApiUser") {
_addStringSetting(
hostConfigSettings,
_settings[key],
subKey,
subValue,
Icons.person,
);
} else if (subKey == "HostNameOrIp") {
_addStringSetting(
hostConfigSettings,
_settings[key],
subKey,
subValue,
Icons.computer,
);
} else if (subKey == "Path") {
_addStringSetting(
hostConfigSettings,
_settings[key],
subKey,
subValue,
Icons.route,
);
} else if (subKey == "Protocol") {
_addListSetting(
hostConfigSettings,
_settings[key],
subKey,
subValue,
["http", "https"],
Icons.http,
);
} else if (subKey == "TimeoutMs") {
_addIntSetting(
hostConfigSettings,
_settings[key],
subKey,
subValue,
);
}
});
} else if (key == "RemoteConfig") {
value.forEach((subKey, subValue) {
if (subKey == "ApiPort") {
_addIntSetting(
remoteConfigSettings,
_settings[key],
subKey,
subValue,
);
} else if (subKey == "EncryptionToken") {
_addPasswordSetting(
remoteConfigSettings,
_settings[key],
subKey,
subValue,
);
} else if (subKey == "HostNameOrIp") {
_addStringSetting(
remoteConfigSettings,
_settings[key],
subKey,
subValue,
Icons.computer,
);
} else if (subKey == "MaxConnections") {
_addIntSetting(
remoteConfigSettings,
_settings[key],
subKey,
subValue,
);
} else if (subKey == "ReceiveTimeoutMs") {
_addIntSetting(
remoteConfigSettings,
_settings[key],
subKey,
subValue,
);
} else if (subKey == "SendTimeoutMs") {
_addIntSetting(
remoteConfigSettings,
_settings[key],
subKey,
subValue,
);
}
});
} else if (key == "RemoteMount") {
value.forEach((subKey, subValue) {
if (subKey == "Enable") {
List<SettingsTile> tempSettings = [];
_addBooleanSetting(tempSettings, _settings[key], subKey, subValue);
remoteMountSettings.insertAll(0, tempSettings);
} else if (subKey == "ApiPort") {
_addIntSetting(
remoteMountSettings,
_settings[key],
subKey,
subValue,
);
} else if (subKey == "ClientPoolSize") {
_addIntSetting(
remoteMountSettings,
_settings[key],
subKey,
subValue,
);
} else if (subKey == "EncryptionToken") {
_addPasswordSetting(
remoteMountSettings,
_settings[key],
subKey,
subValue,
);
}
});
} else if (key == "S3Config") {
value.forEach((subKey, subValue) {
if (subKey == "AccessKey") {
_addPasswordSetting(
s3ConfigSettings,
_settings[key],
subKey,
subValue,
);
} else if (subKey == "Bucket") {
_addStringSetting(
s3ConfigSettings,
_settings[key],
subKey,
subValue,
Icons.folder,
);
} else if (subKey == "EncryptionToken") {
_addPasswordSetting(
s3ConfigSettings,
_settings[key],
subKey,
subValue,
);
} else if (subKey == "Region") {
_addStringSetting(
s3ConfigSettings,
_settings[key],
subKey,
subValue,
Icons.map,
);
} else if (subKey == "SecretKey") {
_addPasswordSetting(
s3ConfigSettings,
_settings[key],
subKey,
subValue,
);
} else if (subKey == "TimeoutMs") {
_addIntSetting(s3ConfigSettings, _settings[key], subKey, subValue);
} else if (subKey == "URL") {
_addStringSetting(
s3ConfigSettings,
_settings[key],
subKey,
subValue,
Icons.http,
);
} else if (subKey == "UsePathStyle") {
_addBooleanSetting(
s3ConfigSettings,
_settings[key],
subKey,
subValue,
);
} else if (subKey == "UseRegionInURL") {
_addBooleanSetting(
s3ConfigSettings,
_settings[key],
subKey,
subValue,
);
}
});
} else if (key == "SiaConfig") {
value.forEach((subKey, subValue) {
if (subKey == "Bucket") {
_addStringSetting(
siaConfigSettings,
_settings[key],
subKey,
subValue,
Icons.folder,
);
}
});
}
});
return SettingsList(
shrinkWrap: false,
sections: [
if (encryptConfigSettings.isNotEmpty)
SettingsSection(
title: const Text('Encrypt Config'),
tiles: encryptConfigSettings,
),
if (hostConfigSettings.isNotEmpty)
SettingsSection(
title: const Text('Host Config'),
tiles: hostConfigSettings,
),
if (remoteConfigSettings.isNotEmpty)
SettingsSection(
title: const Text('Remote Config'),
tiles: remoteConfigSettings,
),
if (s3ConfigSettings.isNotEmpty)
SettingsSection(
title: const Text('S3 Config'),
tiles: s3ConfigSettings,
),
if (siaConfigSettings.isNotEmpty)
SettingsSection(
title: const Text('Sia Config'),
tiles: siaConfigSettings,
),
if (remoteMountSettings.isNotEmpty)
SettingsSection(
title: const Text('Remote Mount'),
tiles:
_settings["RemoteMount"]["Enable"] as bool
? remoteMountSettings
: [remoteMountSettings[0]],
),
SettingsSection(title: const Text('Settings'), tiles: commonSettings),
],
);
}
@override
void dispose() {
var settings = widget.mount.mountConfig.settings;
if (!DeepCollectionEquality().equals(_settings, settings)) {
_settings.forEach((key, value) {
if (!DeepCollectionEquality().equals(settings[key], value)) {
if (value is Map<String, dynamic>) {
value.forEach((subKey, subValue) {
if (!DeepCollectionEquality().equals(
settings[key][subKey],
subValue,
)) {
widget.mount.setValue('$key.$subKey', subValue.toString());
}
});
} else {
widget.mount.setValue(key, value.toString());
}
}
});
}
super.dispose();
}
@override
void initState() {
_settings = jsonDecode(jsonEncode(widget.mount.mountConfig.settings));
super.initState();
}
}

View File

@ -0,0 +1,112 @@
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:repertory/helpers.dart';
import 'package:repertory/models/mount.dart';
class MountWidget extends StatefulWidget {
const MountWidget({super.key});
@override
State<MountWidget> createState() => _MountWidgetState();
}
class _MountWidgetState extends State<MountWidget> {
Timer? _timer;
bool _enabled = true;
@override
Widget build(BuildContext context) {
return Card(
child: Consumer<Mount>(
builder: (context, mount, widget) {
final textColor = Theme.of(context).colorScheme.onSurface;
final subTextColor =
Theme.of(context).brightness == Brightness.dark
? Colors.white38
: Colors.black87;
final isActive = mount.state == Icons.toggle_on;
final nameText = SelectableText(
formatMountName(mount.type, mount.name),
style: TextStyle(color: subTextColor),
);
return ListTile(
isThreeLine: true,
leading: IconButton(
icon: Icon(Icons.settings, color: textColor),
onPressed: () {
Navigator.pushNamed(context, '/settings', arguments: mount);
},
),
subtitle: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
nameText,
SelectableText(
mount.path,
style: TextStyle(color: subTextColor),
),
],
),
title: SelectableText(
initialCaps(mount.type),
style: TextStyle(color: textColor, fontWeight: FontWeight.bold),
),
trailing: IconButton(
icon: Icon(
mount.state,
color:
isActive ? Color.fromARGB(255, 163, 96, 76) : subTextColor,
),
onPressed:
_enabled
? () async {
setState(() {
_enabled = false;
});
String? location = mount.path;
if (!isActive && mount.path.isEmpty) {
location = await mount.getMountLocation();
if (location == null) {}
}
mount
.mount(isActive, location: location)
.then((_) {
setState(() {
_enabled = true;
});
})
.catchError((_) {
setState(() {
_enabled = true;
});
});
}
: null,
),
);
},
),
);
}
@override
void dispose() {
_timer?.cancel();
_timer = null;
super.dispose();
}
@override
void initState() {
super.initState();
_timer = Timer.periodic(const Duration(seconds: 5), (_) {
Provider.of<Mount>(context, listen: false).refresh();
});
}
}

View File

@ -0,0 +1,93 @@
name: repertory
description: "Repertory Management Portal"
# The following line prevents the package from being accidentally published to
# pub.dev using `flutter pub publish`. This is preferred for private packages.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
# The following defines the version and build number for your application.
# A version number is three numbers separated by dots, like 1.2.43
# followed by an optional build number separated by a +.
# Both the version and the builder number may be overridden in flutter
# build by specifying --build-name and --build-number, respectively.
# In Android, build-name is used as versionName while build-number used as versionCode.
# Read more about Android versioning at https://developer.android.com/studio/publish/versioning
# In iOS, build-name is used as CFBundleShortVersionString while build-number is used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
# In Windows, build-name is used as the major, minor, and patch parts
# of the product and file versions while build-number is used as the build suffix.
version: 1.0.0+1
environment:
sdk: ^3.7.0
# Dependencies specify other packages that your package needs in order to work.
# To automatically upgrade your package dependencies to the latest versions
# consider running `flutter pub upgrade --major-versions`. Alternatively,
# dependencies can be manually updated by changing the version numbers below to
# the latest version available on pub.dev. To see which dependencies have newer
# versions available, run `flutter pub outdated`.
dependencies:
flutter:
sdk: flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.8
collection: ^1.19.1
http: ^1.3.0
provider: ^6.1.2
settings_ui: ^2.0.2
dev_dependencies:
flutter_test:
sdk: flutter
# The "flutter_lints" package below contains a set of recommended lints to
# encourage good coding practices. The lint set provided by the package is
# activated in the `analysis_options.yaml` file located at the root of your
# package. See that file for information about deactivating specific lint
# rules and activating additional ones.
flutter_lints: ^5.0.0
# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec
# The following section is specific to Flutter packages.
flutter:
# The following line ensures that the Material Icons font is
# included with your application, so that you can use the icons in
# the material Icons class.
uses-material-design: true
# To add assets to your application, add an assets section, like this:
# assets:
# - images/a_dot_burr.jpeg
# - images/a_dot_ham.jpeg
# An image asset can refer to one or more resolution-specific "variants", see
# https://flutter.dev/to/resolution-aware-images
# For details regarding adding assets from package dependencies, see
# https://flutter.dev/to/asset-from-package
# To add custom fonts to your application, add a fonts section here,
# in this "flutter" section. Each entry in this list should have a
# "family" key with the font family name, and a "fonts" key with a
# list giving the asset and other descriptors for the font. For
# example:
# fonts:
# - family: Schyler
# fonts:
# - asset: fonts/Schyler-Regular.ttf
# - asset: fonts/Schyler-Italic.ttf
# style: italic
# - family: Trajan Pro
# fonts:
# - asset: fonts/TrajanPro.ttf
# - asset: fonts/TrajanPro_Bold.ttf
# weight: 700
#
# For details regarding fonts from package dependencies,
# see https://flutter.dev/to/font-from-package

View File

@ -0,0 +1,30 @@
// This is a basic Flutter widget test.
//
// To perform an interaction with a widget in your test, use the WidgetTester
// utility in the flutter_test package. For example, you can send tap and scroll
// gestures. You can also use WidgetTester to find child widgets in the widget
// tree, read text, and verify that the values of widget properties are correct.
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:repertory/main.dart';
void main() {
testWidgets('Counter increments smoke test', (WidgetTester tester) async {
// Build our app and trigger a frame.
await tester.pumpWidget(const MyApp());
// Verify that our counter starts at 0.
expect(find.text('0'), findsOneWidget);
expect(find.text('1'), findsNothing);
// Tap the '+' icon and trigger a frame.
await tester.tap(find.byIcon(Icons.add));
await tester.pump();
// Verify that our counter has incremented.
expect(find.text('0'), findsNothing);
expect(find.text('1'), findsOneWidget);
});
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 917 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

View File

@ -0,0 +1,38 @@
<!DOCTYPE html>
<html>
<head>
<!--
If you are serving your web app in a path other than the root, change the
href value below to reflect the base path you are serving from.
The path provided below has to start and end with a slash "/" in order for
it to work correctly.
For more details:
* https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base
This is a placeholder for base href that will be replaced by the value of
the `--base-href` argument provided to `flutter build`.
-->
<base href="$FLUTTER_BASE_HREF">
<meta charset="UTF-8">
<meta content="IE=Edge" http-equiv="X-UA-Compatible">
<meta name="description" content="A new Flutter project.">
<!-- iOS meta tags & icons -->
<meta name="mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="apple-mobile-web-app-title" content="repertory">
<link rel="apple-touch-icon" href="icons/Icon-192.png">
<!-- Favicon -->
<link rel="icon" type="image/png" href="favicon.png"/>
<title>repertory</title>
<link rel="manifest" href="manifest.json">
</head>
<body>
<script src="flutter_bootstrap.js" async></script>
</body>
</html>

View File

@ -0,0 +1,35 @@
{
"name": "repertory",
"short_name": "repertory",
"start_url": ".",
"display": "standalone",
"background_color": "#0175C2",
"theme_color": "#0175C2",
"description": "A new Flutter project.",
"orientation": "portrait-primary",
"prefer_related_applications": false,
"icons": [
{
"src": "icons/Icon-192.png",
"sizes": "192x192",
"type": "image/png"
},
{
"src": "icons/Icon-512.png",
"sizes": "512x512",
"type": "image/png"
},
{
"src": "icons/Icon-maskable-192.png",
"sizes": "192x192",
"type": "image/png",
"purpose": "maskable"
},
{
"src": "icons/Icon-maskable-512.png",
"sizes": "512x512",
"type": "image/png",
"purpose": "maskable"
}
]
}