fix encryption provider

This commit is contained in:
Scott E. Graves 2023-12-13 13:34:21 -06:00
parent 0e83d84360
commit d6d4b579c9
2 changed files with 67 additions and 51 deletions

View File

@ -259,48 +259,46 @@ auto encrypt_provider::get_directory_items(const std::string &api_path,
return api_error::item_exists;
}
auto result = db::db_select{*db_, source_table}
.column("source_path")
.where("api_path")
.equals(api_path)
.go();
std::optional<db::db_select::row> row;
if (not(result.get_row(row) && row.has_value())) {
auto cfg = config_.get_encrypt_config();
std::string source_path{api_path};
if (api_path != "/") {
res =
utils::encryption::decrypt_file_path(cfg.encryption_token, source_path);
if (res != api_error::success) {
return res;
}
}
source_path =
utils::path::absolute(utils::path::combine(cfg.path, {source_path}));
if (source_path != cfg.path &&
not source_path.starts_with(cfg.path +
utils::path::directory_seperator)) {
return api_error::directory_not_found;
}
auto source_path = row->get_column("source_path").get_value<std::string>();
result = db::db_select{*db_, directory_table}
.column("api_path")
.where("source_path")
.equals(source_path)
.go();
if (not result.has_row()) {
if (not utils::file::is_directory(source_path)) {
return api_error::directory_not_found;
}
try {
for (const auto &dir_entry :
std::filesystem::directory_iterator(source_path)) {
auto iter = std::filesystem::directory_iterator(source_path);
for (const auto &dir_entry : iter) {
try {
std::string current_api_path{};
if (dir_entry.is_directory()) {
result = db::db_select{*db_, directory_table}
.column("api_path")
.where("source_path")
.equals(dir_entry.path().string())
.go();
row.reset();
auto result = db::db_select{*db_, directory_table}
.column("api_path")
.where("source_path")
.equals(dir_entry.path().string())
.go();
std::optional<db::db_select::row> row;
if (result.get_row(row) && row.has_value()) {
current_api_path =
row->get_column("api_path").get_value<std::string>();
}
if (current_api_path.empty()) {
const auto cfg = config_.get_encrypt_config();
for (const auto &child_dir_entry :
std::filesystem::directory_iterator(dir_entry.path())) {
process_directory_entry(child_dir_entry, cfg, current_api_path);
}
process_directory_entry(dir_entry, cfg, current_api_path);
result = db::db_select{*db_, directory_table}
.column("api_path")
@ -317,18 +315,17 @@ auto encrypt_provider::get_directory_items(const std::string &api_path,
}
} else {
std::string api_path_data{};
result = db::db_select{*db_, file_table}
.column("data")
.where("source_path")
.equals(dir_entry.path().string())
.go();
row.reset();
auto result = db::db_select{*db_, file_table}
.column("data")
.where("source_path")
.equals(dir_entry.path().string())
.go();
std::optional<db::db_select::row> row;
if (result.get_row(row) && row.has_value()) {
api_path_data = row->get_column("data").get_value<std::string>();
}
if (api_path_data.empty()) {
const auto cfg = config_.get_encrypt_config();
if (not process_directory_entry(dir_entry, cfg, current_api_path)) {
continue;
}
@ -414,7 +411,7 @@ auto encrypt_provider::process_directory_entry(
auto encrypted_parts = utils::string::split(
utils::path::create_api_path(dir_path.string()), '/', false);
for (std::size_t part_idx = 0U; part_idx < encrypted_parts.size();
for (std::size_t part_idx = 1U; part_idx < encrypted_parts.size();
part_idx++) {
data_buffer encrypted_data;
utils::encryption::encrypt_data(
@ -425,7 +422,7 @@ auto encrypt_provider::process_directory_entry(
encrypted_parts[part_idx] = utils::to_hex_string(encrypted_data);
}
std::size_t current_idx{};
std::size_t current_idx{1U};
std::string current_encrypted_path{};
std::string current_source_path{cfg.path};
for (const auto &part : dir_path) {
@ -436,7 +433,7 @@ auto encrypt_provider::process_directory_entry(
current_source_path =
utils::path::combine(current_source_path, {part.string()});
std::string parent_api_path{};
std::string current_api_path{};
auto result = db::db_select{*db_, directory_table}
.column("api_path")
.where("source_path")
@ -444,33 +441,35 @@ auto encrypt_provider::process_directory_entry(
.go();
std::optional<db::db_select::row> row;
if (result.get_row(row) && row.has_value()) {
parent_api_path = row->get_column("api_path").get_value<std::string>();
current_api_path = row->get_column("api_path").get_value<std::string>();
}
if (parent_api_path.empty()) {
parent_api_path = utils::path::create_api_path(
if (current_api_path.empty()) {
current_api_path = utils::path::create_api_path(
current_encrypted_path + '/' + encrypted_parts.at(current_idx));
auto ins_res = db::db_insert{*db_, directory_table}
.column_value("source_path", current_source_path)
.column_value("api_path", parent_api_path)
.column_value("api_path", current_api_path)
.go();
// TODO handle error
ins_res = db::db_insert{*db_, source_table}
.column_value("api_path", parent_api_path)
.column_value("api_path", current_api_path)
.column_value("source_path", current_source_path)
.go();
// TODO handle error
event_system::instance().raise<filesystem_item_added>(
parent_api_path, utils::path::get_parent_api_path(parent_api_path),
true);
current_api_path,
utils::path::get_parent_api_path(current_api_path), true);
} else {
encrypted_parts[current_idx] =
utils::string::split(parent_api_path, '/', false)[current_idx];
utils::string::split(current_api_path, '/', false)[current_idx];
}
current_encrypted_path = utils::path::create_api_path(
current_encrypted_path + '/' + encrypted_parts[current_idx++]);
current_encrypted_path + '/' + encrypted_parts.at(current_idx++));
std::cout << current_source_path << ':' << current_encrypted_path
<< std::endl;
}
return current_encrypted_path;

View File

@ -22,25 +22,42 @@
#include "test_common.hpp"
#include "app_config.hpp"
#include "file_manager/file_manager.hpp"
#include "events/consumers/console_consumer.hpp"
#include "providers/encrypt/encrypt_provider.hpp"
#include "utils/path_utils.hpp"
namespace repertory {
/* TEST(encrypt_provider, can_construct_encrypt_provider) {
/*
TEST(encrypt_provider, can_construct_encrypt_provider) {
ASSERT_TRUE(
utils::file::delete_directory_recursively("./encrypt_provider_test"));
// console_consumer consumer{};
// event_system::instance().start();
{
console_consumer cc{};
event_system::instance().start();
app_config cfg(provider_type::encrypt, "./encrypt_provider_test");
EXPECT_FALSE(
cfg.set_value_by_name("EncryptConfig.Path", "c:\\src").empty());
encrypt_provider provider(cfg);
file_manager mgr(cfg, provider);
mgr.start();
event_system::instance().stop();
EXPECT_TRUE(provider.start(
[&provider](bool directory, api_file &file) -> api_error {
return provider_meta_handler(provider, directory, file);
},
&mgr));
directory_item_list list{};
EXPECT_EQ(api_error::success, provider.get_directory_items("/", list));
provider.stop();
mgr.stop();
}
// event_system::instance().stop();
ASSERT_TRUE(
utils::file::delete_directory_recursively("./encrypt_provider_test"));
}