This commit is contained in:
2025-01-02 07:08:12 -06:00
parent 75657bec58
commit f44cc99a82
2 changed files with 80 additions and 84 deletions

View File

@ -54,6 +54,10 @@ private:
const std::string &object_name) const const std::string &object_name) const
-> api_error; -> 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, [[nodiscard]] auto create_directory_paths(const std::string &api_path,
const std::string &key) const const std::string &key) const
-> api_error; -> api_error;

View File

@ -40,9 +40,9 @@
#include "utils/time.hpp" #include "utils/time.hpp"
namespace { namespace {
[[nodiscard]] auto [[nodiscard]] auto set_request_path(auto &request,
set_request_path(auto &request, const std::string &object_name)
const std::string &object_name) -> repertory::api_error { -> repertory::api_error {
request.path = object_name; request.path = object_name;
if (request.path.substr(1U).size() > repertory::max_s3_object_name_length) { if (request.path.substr(1U).size() > repertory::max_s3_object_name_length) {
return repertory::api_error::name_too_long; return repertory::api_error::name_too_long;
@ -56,8 +56,9 @@ namespace repertory {
s3_provider::s3_provider(app_config &config, i_http_comm &comm) s3_provider::s3_provider(app_config &config, i_http_comm &comm)
: base_provider(config, comm) {} : base_provider(config, comm) {}
auto s3_provider::add_if_not_found( auto s3_provider::add_if_not_found(api_file &file,
api_file &file, const std::string &object_name) const -> api_error { const std::string &object_name) const
-> api_error {
api_meta_map meta{}; api_meta_map meta{};
auto res = get_item_meta(file.api_path, meta); auto res = get_item_meta(file.api_path, meta);
if (res == api_error::item_not_found) { if (res == api_error::item_not_found) {
@ -84,7 +85,7 @@ auto s3_provider::convert_api_date(std::string_view date) -> std::uint64_t {
utils::string::split(date_parts.at(1U), 'Z', true).at(0U)) * utils::string::split(date_parts.at(1U), 'Z', true).at(0U)) *
1000000UL; 1000000UL;
struct tm tm1 {}; struct tm tm1{};
#if defined(_WIN32) #if defined(_WIN32)
utils::time::strptime(date_time.c_str(), "%Y-%m-%dT%T", &tm1); utils::time::strptime(date_time.c_str(), "%Y-%m-%dT%T", &tm1);
return nanos + utils::time::windows_time_t_to_unix_time(_mkgmtime(&tm1)); return nanos + utils::time::windows_time_t_to_unix_time(_mkgmtime(&tm1));
@ -95,36 +96,12 @@ auto s3_provider::convert_api_date(std::string_view date) -> std::uint64_t {
#endif // defined(_WIN32) #endif // defined(_WIN32)
} }
auto s3_provider::create_directory_impl(const std::string &api_path, auto s3_provider::create_directory_object(const std::string &api_path,
api_meta_map &meta) -> api_error { const std::string &object_name) const
-> api_error {
REPERTORY_USES_FUNCTION_NAME(); REPERTORY_USES_FUNCTION_NAME();
const auto &cfg = get_s3_config(); 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; std::string response_data;
curl::requests::http_put_file put_dir{}; curl::requests::http_put_file put_dir{};
@ -141,6 +118,7 @@ auto s3_provider::create_directory_impl(const std::string &api_path,
return res; return res;
} }
stop_type stop_requested{false};
long response_code{}; long response_code{};
if (not get_comm().make_request(put_dir, response_code, stop_requested)) { if (not get_comm().make_request(put_dir, response_code, stop_requested)) {
utils::error::raise_api_path_error(function_name, api_path, 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; return api_error::success;
} }
auto s3_provider::create_directory_paths( auto s3_provider::create_directory_impl(const std::string &api_path,
const std::string &api_path, const std::string &key) const -> api_error { 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(); REPERTORY_USES_FUNCTION_NAME();
if (api_path == "/") { if (api_path == "/") {
@ -205,42 +218,16 @@ auto s3_provider::create_directory_paths(
} }
if (not exists) { if (not exists) {
std::string response_data; res = create_directory_object(api_path,
curl::requests::http_put_file put_dir{}; (is_encrypted ? cur_key : cur_path));
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) + '/');
if (res != api_error::success) { if (res != api_error::success) {
return res; 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, 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); 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; return 0U;
} }
auto s3_provider::get_directory_items_impl( auto s3_provider::get_directory_items_impl(const std::string &api_path,
const std::string &api_path, directory_item_list &list) const -> api_error { directory_item_list &list) const
-> api_error {
REPERTORY_USES_FUNCTION_NAME(); REPERTORY_USES_FUNCTION_NAME();
const auto &cfg{get_s3_config()}; const auto &cfg{get_s3_config()};
@ -519,8 +507,8 @@ auto s3_provider::get_directory_items_impl(
return api_error::success; return api_error::success;
} }
auto s3_provider::get_file(const std::string &api_path, auto s3_provider::get_file(const std::string &api_path, api_file &file) const
api_file &file) const -> api_error { -> api_error {
REPERTORY_USES_FUNCTION_NAME(); REPERTORY_USES_FUNCTION_NAME();
try { try {
@ -559,8 +547,8 @@ auto s3_provider::get_file(const std::string &api_path,
return api_error::error; return api_error::error;
} }
auto s3_provider::get_file_list(api_file_list &list, auto s3_provider::get_file_list(api_file_list &list, std::string &marker) const
std::string &marker) const -> api_error { -> api_error {
REPERTORY_USES_FUNCTION_NAME(); REPERTORY_USES_FUNCTION_NAME();
try { try {
@ -646,8 +634,9 @@ auto s3_provider::get_file_list(api_file_list &list,
return api_error::error; return api_error::error;
} }
auto s3_provider::get_last_modified( auto s3_provider::get_last_modified(bool directory,
bool directory, const std::string &api_path) const -> std::uint64_t { const std::string &api_path) const
-> std::uint64_t {
bool is_encrypted{}; bool is_encrypted{};
std::string object_name; std::string object_name;
head_object_result result{}; head_object_result result{};
@ -657,9 +646,10 @@ auto s3_provider::get_last_modified(
: utils::time::get_time_now(); : utils::time::get_time_now();
} }
auto s3_provider::get_object_info( auto s3_provider::get_object_info(bool directory, const std::string &api_path,
bool directory, const std::string &api_path, bool &is_encrypted, bool &is_encrypted, std::string &object_name,
std::string &object_name, head_object_result &result) const -> api_error { head_object_result &result) const
-> api_error {
REPERTORY_USES_FUNCTION_NAME(); REPERTORY_USES_FUNCTION_NAME();
try { try {
@ -718,10 +708,12 @@ auto s3_provider::get_object_info(
return api_error::error; return api_error::error;
} }
auto s3_provider::get_object_list( auto s3_provider::get_object_list(std::string &response_data,
std::string &response_data, long &response_code, long &response_code,
std::optional<std::string> delimiter, std::optional<std::string> prefix, std::optional<std::string> delimiter,
std::optional<std::string> token) const -> bool { std::optional<std::string> prefix,
std::optional<std::string> token) const
-> bool {
curl::requests::http_get get{}; curl::requests::http_get get{};
get.allow_timeout = true; get.allow_timeout = true;
get.aws_service = "aws:amz:" + get_s3_config().region + ":s3"; 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); return std::numeric_limits<std::int64_t>::max() / std::int64_t(2);
} }
auto s3_provider::is_directory(const std::string &api_path, auto s3_provider::is_directory(const std::string &api_path, bool &exists) const
bool &exists) const -> api_error { -> api_error {
REPERTORY_USES_FUNCTION_NAME(); REPERTORY_USES_FUNCTION_NAME();
try { try {
@ -777,8 +769,8 @@ auto s3_provider::is_directory(const std::string &api_path,
return api_error::error; return api_error::error;
} }
auto s3_provider::is_file(const std::string &api_path, auto s3_provider::is_file(const std::string &api_path, bool &exists) const
bool &exists) const -> api_error { -> api_error {
REPERTORY_USES_FUNCTION_NAME(); REPERTORY_USES_FUNCTION_NAME();
try { try {