This commit is contained in:
Scott E. Graves 2023-12-13 15:22:19 -06:00
parent 7112fbee7e
commit 94675a3011

View File

@ -414,149 +414,6 @@ auto encrypt_provider::get_file(const std::string &api_path,
return api_error::success;
}
auto encrypt_provider::process_directory_entry(
const std::filesystem::directory_entry &dir_entry,
const encrypt_config &cfg, std::string &api_path) const -> bool {
const auto add_directory = [this, &cfg](auto dir_path) -> std::string {
auto encrypted_parts = utils::string::split(
utils::path::create_api_path(dir_path.string()), '/', false);
for (std::size_t part_idx = 1U; part_idx < encrypted_parts.size();
part_idx++) {
data_buffer encrypted_data;
utils::encryption::encrypt_data(
cfg.encryption_token, encrypted_parts.at(part_idx).c_str(),
strnlen(encrypted_parts.at(part_idx).c_str(),
encrypted_parts.at(part_idx).size()),
encrypted_data);
encrypted_parts[part_idx] = utils::to_hex_string(encrypted_data);
}
std::size_t current_idx{1U};
std::string current_encrypted_path{};
std::string current_source_path{cfg.path};
for (const auto &part : dir_path) {
if (part.string() == "/") {
continue;
}
current_source_path =
utils::path::combine(current_source_path, {part.string()});
std::string current_api_path{};
auto result = db::db_select{*db_, directory_table}
.column("api_path")
.where("source_path")
.equals(current_source_path)
.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()) {
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", current_api_path)
.go();
// TODO handle error
ins_res = db::db_insert{*db_, source_table}
.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>(
current_api_path,
utils::path::get_parent_api_path(current_api_path), true);
} else {
encrypted_parts[current_idx] =
utils::string::split(current_api_path, '/', false)[current_idx];
}
current_encrypted_path = utils::path::create_api_path(
current_encrypted_path + '/' + encrypted_parts.at(current_idx++));
std::cout << current_source_path << ':' << current_encrypted_path
<< std::endl;
}
return current_encrypted_path;
};
if (dir_entry.is_directory()) {
api_path = add_directory(dir_entry.path().lexically_relative(cfg.path));
return false;
}
if (dir_entry.is_regular_file() && not dir_entry.is_symlink()) {
const auto relative_path = dir_entry.path().lexically_relative(cfg.path);
std::string api_path_data{};
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>();
}
std::string api_parent{};
result = db::db_select{*db_, directory_table}
.column("api_path")
.where("source_path")
.equals(dir_entry.path().parent_path().string())
.go();
row.reset();
if (result.get_row(row) && row.has_value()) {
api_parent = row->get_column("api_path").get_value<std::string>();
}
if (api_parent.empty()) {
api_parent = add_directory(relative_path.parent_path());
}
if (api_path_data.empty()) {
stop_type stop_requested = false;
utils::encryption::encrypting_reader reader(
relative_path.filename().string(), dir_entry.path().string(),
stop_requested, cfg.encryption_token,
relative_path.parent_path().string());
api_path = utils::path::create_api_path(api_parent + "/" +
reader.get_encrypted_file_name());
auto iv_list = reader.get_iv_list();
json data = {
{"api_path", api_path},
{"iv_list", iv_list},
{"original_file_size", dir_entry.file_size()},
};
auto ins_res = db::db_insert{*db_, file_table}
.column_value("source_path", dir_entry.path().string())
.column_value("data", data.dump())
.go();
// TODO handle error
ins_res = db::db_insert{*db_, source_table}
.column_value("api_path", api_path)
.column_value("source_path", dir_entry.path().string())
.go();
// TODO handle error
event_system::instance().raise<filesystem_item_added>(api_path,
api_parent, false);
} else {
api_path = json::parse(api_path_data)["api_path"].get<std::string>();
}
return true;
}
return false;
}
auto encrypt_provider::get_file_list(api_file_list &list) const -> api_error {
const auto cfg = config_.get_encrypt_config();
@ -818,6 +675,149 @@ auto encrypt_provider::is_online() const -> bool {
auto encrypt_provider::is_rename_supported() const -> bool { return false; }
auto encrypt_provider::process_directory_entry(
const std::filesystem::directory_entry &dir_entry,
const encrypt_config &cfg, std::string &api_path) const -> bool {
const auto add_directory = [this, &cfg](auto dir_path) -> std::string {
auto encrypted_parts = utils::string::split(
utils::path::create_api_path(dir_path.string()), '/', false);
for (std::size_t part_idx = 1U; part_idx < encrypted_parts.size();
part_idx++) {
data_buffer encrypted_data;
utils::encryption::encrypt_data(
cfg.encryption_token, encrypted_parts.at(part_idx).c_str(),
strnlen(encrypted_parts.at(part_idx).c_str(),
encrypted_parts.at(part_idx).size()),
encrypted_data);
encrypted_parts[part_idx] = utils::to_hex_string(encrypted_data);
}
std::size_t current_idx{1U};
std::string current_encrypted_path{};
std::string current_source_path{cfg.path};
for (const auto &part : dir_path) {
if (part.string() == "/") {
continue;
}
current_source_path =
utils::path::combine(current_source_path, {part.string()});
std::string current_api_path{};
auto result = db::db_select{*db_, directory_table}
.column("api_path")
.where("source_path")
.equals(current_source_path)
.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()) {
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", current_api_path)
.go();
// TODO handle error
ins_res = db::db_insert{*db_, source_table}
.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>(
current_api_path,
utils::path::get_parent_api_path(current_api_path), true);
} else {
encrypted_parts[current_idx] =
utils::string::split(current_api_path, '/', false)[current_idx];
}
current_encrypted_path = utils::path::create_api_path(
current_encrypted_path + '/' + encrypted_parts.at(current_idx++));
std::cout << current_source_path << ':' << current_encrypted_path
<< std::endl;
}
return current_encrypted_path;
};
if (dir_entry.is_directory()) {
api_path = add_directory(dir_entry.path().lexically_relative(cfg.path));
return false;
}
if (dir_entry.is_regular_file() && not dir_entry.is_symlink()) {
const auto relative_path = dir_entry.path().lexically_relative(cfg.path);
std::string api_path_data{};
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>();
}
std::string api_parent{};
result = db::db_select{*db_, directory_table}
.column("api_path")
.where("source_path")
.equals(dir_entry.path().parent_path().string())
.go();
row.reset();
if (result.get_row(row) && row.has_value()) {
api_parent = row->get_column("api_path").get_value<std::string>();
}
if (api_parent.empty()) {
api_parent = add_directory(relative_path.parent_path());
}
if (api_path_data.empty()) {
stop_type stop_requested = false;
utils::encryption::encrypting_reader reader(
relative_path.filename().string(), dir_entry.path().string(),
stop_requested, cfg.encryption_token,
relative_path.parent_path().string());
api_path = utils::path::create_api_path(api_parent + "/" +
reader.get_encrypted_file_name());
auto iv_list = reader.get_iv_list();
json data = {
{"api_path", api_path},
{"iv_list", iv_list},
{"original_file_size", dir_entry.file_size()},
};
auto ins_res = db::db_insert{*db_, file_table}
.column_value("source_path", dir_entry.path().string())
.column_value("data", data.dump())
.go();
// TODO handle error
ins_res = db::db_insert{*db_, source_table}
.column_value("api_path", api_path)
.column_value("source_path", dir_entry.path().string())
.go();
// TODO handle error
event_system::instance().raise<filesystem_item_added>(api_path,
api_parent, false);
} else {
api_path = json::parse(api_path_data)["api_path"].get<std::string>();
}
return true;
}
return false;
}
auto encrypt_provider::read_file_bytes(const std::string &api_path,
std::size_t size, std::uint64_t offset,
data_buffer &data,