Bug fixing
This commit is contained in:
@@ -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) {
|
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;
|
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;
|
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");
|
res.set_content(response.dump(), "application/json");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Try to interpret the key as a hash directly
|
||||||
|
uint64_t hash_value;
|
||||||
try {
|
try {
|
||||||
nlohmann::json response = {{"result", "success"}, {"metadata", entry.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");
|
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) {
|
} catch (const nlohmann::json::exception& e) {
|
||||||
std::cerr << "Error serializing metadata for " << label_tag << ": " << e.what() << std::endl;
|
std::cerr << "Error serializing metadata for hash " << hash_str << ": " << e.what() << std::endl;
|
||||||
res.status = 500;
|
res.status = 500;
|
||||||
nlohmann::json response = {{"result", "error"}, {"error", "Internal server error: Failed to serialize metadata"}};
|
nlohmann::json response = {{"result", "error"}, {"error", "Internal server error: Failed to serialize metadata"}};
|
||||||
res.set_content(response.dump(), "application/json");
|
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 {
|
std::pair<std::string, std::string> Server::parse_label_tag(const std::string& label_tag) const {
|
||||||
|
Reference in New Issue
Block a user