refactor s3 provider
This commit is contained in:
parent
b87e1df140
commit
1ee533591c
@ -71,9 +71,9 @@ private:
|
|||||||
-> api_error;
|
-> api_error;
|
||||||
|
|
||||||
[[nodiscard]] auto
|
[[nodiscard]] auto
|
||||||
get_object_info(const std::string &api_path, bool &is_encrypted,
|
get_object_info(bool directory, const std::string &api_path,
|
||||||
std::string &object_name, head_object_result &result) const
|
bool &is_encrypted, std::string &object_name,
|
||||||
-> api_error;
|
head_object_result &result) const -> api_error;
|
||||||
|
|
||||||
void remove_deleted_files();
|
void remove_deleted_files();
|
||||||
|
|
||||||
|
@ -92,9 +92,9 @@ auto s3_provider::create_api_file(const std::string &path,
|
|||||||
const std::string &key, std::uint64_t size)
|
const std::string &key, std::uint64_t size)
|
||||||
-> api_file {
|
-> api_file {
|
||||||
api_file file{};
|
api_file file{};
|
||||||
file.accessed_date = utils::get_file_time_now();
|
|
||||||
file.api_parent = utils::path::get_parent_api_path(file.api_path);
|
|
||||||
file.api_path = utils::path::create_api_path(path);
|
file.api_path = utils::path::create_api_path(path);
|
||||||
|
file.api_parent = utils::path::get_parent_api_path(file.api_path);
|
||||||
|
file.accessed_date = utils::get_file_time_now();
|
||||||
file.changed_date = utils::get_file_time_now();
|
file.changed_date = utils::get_file_time_now();
|
||||||
file.creation_date = utils::get_file_time_now();
|
file.creation_date = utils::get_file_time_now();
|
||||||
file.file_size = size;
|
file.file_size = size;
|
||||||
@ -322,8 +322,8 @@ auto s3_provider::create_path_directories(const std::string &api_path,
|
|||||||
return api_error::error;
|
return api_error::error;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string cur_key;
|
std::string cur_key{'/'};
|
||||||
std::string cur_path;
|
std::string cur_path{'/'};
|
||||||
for (std::size_t i = 0U; i < path_parts.size(); i++) {
|
for (std::size_t i = 0U; i < path_parts.size(); i++) {
|
||||||
if (is_encrypted) {
|
if (is_encrypted) {
|
||||||
cur_key = utils::path::create_api_path(
|
cur_key = utils::path::create_api_path(
|
||||||
@ -407,7 +407,6 @@ auto s3_provider::get_directory_items(const std::string &api_path,
|
|||||||
try {
|
try {
|
||||||
const auto cfg = config_.get_s3_config();
|
const auto cfg = config_.get_s3_config();
|
||||||
const auto is_encrypted = not cfg.encryption_token.empty();
|
const auto is_encrypted = not cfg.encryption_token.empty();
|
||||||
|
|
||||||
std::string key;
|
std::string key;
|
||||||
if (is_encrypted) {
|
if (is_encrypted) {
|
||||||
auto res = get_item_meta(api_path, META_KEY, key);
|
auto res = get_item_meta(api_path, META_KEY, key);
|
||||||
@ -466,10 +465,16 @@ auto s3_provider::get_directory_items(const std::string &api_path,
|
|||||||
dir_item.directory = directory;
|
dir_item.directory = directory;
|
||||||
dir_item.size = get_size(dir_item);
|
dir_item.size = get_size(dir_item);
|
||||||
auto res = get_item_meta(child_api_path, dir_item.meta);
|
auto res = get_item_meta(child_api_path, dir_item.meta);
|
||||||
if (not directory && res == api_error::item_not_found) {
|
if (res == api_error::item_not_found) {
|
||||||
api_file file{};
|
if (directory) {
|
||||||
res = get_file(child_api_path, file);
|
res = create_path_directories(child_api_path, child_object_name);
|
||||||
|
} else {
|
||||||
|
auto file =
|
||||||
|
create_api_file(child_api_path, child_object_name, dir_item.size);
|
||||||
|
res = add_if_not_found(file, child_object_name);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (res != api_error::success) {
|
if (res != api_error::success) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
@ -487,9 +492,9 @@ auto s3_provider::get_directory_items(const std::string &api_path,
|
|||||||
|
|
||||||
node_list = doc.select_nodes("/ListBucketResult/Contents");
|
node_list = doc.select_nodes("/ListBucketResult/Contents");
|
||||||
for (const auto &node : node_list) {
|
for (const auto &node : node_list) {
|
||||||
auto child_object_name =
|
auto child_object_name = utils::path::create_api_path(
|
||||||
std::string{node.node().select_node("Key").node().text().as_string()};
|
node.node().select_node("Key").node().text().as_string());
|
||||||
if (child_object_name != prefix) {
|
if (child_object_name != utils::path::create_api_path(prefix)) {
|
||||||
auto size = node.node().select_node("Size").node().text().as_ullong();
|
auto size = node.node().select_node("Size").node().text().as_ullong();
|
||||||
add_directory_item(
|
add_directory_item(
|
||||||
false, child_object_name,
|
false, child_object_name,
|
||||||
@ -532,7 +537,8 @@ auto s3_provider::get_file(const std::string &api_path, api_file &file) const
|
|||||||
bool is_encrypted{};
|
bool is_encrypted{};
|
||||||
std::string object_name;
|
std::string object_name;
|
||||||
head_object_result result{};
|
head_object_result result{};
|
||||||
auto res = get_object_info(api_path, is_encrypted, object_name, result);
|
auto res =
|
||||||
|
get_object_info(false, api_path, is_encrypted, object_name, result);
|
||||||
if (res != api_error::success) {
|
if (res != api_error::success) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
@ -716,7 +722,7 @@ auto s3_provider::get_filesystem_item_from_source_path(
|
|||||||
return get_filesystem_item(api_path, false, fsi);
|
return get_filesystem_item(api_path, false, fsi);
|
||||||
}
|
}
|
||||||
|
|
||||||
auto s3_provider::get_object_info(const std::string &api_path,
|
auto s3_provider::get_object_info(bool directory, const std::string &api_path,
|
||||||
bool &is_encrypted, std::string &object_name,
|
bool &is_encrypted, std::string &object_name,
|
||||||
head_object_result &result) const
|
head_object_result &result) const
|
||||||
-> api_error {
|
-> api_error {
|
||||||
@ -737,7 +743,7 @@ auto s3_provider::get_object_info(const std::string &api_path,
|
|||||||
curl::requests::http_head head{};
|
curl::requests::http_head head{};
|
||||||
head.allow_timeout = true;
|
head.allow_timeout = true;
|
||||||
head.aws_service = "aws:amz:" + cfg.region + ":s3";
|
head.aws_service = "aws:amz:" + cfg.region + ":s3";
|
||||||
head.path = object_name;
|
head.path = directory ? object_name + '/' : object_name;
|
||||||
head.response_headers = http_headers{};
|
head.response_headers = http_headers{};
|
||||||
|
|
||||||
stop_type stop_requested{false};
|
stop_type stop_requested{false};
|
||||||
@ -832,19 +838,18 @@ auto s3_provider::get_used_drive_space() const -> std::uint64_t {}
|
|||||||
|
|
||||||
auto s3_provider::is_directory(const std::string &api_path, bool &exists) const
|
auto s3_provider::is_directory(const std::string &api_path, bool &exists) const
|
||||||
-> api_error {
|
-> api_error {
|
||||||
|
exists = false;
|
||||||
if (api_path == "/") {
|
if (api_path == "/") {
|
||||||
exists = true;
|
exists = true;
|
||||||
return api_error::success;
|
return api_error::success;
|
||||||
}
|
}
|
||||||
|
|
||||||
exists = false;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
bool is_encrypted{};
|
bool is_encrypted{};
|
||||||
std::string object_name;
|
std::string object_name;
|
||||||
head_object_result result{};
|
head_object_result result{};
|
||||||
auto res = get_object_info(utils::path::create_api_path(api_path) + '/',
|
auto res =
|
||||||
is_encrypted, object_name, result);
|
get_object_info(true, api_path, is_encrypted, object_name, result);
|
||||||
if (res != api_error::item_not_found && res != api_error::success) {
|
if (res != api_error::item_not_found && res != api_error::success) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
@ -859,18 +864,17 @@ auto s3_provider::is_directory(const std::string &api_path, bool &exists) const
|
|||||||
|
|
||||||
auto s3_provider::is_file(const std::string &api_path, bool &exists) const
|
auto s3_provider::is_file(const std::string &api_path, bool &exists) const
|
||||||
-> api_error {
|
-> api_error {
|
||||||
|
exists = false;
|
||||||
if (api_path == "/") {
|
if (api_path == "/") {
|
||||||
return api_error::success;
|
return api_error::success;
|
||||||
}
|
}
|
||||||
|
|
||||||
exists = false;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
bool is_encrypted{};
|
bool is_encrypted{};
|
||||||
std::string object_name;
|
std::string object_name;
|
||||||
head_object_result result{};
|
head_object_result result{};
|
||||||
auto res = get_object_info(utils::path::create_api_path(api_path),
|
auto res =
|
||||||
is_encrypted, object_name, result);
|
get_object_info(false, api_path, is_encrypted, object_name, result);
|
||||||
if (res != api_error::item_not_found && res != api_error::success) {
|
if (res != api_error::item_not_found && res != api_error::success) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
@ -819,12 +819,11 @@ auto sia_provider::is_directory(const std::string &api_path, bool &exists) const
|
|||||||
|
|
||||||
auto sia_provider::is_file(const std::string &api_path, bool &exists) const
|
auto sia_provider::is_file(const std::string &api_path, bool &exists) const
|
||||||
-> api_error {
|
-> api_error {
|
||||||
|
exists = false;
|
||||||
if (api_path == "/") {
|
if (api_path == "/") {
|
||||||
return api_error::success;
|
return api_error::success;
|
||||||
}
|
}
|
||||||
|
|
||||||
exists = false;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
json file_data{};
|
json file_data{};
|
||||||
auto res = get_object_info(api_path, file_data);
|
auto res = get_object_info(api_path, file_data);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user