diff --git a/src/database/mod.rs b/src/database/mod.rs index 42aa35a..595551c 100644 --- a/src/database/mod.rs +++ b/src/database/mod.rs @@ -249,6 +249,22 @@ impl UpEndConnection { .execute(&self.conn)?) } + pub fn retrieve_entry(&self, hash: Hash) -> Result> { + use crate::database::inner::schema::data::dsl::*; + + let entry = data + .filter(identity.eq(Address::Hash(hash).encode()?)) + .load::(&self.conn)?; + + match entry.len() { + 0 => Ok(None), + 1 => Ok(Some(Entry::try_from(entry.get(0).unwrap())?)), + _ => { + unreachable!("Multiple entries returned with the same hash - this should be impossible!") + } + } + } + pub fn retrieve_object(&self, object_address: Address) -> Result> { use crate::database::inner::schema::data::dsl::*; diff --git a/src/routes.rs b/src/routes.rs index c06c2b9..8cbd8aa 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -48,6 +48,16 @@ pub async fn get_raw( .map_err(ErrorInternalServerError)?; if let Address::Hash(hash) = address { let connection = state.upend.connection().map_err(ErrorInternalServerError)?; + + // First check if there's an entry with this hash + let entry = connection + .retrieve_entry(hash.clone()) + .map_err(ErrorInternalServerError)?; + if let Some(entry) = entry { + return Ok(Either::B(HttpResponse::Ok().json(entry))); + } + + // Then, check the files let files = connection .retrieve_file(hash) .map_err(ErrorInternalServerError)?;