refactor
This commit is contained in:
@ -54,6 +54,10 @@ private:
|
||||
const std::string &object_name) const
|
||||
-> api_error;
|
||||
|
||||
[[nodiscard]] auto
|
||||
create_directory_object(const std::string &api_path,
|
||||
const std::string &object_name) const -> api_error;
|
||||
|
||||
[[nodiscard]] auto create_directory_paths(const std::string &api_path,
|
||||
const std::string &key) const
|
||||
-> api_error;
|
||||
|
@ -40,9 +40,9 @@
|
||||
#include "utils/time.hpp"
|
||||
|
||||
namespace {
|
||||
[[nodiscard]] auto
|
||||
set_request_path(auto &request,
|
||||
const std::string &object_name) -> repertory::api_error {
|
||||
[[nodiscard]] auto set_request_path(auto &request,
|
||||
const std::string &object_name)
|
||||
-> repertory::api_error {
|
||||
request.path = object_name;
|
||||
if (request.path.substr(1U).size() > repertory::max_s3_object_name_length) {
|
||||
return repertory::api_error::name_too_long;
|
||||
@ -56,8 +56,9 @@ namespace repertory {
|
||||
s3_provider::s3_provider(app_config &config, i_http_comm &comm)
|
||||
: base_provider(config, comm) {}
|
||||
|
||||
auto s3_provider::add_if_not_found(
|
||||
api_file &file, const std::string &object_name) const -> api_error {
|
||||
auto s3_provider::add_if_not_found(api_file &file,
|
||||
const std::string &object_name) const
|
||||
-> api_error {
|
||||
api_meta_map meta{};
|
||||
auto res = get_item_meta(file.api_path, meta);
|
||||
if (res == api_error::item_not_found) {
|
||||
@ -95,36 +96,12 @@ auto s3_provider::convert_api_date(std::string_view date) -> std::uint64_t {
|
||||
#endif // defined(_WIN32)
|
||||
}
|
||||
|
||||
auto s3_provider::create_directory_impl(const std::string &api_path,
|
||||
api_meta_map &meta) -> api_error {
|
||||
auto s3_provider::create_directory_object(const std::string &api_path,
|
||||
const std::string &object_name) const
|
||||
-> api_error {
|
||||
REPERTORY_USES_FUNCTION_NAME();
|
||||
|
||||
const auto &cfg = get_s3_config();
|
||||
auto is_encrypted = not cfg.encryption_token.empty();
|
||||
stop_type stop_requested{false};
|
||||
|
||||
if (is_encrypted) {
|
||||
std::string encrypted_file_path;
|
||||
auto res = get_item_meta(utils::path::get_parent_api_path(api_path),
|
||||
META_KEY, encrypted_file_path);
|
||||
if (res != api_error::success) {
|
||||
utils::error::raise_api_path_error(function_name, api_path, res,
|
||||
"failed to create file");
|
||||
return res;
|
||||
}
|
||||
|
||||
data_buffer result;
|
||||
utils::encryption::encrypt_data(
|
||||
cfg.encryption_token,
|
||||
*(utils::string::split(api_path, '/', false).end() - 1U), result);
|
||||
|
||||
meta[META_KEY] = utils::path::create_api_path(
|
||||
utils::path::combine(utils::path::create_api_path(encrypted_file_path),
|
||||
{utils::collection::to_hex_string(result)}));
|
||||
}
|
||||
|
||||
auto object_name =
|
||||
utils::path::create_api_path(is_encrypted ? meta[META_KEY] : api_path);
|
||||
|
||||
std::string response_data;
|
||||
curl::requests::http_put_file put_dir{};
|
||||
@ -141,6 +118,7 @@ auto s3_provider::create_directory_impl(const std::string &api_path,
|
||||
return res;
|
||||
}
|
||||
|
||||
stop_type stop_requested{false};
|
||||
long response_code{};
|
||||
if (not get_comm().make_request(put_dir, response_code, stop_requested)) {
|
||||
utils::error::raise_api_path_error(function_name, api_path,
|
||||
@ -159,8 +137,43 @@ auto s3_provider::create_directory_impl(const std::string &api_path,
|
||||
return api_error::success;
|
||||
}
|
||||
|
||||
auto s3_provider::create_directory_paths(
|
||||
const std::string &api_path, const std::string &key) const -> api_error {
|
||||
auto s3_provider::create_directory_impl(const std::string &api_path,
|
||||
api_meta_map &meta) -> api_error {
|
||||
REPERTORY_USES_FUNCTION_NAME();
|
||||
|
||||
const auto &cfg = get_s3_config();
|
||||
auto is_encrypted = not cfg.encryption_token.empty();
|
||||
|
||||
if (is_encrypted) {
|
||||
std::string encrypted_file_path;
|
||||
auto res = get_item_meta(utils::path::get_parent_api_path(api_path),
|
||||
META_KEY, encrypted_file_path);
|
||||
if (res != api_error::success) {
|
||||
utils::error::raise_api_path_error(function_name, api_path, res,
|
||||
"failed to create file");
|
||||
return res;
|
||||
}
|
||||
|
||||
data_buffer result;
|
||||
utils::encryption::encrypt_data(
|
||||
cfg.encryption_token,
|
||||
*(utils::string::split(api_path, '/', false).end() - 1U), result);
|
||||
|
||||
meta[META_KEY] = utils::path::create_api_path(
|
||||
utils::path::combine(utils::path::create_api_path(encrypted_file_path),
|
||||
{
|
||||
utils::collection::to_hex_string(result),
|
||||
}));
|
||||
}
|
||||
|
||||
return create_directory_object(
|
||||
api_path,
|
||||
utils::path::create_api_path(is_encrypted ? meta[META_KEY] : api_path));
|
||||
}
|
||||
|
||||
auto s3_provider::create_directory_paths(const std::string &api_path,
|
||||
const std::string &key) const
|
||||
-> api_error {
|
||||
REPERTORY_USES_FUNCTION_NAME();
|
||||
|
||||
if (api_path == "/") {
|
||||
@ -205,42 +218,16 @@ auto s3_provider::create_directory_paths(
|
||||
}
|
||||
|
||||
if (not exists) {
|
||||
std::string response_data;
|
||||
curl::requests::http_put_file put_dir{};
|
||||
put_dir.allow_timeout = true;
|
||||
put_dir.aws_service = "aws:amz:" + cfg.region + ":s3";
|
||||
put_dir.response_handler = [&response_data](auto &&data,
|
||||
long /*response_code*/) {
|
||||
response_data = std::string(data.begin(), data.end());
|
||||
};
|
||||
|
||||
res = set_request_path(put_dir,
|
||||
(is_encrypted ? cur_key : cur_path) + '/');
|
||||
res = create_directory_object(api_path,
|
||||
(is_encrypted ? cur_key : cur_path));
|
||||
if (res != api_error::success) {
|
||||
return res;
|
||||
}
|
||||
|
||||
stop_type stop_requested{false};
|
||||
long response_code{};
|
||||
if (not get_comm().make_request(put_dir, response_code,
|
||||
stop_requested)) {
|
||||
utils::error::raise_api_path_error(
|
||||
function_name, cur_path, api_error::comm_error,
|
||||
"failed to create directory object");
|
||||
return api_error::comm_error;
|
||||
}
|
||||
|
||||
if (response_code != http_error_codes::ok) {
|
||||
utils::error::raise_api_path_error(
|
||||
function_name, api_path, response_code,
|
||||
fmt::format("failed to create directory|response|{}",
|
||||
response_data));
|
||||
return api_error::comm_error;
|
||||
}
|
||||
}
|
||||
|
||||
auto dir = create_api_file(cur_path, cur_key, 0U,
|
||||
get_last_modified(true, cur_path));
|
||||
exists ? get_last_modified(true, cur_path)
|
||||
: utils::time::get_time_now());
|
||||
get_api_item_added()(true, dir);
|
||||
}
|
||||
|
||||
@ -367,8 +354,9 @@ auto s3_provider::get_directory_item_count(const std::string &api_path) const
|
||||
return 0U;
|
||||
}
|
||||
|
||||
auto s3_provider::get_directory_items_impl(
|
||||
const std::string &api_path, directory_item_list &list) const -> api_error {
|
||||
auto s3_provider::get_directory_items_impl(const std::string &api_path,
|
||||
directory_item_list &list) const
|
||||
-> api_error {
|
||||
REPERTORY_USES_FUNCTION_NAME();
|
||||
|
||||
const auto &cfg{get_s3_config()};
|
||||
@ -519,8 +507,8 @@ auto s3_provider::get_directory_items_impl(
|
||||
return api_error::success;
|
||||
}
|
||||
|
||||
auto s3_provider::get_file(const std::string &api_path,
|
||||
api_file &file) const -> api_error {
|
||||
auto s3_provider::get_file(const std::string &api_path, api_file &file) const
|
||||
-> api_error {
|
||||
REPERTORY_USES_FUNCTION_NAME();
|
||||
|
||||
try {
|
||||
@ -559,8 +547,8 @@ auto s3_provider::get_file(const std::string &api_path,
|
||||
return api_error::error;
|
||||
}
|
||||
|
||||
auto s3_provider::get_file_list(api_file_list &list,
|
||||
std::string &marker) const -> api_error {
|
||||
auto s3_provider::get_file_list(api_file_list &list, std::string &marker) const
|
||||
-> api_error {
|
||||
REPERTORY_USES_FUNCTION_NAME();
|
||||
|
||||
try {
|
||||
@ -646,8 +634,9 @@ auto s3_provider::get_file_list(api_file_list &list,
|
||||
return api_error::error;
|
||||
}
|
||||
|
||||
auto s3_provider::get_last_modified(
|
||||
bool directory, const std::string &api_path) const -> std::uint64_t {
|
||||
auto s3_provider::get_last_modified(bool directory,
|
||||
const std::string &api_path) const
|
||||
-> std::uint64_t {
|
||||
bool is_encrypted{};
|
||||
std::string object_name;
|
||||
head_object_result result{};
|
||||
@ -657,9 +646,10 @@ auto s3_provider::get_last_modified(
|
||||
: utils::time::get_time_now();
|
||||
}
|
||||
|
||||
auto s3_provider::get_object_info(
|
||||
bool directory, const std::string &api_path, bool &is_encrypted,
|
||||
std::string &object_name, head_object_result &result) const -> api_error {
|
||||
auto s3_provider::get_object_info(bool directory, const std::string &api_path,
|
||||
bool &is_encrypted, std::string &object_name,
|
||||
head_object_result &result) const
|
||||
-> api_error {
|
||||
REPERTORY_USES_FUNCTION_NAME();
|
||||
|
||||
try {
|
||||
@ -718,10 +708,12 @@ auto s3_provider::get_object_info(
|
||||
return api_error::error;
|
||||
}
|
||||
|
||||
auto s3_provider::get_object_list(
|
||||
std::string &response_data, long &response_code,
|
||||
std::optional<std::string> delimiter, std::optional<std::string> prefix,
|
||||
std::optional<std::string> token) const -> bool {
|
||||
auto s3_provider::get_object_list(std::string &response_data,
|
||||
long &response_code,
|
||||
std::optional<std::string> delimiter,
|
||||
std::optional<std::string> prefix,
|
||||
std::optional<std::string> token) const
|
||||
-> bool {
|
||||
curl::requests::http_get get{};
|
||||
get.allow_timeout = true;
|
||||
get.aws_service = "aws:amz:" + get_s3_config().region + ":s3";
|
||||
@ -749,8 +741,8 @@ auto s3_provider::get_total_drive_space() const -> std::uint64_t {
|
||||
return std::numeric_limits<std::int64_t>::max() / std::int64_t(2);
|
||||
}
|
||||
|
||||
auto s3_provider::is_directory(const std::string &api_path,
|
||||
bool &exists) const -> api_error {
|
||||
auto s3_provider::is_directory(const std::string &api_path, bool &exists) const
|
||||
-> api_error {
|
||||
REPERTORY_USES_FUNCTION_NAME();
|
||||
|
||||
try {
|
||||
@ -777,8 +769,8 @@ auto s3_provider::is_directory(const std::string &api_path,
|
||||
return api_error::error;
|
||||
}
|
||||
|
||||
auto s3_provider::is_file(const std::string &api_path,
|
||||
bool &exists) const -> api_error {
|
||||
auto s3_provider::is_file(const std::string &api_path, bool &exists) const
|
||||
-> api_error {
|
||||
REPERTORY_USES_FUNCTION_NAME();
|
||||
|
||||
try {
|
||||
|
Reference in New Issue
Block a user