extract common behavior
This commit is contained in:
parent
b3aa28d085
commit
1766f91697
@ -83,6 +83,10 @@ protected:
|
||||
return config_;
|
||||
}
|
||||
|
||||
[[nodiscard]] virtual auto
|
||||
get_directory_items_impl(const std::string &api_path,
|
||||
directory_item_list &list) const -> api_error = 0;
|
||||
|
||||
[[nodiscard]] auto get_db() const -> rocksdb::DB * { return db_.get(); }
|
||||
|
||||
[[nodiscard]] auto get_file_mgr() -> i_file_manager * { return fm_; }
|
||||
@ -107,6 +111,10 @@ public:
|
||||
std::string &api_path) const
|
||||
-> api_error override;
|
||||
|
||||
[[nodiscard]] auto get_directory_items(const std::string &api_path,
|
||||
directory_item_list &list) const
|
||||
-> api_error override;
|
||||
|
||||
[[nodiscard]] auto get_file_size(const std::string &api_path,
|
||||
std::uint64_t &file_size) const
|
||||
-> api_error override;
|
||||
|
@ -49,10 +49,6 @@ private:
|
||||
const std::string &object_name) const
|
||||
-> api_error;
|
||||
|
||||
[[nodiscard]] auto create_directory_impl(const std::string &api_path,
|
||||
api_meta_map &meta)
|
||||
-> api_error override;
|
||||
|
||||
[[nodiscard]] auto create_file_extra(const std::string &api_path,
|
||||
api_meta_map &meta)
|
||||
-> api_error override;
|
||||
@ -75,14 +71,19 @@ private:
|
||||
std::optional<std::string> prefix = std::nullopt) const
|
||||
-> bool;
|
||||
|
||||
protected:
|
||||
[[nodiscard]] auto create_directory_impl(const std::string &api_path,
|
||||
api_meta_map &meta)
|
||||
-> api_error override;
|
||||
|
||||
[[nodiscard]] auto get_directory_items_impl(const std::string &api_path,
|
||||
directory_item_list &list) const
|
||||
-> api_error override;
|
||||
|
||||
public:
|
||||
[[nodiscard]] auto get_directory_item_count(const std::string &api_path) const
|
||||
-> std::uint64_t override;
|
||||
|
||||
[[nodiscard]] auto get_directory_items(const std::string &api_path,
|
||||
directory_item_list &list) const
|
||||
-> api_error override;
|
||||
|
||||
[[nodiscard]] auto get_file(const std::string &api_path, api_file &file) const
|
||||
-> api_error override;
|
||||
|
||||
|
@ -54,14 +54,14 @@ protected:
|
||||
api_meta_map &meta)
|
||||
-> api_error override;
|
||||
|
||||
[[nodiscard]] auto get_directory_items_impl(const std::string &api_path,
|
||||
directory_item_list &list) const
|
||||
-> api_error override;
|
||||
|
||||
public:
|
||||
[[nodiscard]] auto get_directory_item_count(const std::string &api_path) const
|
||||
-> std::uint64_t override;
|
||||
|
||||
[[nodiscard]] auto get_directory_items(const std::string &api_path,
|
||||
directory_item_list &list) const
|
||||
-> api_error override;
|
||||
|
||||
[[nodiscard]] auto get_file(const std::string &api_path, api_file &file) const
|
||||
-> api_error override;
|
||||
|
||||
|
@ -246,6 +246,49 @@ auto base_provider::get_api_path_from_source(const std::string &source_path,
|
||||
return api_error::item_not_found;
|
||||
}
|
||||
|
||||
auto base_provider::get_directory_items(const std::string &api_path,
|
||||
directory_item_list &list) const
|
||||
-> api_error {
|
||||
bool exists{};
|
||||
auto res = is_directory(api_path, exists);
|
||||
if (res != api_error::success) {
|
||||
return res;
|
||||
}
|
||||
if (not exists) {
|
||||
return api_error::directory_not_found;
|
||||
}
|
||||
|
||||
try {
|
||||
res = get_directory_items_impl(api_path, list);
|
||||
if (res != api_error::success) {
|
||||
return res;
|
||||
}
|
||||
} catch (const std::exception &e) {
|
||||
utils::error::raise_api_path_error(__FUNCTION__, api_path, e,
|
||||
"failed to get directory items");
|
||||
return api_error::error;
|
||||
}
|
||||
|
||||
std::sort(list.begin(), list.end(),
|
||||
[](const auto &item1, const auto &item2) -> bool {
|
||||
return (item1.directory && not item2.directory) ||
|
||||
(not(item2.directory && not item1.directory) &&
|
||||
(item1.api_path.compare(item2.api_path) < 0));
|
||||
});
|
||||
|
||||
list.insert(list.begin(), directory_item{
|
||||
"..",
|
||||
"",
|
||||
true,
|
||||
});
|
||||
list.insert(list.begin(), directory_item{
|
||||
".",
|
||||
"",
|
||||
true,
|
||||
});
|
||||
return api_error::success;
|
||||
}
|
||||
|
||||
auto base_provider::get_file_size(const std::string &api_path,
|
||||
std::uint64_t &file_size) const -> api_error {
|
||||
bool exists{};
|
||||
|
@ -254,26 +254,18 @@ auto s3_provider::get_directory_item_count(const std::string &api_path) const
|
||||
return 0U;
|
||||
}
|
||||
|
||||
auto s3_provider::get_directory_items(const std::string &api_path,
|
||||
auto s3_provider::get_directory_items_impl(const std::string &api_path,
|
||||
directory_item_list &list) const
|
||||
-> api_error {
|
||||
try {
|
||||
bool exists{};
|
||||
auto res = is_directory(api_path, exists);
|
||||
if (res != api_error::success) {
|
||||
return res;
|
||||
}
|
||||
if (not exists) {
|
||||
return api_error::directory_not_found;
|
||||
}
|
||||
|
||||
const auto cfg = get_config().get_s3_config();
|
||||
const auto is_encrypted = not cfg.encryption_token.empty();
|
||||
|
||||
auto ret = api_error::success;
|
||||
std::string key;
|
||||
if (is_encrypted) {
|
||||
res = get_item_meta(api_path, META_KEY, key);
|
||||
if (res != api_error::success) {
|
||||
return res;
|
||||
ret = get_item_meta(api_path, META_KEY, key);
|
||||
if (ret != api_error::success) {
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
@ -313,10 +305,10 @@ auto s3_provider::get_directory_items(const std::string &api_path,
|
||||
std::string child_object_name;
|
||||
if (is_encrypted) {
|
||||
child_object_name = child_api_path;
|
||||
res = utils::encryption::decrypt_file_path(cfg.encryption_token,
|
||||
ret = utils::encryption::decrypt_file_path(cfg.encryption_token,
|
||||
child_api_path);
|
||||
if (res != api_error::success) {
|
||||
return res;
|
||||
if (ret != api_error::success) {
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
@ -325,35 +317,34 @@ auto s3_provider::get_directory_items(const std::string &api_path,
|
||||
dir_item.api_parent = utils::path::get_parent_api_path(dir_item.api_path);
|
||||
dir_item.directory = directory;
|
||||
dir_item.size = get_size(dir_item);
|
||||
res = get_item_meta(child_api_path, dir_item.meta);
|
||||
if (res == api_error::item_not_found) {
|
||||
ret = get_item_meta(child_api_path, dir_item.meta);
|
||||
if (ret == api_error::item_not_found) {
|
||||
if (directory) {
|
||||
res = create_path_directories(child_api_path, child_object_name);
|
||||
if (res != api_error::success) {
|
||||
return res;
|
||||
ret = create_path_directories(child_api_path, child_object_name);
|
||||
if (ret != api_error::success) {
|
||||
return ret;
|
||||
}
|
||||
} 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) {
|
||||
return res;
|
||||
ret = add_if_not_found(file, child_object_name);
|
||||
if (ret != api_error::success) {
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
res = get_item_meta(child_api_path, dir_item.meta);
|
||||
ret = get_item_meta(child_api_path, dir_item.meta);
|
||||
}
|
||||
|
||||
if (res != api_error::success) {
|
||||
return res;
|
||||
if (ret != api_error::success) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
list.push_back(std::move(dir_item));
|
||||
return api_error::success;
|
||||
};
|
||||
|
||||
auto node_list =
|
||||
doc.select_nodes("/ListBucketResult/CommonPrefixes/Prefix");
|
||||
auto node_list = doc.select_nodes("/ListBucketResult/CommonPrefixes/Prefix");
|
||||
for (const auto &node : node_list) {
|
||||
add_directory_item(
|
||||
true, node.node().text().as_string(),
|
||||
@ -376,29 +367,7 @@ auto s3_provider::get_directory_items(const std::string &api_path,
|
||||
}
|
||||
}
|
||||
|
||||
std::sort(list.begin(), list.end(),
|
||||
[](const auto &item1, const auto &item2) -> bool {
|
||||
return (item1.directory && not item2.directory) ||
|
||||
(not(item2.directory && not item1.directory) &&
|
||||
(item1.api_path.compare(item2.api_path) < 0));
|
||||
});
|
||||
|
||||
list.insert(list.begin(), directory_item{
|
||||
"..",
|
||||
"",
|
||||
true,
|
||||
});
|
||||
list.insert(list.begin(), directory_item{
|
||||
".",
|
||||
"",
|
||||
true,
|
||||
});
|
||||
return api_error::success;
|
||||
} catch (const std::exception &e) {
|
||||
utils::error::raise_error(__FUNCTION__, e, "exception occurred");
|
||||
}
|
||||
|
||||
return api_error::error;
|
||||
return ret;
|
||||
}
|
||||
|
||||
auto s3_provider::get_file(const std::string &api_path, api_file &file) const
|
||||
|
@ -99,19 +99,9 @@ auto sia_provider::get_directory_item_count(const std::string &api_path) const
|
||||
return 0U;
|
||||
}
|
||||
|
||||
auto sia_provider::get_directory_items(const std::string &api_path,
|
||||
auto sia_provider::get_directory_items_impl(const std::string &api_path,
|
||||
directory_item_list &list) const
|
||||
-> api_error {
|
||||
bool exists{};
|
||||
auto res = is_directory(api_path, exists);
|
||||
if (res != api_error::success) {
|
||||
return res;
|
||||
}
|
||||
if (not exists) {
|
||||
return api_error::directory_not_found;
|
||||
}
|
||||
|
||||
try {
|
||||
json object_list{};
|
||||
if (not get_object_list(api_path, object_list)) {
|
||||
return api_error::comm_error;
|
||||
@ -131,13 +121,12 @@ auto sia_provider::get_directory_items(const std::string &api_path,
|
||||
api_file file{};
|
||||
|
||||
api_meta_map meta{};
|
||||
if (get_item_meta(entry_api_path, meta) ==
|
||||
api_error::item_not_found) {
|
||||
if (get_item_meta(entry_api_path, meta) == api_error::item_not_found) {
|
||||
file = create_api_file(
|
||||
entry_api_path, "",
|
||||
directory ? 0U : entry["size"].get<std::uint64_t>());
|
||||
get_api_item_added()(directory, file);
|
||||
res = get_item_meta(entry_api_path, meta);
|
||||
auto res = get_item_meta(entry_api_path, meta);
|
||||
if (res != api_error::success) {
|
||||
utils::error::raise_error(__FUNCTION__, res,
|
||||
"failed to get item meta");
|
||||
@ -164,29 +153,7 @@ auto sia_provider::get_directory_items(const std::string &api_path,
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (const std::exception &e) {
|
||||
utils::error::raise_api_path_error(__FUNCTION__, api_path, e,
|
||||
"failed to get directory items");
|
||||
return api_error::error;
|
||||
}
|
||||
|
||||
std::sort(list.begin(), list.end(),
|
||||
[](const auto &item1, const auto &item2) -> bool {
|
||||
return (item1.directory && not item2.directory) ||
|
||||
(not(item2.directory && not item1.directory) &&
|
||||
(item1.api_path.compare(item2.api_path) < 0));
|
||||
});
|
||||
|
||||
list.insert(list.begin(), directory_item{
|
||||
"..",
|
||||
"",
|
||||
true,
|
||||
});
|
||||
list.insert(list.begin(), directory_item{
|
||||
".",
|
||||
"",
|
||||
true,
|
||||
});
|
||||
return api_error::success;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user