Tests passed.

This commit is contained in:
Your Name
2025-05-03 15:46:44 +12:00
parent d678f1923e
commit 3413cf832d
6 changed files with 84 additions and 230 deletions

View File

@@ -143,6 +143,11 @@ void Server::setup_routes() {
handle_get_hash(req, res);
});
// Check if object exists by hash or label:tag
server_.Get("/exists/(.*)", [this](const httplib::Request& req, httplib::Response& res) {
handle_exists(req, res);
});
// Get directory listing
server_.Get("/dir", [this](const httplib::Request& req, httplib::Response& res) {
handle_get_directory(req, res);
@@ -529,4 +534,48 @@ void Server::handle_append_tag(const httplib::Request& req, httplib::Response& r
res.set_content(response.dump(), "application/json");
}
void Server::handle_exists(const httplib::Request& req, httplib::Response& res) {
const auto& key = req.matches[1].str();
std::string hash_str = key;
// Check if the key is a label:tag format
dbEntry entry;
if (db_->get(key, entry)) {
// Got it from label:tag, use the hash
hash_str = entry.hash;
}
// If we couldn't determine a hash
if (hash_str.empty()) {
nlohmann::json response = {{"result", "success"}, {"exists", false}};
res.set_content(response.dump(), "application/json");
return;
}
// Try to interpret the key as a hash directly
uint64_t hash_value;
try {
hash_value = std::stoull(hash_str);
} catch (const std::invalid_argument& e) {
nlohmann::json response = {{"result", "success"}, {"exists", false}};
res.set_content(response.dump(), "application/json");
return;
}
std::stringstream oss;
oss << hash_value;
if (oss.str() != hash_str) {
nlohmann::json response = {{"result", "success"}, {"exists", false}};
res.set_content(response.dump(), "application/json");
return;
}
// Hash is valid, check if the file exists
std::filesystem::path file_path = config_.object_store_path / oss.str();
bool exists = std::filesystem::exists(file_path) && std::filesystem::is_regular_file(file_path);
nlohmann::json response = {{"result", "success"}, {"exists", exists}};
res.set_content(response.dump(), "application/json");
}
} // namespace simple_object_storage