From 432b28484fc7653aa63a29500b03060d76d4f307 Mon Sep 17 00:00:00 2001 From: NicolasCantu Date: Wed, 12 Nov 2025 09:59:39 +0100 Subject: [PATCH] Improved way to test the data --- src/lib.rs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 1780111..392601d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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) -> tide::Result) -> tide::Result { + 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) -> tide::Server { 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 }