From 652ef879029db4ff10147e0bdc2bf8b471d287b8 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 25 May 2025 12:00:57 +1200 Subject: [PATCH] Bug fixing --- src/server.cpp | 67 +++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 58 insertions(+), 9 deletions(-) diff --git a/src/server.cpp b/src/server.cpp index 472ad57..a6bcccc 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -270,25 +270,74 @@ void Server::handle_get_directory(const httplib::Request& /*req*/, httplib::Resp } void Server::handle_get_metadata(const httplib::Request& req, httplib::Response& res) { - const auto& label_tag = req.matches[1].str(); + 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(label_tag, 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()) { res.status = 404; - nlohmann::json response = {{"result", "error"}, {"error", "Metadata not found for label:tag: " + label_tag}}; + nlohmann::json response = {{"result", "error"}, {"error", "Metadata not found for: " + key}}; res.set_content(response.dump(), "application/json"); return; } + // Try to interpret the key as a hash directly + uint64_t hash_value; try { - nlohmann::json response = {{"result", "success"}, {"metadata", entry.metadata}}; - res.set_content(response.dump(), "application/json"); - } catch (const nlohmann::json::exception& e) { - std::cerr << "Error serializing metadata for " << label_tag << ": " << e.what() << std::endl; - res.status = 500; - nlohmann::json response = {{"result", "error"}, {"error", "Internal server error: Failed to serialize metadata"}}; + hash_value = std::stoull(hash_str); + } catch (const std::invalid_argument& e) { + res.status = 404; + nlohmann::json response = {{"result", "error"}, {"error", "Invalid hash: " + hash_str}}; res.set_content(response.dump(), "application/json"); + return; } + + std::stringstream oss; + oss << hash_value; + if (oss.str() != hash_str) { + res.status = 404; + nlohmann::json response = {{"result", "error"}, {"error", "Invalid hash: " + hash_str}}; + res.set_content(response.dump(), "application/json"); + return; + } + + // Hash is valid, look up the metadata + std::vector entries; + if (!db_->list(entries)) { + res.status = 500; + nlohmann::json response = {{"result", "error"}, {"error", "Failed to retrieve metadata"}}; + res.set_content(response.dump(), "application/json"); + return; + } + + // Find the entry with matching hash + for (const auto& e : entries) { + if (e.hash == hash_str) { + try { + nlohmann::json response = {{"result", "success"}, {"metadata", e.metadata}}; + res.set_content(response.dump(), "application/json"); + return; + } catch (const nlohmann::json::exception& e) { + std::cerr << "Error serializing metadata for hash " << hash_str << ": " << e.what() << std::endl; + res.status = 500; + nlohmann::json response = {{"result", "error"}, {"error", "Internal server error: Failed to serialize metadata"}}; + res.set_content(response.dump(), "application/json"); + return; + } + } + } + + // No entry found with this hash + res.status = 404; + nlohmann::json response = {{"result", "error"}, {"error", "Metadata not found for hash: " + hash_str}}; + res.set_content(response.dump(), "application/json"); } std::pair Server::parse_label_tag(const std::string& label_tag) const {