Bug fixing

This commit is contained in:
Your Name
2025-05-25 12:00:57 +12:00
parent 591a77cfb5
commit 652ef87902

View File

@@ -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<dbEntry> 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<std::string, std::string> Server::parse_label_tag(const std::string& label_tag) const {