extract common behavior
This commit is contained in:
@@ -114,6 +114,106 @@ auto base_provider::create_directory_clone_source_meta(
|
||||
return create_directory(api_path, meta);
|
||||
}
|
||||
|
||||
auto base_provider::create_directory(const std::string &api_path,
|
||||
api_meta_map &meta) -> api_error {
|
||||
bool exists{};
|
||||
auto res = is_directory(api_path, exists);
|
||||
if (res != api_error::success) {
|
||||
return res;
|
||||
}
|
||||
if (exists) {
|
||||
utils::error::raise_api_path_error(__FUNCTION__, api_path,
|
||||
api_error::directory_exists,
|
||||
"failed to create directory");
|
||||
return api_error::directory_exists;
|
||||
}
|
||||
|
||||
res = is_file(api_path, exists);
|
||||
if (res != api_error::success) {
|
||||
utils::error::raise_api_path_error(__FUNCTION__, api_path, res,
|
||||
"failed to create directory");
|
||||
return res;
|
||||
}
|
||||
if (exists) {
|
||||
utils::error::raise_api_path_error(__FUNCTION__, api_path,
|
||||
api_error::item_exists,
|
||||
"failed to create directory");
|
||||
return api_error::item_exists;
|
||||
}
|
||||
|
||||
try {
|
||||
res = create_directory_impl(api_path, meta);
|
||||
if (res != api_error::success) {
|
||||
utils::error::raise_api_path_error(__FUNCTION__, api_path, res,
|
||||
"failed to create directory");
|
||||
return res;
|
||||
}
|
||||
} catch (const std::exception &e) {
|
||||
utils::error::raise_api_path_error(__FUNCTION__, api_path, e,
|
||||
"failed to create directory");
|
||||
return api_error::error;
|
||||
}
|
||||
|
||||
meta[META_DIRECTORY] = utils::string::from_bool(true);
|
||||
return set_item_meta(api_path, meta);
|
||||
}
|
||||
|
||||
auto base_provider::create_file(const std::string &api_path, api_meta_map &meta)
|
||||
-> api_error {
|
||||
bool exists{};
|
||||
auto res = is_directory(api_path, exists);
|
||||
if (res != api_error::success && res != api_error::item_not_found) {
|
||||
return res;
|
||||
}
|
||||
if (exists) {
|
||||
utils::error::raise_api_path_error(__FUNCTION__, api_path,
|
||||
api_error::directory_exists,
|
||||
"failed to create file");
|
||||
return api_error::directory_exists;
|
||||
}
|
||||
|
||||
res = is_file(api_path, exists);
|
||||
if (res != api_error::success && res != api_error::item_not_found) {
|
||||
return res;
|
||||
}
|
||||
if (exists) {
|
||||
utils::error::raise_api_path_error(__FUNCTION__, api_path,
|
||||
api_error::item_exists,
|
||||
"failed to create file");
|
||||
return api_error::item_exists;
|
||||
}
|
||||
|
||||
try {
|
||||
res = create_file_extra(api_path, meta);
|
||||
if (res != api_error::success) {
|
||||
utils::error::raise_api_path_error(__FUNCTION__, api_path, res,
|
||||
"failed to create file");
|
||||
return res;
|
||||
}
|
||||
|
||||
meta[META_DIRECTORY] = utils::string::from_bool(false);
|
||||
res = set_item_meta(api_path, meta);
|
||||
if (res != api_error::success) {
|
||||
utils::error::raise_api_path_error(__FUNCTION__, api_path, res,
|
||||
"failed to create file");
|
||||
return res;
|
||||
}
|
||||
|
||||
stop_type stop_requested{false};
|
||||
res = upload_file(api_path, meta[META_SOURCE], stop_requested);
|
||||
if (res != api_error::success) {
|
||||
get_db()->Delete(rocksdb::WriteOptions(), api_path);
|
||||
}
|
||||
|
||||
return res;
|
||||
} catch (const std::exception &e) {
|
||||
utils::error::raise_api_path_error(__FUNCTION__, api_path, e,
|
||||
"failed to create file");
|
||||
}
|
||||
|
||||
return api_error::error;
|
||||
}
|
||||
|
||||
auto base_provider::get_api_path_from_source(const std::string &source_path,
|
||||
std::string &api_path) const
|
||||
-> api_error {
|
||||
|
@@ -90,155 +90,80 @@ auto s3_provider::add_if_not_found(api_file &file,
|
||||
return api_error::success;
|
||||
}
|
||||
|
||||
auto s3_provider::create_directory(const std::string &api_path,
|
||||
api_meta_map &meta) -> api_error {
|
||||
|
||||
bool exists{};
|
||||
auto res = is_directory(api_path, exists);
|
||||
if (res != api_error::success) {
|
||||
return res;
|
||||
}
|
||||
if (exists) {
|
||||
utils::error::raise_api_path_error(__FUNCTION__, api_path,
|
||||
api_error::directory_exists,
|
||||
"failed to create directory");
|
||||
return api_error::directory_exists;
|
||||
}
|
||||
|
||||
res = is_file(api_path, exists);
|
||||
if (res != api_error::success) {
|
||||
return res;
|
||||
}
|
||||
if (exists) {
|
||||
utils::error::raise_api_path_error(__FUNCTION__, api_path,
|
||||
api_error::item_exists,
|
||||
"failed to create directory");
|
||||
return api_error::item_exists;
|
||||
}
|
||||
|
||||
try {
|
||||
const auto cfg = get_config().get_s3_config();
|
||||
const auto is_encrypted = not cfg.encryption_token.empty();
|
||||
stop_type stop_requested{false};
|
||||
|
||||
if (is_encrypted) {
|
||||
std::string encrypted_file_path;
|
||||
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__, 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::to_hex_string(result)}));
|
||||
}
|
||||
|
||||
const auto object_name =
|
||||
utils::path::create_api_path(is_encrypted ? meta[META_KEY] : api_path);
|
||||
|
||||
curl::requests::http_put_file put_file{};
|
||||
put_file.allow_timeout = true;
|
||||
put_file.aws_service = "aws:amz:" + cfg.region + ":s3";
|
||||
put_file.path = object_name + '/';
|
||||
|
||||
long response_code{};
|
||||
if (not get_comm().make_request(put_file, response_code, stop_requested)) {
|
||||
utils::error::raise_api_path_error(__FUNCTION__, api_path,
|
||||
api_error::comm_error,
|
||||
"failed to create directory");
|
||||
return api_error::comm_error;
|
||||
}
|
||||
|
||||
if (response_code != http_error_codes::ok) {
|
||||
utils::error::raise_api_path_error(__FUNCTION__, api_path, response_code,
|
||||
"failed to create directory");
|
||||
return api_error::comm_error;
|
||||
}
|
||||
} catch (const std::exception &e) {
|
||||
utils::error::raise_api_path_error(__FUNCTION__, api_path, e,
|
||||
"failed to create directory");
|
||||
return api_error::error;
|
||||
}
|
||||
|
||||
meta[META_DIRECTORY] = utils::string::from_bool(true);
|
||||
return set_item_meta(api_path, meta);
|
||||
}
|
||||
|
||||
auto s3_provider::create_file(const std::string &api_path, api_meta_map &meta)
|
||||
-> api_error {
|
||||
bool exists{};
|
||||
auto res = is_directory(api_path, exists);
|
||||
if (res != api_error::success && res != api_error::item_not_found) {
|
||||
return res;
|
||||
}
|
||||
if (exists) {
|
||||
utils::error::raise_api_path_error(__FUNCTION__, api_path,
|
||||
api_error::directory_exists,
|
||||
"failed to create file");
|
||||
return api_error::directory_exists;
|
||||
}
|
||||
|
||||
res = is_file(api_path, exists);
|
||||
if (res != api_error::success && res != api_error::item_not_found) {
|
||||
return res;
|
||||
}
|
||||
if (exists) {
|
||||
utils::error::raise_api_path_error(__FUNCTION__, api_path,
|
||||
api_error::item_exists,
|
||||
"failed to create file");
|
||||
return api_error::item_exists;
|
||||
}
|
||||
|
||||
auto s3_provider::create_directory_impl(const std::string &api_path,
|
||||
api_meta_map &meta) -> api_error {
|
||||
const auto cfg = get_config().get_s3_config();
|
||||
const auto is_encrypted = not cfg.encryption_token.empty();
|
||||
stop_type stop_requested{false};
|
||||
try {
|
||||
if (not get_config().get_s3_config().encryption_token.empty()) {
|
||||
std::string encrypted_file_path;
|
||||
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__, api_path, res,
|
||||
"failed to create file");
|
||||
return res;
|
||||
}
|
||||
|
||||
data_buffer result;
|
||||
utils::encryption::encrypt_data(
|
||||
get_config().get_s3_config().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::to_hex_string(result)}));
|
||||
}
|
||||
|
||||
meta[META_DIRECTORY] = utils::string::from_bool(false);
|
||||
res = set_item_meta(api_path, meta);
|
||||
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__, api_path, res,
|
||||
"failed to create file");
|
||||
return res;
|
||||
}
|
||||
|
||||
res = upload_file(api_path, meta[META_SOURCE], stop_requested);
|
||||
if (res != api_error::success) {
|
||||
get_db()->Delete(rocksdb::WriteOptions(), api_path);
|
||||
}
|
||||
data_buffer result;
|
||||
utils::encryption::encrypt_data(
|
||||
cfg.encryption_token,
|
||||
*(utils::string::split(api_path, '/', false).end() - 1U), result);
|
||||
|
||||
return res;
|
||||
} catch (const std::exception &e) {
|
||||
utils::error::raise_api_path_error(__FUNCTION__, api_path, e,
|
||||
"failed to create file");
|
||||
meta[META_KEY] = utils::path::create_api_path(
|
||||
utils::path::combine(utils::path::create_api_path(encrypted_file_path),
|
||||
{utils::to_hex_string(result)}));
|
||||
}
|
||||
|
||||
return api_error::error;
|
||||
const auto object_name =
|
||||
utils::path::create_api_path(is_encrypted ? meta[META_KEY] : api_path);
|
||||
|
||||
curl::requests::http_put_file put_file{};
|
||||
put_file.allow_timeout = true;
|
||||
put_file.aws_service = "aws:amz:" + cfg.region + ":s3";
|
||||
put_file.path = object_name + '/';
|
||||
|
||||
long response_code{};
|
||||
if (not get_comm().make_request(put_file, response_code, stop_requested)) {
|
||||
utils::error::raise_api_path_error(__FUNCTION__, api_path,
|
||||
api_error::comm_error,
|
||||
"failed to create directory");
|
||||
return api_error::comm_error;
|
||||
}
|
||||
|
||||
if (response_code != http_error_codes::ok) {
|
||||
utils::error::raise_api_path_error(__FUNCTION__, api_path, response_code,
|
||||
"failed to create directory");
|
||||
return api_error::comm_error;
|
||||
}
|
||||
|
||||
return api_error::success;
|
||||
}
|
||||
|
||||
auto s3_provider::create_file_extra(const std::string &api_path,
|
||||
api_meta_map &meta) -> api_error {
|
||||
if (not get_config().get_s3_config().encryption_token.empty()) {
|
||||
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__, api_path, res,
|
||||
"failed to create file");
|
||||
return res;
|
||||
}
|
||||
|
||||
data_buffer result;
|
||||
utils::encryption::encrypt_data(
|
||||
get_config().get_s3_config().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::to_hex_string(result)}));
|
||||
}
|
||||
|
||||
return api_error::success;
|
||||
}
|
||||
|
||||
auto s3_provider::create_path_directories(const std::string &api_path,
|
||||
|
@@ -109,110 +109,28 @@ auto sia_provider::get_object_list(const std::string &api_path,
|
||||
return true;
|
||||
}
|
||||
|
||||
auto sia_provider::create_directory(const std::string &api_path,
|
||||
api_meta_map &meta) -> api_error {
|
||||
bool exists{};
|
||||
auto res = is_directory(api_path, exists);
|
||||
if (res != api_error::success) {
|
||||
return res;
|
||||
}
|
||||
if (exists) {
|
||||
utils::error::raise_api_path_error(__FUNCTION__, api_path,
|
||||
api_error::directory_exists,
|
||||
"failed to create directory");
|
||||
return api_error::directory_exists;
|
||||
}
|
||||
|
||||
res = is_file(api_path, exists);
|
||||
if (res != api_error::success) {
|
||||
return res;
|
||||
}
|
||||
if (exists) {
|
||||
utils::error::raise_api_path_error(__FUNCTION__, api_path,
|
||||
api_error::item_exists,
|
||||
"failed to create directory");
|
||||
return api_error::item_exists;
|
||||
}
|
||||
|
||||
try {
|
||||
curl::requests::http_put_file put_file{};
|
||||
put_file.path = "/api/worker/objects" + api_path + "/";
|
||||
|
||||
long response_code{};
|
||||
stop_type stop_requested{};
|
||||
if (not get_comm().make_request(put_file, response_code, stop_requested)) {
|
||||
utils::error::raise_api_path_error(__FUNCTION__, api_path,
|
||||
api_error::comm_error,
|
||||
"failed to create directory");
|
||||
return api_error::comm_error;
|
||||
}
|
||||
|
||||
if (response_code != http_error_codes::ok) {
|
||||
utils::error::raise_api_path_error(__FUNCTION__, api_path, response_code,
|
||||
"failed to create directory");
|
||||
return api_error::comm_error;
|
||||
}
|
||||
} catch (const std::exception &e) {
|
||||
utils::error::raise_api_path_error(__FUNCTION__, api_path, e,
|
||||
"failed to create directory");
|
||||
return api_error::error;
|
||||
}
|
||||
|
||||
meta[META_DIRECTORY] = utils::string::from_bool(true);
|
||||
return set_item_meta(api_path, meta);
|
||||
}
|
||||
|
||||
auto sia_provider::create_file(const std::string &api_path, api_meta_map &meta)
|
||||
auto sia_provider::create_directory_impl(const std::string &api_path,
|
||||
api_meta_map & /* meta */)
|
||||
-> api_error {
|
||||
bool exists{};
|
||||
auto res = is_directory(api_path, exists);
|
||||
if (res != api_error::success && res != api_error::item_not_found) {
|
||||
return res;
|
||||
}
|
||||
if (exists) {
|
||||
curl::requests::http_put_file put_file{};
|
||||
put_file.path = "/api/worker/objects" + api_path + "/";
|
||||
|
||||
long response_code{};
|
||||
stop_type stop_requested{};
|
||||
if (not get_comm().make_request(put_file, response_code, stop_requested)) {
|
||||
utils::error::raise_api_path_error(__FUNCTION__, api_path,
|
||||
api_error::directory_exists,
|
||||
"failed to create file");
|
||||
return api_error::directory_exists;
|
||||
api_error::comm_error,
|
||||
"failed to create directory");
|
||||
return api_error::comm_error;
|
||||
}
|
||||
|
||||
res = is_file(api_path, exists);
|
||||
if (res != api_error::success && res != api_error::item_not_found) {
|
||||
return res;
|
||||
}
|
||||
if (exists) {
|
||||
utils::error::raise_api_path_error(__FUNCTION__, api_path,
|
||||
api_error::item_exists,
|
||||
"failed to create file");
|
||||
return api_error::item_exists;
|
||||
if (response_code != http_error_codes::ok) {
|
||||
utils::error::raise_api_path_error(__FUNCTION__, api_path, response_code,
|
||||
"failed to create directory");
|
||||
return api_error::comm_error;
|
||||
}
|
||||
|
||||
try {
|
||||
curl::requests::http_put_file put_file{};
|
||||
put_file.path = "/api/worker/objects" + api_path;
|
||||
|
||||
long response_code{};
|
||||
stop_type stop_requested{};
|
||||
if (not get_comm().make_request(put_file, response_code, stop_requested)) {
|
||||
utils::error::raise_api_path_error(__FUNCTION__, api_path,
|
||||
api_error::comm_error,
|
||||
"failed to create file");
|
||||
return api_error::comm_error;
|
||||
}
|
||||
|
||||
if (response_code != http_error_codes::ok) {
|
||||
utils::error::raise_api_path_error(__FUNCTION__, api_path, response_code,
|
||||
"failed to create file");
|
||||
return api_error::comm_error;
|
||||
}
|
||||
} catch (const std::exception &e) {
|
||||
utils::error::raise_api_path_error(__FUNCTION__, api_path, e,
|
||||
"failed to create file");
|
||||
return api_error::error;
|
||||
}
|
||||
|
||||
meta[META_DIRECTORY] = utils::string::from_bool(false);
|
||||
return set_item_meta(api_path, meta);
|
||||
return api_error::success;
|
||||
}
|
||||
|
||||
auto sia_provider::get_directory_item_count(const std::string &api_path) const
|
||||
|
Reference in New Issue
Block a user