extract common behavior
All checks were successful
BlockStorage/repertory_osx_builds/pipeline/head This commit looks good
BlockStorage/repertory_linux_builds/pipeline/head This commit looks good

This commit is contained in:
2023-11-14 19:49:46 -06:00
parent 1766f91697
commit ee34a1e361
6 changed files with 239 additions and 279 deletions

View File

@@ -43,6 +43,7 @@ auto sia_provider::create_directory_impl(const std::string &api_path,
api_meta_map & /* meta */)
-> api_error {
curl::requests::http_put_file put_file{};
put_file.allow_timeout = true;
put_file.path = "/api/worker/objects" + api_path + "/";
long response_code{};
@@ -538,26 +539,8 @@ auto sia_provider::read_file_bytes(const std::string &api_path,
return res;
}
auto sia_provider::remove_directory(const std::string &api_path) -> api_error {
const auto notify_end = [&api_path](api_error error) -> api_error {
if (error == api_error::success) {
event_system::instance().raise<directory_removed>(api_path);
} else {
event_system::instance().raise<directory_remove_failed>(
api_path, api_error_to_string(error));
}
return error;
};
bool exists{};
auto res = is_directory(api_path, exists);
if (res != api_error::success) {
return res;
}
if (not exists) {
return notify_end(api_error::item_not_found);
}
auto sia_provider::remove_directory_impl(const std::string &api_path)
-> api_error {
curl::requests::http_delete del{};
del.allow_timeout = true;
del.path = "/api/bus/objects" + api_path + "/";
@@ -568,71 +551,19 @@ auto sia_provider::remove_directory(const std::string &api_path) -> api_error {
utils::error::raise_api_path_error(__FUNCTION__, api_path,
api_error::comm_error,
"failed to remove directory");
return notify_end(api_error::comm_error);
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 remove directory");
return notify_end(api_error::comm_error);
return api_error::comm_error;
}
auto res2 = get_db()->Delete(rocksdb::WriteOptions(), api_path);
if (not res2.ok()) {
utils::error::raise_api_path_error(__FUNCTION__, api_path, res2.code(),
"failed to remove directory");
return notify_end(api_error::error);
}
return notify_end(api_error::success);
return api_error::success;
}
auto sia_provider::remove_file(const std::string &api_path) -> api_error {
const auto notify_end = [&api_path](api_error error) -> api_error {
if (error == api_error::success) {
event_system::instance().raise<file_removed>(api_path);
} else {
event_system::instance().raise<file_remove_failed>(
api_path, api_error_to_string(error));
}
return error;
};
const auto remove_file_meta = [this, &api_path, &notify_end]() -> api_error {
api_meta_map meta{};
auto res = get_item_meta(api_path, meta);
auto res2 = get_db()->Delete(rocksdb::WriteOptions(), api_path);
if (not res2.ok()) {
utils::error::raise_api_path_error(__FUNCTION__, api_path, res2.code(),
"failed to remove file");
return notify_end(api_error::error);
}
return notify_end(res);
};
bool exists{};
auto res = is_directory(api_path, exists);
if (res != api_error::success) {
return notify_end(res);
}
if (exists) {
exists = false;
return notify_end(api_error::directory_exists);
}
res = is_file(api_path, exists);
if (res != api_error::success) {
return notify_end(res);
}
if (not exists) {
event_system::instance().raise<file_remove_failed>(
api_path, api_error_to_string(api_error::item_not_found));
return remove_file_meta();
}
auto sia_provider::remove_file_impl(const std::string &api_path) -> api_error {
curl::requests::http_delete del{};
del.allow_timeout = true;
del.path = "/api/bus/objects" + api_path;
@@ -642,17 +573,17 @@ auto sia_provider::remove_file(const std::string &api_path) -> api_error {
if (not get_comm().make_request(del, response_code, stop_requested)) {
utils::error::raise_api_path_error(
__FUNCTION__, api_path, api_error::comm_error, "failed to remove file");
return notify_end(api_error::comm_error);
return api_error::comm_error;
}
if (response_code != http_error_codes::ok &&
response_code != http_error_codes::not_found) {
utils::error::raise_api_path_error(__FUNCTION__, api_path, response_code,
"failed to remove file");
return notify_end(api_error::comm_error);
return api_error::comm_error;
}
return remove_file_meta();
return api_error::success;
}
auto sia_provider::rename_file(const std::string & /*from_api_path*/,
@@ -673,44 +604,27 @@ void sia_provider::stop() {
event_system::instance().raise<service_shutdown_end>("sia_provider");
}
auto sia_provider::upload_file(const std::string &api_path,
const std::string &source_path,
stop_type &stop_requested) -> api_error {
event_system::instance().raise<provider_upload_begin>(api_path, source_path);
auto sia_provider::upload_file_impl(const std::string &api_path,
const std::string &source_path,
stop_type &stop_requested) -> api_error {
curl::requests::http_put_file put_file{};
put_file.path = "/api/worker/objects" + api_path;
put_file.source_path = source_path;
const auto notify_end = [&api_path,
&source_path](api_error error) -> api_error {
event_system::instance().raise<provider_upload_end>(api_path, source_path,
error);
return error;
};
try {
curl::requests::http_put_file put_file{};
put_file.path = "/api/worker/objects" + api_path;
put_file.source_path = source_path;
long response_code{};
if (not get_comm().make_request(put_file, response_code, stop_requested)) {
utils::error::raise_api_path_error(__FUNCTION__, api_path, source_path,
api_error::comm_error,
"failed to upload file");
return notify_end(api_error::comm_error);
}
if (response_code != http_error_codes::ok) {
utils::error::raise_api_path_error(__FUNCTION__, api_path, source_path,
response_code,
"failed to upload file");
return notify_end(api_error::comm_error);
}
return notify_end(api_error::success);
} catch (const std::exception &e) {
utils::error::raise_api_path_error(__FUNCTION__, api_path, source_path, e,
long response_code{};
if (not get_comm().make_request(put_file, response_code, stop_requested)) {
utils::error::raise_api_path_error(__FUNCTION__, api_path, source_path,
api_error::comm_error,
"failed to upload file");
return api_error::comm_error;
}
return notify_end(api_error::error);
if (response_code != http_error_codes::ok) {
utils::error::raise_api_path_error(__FUNCTION__, api_path, source_path,
response_code, "failed to upload file");
return api_error::comm_error;
}
return api_error::success;
}
} // namespace repertory