Improved way to test the data

This commit is contained in:
NicolasCantu 2025-11-12 09:59:39 +01:00
parent dbb6aff09d
commit 432b28484f

View File

@ -83,6 +83,11 @@ impl StorageService {
Ok(buffer)
}
pub async fn data_exists(&self, key: &str) -> bool {
let file_path = format!("{}/{}/{}", self.storage_dir, &key[..2], &key[2..]);
Path::new(&file_path).exists().await
}
pub async fn cleanup_expired_files_once(&self) -> Result<(), String> {
let mut entries = read_dir(&self.storage_dir)
.await
@ -253,11 +258,37 @@ pub async fn handle_retrieve(req: Request<StorageService>) -> tide::Result<Respo
}
}
pub async fn handle_test(req: Request<StorageService>) -> tide::Result<Response> {
let key: String = req.param("key")?.to_string();
if key.len() != 64 || !key.chars().all(|c| c.is_ascii_hexdigit()) {
return Ok(Response::builder(StatusCode::BadRequest)
.body("Invalid key: must be a 32 bytes hex string.".to_string())
.build());
}
let svc = req.state();
if svc.data_exists(&key).await {
Ok(Response::builder(StatusCode::Ok)
.body(serde_json::to_value(&ApiResponse {
message: "Data exists.".to_string(),
})?)
.build())
} else {
Ok(Response::builder(StatusCode::NotFound)
.body(serde_json::to_value(&ApiResponse {
message: "Key not found.".to_string(),
})?)
.build())
}
}
pub fn create_app(no_ttl_permanent: bool, storage_dir: impl Into<String>) -> tide::Server<StorageService> {
let svc = StorageService::new(storage_dir);
let mut app = tide::with_state(svc);
app.at("/health").get(handle_health);
app.at("/store/:key").post(move |req| handle_store(req, no_ttl_permanent));
app.at("/retrieve/:key").get(handle_retrieve);
app.at("/test/:key").get(handle_test);
app
}