/raw/ queries of entries

feat/vaults
Tomáš Mládek 2022-01-14 22:04:53 +01:00
parent 2f6a265af8
commit 338be4be10
No known key found for this signature in database
GPG Key ID: ED21612889E75EC5
2 changed files with 26 additions and 0 deletions

View File

@ -249,6 +249,22 @@ impl UpEndConnection {
.execute(&self.conn)?)
}
pub fn retrieve_entry(&self, hash: Hash) -> Result<Option<Entry>> {
use crate::database::inner::schema::data::dsl::*;
let entry = data
.filter(identity.eq(Address::Hash(hash).encode()?))
.load::<models::Entry>(&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<Vec<Entry>> {
use crate::database::inner::schema::data::dsl::*;

View File

@ -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)?;