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

@@ -563,6 +563,97 @@ void base_provider::remove_deleted_files() {
}
}
auto base_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 *const function_name = __FUNCTION__;
const auto remove_file_meta = [this, &api_path, &function_name,
&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_name, 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();
}
res = remove_file_impl(api_path);
if (res != api_error::success) {
return notify_end(res);
}
return remove_file_meta();
}
auto base_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 notify_end(res);
}
if (not exists) {
return notify_end(api_error::item_not_found);
}
res = remove_directory_impl(api_path);
if (res != api_error::success) {
return notify_end(res);
}
auto status = get_db()->Delete(rocksdb::WriteOptions(), api_path);
if (not status.ok()) {
utils::error::raise_api_path_error(__FUNCTION__, api_path, status.code(),
"failed to remove directory");
return notify_end(api_error::error);
}
return notify_end(api_error::success);
}
auto base_provider::remove_item_meta(const std::string &api_path,
const std::string &key) -> api_error {
api_meta_map meta{};
@@ -687,4 +778,24 @@ void base_provider::stop() {
polling::instance().remove_callback("check_deleted");
db_.reset();
}
auto base_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);
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 {
return notify_end(upload_file_impl(api_path, source_path, stop_requested));
} catch (const std::exception &e) {
utils::error::raise_error(__FUNCTION__, e, "exception occurred");
}
return notify_end(api_error::error);
}
} // namespace repertory