Tests passed.
This commit is contained in:
@@ -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
|
@@ -31,6 +31,7 @@ private:
|
||||
void handle_delete_tag(const httplib::Request& req, httplib::Response& res);
|
||||
void handle_delete_object(const httplib::Request& req, httplib::Response& res);
|
||||
void handle_append_tag(const httplib::Request& req, httplib::Response& res);
|
||||
void handle_exists(const httplib::Request& req, httplib::Response& res);
|
||||
std::pair<std::string, std::string> parse_label_tag(const std::string& label_tag) const;
|
||||
void add_file_metadata(const std::string &file_path, nlohmann::json &metadata) const;
|
||||
|
||||
|
Reference in New Issue
Block a user